Traneptora / jxlatte

Java JPEG XL decoder
MIT License
45 stars 5 forks source link

java.io.IOException: Stream closed when executing the code from the readme #21

Closed elbosso closed 1 year ago

elbosso commented 1 year ago
package de.elbosso.scratch.misc;

import com.thebombzen.jxlatte.JXLDecoder;
import com.thebombzen.jxlatte.JXLImage;
import com.thebombzen.jxlatte.io.PNGWriter;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class JpegXlTest
{
    public static void main(java.lang.String[] args) throws IOException
    {
        InputStream input = new java.io.FileInputStream("/home/elbosso/src/language_java/jxlatte/samples/ants.jxl");
        OutputStream output = new java.io.FileOutputStream("/tmp/ants.png");
        JXLDecoder decoder = new JXLDecoder(input);
        JXLImage image = decoder.decode();
        PNGWriter writer = new PNGWriter(image);
        writer.write(output);
    }
}

gives

Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.thebombzen.jxlatte.frame.Frame.decodeFrame(Frame.java:436)
    at com.thebombzen.jxlatte.JXLCodestreamDecoder.decode(JXLCodestreamDecoder.java:375)
    at com.thebombzen.jxlatte.JXLCodestreamDecoder.decode(JXLCodestreamDecoder.java:319)
    at com.thebombzen.jxlatte.JXLDecoder.decode(JXLDecoder.java:48)
    at de.elbosso.scratch.misc.JpegXlTest.main(JpegXlTest.java:18)
Caused by: java.io.IOException: Stream closed
    at java.base/java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:157)
    at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:244)
    at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:263)
    at java.base/java.io.DataInputStream.readInt(DataInputStream.java:393)
    at java.base/java.io.DataInputStream.readFloat(DataInputStream.java:452)
    at com.thebombzen.jxlatte.frame.vardct.HFGlobal.readDefaultWeights(HFGlobal.java:200)
    at com.thebombzen.jxlatte.frame.vardct.HFGlobal.<clinit>(HFGlobal.java:213)
    ... 5 more
Traneptora commented 1 year ago

It looks like the stream is being closed prematurely by a finalizer. What happens if you explicitly close the stream at the end with input.close(); output.close();

Traneptora commented 1 year ago

Actually, wait, it looks like it's having an issue reading the default weights from the jar file. How did you add it to your classpath? The jar file contains resources that need to be present.

elbosso commented 1 year ago

You are of course right - i moved the project from meson over to maven as a build system and forgot about the resources folder - now everything works. I did this because i have not had any experience yet with meson and could not get it to run: the first step (meson setup) already failed with an error message that i could not understand so i groped for familiar territory...