org.inria.ns.reflex.util.io
Class ExtensibleBufferedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by org.inria.ns.reflex.util.io.ExtensibleBufferedInputStream
All Implemented Interfaces:
Closeable, Unwrappable

public class ExtensibleBufferedInputStream
extends FilterInputStream
implements Unwrappable

A buffer that always grows once marked until reset.

Author:
Philippe Poulard

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.inria.ns.reflex.structures.Unwrappable
Unwrappable.Unwrapper
 
Field Summary
 
Fields inherited from interface org.inria.ns.reflex.structures.Unwrappable
UNWRAPPER
 
Constructor Summary
ExtensibleBufferedInputStream(InputStream in)
          Creates an ExtensibleBufferedInputStream based on a given input stream.
ExtensibleBufferedInputStream(InputStream in, int size)
          Creates an ExtensibleBufferedInputStream with the specified buffer size, and based on a given input stream.
 
Method Summary
 int available()
          Returns the number of bytes that can be read from this input stream without blocking.
 void close()
          Closes this input stream and releases any system resources associated with the stream.
 void mark(int readlimit)
          See the general contract of the mark method of InputStream.
 boolean markSupported()
          Tests if this input stream supports the mark and reset methods.
 int read()
          See the general contract of the read method of InputStream.
 int read(byte[] b, int off, int len)
          Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
 void reset()
          See the general contract of the reset method of InputStream.
 long skip(long n)
          See the general contract of the skip method of InputStream.
 Object unwrap()
          Unwrap the underlying input stream.
 
Methods inherited from class java.io.FilterInputStream
read
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExtensibleBufferedInputStream

public ExtensibleBufferedInputStream(InputStream in)
Creates an ExtensibleBufferedInputStream based on a given input stream. This new buffer doesn't bufferize the data once created : mark(int) must be invoked explicitely. The default buffer size is set to 2048.

Parameters:
in - The underlying input stream.

ExtensibleBufferedInputStream

public ExtensibleBufferedInputStream(InputStream in,
                                     int size)
Creates an ExtensibleBufferedInputStream with the specified buffer size, and based on a given input stream. This new buffer doesn't bufferize the data once created : mark(int) must be invoked explicitely.

Parameters:
in - The underlying input stream.
size - The buffer size.
Throws:
IllegalArgumentException - if size <= 0.
Method Detail

read

public int read()
         throws IOException
See the general contract of the read method of InputStream.

Overrides:
read in class FilterInputStream
Returns:
The next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if an I/O error occurs.
See Also:
FilterInputStream.in

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

This method implements the general contract of the corresponding read method of the InputStream class.

Overrides:
read in class FilterInputStream
Parameters:
b - destination buffer.
off - offset at which to start storing bytes.
len - maximum number of bytes to read.
Returns:
the number of bytes read, or -1 if the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs.

skip

public long skip(long n)
          throws IOException
See the general contract of the skip method of InputStream.

Overrides:
skip in class FilterInputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
IOException - if an I/O error occurs.

available

public int available()
              throws IOException
Returns the number of bytes that can be read from this input stream without blocking.

The available method of BufferedInputStream returns the sum of the the number of bytes remaining to be read in the buffer (count - pos) and the result of calling the available method of the underlying input stream.

Overrides:
available in class FilterInputStream
Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
IOException - if an I/O error occurs.
See Also:
FilterInputStream

mark

public void mark(int readlimit)
See the general contract of the mark method of InputStream. Once marked, everything read in the underlying input stream is buffered. Setting the read limit to 0 consist on canceling the mark.

Overrides:
mark in class FilterInputStream
Parameters:
readlimit - Unused : data is buffered until reset() is invoked, or until the read limit is set to 0 (in this case, everything that had been buffered is lost).
See Also:
BufferedInputStream.reset()

reset

public void reset()
           throws IOException
See the general contract of the reset method of InputStream.

The mark is always valid ; this method never fails.

Overrides:
reset in class FilterInputStream
Throws:
IOException - For compatibility.
See Also:
BufferedInputStream.mark(int)

markSupported

public boolean markSupported()
Tests if this input stream supports the mark and reset methods. The markSupported method of ExtensibleBufferedInputStream returns true.

Overrides:
markSupported in class FilterInputStream
Returns:
true.
See Also:
InputStream.mark(int), InputStream.reset()

close

public void close()
           throws IOException
Closes this input stream and releases any system resources associated with the stream.

Specified by:
close in interface Closeable
Overrides:
close in class FilterInputStream
Throws:
IOException - if an I/O error occurs.

unwrap

public Object unwrap()
Unwrap the underlying input stream.

Specified by:
unwrap in interface Unwrappable
See Also:
Unwrappable.unwrap()