Class BoundedInputStream

All Implemented Interfaces:
Closeable, AutoCloseable

public class BoundedInputStream extends ProxyInputStream
Reads bytes up to a maximum count and stops once reached.

To build an instance, see BoundedInputStream.AbstractBuilder.

By default, a BoundedInputStream is unbound; so make sure to call BoundedInputStream.AbstractBuilder.setMaxCount(long).

You can find out how many bytes this stream has seen so far by calling getCount(). This value reflects bytes read and skipped.

Using a ServletInputStream

A ServletInputStream can block if you try to read content that isn't there because it doesn't know whether the content hasn't arrived yet or whether the content has finished. Initialize an BoundedInputStream with the Content-Length sent in the ServletInputStream's header, this stop it from blocking, providing it's been sent with a correct content length in the first place.

Using NIO


 BoundedInputStream s = BoundedInputStream.builder()
   .setPath(Paths.get("MyFile.xml"))
   .setMaxCount(1024)
   .setPropagateClose(false)
   .get();
 
 

Using IO


 BoundedInputStream s = BoundedInputStream.builder()
   .setFile(new File("MyFile.xml"))
   .setMaxCount(1024)
   .setPropagateClose(false)
   .get();
 
 

Counting Bytes

You can set the running count when building, which is most useful when starting from another stream:


 InputStream in = ...;
 BoundedInputStream s = BoundedInputStream.builder()
   .setInputStream(in)
   .setCount(12)
   .setMaxCount(1024)
   .setPropagateClose(false)
   .get();
 
 
Since:
2.0
See Also:
  • Field Details

    • count

      private long count
      The current count of bytes counted.
    • mark

      private long mark
      The current mark.
    • maxCount

      private final long maxCount
      The max count of bytes to read.
    • propagateClose

      private boolean propagateClose
      Flag if close should be propagated. TODO Make final in 3.0.
  • Constructor Details

    • BoundedInputStream

      @Deprecated public BoundedInputStream(InputStream in)
      Deprecated.
      Constructs a new BoundedInputStream that wraps the given input stream and is unlimited.
      Parameters:
      in - The wrapped input stream.
    • BoundedInputStream

      @Deprecated public BoundedInputStream(InputStream inputStream, long maxCount)
      Deprecated.
      Constructs a new BoundedInputStream that wraps the given input stream and limits it to a certain size.
      Parameters:
      inputStream - The wrapped input stream.
      maxCount - The maximum number of bytes to return.
    • BoundedInputStream

      BoundedInputStream(InputStream inputStream, long count, long maxCount, boolean propagateClose)
      Constructs a new BoundedInputStream that wraps the given input stream and limits it to a certain size.
      Parameters:
      inputStream - The wrapped input stream.
      count - The current number of bytes read.
      maxCount - The maximum number of bytes to return.
      propagateClose - true if calling close() propagates to the close() method of the underlying stream or false if it does not.
  • Method Details