On Fri, 22 Jul 2022 12:19:42 GMT, Prasanta Sadhukhan <[email protected]> 
wrote:

> But it seems in.available() does not change when we read from the stream. For 
> eg, if we read 512 bytes from stream, I would expect (2147483647 -512) bytes 
> but I still see available() returns 2147483647 so this will not work AFICS

It does, you can play with 
[this](https://gist.github.com/azvegint/65b30efa2749e5af3fae6a247c344677) and 
provide large file as argument.

However, frequent calling of `available()` may reduce performance, so I agree 
with your solution.


> I tried creating ByteArrayInputStream of 3GB but it fails with 
> NegativeArraySizeException because of overflow so I am not sure if it's 
> possible.

You can try something like:

InputStream inputStream = new InputStream() {
    final long SIZE = (long) (Integer.MAX_VALUE * 1.5);
    long read = 0;

    @Override
    public int available() {
        return (int) Math.min(SIZE - read, Integer.MAX_VALUE);
    }

    @Override
    public int read() {
        return (SIZE - read++ > 0) ? 1 : -1;
    }
};

-------------

PR: https://git.openjdk.org/jdk/pull/9588

Reply via email to