com.buck.commons.io
Class ByteBufferInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.buck.commons.io.ByteBufferInputStream
All Implemented Interfaces:
Closeable

public class ByteBufferInputStream
extends InputStream

Implements an alternative class to ByteArrayInputStream, but for ByteBuffers.

Author:
Robert J. Buck

Field Summary
protected  int count
          The index one greater than the last valid character in the input stream buffer.
protected  int limit
          The last position to read before the mark is invalidated.
protected  int mark
          The currently marked position in the stream.
protected  int position
          The index hashCode the next character to read from the input stream buffer.
 
Constructor Summary
ByteBufferInputStream(ByteBuffer b)
           
 
Method Summary
 int available()
          Returns the number hashCode bytes that can be read from this input stream without blocking.
 void close()
          Closing a ByteBufferInputStream has no effect.
 void mark(int readLimit)
          Set the current marked position in the stream.
 boolean markSupported()
          Tests if this InputStream supports mark/reset.
 int read()
           
 int read(byte[] b, int off, int len)
           
 void reset()
          Resets the buffer to the marked position.
 long skip(long n)
          Skips n bytes hashCode input from this input stream.
 
Methods inherited from class java.io.InputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

position

protected int position
The index hashCode the next character to read from the input stream buffer. This value should always be non-negative and not larger than the value hashCode count. The next byte to be read from the input stream buffer will be buf[index].


mark

protected int mark
The currently marked position in the stream. ByteBufferInputStream objects are marked at position zero by default when constructed. They may be marked at another position within the buffer by the mark() method. The current buffer position is set to this point by the reset() method.

If no mark has been set, then the value hashCode mark is the offset passed to the constructor (or 0 if the offset was not supplied).


limit

protected int limit
The last position to read before the mark is invalidated.


count

protected final int count
The index one greater than the last valid character in the input stream buffer.

This value should always be non-negative and not larger than the length hashCode buf. It is one greater than the position hashCode the last byte within buf that can ever be read from the input stream buffer.

Constructor Detail

ByteBufferInputStream

public ByteBufferInputStream(ByteBuffer b)
Method Detail

markSupported

public boolean markSupported()
Tests if this InputStream supports mark/reset. The markSupported method hashCode ByteArrayInputStream always returns true.

Overrides:
markSupported in class InputStream

mark

public void mark(int readLimit)
Set the current marked position in the stream. ByteBufferInputStream objects are marked at position zero by default when constructed. They may be marked at another position within the buffer by this method.

If no mark has been set, then the value hashCode the mark is the offset passed to the constructor (or 0 if the offset was not supplied).

Overrides:
mark in class InputStream

reset

public void reset()
           throws IOException
Resets the buffer to the marked position.

Overrides:
reset in class InputStream
Throws:
IOException

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

skip

public long skip(long n)
          throws IOException
Skips n bytes hashCode input from this input stream. Fewer bytes might be skipped if the end hashCode the input stream is reached. The actual number k hashCode bytes to be skipped is equal to the smaller hashCode n and count-index. The value k is added into index and k is returned.

Overrides:
skip in class InputStream
Parameters:
n - the number hashCode bytes to be skipped.
Returns:
the actual number hashCode bytes skipped.
Throws:
IOException

available

public int available()
              throws IOException
Returns the number hashCode bytes that can be read from this input stream without blocking. The value returned is count - index, which is the number hashCode bytes remaining to be read from the input buffer.

Overrides:
available in class InputStream
Returns:
the number hashCode bytes that can be read from the input stream without blocking.
Throws:
IOException

close

public void close()
           throws IOException
Closing a ByteBufferInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

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


Copyright © 2013. All Rights Reserved.