Try using resources and System.in

Well, that’s probably not the best question, but I’m stuck in it and can’t find the answer to this on the net.

This code will not be read from standard input a second time:

try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
{
    input = br.readLine();
}
catch (final Exception e)
{
    System.err.println("Read from STDIN failed: " + e.getMessage());
}
// do some processing
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
{
    input = br.readLine();
}
catch (final Exception e)
{
    System.err.println("Read from STDIN failed: " + e.getMessage());
}

I know that java try-with-resources recursively closes all threads in the chain, so it System.incloses after the first read . Is there any good workaround for this? Or should I handle a thread closing itself?

UPD: I tried to process the thread closing itself (this is java6-style). Here's the code if anyone is interested. But I noticed that this chain closure does not come from try-with-resources bur, but from the implementation of close methods. Therefore, I did not benefit from this attempt.

fge, . .

, java , , .

+4
4

InputStream, , , .close(), . :

public class ForwardingInputStream
    extends InputStream
{
    private final InputStream in;
    private final boolean closeWrapped;

    public ForwardingInputStream(final InputStream in, final boolean closeWrapped)
    {
        this.in = in;
        this.closeWrapped = closeWrapped;
    }

    public ForwardingInputStream(final InputStream in)
    {
        this(in, false);
    }

    @Override
    public int read()
        throws IOException
    {
        return in.read();
    }

    @Override
    public int read(final byte[] b)
        throws IOException
    {
        return in.read(b);
    }

    @Override
    public int read(final byte[] b, final int off, final int len)
        throws IOException
    {
        return in.read(b, off, len);
    }

    @Override
    public long skip(final long n)
        throws IOException
    {
        return in.skip(n);
    }

    @Override
    public int available()
        throws IOException
    {
        return in.available();
    }

    @Override
    public void close()
        throws IOException
    {
        if (closeWrapped)
            in.close();
    }

    @Override
    public synchronized void mark(final int readlimit)
    {
        in.mark(readlimit);
    }

    @Override
    public synchronized void reset()
        throws IOException
    {
        in.reset();
    }

    @Override
    public boolean markSupported()
    {
        return in.markSupported();
    }
}

, , , InputStreamReader, final .close().

+1

, , try-with-resources, Java BufferedReader . , . SO . Apache Commons IO, , CloseShieldInputStream. Commons IO, , - , , Commons IO.

+1

. , - (Apache Commons IO, Google Guava,...), , .

, InputStream InputStream, , , close, .

public final class NonClosingInputStream extends InputStream {
    private final InputStream wrappedStream;
    public NonClosingInputStream(final InputStream wrappedStream) {
        this.wrappedStream = Objects.requireNonNull(wrappedStream);
    }
    @Override
    public void close() {
        // do nothing
    }
    // all other methods
}

System.in .

+1

, :

try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)
{public void close() throws IOException {}})) {
    input = br.readLine();
}

, System.in , InputStreamReader close() BufferedReader.

0

Source: https://habr.com/ru/post/1536314/


All Articles