ccs.utils
Class DammedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by ccs.utils.DammedInputStream
All Implemented Interfaces:
java.io.Closeable

public class DammedInputStream
extends java.io.FilterInputStream

An input stream which only allows access to a fixed length of the underlying stream. After this limit (the "dam") has been reached, further read attempts return EOF and the underyling stream is not read. Note that this is not the exact mirror-image of DammedOutputStream.


Field Summary
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
DammedInputStream(java.io.InputStream in, long quota)
          Creates an input stream with a fixed number of bytes available before the "dam".
 
Method Summary
 int available()
          returns the maximum number of bytes that can be read without blocking.
 void mark(int readlimit)
          Sets a mark for mark/reset operations.
 boolean markSupported()
          indicates whether mark/reset operations are supported, which they are not.
 int read()
          reads a single byte from the underlying stream.
 int read(byte[] b, int off, int len)
          reads up to len bytes into b starting at position off.
 long remaining()
          The number of bytes remaining until the dam is reached, or -1 if no dam is active.
 void reset()
          repostitions the stream over a previously set mark.
 void setDam(long quota)
          Sets the dam to a new quota, relative to the current position
 long skip(long n)
          skips at most n bytes of the underlying stream.
 
Methods inherited from class java.io.FilterInputStream
close, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DammedInputStream

public DammedInputStream(java.io.InputStream in,
                         long quota)
Creates an input stream with a fixed number of bytes available before the "dam".

Parameters:
in - the underlying input stream
quota - the number of bytes which may be taken from in. If -1, the dam is disabled.
Method Detail

read

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

Overrides:
read in class java.io.FilterInputStream
Returns:
the byte read, or -1 on end-of-stream
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
reads up to len bytes into b starting at position off.

Overrides:
read in class java.io.FilterInputStream
Parameters:
b - the array to read into.
off - the offset to start from.
len - the maximum length to read.
Returns:
the number of bytes read, or -1 for EOF.
Throws:
java.io.IOException - if an I/O error occurs.

skip

public long skip(long n)
          throws java.io.IOException
skips at most n bytes of the underlying stream.

Overrides:
skip in class java.io.FilterInputStream
Parameters:
n - the maximum number of bytes to skip.
Returns:
the number actually skipped.
Throws:
java.io.IOException - if an I/O error occurs.

available

public int available()
              throws java.io.IOException
returns the maximum number of bytes that can be read without blocking.

Overrides:
available in class java.io.FilterInputStream
Returns:
the number
Throws:
java.io.IOException - if an I/O error occurs.

remaining

public long remaining()
The number of bytes remaining until the dam is reached, or -1 if no dam is active.


mark

public void mark(int readlimit)
Sets a mark for mark/reset operations. Well, it would if these were supported, but they aren't, so it actually does nothing.

Overrides:
mark in class java.io.FilterInputStream
Parameters:
readlimit - the length readable before the mark becomes invalid (if it were valid at all).

reset

public void reset()
           throws java.io.IOException
repostitions the stream over a previously set mark. But this is not supported, so it throws.

Overrides:
reset in class java.io.FilterInputStream
Throws:
java.io.IOException - if called.

markSupported

public boolean markSupported()
indicates whether mark/reset operations are supported, which they are not.

Overrides:
markSupported in class java.io.FilterInputStream
Returns:
false (mark/reset is not supported).

setDam

public void setDam(long quota)
Sets the dam to a new quota, relative to the current position. -1 to disable.