javaFlacEncoder
Class EncodedElement

java.lang.Object
  extended by javaFlacEncoder.EncodedElement
Direct Known Subclasses:
EncodedElement_32

public class EncodedElement
extends java.lang.Object

EncodedElement class provides is used to store data in the proper bitstream format for FLAC audio. Methods are provided to easily append values to the bitstream. Conceptually, an EncodedElement is a list structure, in which the encoded data is stored in a byte array. It is assumed that any data stored by objects of this class will be retrieved through direct access to the underlying byte array. This byte array is exposed to outside objects primarily to allow faster access than "proper" object-oriented design might allow.

Author:
Preston Lacey

Field Summary
protected  int offset
          Used to signify the index of the first valid bit of the data array.
 
Constructor Summary
EncodedElement()
          Constructor, creates an empty element with offset of zero and array size of 100.
EncodedElement(byte[] array, int off)
          Constructor.
EncodedElement(byte[] array, int off, int usedBits)
           
EncodedElement(int size, int off)
          Constructor.
 
Method Summary
 EncodedElement addInt(int input, int bitCount)
          Add a number of bits from an int to the end of this list's data.
static void addInt(int value, int count, int startPos, byte[] dest)
          This method adds a given number of bits of an int to a byte array.
 EncodedElement addLong(long input, int bitCount)
          Add a number of bits from a long to the end of this list's data.
static void addLong(long input, int count, int startPos, byte[] dest)
          This method adds a given number of bits of a long to a byte array.
 boolean attachEnd(EncodedElement e)
          Attach an element to the end of this list.
 void clear(int size, int off)
          Completely clear this element and use the given size and offset for the new data array.
 void clear(int size, int off, boolean keep)
          Completely clear this element and use the given size and offset for the new data array.
 short getCRC16()
           
protected static EncodedElement getEnd_S(EncodedElement e)
          Return the last element of the list given.
 EncodedElement getEnd()
          Return the last element of the list given.
 int getTotalBits()
          Total number of usable bits stored by this entire list.
 EncodedElement packInt(int[] inputArray, int bitSize, int start, int skip, int countA)
          Append an equal number of bits from each int in an array within given limits to the end of this list.
static void packInt(int[] inputArray, int bitSize, int startPosIn, int start, int skip, int countA, byte[] dest)
          Append an equal number of bits from each int in an array within given limits to the given byte array.
 EncodedElement packIntByBits(int[] inputA, int[] inputBits, int inputOffset, int countA)
          Pack a number of bits from each int of an array(within given limits)to the end of this list.
static void packIntByBits(int[] inputA, int[] inputBits, int inputOffset, int countA, int startPosIn, byte[] dest)
          Pack a number of bits from each int of an array(within given limits)to the end of this list.
 boolean padToByte()
          Force the usable data stored in this list ends on a a byte boundary, by padding to the end with zeros.
protected  void print()
           
protected  void print(int childCount)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

offset

protected int offset
Used to signify the index of the first valid bit of the data array. For purposes of speed, it is not always best to pack the data starting at bit zero of first byte. offset must always be less than or equal to usableBits.

Constructor Detail

EncodedElement

public EncodedElement()
Constructor, creates an empty element with offset of zero and array size of 100. This array can be replaced with a call to setData(...).


EncodedElement

public EncodedElement(byte[] array,
                      int off)
Constructor. Creates an EncodedElement with the given size and offset

Parameters:
array - Byte array to use for this element.
off - Offset to use for this element. Usablebits will also be set to this value.

EncodedElement

public EncodedElement(byte[] array,
                      int off,
                      int usedBits)

EncodedElement

public EncodedElement(int size,
                      int off)
Constructor. Creates an EncodedElement with the given size and offset

Parameters:
size - Size of data array to use(in bytes)
off - Offset to use for this element. Usablebits will also be set to this value.
Method Detail

clear

public void clear(int size,
                  int off)
Completely clear this element and use the given size and offset for the new data array.

Parameters:
size - Size of data array to use(in bytes)
off - Offset to use for this element. Usablebits will also be set to this value.

clear

public void clear(int size,
                  int off,
                  boolean keep)
Completely clear this element and use the given size and offset for the new data array.

Parameters:
size - Size of data array to use(in bytes)
off - Offset to use for this element. Usablebits will also be set to this value.
keep - true to keep current backing array, false to create new one.

getEnd_S

protected static EncodedElement getEnd_S(EncodedElement e)
Return the last element of the list given. This is a static funtion to provide minor speed improvement. Loops through all elements' "next" pointers, till the last is found.

Parameters:
e - EncodedElement list to find end of.
Returns:
Final element in list.

