net.sf.mmm.util.io.impl
Class BufferInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by net.sf.mmm.util.io.impl.BufferInputStream
All Implemented Interfaces:
Closeable, ByteProcessable

public class BufferInputStream
extends InputStream
implements ByteProcessable

This class represents a InputStream that works like a BufferedInputStream but exposes its internal state to be used for lookahead operations.
ATTENTION:
This class is intended for internal usage only.

Since:
1.0.1
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Nested Class Summary
protected static class BufferInputStream.CopyProcessor
          This inner class is the ByteProcessor used to copy bytes from the buffer to the caller consuming data from this stream.
 
Field Summary
private  ByteArrayBufferBuffer buffer
          the internal buffer.
private  BufferInputStream.CopyProcessor copyProcessor
          The processor used to copy the bytes from the buffer to the readers buffer.
private static int DEFAULT_CAPACITY
          The default capacity used by BufferInputStream(InputStream).
private  boolean eos
          true if the end of the stream has been reached, false otherwise.
private  InputStream inStream
          The adapted InputStream to buffer or null if closed.
 
Constructor Summary
  BufferInputStream(InputStream inStream)
          The constructor.
  BufferInputStream(InputStream inStream, int capacity)
          The constructor.
private BufferInputStream(InputStream inStream, int capacity, int bufferCount)
          The constructor.
 
Method Summary
 int available()
          
 void close()
          
 ProcessableByteArrayBuffer createLookaheadBuffer()
          This method creates a new buffer for lookahead operations on the data from the underlying stream.
protected  void ensureOpen()
          This method ensures that this stream has NOT been closed.
 boolean fill()
          This method (re-)fills the internal buffer as far as data is available from the underlying InputStream without consuming data from the stream.
 long process(ByteProcessor processor, long length)
          This method processes the number of bytes given by length (as far as available) using the given processor.
 int read()
          
 int read(byte[] bytes, int offset, int length)
          
 long skip(long skipCount)
          
 
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CAPACITY

private static final int DEFAULT_CAPACITY
The default capacity used by BufferInputStream(InputStream).

See Also:
Constant Field Values

inStream

private InputStream inStream
The adapted InputStream to buffer or null if closed.


buffer

private final ByteArrayBufferBuffer buffer
the internal buffer.


eos

private boolean eos
true if the end of the stream has been reached, false otherwise.


copyProcessor

private BufferInputStream.CopyProcessor copyProcessor
The processor used to copy the bytes from the buffer to the readers buffer.

Constructor Detail

BufferInputStream

public BufferInputStream(InputStream inStream)
The constructor.

Parameters:
inStream - is the InputStream to adapt.

BufferInputStream

public BufferInputStream(InputStream inStream,
                         int capacity)
The constructor.

Parameters:
inStream - is the InputStream to adapt.
capacity - is the capacity for each of the two ByteArrayBufferImpl s. Please note that therefore the double amount of memory is allocated.

BufferInputStream

private BufferInputStream(InputStream inStream,
                          int capacity,
                          int bufferCount)
The constructor.

Parameters:
inStream - is the InputStream to adapt.
capacity - is the capacity for each of the two ByteArrayBufferImpl s. Please note that therefore the double amount of memory is allocated.
bufferCount - is the number of buffers to use.
Method Detail

ensureOpen

protected void ensureOpen()
                   throws IOException
This method ensures that this stream has NOT been closed.

Throws:
IOException - if this stream has been closed.

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

fill

public boolean fill()
             throws IOException
This method (re-)fills the internal buffer as far as data is available from the underlying InputStream without consuming data from the stream. If the internal buffer is already filled, the call of this method will have no effect.

Returns:
true if the end of the stream was encountered while (re)filling the internal buffer, false otherwise.
Throws:
IOException - if the operation fails.

skip

public long skip(long skipCount)
          throws IOException

Overrides:
skip in class InputStream
Throws:
IOException

read

public int read()
         throws IOException

Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] bytes,
                int offset,
                int length)
         throws IOException

Overrides:
read in class InputStream
Throws:
IOException

available

public int available()
              throws IOException

Overrides:
available in class InputStream
Throws:
IOException

process

public long process(ByteProcessor processor,
                    long length)
This method processes the number of bytes given by length (as far as available) using the given processor.

Specified by:
process in interface ByteProcessable
Parameters:
processor - is the ByteProcessor called to process the bytes. It may be called multiple types if the data is sliced into multiple byte-arrays.
length - is the desired number of bytes to process. The value has to be greater or equal to 0. A value of 0 will have no effect. If you want to process all available data to the end of stream or buffer you may use Long.MAX_VALUE.
Returns:
the number of bytes that have actually been processed. For a sufficient length this will typically be equal to length. However if the end of the data has been reached, a smaller value is returned. The value will always be greater or equal to 0.

createLookaheadBuffer

public ProcessableByteArrayBuffer createLookaheadBuffer()
This method creates a new buffer for lookahead operations on the data from the underlying stream. That buffer is a view on the internal buffer of this stream with its own state for the read position. Consuming data from that buffer will NOT influence the state of this buffer, while consuming data from this stream will refill the returned buffer.

Returns:
the lookahead buffer.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.