Java InputStream closed on stream

I am trying to read from an InputStream on a stream.

The class to be executed by Thread looks like this:

static private class Runner implements Runnable {

    private InputStream fis;
    private OutputStream fos;

    public Runner(InputStream fis, OutputStream fos) throws IOException {

        int blu = fis.available();

        System.out.println(blu);

        this.fis = fis;

        int bla = this.fis.available();

        System.out.println(bla);
        this.fos = fos;
    }

    @Override
    public void run() {

        try {
            int bla = fis.available();

            System.out.println(bla);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(fis);
        System.out.println(fos);

    }

}

      

The theme is created this way

final Runnable runnable = new Runner(fis, fos);
final Thread thread = new Thread(runnable);
thread.start();

      

And the run method must be running on the thread. But as soon as it is executed I get Error

java.nio.channels.ClosedChannelException

      

I debugged it and the InputStream was closed.

Why is the input stream being closed on the stream? Is there an alternative that I should be using?

EDIT:

I forgot to mention that they open in a try block like this and after that the main function exits.

try (InputStream fis = Files.newInputStream(sourcePath)) {
        try (OutputStream fos = Files.newOutputStream(sinkPath)) {

            final Runnable runnable = new Runner(fis, fos);
            final Thread thread = new Thread(runnable);
            thread.start();
        }

    }

      

+3


source to share


2 answers


Those try-with-resources blocks close the corresponding streams when the block exits. This is great when you plan on using streams within blocks. But since you want to continue using threads on a different thread after the end of the block, get rid of the blocks.



InputStream  fis = Files.newInputStream (sourcePath);
OutputStream fos = Files.newOutputStream(sinkPath);

final Runnable runnable = new Runner(fis, fos);
final Thread   thread   = new Thread(runnable);
thread.start();

      

+4


source


Since you have this in Try, it closes when the thread leaves the try block. Thread.start () doesn't hang, so it closes automatically.

Do it:

InputStream  fis = Files.newInputStream (sourcePath);
OutputStream fos = Files.newOutputStream(sinkPath);

final Runnable runnable = new Runner(fis, fos);
final Thread thread = new Thread(runnable);
thread.start();

      



And in your stream:

   public void run() {

        try {
            int bla = fis.available();

            System.out.println(bla);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            System.out.println(fis);
            System.out.println(fos);
            fis.close();
            fis.close();
        }
    }

      

+3


source







All Articles