com.jkristian.lang
Class ByteArraySequence

java.lang.Object
  extended by com.jkristian.lang.ByteArraySequence
All Implemented Interfaces:
ByteSequence

public class ByteArraySequence
extends java.lang.Object
implements ByteSequence

A ByteSequence that simply contains an array of bytes. The array is referenced, not copied; so changes to the array are visible via the containing object.

Author:
John Kristian

Constructor Summary
ByteArraySequence(byte[] data)
           
 
Method Summary
 byte byteAt(int index)
           
static ByteArraySequence copy(byte[] data)
          Copy the given data into a new ByteArraySequence.
static ByteArraySequence copy(byte[] data, int start, int end)
          Copy the given data into a new ByteArraySequence.
static ByteArraySequence copy(ByteSequence data)
          Copy the given data into a new ByteArraySequence.
static ByteArraySequence copy(ByteSequence data, int start, int end)
          Copy the given data into a new ByteArraySequence.
 void copy(int start, int end, byte[] into)
          Copy a sub-sequence into a given array.
 void copy(int start, int end, byte[] into, int intoStart)
          Copy a sub-sequence into a given array.
 boolean equals(java.lang.Object that)
           
 int hashCode()
           
 int length()
           
 ByteSequence subSequence(int start, int end)
          Create a read-only view of this sequence.
 byte[] toByteArray()
          Copy this sequence into a new array.
 byte[] toByteArray(int start, int end)
          Copy a sub-sequence into a new array.
 java.lang.String toString()
           
 java.lang.String toString(int start, int end, java.lang.String encoding)
          new String(toByteArray(start, end), encoding)
 java.lang.String toString(java.lang.String encoding)
          toString(0, length(), encoding)
 void writeTo(int start, int end, java.io.OutputStream out)
          Copy a sub-sequence into an OutputStream.
 void writeTo(java.io.OutputStream out)
          Copy this sequence into an OutputStream.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ByteArraySequence

public ByteArraySequence(byte[] data)
Method Detail

copy

public static ByteArraySequence copy(byte[] data)
Copy the given data into a new ByteArraySequence. Equivalent to copy(data, 0, data.length).


copy

public static ByteArraySequence copy(byte[] data,
                                     int start,
                                     int end)
Copy the given data into a new ByteArraySequence. The returned object is almost unmodifiable: it can be modified via reflection, or by writing it to an OutputStream that modifies the output data (which is very unusual behavior for an OutputStream).


copy

public static ByteArraySequence copy(ByteSequence data)
Copy the given data into a new ByteArraySequence. Equivalent to copy(data, 0, data.length()).


copy

public static ByteArraySequence copy(ByteSequence data,
                                     int start,
                                     int end)
Copy the given data into a new ByteArraySequence. The returned object is almost unmodifiable: it can be modified via reflection, or by writing it to an OutputStream that modifies the output data (which is very unusual behavior for an OutputStream).


length

public int length()
Specified by:
length in interface ByteSequence

byteAt

public byte byteAt(int index)
Specified by:
byteAt in interface ByteSequence

copy

public void copy(int start,
                 int end,
                 byte[] into)
Description copied from interface: ByteSequence
Copy a sub-sequence into a given array. Equivalent to copy(start, end, into, 0).

Specified by:
copy in interface ByteSequence

copy

public void copy(int start,
                 int end,
                 byte[] into,
                 int intoStart)
Description copied from interface: ByteSequence
Copy a sub-sequence into a given array.

Specified by:
copy in interface ByteSequence

toByteArray

public byte[] toByteArray()
Description copied from interface: ByteSequence
Copy this sequence into a new array. Equivalent to toByteArray(0, length()).

Specified by:
toByteArray in interface ByteSequence

toByteArray

public byte[] toByteArray(int start,
                          int end)
Description copied from interface: ByteSequence
Copy a sub-sequence into a new array.

Specified by:
toByteArray in interface ByteSequence

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(java.lang.String encoding)
                          throws java.io.UnsupportedEncodingException
Description copied from interface: ByteSequence
toString(0, length(), encoding)

Specified by:
toString in interface ByteSequence
Throws:
java.io.UnsupportedEncodingException

toString

public java.lang.String toString(int start,
                                 int end,
                                 java.lang.String encoding)
                          throws java.io.UnsupportedEncodingException
Description copied from interface: ByteSequence
new String(toByteArray(start, end), encoding)

Specified by:
toString in interface ByteSequence
Throws:
java.io.UnsupportedEncodingException

writeTo

public void writeTo(java.io.OutputStream out)
             throws java.io.IOException
Description copied from interface: ByteSequence
Copy this sequence into an OutputStream. Equivalent to writeTo(0, length(), out).

Specified by:
writeTo in interface ByteSequence
Throws:
java.io.IOException

writeTo

public void writeTo(int start,
                    int end,
                    java.io.OutputStream out)
             throws java.io.IOException
Description copied from interface: ByteSequence
Copy a sub-sequence into an OutputStream.

Specified by:
writeTo in interface ByteSequence
Throws:
java.io.IOException

subSequence

public ByteSequence subSequence(int start,
                                int end)
Description copied from interface: ByteSequence
Create a read-only view of this sequence.

The returned sequence shares this sequence's content; that is, any changes to the bytes in the given range of this sequence will be visible in the sub-sequence.

Specified by:
subSequence in interface ByteSequence

equals

public boolean equals(java.lang.Object that)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object