getEnd

public EncodedElement getEnd()
Return the last element of the list given. Loops through all elements' "next" pointers, till the last is found.

Returns:
last element in this list

attachEnd

public boolean attachEnd(EncodedElement e)
Attach an element to the end of this list.

Parameters:
e - Element to attach.
Returns:
True if element was attached, false otherwise.

addLong

public EncodedElement addLong(long input,
                              int bitCount)
Add a number of bits from a long to the end of this list's data. Will add a new element if necessary. The bits stored are taken from the lower- order of input.

Parameters:
input - Long containing bits to append to end.
bitCount - Number of bits to append.
Returns:
EncodedElement which actually contains the appended value.

addInt

public EncodedElement addInt(int input,
                             int bitCount)
Add a number of bits from an int to the end of this list's data. Will add a new element if necessary. The bits stored are taken from the lower- order of input.

Parameters:
input - Int containing bits to append to end.
bitCount - Number of bits to append.
Returns:
EncodedElement which actually contains the appended value.

packInt

public EncodedElement packInt(int[] inputArray,
                              int bitSize,
                              int start,
                              int skip,
                              int countA)
Append an equal number of bits from each int in an array within given limits to the end of this list.

Parameters:
inputArray - Array storing input values.
bitSize - number of bits to store from each value.
start - index of first usable index.
skip - number of indices to skip between values(in case input data is interleaved with non-desirable data).
countA - Number of total indices to store from.
Returns:
EncodedElement containing end of packed data. Data may flow between multiple EncodedElement's, if an existing element was not large enough for all values.

packIntByBits

public EncodedElement packIntByBits(int[] inputA,
                                    int[] inputBits,
                                    int inputOffset,
                                    int countA)
Pack a number of bits from each int of an array(within given limits)to the end of this list.

Parameters:
inputA - Array containing input values.
inputBits - Array containing number of bits to use for each index packed. This array should be equal in size to the inputA array.
inputOffset - Index of first usable index.
countA - Number of indices to pack.
Returns:
EncodedElement containing end of packed data. Data may flow between multiple EncodedElement's, if an existing element was not large enough for all values.

getTotalBits

public int getTotalBits()
Total number of usable bits stored by this entire list. This sums the difference of each list element's "usableBits" and "offset".

Returns:
Total valid bits in this list.

addInt

public static void addInt(int value,
                          int count,
                          int startPos,
                          byte[] dest)
This method adds a given number of bits of an int to a byte array.

Parameters:
value - int to store bits from
count - number of low-order bits to store
startPos - start bit location in array to begin writing
dest - array to store bits in. dest MUST have enough space to store the given data, or this function will fail.

addLong

public static void addLong(long input,
                           int count,
                           int startPos,
                           byte[] dest)
This method adds a given number of bits of a long to a byte array.

Parameters:
input - long to store bits from
count - number of low-order bits to store
startPos - start bit location in array to begin writing
dest - array to store bits in. dest MUST have enough space to store the given data, or this function will fail.

packInt

public static void packInt(int[] inputArray,
                           int bitSize,
                           int startPosIn,
                           int start,
                           int skip,
                           int countA,
                           byte[] dest)
Append an equal number of bits from each int in an array within given limits to the given byte array.

Parameters:
inputArray - Array storing input values.
bitSize - number of bits to store from each value.
start - index of first usable index.
skip - number of indices to skip between values(in case input data is interleaved with non-desirable data).
countA - Number of total indices to store from.
startPosIn - First usable index in destination array(byte index = startPosIn/8, bit within that byte = startPosIn%8)
dest - Destination array to store input values in. This array *must* be large enough to store all values or this method will fail in an undefined manner.

packIntByBits

public static void packIntByBits(int[] inputA,
                                 int[] inputBits,
                                 int inputOffset,
                                 int countA,
                                 int startPosIn,
                                 byte[] dest)
Pack a number of bits from each int of an array(within given limits)to the end of this list.

Parameters:
inputA - Array containing input values.
inputBits - Array containing number of bits to use for each index packed. This array should be equal in size to the inputA array.
inputOffset - Index of first usable index.
countA - Number of indices to pack.
startPosIn - First usable bit-level index in destination array(byte index = startPosIn/8, bit within that byte = startPosIn%8)
dest - Destination array to store input values in. This array *must* be large enough to store all values or this method will fail in an undefined manner.

padToByte

public boolean padToByte()
Force the usable data stored in this list ends on a a byte boundary, by padding to the end with zeros.

Returns:
true if the data was padded, false if it already ended on a byte boundary.

getCRC16

public short getCRC16()

print

protected void print()

print

protected void print(int childCount)