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
ConstructorDescriptionByteSequence
(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 TypeMethodDescriptionint
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[]
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.
-
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 ifbuf
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 ifbuf
is 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 fromtheInputStream
into the byte sequence until the end-of-stream is encountered, thentheInputStream
is closed. IftheInputStream
is 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 iftheByteSequence
is 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 intobuf
starting at index 0. The number of bytes copied isbuf.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 ifbuf
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 intobuf
starting at indexoff
. The number of bytes copied islen
or this byte sequence's length, whichever is smaller.- Parameters:
buf
- Buffer to hold the copy.off
- Index inbuf
at which to start copying.len
- Maximum number of bytes to copy.- Returns:
- Actual number of bytes copied.
- Throws:
NullPointerException
- (unchecked exception) Thrown ifbuf
is 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.
-