org.terrier.utility
Class FixedSizeInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by org.terrier.utility.FixedSizeInputStream
All Implemented Interfaces:
java.io.Closeable

public class FixedSizeInputStream
extends java.io.FilterInputStream

An inputstream which only reads a fixed length of a parent input stream. No buffering is done.

Author:
Craig Macdonald

Field Summary
protected  long maxsize
          maximum bytes to read
protected  long size
          number of bytes read so far
protected  boolean suppressClose
          prevent a close() from doing anyhing, like closing the underlying stream
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
FixedSizeInputStream(java.io.InputStream in, long _maxsize)
          create a new FixedSizeInputStream, using in as the underlying InputStream, and maxsize as the maximum number of bytes to read.
 
Method Summary
 void close()
          
 void mark(int readAheadLimit)
          
 boolean markSupported()
          
 int read()
          Read a single byte from the underlying Reader.
 int read(byte[] cbuf, int off, int len)
          Read bytes into a portion of an array.
 void reset()
          
 long skip(long n)
          Skips n bytes from the stream.
 void suppressClose()
          Sets it so that close() will never be called
 
Methods inherited from class java.io.FilterInputStream
available, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxsize

protected final long maxsize
maximum bytes to read


size

protected long size
number of bytes read so far


suppressClose

protected boolean suppressClose
prevent a close() from doing anyhing, like closing the underlying stream

Constructor Detail

FixedSizeInputStream

public FixedSizeInputStream(java.io.InputStream in,
                            long _maxsize)
create a new FixedSizeInputStream, using in as the underlying InputStream, and maxsize as the maximum number of bytes to read.

Parameters:
in - underlying InputStream to read bytes from.
_maxsize - maximum number of bytes to read.
Method Detail

read

public int read()
         throws java.io.IOException
Read a single byte from the underlying Reader.

Overrides:
read in class java.io.FilterInputStream
Returns:
The byte read, or -1 if the end of the underlying stream has been reached or the maximum allowed number of bytes has been read from it.
Throws:
java.io.IOException - If an I/O error occurs.

read

public int read(byte[] cbuf,
                int off,
                int len)
         throws java.io.IOException
Read bytes into a portion of an array.

Overrides:
read in class java.io.FilterInputStream
Parameters:
cbuf - 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:
java.io.IOException - If an I/O error occurs in the underlying reader.

skip

public long skip(long n)
          throws java.io.IOException
Skips n bytes from the stream. If the end of the stream has been reached before reading n bytes then it returns. NB: This method uses read() internally.

Overrides:
skip in class java.io.FilterInputStream
Parameters:
n - long the number of characters to skip.
Returns:
long the number of characters skipped.
Throws:
java.io.IOException - if there is any error while reading from the stream.

markSupported

public boolean markSupported()

Overrides:
markSupported in class java.io.FilterInputStream

mark

public void mark(int readAheadLimit)

Overrides:
mark in class java.io.FilterInputStream

reset

public void reset()
           throws java.io.IOException

Overrides:
reset in class java.io.FilterInputStream
Throws:
java.io.IOException

suppressClose

public void suppressClose()
Sets it so that close() will never be called


close

public void close()
           throws java.io.IOException

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterInputStream
Throws:
java.io.IOException


Terrier 3.5. Copyright © 2004-2011 University of Glasgow