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.FilterInputStream
An 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 long
maxsize
maximum bytes to readprotected long
size
number of bytes read so farprotected boolean
suppressClose
prevent 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 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
-
-
-
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 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.IOException
Read bytes into a portion of an array.- Overrides:
read
in 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.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 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:
markSupported
in classjava.io.FilterInputStream
-
mark
public void mark(int readAheadLimit)
- Overrides:
mark
in classjava.io.FilterInputStream
-
reset
public void reset() throws java.io.IOException
- Overrides:
reset
in 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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
-