com.gc.iotools.stream.reader
Class SizeLimitReader

java.lang.Object
  extended by java.io.Reader
      extended by com.gc.iotools.stream.reader.SizeLimitReader
All Implemented Interfaces:
Closeable, Readable

public class SizeLimitReader
extends Reader

A Reader wrapper that will read only a definite number of bytes from the underlying stream.

Since:
1.2.7
Version:
$Id: SizeLimitReader.java 527 2014-02-24 19:29:50Z gabriele.contini@gmail.com $
Author:
dvd.smnt
See Also:
Reader

Field Summary
protected  long currentPosition
          The number of bytes that have been read from the in stream.
protected  Reader in
          The underlying stream from which data are read.
protected  long markPosition
          The position in the stream when mark(int) was called.
protected  long maxSize
          The number of bytes to read at most from the in stream.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
SizeLimitReader(Reader in, long maxCharacterSize)
           Create a new SizeLimitReader from another stream given a size limit.
 
Method Summary
 void close()
           Close the underlying stream.
 long getBytesLeft()
          Get the maximum number of bytes left to read before the limit, set in the constructor, is reached.
 long getBytesRead()
          Get the number of bytes actually read from this stream.
 long getMaxSize()
          Get the number of total bytes (including bytes already read) that can be read from this stream (as set in the constructor).
 void mark(int readlimit)
          
 boolean markSupported()
          
 int read()
          
 int read(char[] cbuf)
          
 int read(char[] cbuf, int off, int len)
          
 void reset()
          
 long skip(long n)
          
 
Methods inherited from class java.io.Reader
read, ready
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

currentPosition

protected long currentPosition
The number of bytes that have been read from the in stream. Read methods should check to ensure that currentPosition never exceeds maxSize.


in

protected Reader in
The underlying stream from which data are read. All methods are forwarded to it, after checking that the maxSize size that hasn't been reached.


markPosition

protected long markPosition
The position in the stream when mark(int) was called. It's used to reset the current position when reset() is called.


maxSize

protected final long maxSize
The number of bytes to read at most from the in stream. Read methods should check to ensure that the number of bytes read never exceeds maxSize.

Constructor Detail

SizeLimitReader

public SizeLimitReader(Reader in,
                       long maxCharacterSize)

Create a new SizeLimitReader from another stream given a size limit.

Bytes are read from the underlying stream until size limit is reached.

Parameters:
in - The underlying input stream from where the data is read.
maxCharacterSize - the max number of bytes to allow to be read from the underlying stream.
Method Detail

close

public void close()
           throws IOException

Close the underlying stream. Calling this method may make data on the underlying stream unavailable.

Specified by:
close in interface Closeable
Specified by:
close in class Reader
Throws:
IOException

getBytesLeft

public long getBytesLeft()
Get the maximum number of bytes left to read before the limit, set in the constructor, is reached.

Returns:
The number of bytes that (at a maximum) are left to be taken from this stream.

getBytesRead

public long getBytesRead()
Get the number of bytes actually read from this stream.

Returns:
number of bytes that have already been taken from this stream.

getMaxSize

public long getMaxSize()
Get the number of total bytes (including bytes already read) that can be read from this stream (as set in the constructor).

Returns:
Maximum bytes that can be read until the size limit runs out

mark

public void mark(int readlimit)
          throws IOException

Overrides:
mark in class Reader
Throws:
IOException

markSupported

public boolean markSupported()

Overrides:
markSupported in class Reader

read

public int read()
         throws IOException

Overrides:
read in class Reader
Throws:
IOException

read

public int read(char[] cbuf)
         throws IOException

Overrides:
read in class Reader
Throws:
IOException

read

public int read(char[] cbuf,
                int off,
                int len)
         throws IOException

Specified by:
read in class Reader
Throws:
IOException

reset

public void reset()
           throws IOException

Overrides:
reset in class Reader
Throws:
IOException

skip

public long skip(long n)
          throws IOException

Overrides:
skip in class Reader
Throws:
IOException


Copyright © 2008–2014. All rights reserved.