edu.rit.util
Class ByteSequence

java.lang.Object
  extended by edu.rit.util.ByteSequence

public class ByteSequence
extends Object

Class ByteSequence provides an abstraction for a sequence of bytes. The contents of the byte sequence are specified at construction time; the contents may come from a byte array, an input stream, or another byte sequence. You can obtain the byte sequence's contents as a byte array or write the byte sequence's contents to an output stream.


Constructor Summary
ByteSequence(byte[] buf)
          Construct a new byte sequence whose contents are a copy of the given byte array.
ByteSequence(byte[] buf, int off, int len)
          Construct a new byte sequence whose contents are a copy of a portion of the given byte array.
ByteSequence(ByteSequence theByteSequence)
          Construct a new byte sequence whose contents are a copy of the given byte sequence.
ByteSequence(InputStream theInputStream)
          Construct a new byte sequence whose contents come from the given input stream.
 
Method Summary
 int copy(byte[] buf)
          Copy this byte sequence's contents into the given byte array.
 int copy(byte[] buf, int off, int len)
          Copy this byte sequence's contents into a portion of the given byte array.
 int length()
          Obtain the length of this byte sequence.
 byte[] toByteArray()
          Obtain a byte array with a copy of this byte sequence's contents.
 void write(DataOutput theOutputStream)
          Write this byte sequence's contents to the given data output stream.
 void write(OutputStream theOutputStream)
          Write this byte sequence's contents to the given output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteSequence

public ByteSequence(byte[] buf)
Construct a new byte sequence whose contents are a copy of the given byte array.

Parameters:
buf - Byte array to copy.
Throws:
NullPointerException - Thrown if buf is null.

ByteSequence

public ByteSequence(byte[] buf,
                    int off,
                    int len)
Construct a new byte sequence whose contents are a copy of a portion of the given byte array.

Parameters:
buf - Byte array to copy.
off - Index of first byte to copy.
len - Number of bytes to copy.
Throws:
NullPointerException - Thrown if buf is null.
IndexOutOfBoundsException - Thrown if off < 0, len < 0, or off+len > buf.length.

ByteSequence

public ByteSequence(InputStream theInputStream)
             throws IOException
Construct a new byte sequence whose contents come from the given input stream. Bytes are read from theInputStream into the byte sequence until the end-of-stream is encountered, then theInputStream is closed. If theInputStream is null, the byte sequence's length is 0.

Parameters:
theInputStream - Input stream, or null.
Throws:
IOException - Thrown if an I/O error occurred.

ByteSequence

public ByteSequence(ByteSequence theByteSequence)
Construct a new byte sequence whose contents are a copy of the given byte sequence.

Parameters:
theByteSequence - Byte sequence to copy.
Throws:
NullPointerException - Thrown if theByteSequence is null.
Method Detail

length

public int length()
Obtain the length of this byte sequence.

Returns:
Number of bytes.

toByteArray

public byte[] toByteArray()
Obtain a byte array with a copy of this byte sequence's contents. A new byte array of the proper size is created and returned.

Returns:
Contents.

copy

public int copy(byte[] buf)
Copy this byte sequence's contents into the given byte array. Bytes are copied into buf starting at index 0. The number of bytes copied is buf.length or this byte sequence's length, whichever is smaller.

Parameters:
buf - Buffer to hold the copy.
Returns:
Actual number of bytes copied.
Throws:
NullPointerException - (unchecked exception) Thrown if buf is null.

copy

public int copy(byte[] buf,
                int off,
                int len)
Copy this byte sequence's contents into a portion of the given byte array. Bytes are copied into buf starting at index off. The number of bytes copied is len or this byte sequence's length, whichever is smaller.

Parameters:
buf - Buffer to hold the copy.
off - Index in buf at which to start copying.
len - Maximum number of bytes to copy.
Returns:
Actual number of bytes copied.
Throws:
NullPointerException - (unchecked exception) Thrown if buf is null.
IndexOutOfBoundsException - Thrown if off < 0, len < 0, or off+len > buf.length.

write

public void write(OutputStream theOutputStream)
           throws IOException
Write this byte sequence's contents to the given output stream.

Parameters:
theOutputStream - Output stream.
Throws:
IOException - Thrown if an I/O error occurred.

write

public void write(DataOutput theOutputStream)
           throws IOException
Write this byte sequence's contents to the given data output stream.

Parameters:
theOutputStream - Data output stream.
Throws:
IOException - Thrown if an I/O error occurred.


Copyright © 2005-2012 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.