Package org.terrier.utility
Class FixedSizeInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.terrier.utility.FixedSizeInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class FixedSizeInputStream extends java.io.FilterInputStreamAn inputstream which only reads a fixed length of a parent input stream. No buffering is done.- Author:
- Craig Macdonald
-
-
Field Summary
Fields Modifier and Type Field Description protected longmaxsizemaximum bytes to readprotected longsizenumber of bytes read so farprotected booleansuppressCloseprevent a close() from doing anyhing, like closing the underlying stream
-
Constructor Summary
Constructors Constructor Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidmark(int readAheadLimit)booleanmarkSupported()intread()Read a single byte from the underlying Reader.intread(byte[] cbuf, int off, int len)Read bytes into a portion of an array.voidreset()longskip(long n)Skips n bytes from the stream.voidsuppressClose()Sets it so that close() will never be called
-
-
-
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.IOExceptionRead a single byte from the underlying Reader.- Overrides:
readin classjava.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.IOExceptionRead bytes into a portion of an array.- Overrides:
readin classjava.io.FilterInputStream- Parameters:
cbuf- Destination bufferoff- Offset at which to start storing byteslen- 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.IOExceptionSkips 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:
skipin classjava.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:
markSupportedin classjava.io.FilterInputStream
-
mark
public void mark(int readAheadLimit)
- Overrides:
markin classjava.io.FilterInputStream
-
reset
public void reset() throws java.io.IOException- Overrides:
resetin classjava.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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.FilterInputStream- Throws:
java.io.IOException
-
-