org.wegra.io
Class RandomInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byorg.wegra.io.RandomInputStream

public final class RandomInputStream
extends java.io.InputStream

InputStream which produces pseudorandom data. This class is not thread-safe.

Version:
1.0 (2003.06.18)
Author:
wegra(Bok-Youn Lee)

Nested Class Summary
static class RandomInputStream.Test
          Inner class for testing RandomInputStream
 
Constructor Summary
RandomInputStream()
          Creates a RandomInputStream as default setting.
RandomInputStream(int size)
          Creates a RandomInputStream has specific size.
RandomInputStream(int size, long seed)
          Creates a RandomInputStream has specific seed and data size.
RandomInputStream(long seed)
          Creates a RandomInputStream has specific seed to generate pseudo-random data.
 
Method Summary
 int available()
          Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
 void close()
          Closes this input stream and releases any system resources associated with the stream.
 boolean markSupported()
          Returns false, because this input stream doesn't support mark operation.
 int read()
          Reads the next byte of data from the input stream.
 int read(byte[] buffer)
          Reads some number of bytes from the input stream and stores them into the buffer array buffer.
 int read(byte[] buffer, int offset, int length)
          Reads up to length bytes of data from the input stream into an array of bytes.
 void reset()
          Throws IOException because this input stream does not support mark/reset operation.
 long skip(long number)
          Skips over and discards number bytes of data from this input stream.
 
Methods inherited from class java.io.InputStream
mark
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomInputStream

public RandomInputStream()
Creates a RandomInputStream as default setting. This stream's data size is unlimited.


RandomInputStream

public RandomInputStream(int size)
Creates a RandomInputStream has specific size.

Parameters:
size - size(length) of stream.

RandomInputStream

public RandomInputStream(long seed)
Creates a RandomInputStream has specific seed to generate pseudo-random data. This stream's data size is unlimited.

Parameters:
seed - seed to generate pseudo-random data.

RandomInputStream

public RandomInputStream(int size,
                         long seed)
Creates a RandomInputStream has specific seed and data size.

Parameters:
size - size of stream.
seed - seed to generate pseudo-random data.
Method Detail

available

public int available()
              throws java.io.IOException
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or or another thread.

Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
java.io.IOException - If this stream was closed already.

close

public void close()
Closes this input stream and releases any system resources associated with the stream.


markSupported

public boolean markSupported()
Returns false, because this input stream doesn't support mark operation.

Returns:
false.
See Also:
InputStream.mark(int), reset()

read

public int read()
         throws java.io.IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - If this stream was closed already.

read

public int read(byte[] buffer)
         throws java.io.IOException
Reads some number of bytes from the input stream and stores them into the buffer array buffer. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

If buffer is null, a NullPointerException is thrown. If the length of buffer is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into buffer.

The first byte read is stored into element buffer[0], the next one into buffer[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements buffer[0] through buffer[k - 1] , leaving elements buffer[k] through buffer[buffer.length - 1] unaffected.

If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed.

Parameters:
buffer - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - Never throws.
See Also:
read(byte[], int, int)

read

public int read(byte[] buffer,
                int offset,
                int length)
         throws java.io.IOException
Reads up to length bytes of data from the input stream into an array of bytes. An attempt is made to read as many as length bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If buffer is null, a NullPointerException is thrown.

If offset is negative, or length is negative, or offset + length is greater than the length of the array buffer, then an IndexOutOfBoundsException is thrown.

If length is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into buffer.

The first byte read is stored into element buffer[offset] , the next one into buffer[offset + 1], and so on. The number of bytes read is, at most, equal to length. Let k be the number of bytes actually read; these bytes will be stored in elements buffer[offset] through buffer[offset + k -1], leaving elements buffer[offset + k] through b[off + length - 1] unaffected.

In every case, elements buffer[0] through buffer[offset] and elements buffer[offset + length] through buffer[buffer.length-1] are unaffected.

If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed.

Parameters:
buffer - the buffer into which the data is read.
offset - the start offset in array buffer at which the data is written.
length - the maximum number of bytes to read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - If this stream was closed already.
See Also:
read()

skip

public long skip(long number)
          throws java.io.IOException
Skips over and discards number bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before number bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If number is negative, no bytes are skipped.

Parameters:
number - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - If this stream was closed already.

reset

public void reset()
           throws java.io.IOException
Throws IOException because this input stream does not support mark/reset operation.

Throws:
java.io.IOException - always if you call.