Package edu.rit.util
Class ByteSequence
java.lang.Object
edu.rit.util.ByteSequence
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.
- Version:
- 02-Nov-2006
- Author:
- Alan Kaminsky
-
Constructor Summary
ConstructorsConstructorDescriptionByteSequence(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
Modifier and TypeMethodDescriptionintcopy(byte[] buf) Copy this byte sequence's contents into the given byte array.intcopy(byte[] buf, int off, int len) Copy this byte sequence's contents into a portion of the given byte array.intlength()Obtain the length of this byte sequence.byte[]Obtain a byte array with a copy of this byte sequence's contents.voidwrite(DataOutput theOutputStream) Write this byte sequence's contents to the given data output stream.voidwrite(OutputStream theOutputStream) Write this byte sequence's contents to the given output stream.
-
Constructor Details
-
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 ifbufis 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 ifbufis null.IndexOutOfBoundsException- Thrown ifoff< 0,len< 0, oroff+len>buf.length.
-
ByteSequence
Construct a new byte sequence whose contents come from the given input stream. Bytes are read fromtheInputStreaminto the byte sequence until the end-of-stream is encountered, thentheInputStreamis closed. IftheInputStreamis null, the byte sequence's length is 0.- Parameters:
theInputStream- Input stream, or null.- Throws:
IOException- Thrown if an I/O error occurred.IOException- if any.
-
ByteSequence
Construct a new byte sequence whose contents are a copy of the given byte sequence.- Parameters:
theByteSequence- Byte sequence to copy.- Throws:
NullPointerException- Thrown iftheByteSequenceis null.
-
-
Method Details
-
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 intobufstarting at index 0. The number of bytes copied isbuf.lengthor 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 ifbufis 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 intobufstarting at indexoff. The number of bytes copied islenor this byte sequence's length, whichever is smaller.- Parameters:
buf- Buffer to hold the copy.off- Index inbufat which to start copying.len- Maximum number of bytes to copy.- Returns:
- Actual number of bytes copied.
- Throws:
NullPointerException- (unchecked exception) Thrown ifbufis null.IndexOutOfBoundsException- Thrown ifoff< 0,len< 0, oroff+len>buf.length.
-
write
Write this byte sequence's contents to the given output stream.- Parameters:
theOutputStream- Output stream.- Throws:
IOException- Thrown if an I/O error occurred.IOException- if any.
-
write
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.IOException- if any.
-