org.vishia.byteData
Class ByteDataAccess

java.lang.Object
  extended by org.vishia.byteData.ByteDataAccess
Direct Known Subclasses:
ByteDataAccessDbg, ByteDataAccessExample.MyDataAccess_A, ByteDataAccessExample.MyDataAccess_head, DateValues_Jc, Field_Jc, InspcDataExchangeAccess.Datagram, InspcDataExchangeAccess.Info, InspcDataExchangeAccess.SetValue, MainEmulation.WriteValues.GrvLineAccess, MemUnit, OamVariables.OamVariablesByteAccess, Object_Jc, RawDataAccess

public abstract class ByteDataAccess
extends java.lang.Object

This class is a base class to control the access to binary data. The binary data may typically used or produced from a part of software written in C or C++. There the binary data are struct-constructs.
It is able to support several kinds of struct constructs:

This application possibilities show a capable of development system to access binary data. The other, hand made way was calculate with indices of the byte[idx] specially user programmed. This class helps to make such complex index calculation needlessly. One struct at C level corresponds with one derivated class of ByteDataAccess. But last also a generation of the java code from C/C++-header files containing the structs is able to. So the definition of byte positions are made only on one source. The C/C++ is primary thereby.

children, currentChild, addChild

Children are used to evaluate or write different data structures after a known structure. The children may be from several derivated types of this class. With children and children inside children a tree of different data can be built or evaluated. If no child is added yet, the indices have the following values: A call of next() sets the indices to a possible but not yet defined current child: A call of addChild() or its adequate addChildXY() sets the indices to the given current child: The length of the current Child may be set while evaluating the child's data. The user should be call setLengthElement(int) with the child or setLengthCurrentChildElement(int) with the parent, respectively with this. If this methods are not called, but next() or addChild...() is called however, without a known length but this (the parent) knows the rules to determine the length of its possible children, it is possible to do that. The method specifyLengthCurrentChildElement() supplied the number of bytes. But if this method is not overwritten in the inherited class, an exception is thrown.
The UML structure of such an class in a environment may be shown with the followed object model diagram,
<+>---> is a composition, <>---> is a aggregation, <|---- is a inherition.
                      +-------------------------------+                 +----------+
                      | ByteDataAccess                |----data-------->| byte[]   |
                      |-------------------------------|                 +----------+
                      |idxBegin:int                   |
                      |idxChild:int                   |<---------------+ a known parent
  +-------------+     |idxEnd:int                     |---parent-------+ setted in addChild()
  | derivated   |     |-------------------------------|
  | user        |---|>|specifyLengthElement()         |
  | classes     |     |specifyLengthElementHead()     |--currentChild--+ the actual child element
  +-------------+     |specifyLengthCurrentChild()    |<---------------+
                      +-------------------------------+
 


Field Summary
static int _version_
          The version.
protected  boolean bBigEndian
          Flag is set or get data in big endian or little endian (if false)
protected  boolean bExpand
           
(package private)  java.lang.String charset
          The charset.
private  ByteDataAccess currentChild
          The child on end to add() something
protected  byte[] data
          The array containing the binary data.
protected  int idxBegin
          Index of the beginning of the actual element in data
protected  int idxCurrentChild
          Index within the at position of the current child element.
protected  int idxCurrentChildEnd
          Index of the currents child end.
protected  int idxEnd
          Index of the end of the actual element in data
protected  int idxFirstChild
          Index of the first child element, it is after the head.
static byte kEndOfElements
          Definition of the code of end of information, return from next()
protected static int kIdxElementCode
          Index in the data, position of element code
static byte kNothing
          Definition of the code of no information, return from next()
static byte kText
          Aussage: es ist ein String (XML: text()), kein Tag im String
static byte kUndefined
          coding: the value is undefined
private  ByteDataAccess parent
          The parent XmlBinCodeElement, necessary only for add() and expand().
 
Constructor Summary
ByteDataAccess()
          Constructs a new empty instance.
 
Method Summary
protected  long _getLong(int idxInChild, int nrofBytesAndSign)
          Returns the content of 1 to 8 bytes inside the actual element as a long number, big- or little-endian depending on setBigEndian().
protected  java.lang.String _getString(int idx, int nrofBytes)
          sets the content inside the acutal element with the character bytes from the given String.
protected  void _setLong(int idx, int nrofBytes, long val)
          sets the content of 1 to 8 bytes inside the actual element as a long number, big- or little-endian depending on setBigEndian().
protected  void _setString(int idx, int nrofBytes, java.lang.String value, java.lang.String sEncoding, boolean preventCtrlChars)
          sets the content inside the actual element with the character bytes from the given String.
 boolean addChild(ByteDataAccess child)
          adds an child Element after the current child or as first child after head.
 void addChildEmpty(ByteDataAccess child)
          Adds a child Element at current end of data to write data.
 void addChildFloat(float value)
          Adds a child for 1 integer value without a child instance, and sets the value as integer.
 void addChildInteger(int nrofBytes, long value)
          Adds a child for 1 integer value without a child instance, and sets the value as integer.
 void addChildString(java.lang.CharSequence value)
          Adds a child with String value.
 void addChildString(java.lang.CharSequence value, java.lang.String sEncoding)
          Adds a child with String value.
 void addChildString(java.lang.String value)
          Adds a child with String value.
 void addChildString(java.lang.String value, java.lang.String sEncoding, boolean preventCtrlChars)
          Adds a child with String value.
 int addText(java.lang.String ss)
          Deprecated.  
 void assignAsChild(ByteDataAccess parent)
          Deprecated. use addChild()
 void assignAtIndex(int idxChildInParent, ByteDataAccess parent)
          assigns the element to the given position of the parents data to present a child of the parent.
 void assignAtIndex(int idxChildInParent, int lengthChild, ByteDataAccess parent)
          assigns the element to the given position of the parents data to present a child of the parent with a defined length.
protected  void assignCasted_i(ByteDataAccess src, int offsetCastToInput, int lengthDst)
          Assigns this element to the same position in data, but it is another view.
 void assignData(byte[] data, int length)
          Assigns new data to this element.
 void assignData(byte[] data, int lengthData, int index)
          Assigns new data to this element at given index in data.
protected  void assignDataToFixChilds()
          This method is called inside all assign...() methods.
protected  void assignDowncast_i(ByteDataAccess input)
          Older form, see protected method assignCasted_i(ByteDataAccess, int ) If a cast is possible, it should be programmed in the derivated class.
 void assignEmpty(byte[] data)
          Initializes a top level, the data are considered as non initalized.
 void copyData(int[] dst)
          copies some data to a int[], primarily to debug a content.
 void copyDataFrom(ByteDataAccess src)
          Copies the data into a byte[]
protected  void correctCurrentChildEnd(int idxEndNew)
          Increments the idxEnd and the idxCurrentChildEnd if a new child is added.
private  void correctIdxChildEnd(int idxCurrentChildEndNew)
          Increments the idxEnd if a new child is added.
 void detach()
          Remove all connections.
 void elementAt(int indexObjectArray)
          Counts the idxChild by given index, idxChild is ByteCount from idxBegin
 void expandParent()
          Expands the end index of the parent, it means the management of the expanse of the data.
protected  char getChar(int idx)
          Returns the content of 1 bytes as ASCII inside the actual element.
 double getChildDouble()
          Adds a child for 1 double value without a child instance, but returns the value as integer.
 float getChildFloat()
          Adds a child for 1 float value without a child instance, but returns the value as integer.
 long getChildInteger(int nrofBytes)
          Adds a child for 1 integer value without a child instance, but returns the value as integer.
 java.lang.String getChildString(int nrofBytes)
          Adds a child for a String value without a child instance, but returns the value as String.
 ByteDataAccess getCurrentChild()
           
 byte[] getData()
          Returns the data buffer itself.
protected  double getDouble(int idx)
           
protected  float getFloat(int idx)
          Gets a float value from the content of 4 byte.
protected  float getFloat(int idxBytes, int idxArray, int lengthArray)
           
protected  short getInt16(int idx)
          Returns the content of 2 bytes as a positive nr between 0..65535 inside the actual element.
protected  int getInt16(int idxBytes, int idxArray, int lengthArray)
           
 int getInt32(int idx)
          Returns the content of 4 bytes inside the actual element as a integer number between -2147483648 and 2147483647, big- or little-endian depending on setBigEndian().
protected  int getInt32(int idxBytes, int idxArray, int lengthArray)
           
protected  long getInt64(int idx)
           
protected  byte getInt8(int idx)
          Returns the content of 1 bytes as a positive or negative nr between -128..127 inside the actual element.
protected  int getInt8(int idxBytes, int idxArray, int lengthArray)
           
 int getLength()
          Returns the length of the existing actual element.
 int getLengthCurrentChildElement()
          Returns the length of the current child element.
 int getLengthHead()
          returns the length of the head.
 int getLengthTotal()
          Returns the length of the data.
 int getMaxNrofBytes()
          returns the number number of bytes there are max available from position of the current child
 int getMaxNrofBytesForNextChild()
          returns the number number of bytes there are max available from position of a next current child
 int getPositionInBuffer()
          Returns the position of the Element data in the assigned buffer.
 int getPositionNextChildInBuffer()
          Returns the position of the current child in the assigned buffer.
protected  java.lang.String getString(int idx, int nmax)
          Returns a String from the given position inside the actual element .
 java.lang.String getText()
          Returns the current string or null on end
protected  int getUint16(int idx)
          Returns the content of 2 bytes as a positive nr between 0..65535, big-endian inside the actual element.
protected  int getUint16(int idxBytes, int idxArray, int lengthArray)
           
 int getUint32(int idx)
           
protected  int getUint32(int idxBytes, int idxArray, int lengthArray)
           
protected  int getUint8(int idx)
          Returns the content of 1 bytes as a positive or negative nr between -128..127 inside the actual element.
protected  int getUint8(int idxBytes, int idxArray, int lengthArray)
           
 boolean isTextByte(byte nn)
          Returns true if the current child element represents a TEXT(), direct ASCII chars, false if the element is a complex element.
 int next()
          Sets the data index to the position after the current child element and returns its code.
protected  void notifyAddChild()
          Notifies, that a child is added.
 void removeChild()
          remove the current child to assign another current child instead of the first one.
 void removeChildren()
          Remove all children.
 void rewind()
          starts the calling loop of next().
 void setBigEndian(boolean val)
          Sets the big or little endian mode.
protected  void setDouble(int idx, double value)
          Set the content of 8 byte from a double variable.
protected  void setFloat(int idx, float value)
          Set the content of 4 byte from a float variable.
protected  void setFloat(int idxBytes, int idxArray, int lengthArray, float val)
           
protected  boolean setIdxCurrentChildEnd(int nrofBytes)
          sets the idxCurrentChildEnd and idxEnd.
private  void setIdxtoNextCurrentChild()
          sets the idxCurrentChild to the known idxCurrentChildEnd.
protected  void setInt16(int idx, int value)
          Set the content of 2 bytes from an integer between -32768..32768, or from an integer number between 0..65535.
protected  void setInt16(int idxBytes, int idxArray, int lengthArray, int val)
           
protected  void setInt32(int idx, int value)
          Set the content of 4 bytes as a integer number between -2147483648 and 2147483647, big- or little-endian depended from setBigEndian().
protected  void setInt32(int idxBytes, int idxArray, int lengthArray, int val)
           
protected  void setInt8(int idx, int value)
          Set the content of 1 bytes as a positive nr between 0..256.
protected  void setInt8(int idxBytes, int idxArray, int lengthArray, int val)
           
 void setLengthCurrentChildElement(int lengthOfCurrentChild)
          Sets the length of the current child element after calling next().
 void setLengthElement(int length)
          Sets the length of the current element, considering all children.
protected  int setString(int idx, int nmax, java.lang.String ss)
          Sets a String to the the given position inside the actual element .
protected  void setUint16(int idx, int value)
          Set the content of 2 bytes as a positive nr between 0..65535, big- or little-endian.
protected  void setUint16(int idxBytes, int idxArray, int lengthArray, int val)
           
protected  void setUint32(int idxBytes, int idxArray, int lengthArray, int val)
           
protected  void setUint32(int idx, long value)
          Set the content of 4 bytes as a positive nr between 0..2pow32-1, big- or little-endian.
protected  void setUint8(int idx, int value)
          Set the content of 1 bytes as a positive nr between 0..255, big- or little-endian.
protected  void setUint8(int idxBytes, int idxArray, int lengthArray, int val)
           
protected abstract  void specifyEmptyDefaultData()
          Sets the elements data to the default empty data.
protected  int specifyLengthCurrentChildElement()
          Returns the length of a child element at current position specified in the derivated class.
protected abstract  int specifyLengthElement()
          Returns the actual length of the whole element presenting with this class.
abstract  int specifyLengthElementHead()
          Specifies the length of the head data.
 boolean sufficingBytesForNextChild(int nrofBytes)
          returns true if the given number of bytes is sufficing in the data from position of next child.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_version_

public static final int _version_
The version.

See Also:
Constant Field Values

data

protected byte[] data
The array containing the binary data.


idxBegin

protected int idxBegin
Index of the beginning of the actual element in data


idxEnd

protected int idxEnd
Index of the end of the actual element in data


bExpand

protected boolean bExpand

idxFirstChild

protected int idxFirstChild
Index of the first child element, it is after the head.


idxCurrentChild

protected int idxCurrentChild
Index within the at position of the current child element. If no current child is known, this index is -1.


idxCurrentChildEnd

protected int idxCurrentChildEnd
Index of the currents child end. If no current child is known this index is equal idxFirstChild, it is the position after the head. If the length of the current child is not known, this index is <= idxCurrentChild.


bBigEndian

protected boolean bBigEndian
Flag is set or get data in big endian or little endian (if false)


parent

private ByteDataAccess parent
The parent XmlBinCodeElement, necessary only for add() and expand().


currentChild

private ByteDataAccess currentChild
The child on end to add() something


charset

java.lang.String charset
The charset.


kEndOfElements

public static final byte kEndOfElements
Definition of the code of end of information, return from next()

See Also:
Constant Field Values

kNothing

public static final byte kNothing
Definition of the code of no information, return from next()

See Also:
Constant Field Values

kText

public static final byte kText
Aussage: es ist ein String (XML: text()), kein Tag im String

See Also:
Constant Field Values

kUndefined

public static final byte kUndefined
coding: the value is undefined

See Also:
Constant Field Values

kIdxElementCode

protected static final int kIdxElementCode
Index in the data, position of element code

See Also:
Constant Field Values
Constructor Detail

ByteDataAccess

public ByteDataAccess()
Constructs a new empty instance. Use assign() to work with it.

Method Detail

getLengthHead

public final int getLengthHead()
returns the length of the head. It is specified by specifyLengthElementHead() of the derivated class.


specifyEmptyDefaultData

protected abstract void specifyEmptyDefaultData()
Sets the elements data to the default empty data. This method should not called outside of this base class, likewise not in the derivated class itself. This method is only called inside this base class itself inside the methods addChild() and assignEmpty(). But only the derivated class knows how to set empty data. It must specify that like the followed sample:
protected void specifyEmptyDefaultData()
{ data[idxBegin + 0] = 0;
  data[idxBegin + kIdxMid] = 0;
  data[idxBegin + kIdxMin] = kNotAValue;
  data[idxBegin + kIdxMax] = kNotAValue;
}


specifyLengthElementHead

public abstract int specifyLengthElementHead()
Specifies the length of the head data. This is the index of the first child relative to the start position of the element. This method is called inside this base class itself inside some methods. Outside the method may be called at example to calculate the size of data.
Only the derivated class knows the position of the first child element. It must specify that like the followed sample:
protected void specifyLengthElementHead()
{ return kIdxFirstChild;  //kIdxFistChild should be defined as static final int.
}

Returns:
Position of the first child element relative to the start position or -1.

specifyLengthElement

protected abstract int specifyLengthElement()
                                     throws java.lang.IllegalArgumentException
Returns the actual length of the whole element presenting with this class. This method should be specified in the derivated class. The method is called inside this base class in the methods assignAsChild() to calculate the idxEnd.
The calculation of the whole length of the element should be considered all children inside this element. The structure is user-specific. Only some examples may be given here: If the actual element are TODO better explain when and why.

Returns:
The length of this existing element or -1, if the length is not fix yet.
Throws:
XmlBinCodeElementException - If the data inside are corrupted, the user can throw this exception.
java.lang.IllegalArgumentException

specifyLengthCurrentChildElement

protected int specifyLengthCurrentChildElement()
                                        throws java.lang.IllegalArgumentException
Returns the length of a child element at current position specified in the derivated class. The derivated class must known its possible child elements and must get there length with enquiry of specifyLengthElement() of the current child types. This method is called inside the getLengthCurrentChildElement()-method esspecially called inside the next()-method to increment the index idxChild. This method is not called if setLengthCurrentChildElement() was called after calling next(). The user may precluding the call of this method if he calls setLengthCurrentChildElement in sequence of the followed sample:
 int eChildCode = dataCode.next();
 switch(eChildCode)
 { case ...:
   { child.assignAsChild(dataCode);             //the child is known yet!
     dataCode.setLengthCurrentChildElement(child.getLength());
     ....
   }
 
 If the user always calls setLengthCurrentChildElement in this manner,
 he don't need to overwrite specifyLengthCurrentChildElement.

Returns:
The length of the existing child element at the current position idxChild.
Throws:
java.lang.IllegalArgumentException

assignData

public void assignData(byte[] data,
                       int length)
                throws java.lang.IllegalArgumentException
Assigns new data to this element.
The user may overwrite this method and call super.assignData(data, length) inside if some additional actions should be done.
This method is also usefull to deassign a current data buffer, call assign(null, 0);.
If the element are using before, its connection to an other parent is dissolved.

Parameters:
data - The data. The length of data may be greater as the number of the significant bytes.
length - Number of significant bytes in data. If length is > data.length, an exception may be thrown in any state of the evaluation.
Throws:
java.lang.IllegalArgumentException - if the length is > data.length

assignData

public void assignData(byte[] data,
                       int lengthData,
                       int index)
                throws java.lang.IllegalArgumentException
Assigns new data to this element at given index in data.
The user may overwrite this method and call super.assignData(data, length) inside if some additional actions should be done.
This method is also usefull to deassign a current data buffer, call assign(null, 0);.
If the element are using before, its connection to an other parent is dissolved.

Parameters:
data - The data. The length of data may be greater as the number of the significant bytes.
lengthData - absolute Number of significant bytes in data from idx=0. If length is > data.length, an exception is thrown. If the length is <0 (especially -1), it means, it is not known outside. Than the element is initialized with its known head length. The length mustn't not ==0, it is tested. Use -1 also if the head length is 0.
index - Start position in data
Throws:
java.lang.IllegalArgumentException

assignEmpty

public final void assignEmpty(byte[] data)
Initializes a top level, the data are considered as non initalized. The length of the head should be a constant value, given from method call specifyLengthElementHead(). The child Positions are set to the end of head, no childs are presumed. The head should be filled with data after that calling some methods like setInt32(int, int).
The children should be added by calling addChild(ByteDataAccess) and filled with data after that.
If the element are using before, its connection to an other parent is dissolved.
Example to shown the principle:
   ...........the data undefined with defined length.........
   +++++                   Head, the length should be known.
        ####****#####****  Space for children,
 

Parameters:
data - The data. The reference should be initialized, it means the data have a defined maximum of length. But it is not tested here.
Throws:
java.lang.IllegalArgumentException

removeChildren

public final void removeChildren()
Remove all children. Let the head unchanged.

Since:
2010-11-16

detach

public final void detach()
Remove all connections. Especially for children.


assignCasted_i

protected final void assignCasted_i(ByteDataAccess src,
                                    int offsetCastToInput,
                                    int lengthDst)
                             throws java.lang.IllegalArgumentException
Assigns this element to the same position in data, but it is another view. This method should be called inside a assignCasted() method if a inner head structure is known and the conclusion of this structure is possible. At result, both ByteDataAccess instances reference the same data, in different views.

Parameters:
src - The known data access
offsetCastToInput - typical 0 if single inherition is used.
Throws:
java.lang.IllegalArgumentException - if a length of the new type is specified but the byte[]-data are shorter. The length of byte[] is tested.

assignDowncast_i

protected final void assignDowncast_i(ByteDataAccess input)
                               throws java.lang.IllegalArgumentException
Older form, see protected method assignCasted_i(ByteDataAccess, int ) If a cast is possible, it should be programmed in the derivated class.

Parameters:
input -
Throws:
java.lang.IllegalArgumentException

assignAsChild

public final void assignAsChild(ByteDataAccess parent)
                         throws java.lang.IllegalArgumentException
Deprecated. use addChild()

assigns the element to the current child position of parent, to represent the current child of the parent. This method should be used by reading out byte[] data if it is detected, that the content of data matches of this derivated type of ByteDataAccess. next() should be called before. The pattern of using is:
 
    int nWhat;    //code of the element
    while( (nWhat = dataElement.next()) != ByteDataAccess.kEndOfElements )
    { switch(nWhat)  //test the first byte of the current element
      { case ByteDataAccess.kText:
        { sText = dataElement.getText();
        } break;
        case code1:
        { code1Element.assignAsChild(dataElement);
          evaluateValue(code1Element);
        } break:
        case code2:
        { code1Element.assignAsChild(dataElement);
          evaluateValue(code1Element);
        } break:
        default: throw new IllegalArgumentException("unknown Element", dataElement);
      }
    }
    
The difference to addChild(ByteDataAccess) is: addChild() is used to writeout to data, addChild() appends the child always after idxEnd, but this method is used to read from data and appends the child at position of the current child.
The data reference is copied, the idxBegin of this element is set to the idxChild of parent, it is the current child position. All other indices are setted calling specifyLengthElementHead(): idxChild and specifyLengthElement(): idxEnd. The idxChildEnd of parent is setted, so calling next() after this operation increments in data after this new child.
If the element are using before, its connection to an other parent is dissolved.

Parameters:
parent - The parent. It should reference data, and a current child position should be set by calling next() before. See sample at next().
Throws:
java.lang.IllegalArgumentException - If the data are wrong. The exception is thrown orginal from specifyLengthElement().

assignAtIndex

public final void assignAtIndex(int idxChildInParent,
                                int lengthChild,
                                ByteDataAccess parent)
                         throws java.lang.IllegalArgumentException
assigns the element to the given position of the parents data to present a child of the parent with a defined length. The difference to addChild(ByteDataAccess) is: The position is given here directly, it should not be the current child but a free child.
The data reference is copied, the idxBegin of this element is set to the idxChild given as parameter. All other indices are set calling specifyLengthElementHead(): idxChild and specifyLengthElement(): idxEnd.
If the element are using before, its connection to an other parent is dissolved.

Parameters:
parent - The parent. It should reference data.
lengthChild - Number of the bytes of the free child.
idxChildInParent - The index of the free child in the data.
Throws:
java.lang.IllegalArgumentException - If the indices are wrong in respect to the data.

assignAtIndex

public final void assignAtIndex(int idxChildInParent,
                                ByteDataAccess parent)
                         throws java.lang.IllegalArgumentException
assigns the element to the given position of the parents data to present a child of the parent. The length of the child is limited to TODO the length of head - or not limited.

Parameters:
parent - The parent. It should reference data.
idxChildInParent - The index of the free child in the data.
Throws:
java.lang.IllegalArgumentException - If the indices are wrong in respect to the data.

assignDataToFixChilds

protected void assignDataToFixChilds()
                              throws java.lang.IllegalArgumentException
This method is called inside all assign...() methods. It should be overridden by a users derivation if any fix childs are present. In the overridden routines the method assignDataAtIndex(byte[], int) should be called for the fix childs. example:

Throws:
java.lang.IllegalArgumentException

addChild

public final boolean addChild(ByteDataAccess child)
                       throws java.lang.IllegalArgumentException
adds an child Element after the current child or as first child after head. With the aid of the child Element the data can be read or write structured.
Some children can be added after a parent like following sample:
 ByteAccessDerivation child = new ByteAccessDerivation();  //empty and unassigned.
 parent.addChild(child);        //The byte[] data of parent are assigned, index after current child index of parent.
 child.addChild(grandchild);    //By adding a child to this child, also the parent's index is corrected.
 

Parameters:
child - The child will be assigned with the data of this at index after the current child's end-index.
Throws:
java.lang.IllegalArgumentException - if the length of the old current child is not determined yet. Either the method specifyLengthElement() should be overwritten or the method setLengthElement(int) for the child or setLengthCurrentChildElement(int) should be called to prevent this exception.
java.lang.IllegalArgumentException - if the length of the head of the new current child is to far for the data. It means, child.idxEnd > data.length.

removeChild

public final void removeChild()
                       throws java.lang.IllegalArgumentException
remove the current child to assign another current child instead of the first one. This method is usefull if data are tested with several structures. It mustn't be called in expand mode. In expand mode you have to be consider about your children.

Parameters:
child -
Throws:
java.lang.IllegalArgumentException

setIdxtoNextCurrentChild

private final void setIdxtoNextCurrentChild()
sets the idxCurrentChild to the known idxCurrentChildEnd. This method is called while addChild. The state before is:

setIdxCurrentChildEnd

protected final boolean setIdxCurrentChildEnd(int nrofBytes)
                                       throws java.lang.IllegalArgumentException
sets the idxCurrentChildEnd and idxEnd. There are two modi:

Parameters:
nrofBytes - of the child
Returns:
true if the data are expanded.
Throws:
java.lang.IllegalArgumentException - if there are not enough data. In expanded mode the data.length are to less. In using existing data: idxEnd are to less.

addChildEmpty

public final void addChildEmpty(ByteDataAccess child)
                         throws java.lang.IllegalArgumentException
Adds a child Element at current end of data to write data. The child's data are initialized with call of child.specifyEmptyDefaultData().

Parameters:
child - The child will associated to this and should be used to add some content.
Throws:
java.lang.IllegalArgumentException

notifyAddChild

protected void notifyAddChild()
Notifies, that a child is added. This method may be overload, if the user must take somme actions, count the number of childs or others.


correctCurrentChildEnd

protected final void correctCurrentChildEnd(int idxEndNew)
Increments the idxEnd and the idxCurrentChildEnd if a new child is added. Called only inside method addChild(child) and recursively to correct in all parents.


correctIdxChildEnd

private final void correctIdxChildEnd(int idxCurrentChildEndNew)
Increments the idxEnd if a new child is added. Called only inside method addChild(child) and recursively to correct in all parents.


expandParent

public final void expandParent()
                        throws java.lang.IllegalArgumentException
Expands the end index of the parent, it means the management of the expanse of the data.

Throws:
java.lang.IllegalArgumentException

getChildInteger

public final long getChildInteger(int nrofBytes)
                           throws java.lang.IllegalArgumentException
Adds a child for 1 integer value without a child instance, but returns the value as integer.

Parameters:
nrofBytes - of the integer
Returns:
value in long format, cast it to (int) if you read only 4 bytes etc.
Throws:
java.lang.IllegalArgumentException - if not data has not enaught bytes.

getChildFloat

public final float getChildFloat()
                          throws java.lang.IllegalArgumentException
Adds a child for 1 float value without a child instance, but returns the value as integer.

Returns:
value in float format
Throws:
java.lang.IllegalArgumentException - if not data has not enaught bytes.

getChildDouble

public final double getChildDouble()
                            throws java.lang.IllegalArgumentException
Adds a child for 1 double value without a child instance, but returns the value as integer.

Returns:
value in float format
Throws:
java.lang.IllegalArgumentException - if not data has not enaught bytes.

addChildInteger

public final void addChildInteger(int nrofBytes,
                                  long value)
                           throws java.lang.IllegalArgumentException
Adds a child for 1 integer value without a child instance, and sets the value as integer.

Parameters:
nrofBytes - of the integer
Throws:
java.lang.IllegalArgumentException

addChildFloat

public final void addChildFloat(float value)
                         throws java.lang.IllegalArgumentException
Adds a child for 1 integer value without a child instance, and sets the value as integer.

Parameters:
nrofBytes - of the integer
Throws:
java.lang.IllegalArgumentException

getChildString

public final java.lang.String getChildString(int nrofBytes)
                                      throws java.lang.IllegalArgumentException,
                                             java.io.UnsupportedEncodingException
Adds a child for a String value without a child instance, but returns the value as String.

Parameters:
nrofBytes - of the integer
Returns:
value in long format, cast it to (int) if you read only 4 bytes etc.
Throws:
java.lang.IllegalArgumentException - if not data has not enaught bytes.
java.io.UnsupportedEncodingException

addChildString

public final void addChildString(java.lang.String value,
                                 java.lang.String sEncoding,
                                 boolean preventCtrlChars)
                          throws java.lang.IllegalArgumentException,
                                 java.io.UnsupportedEncodingException
Adds a child with String value.

Parameters:
value - String to add
sEncoding - If null then use the standard encoding of the system-environment.
preventCtrlChars - true then values < 0x20 are not set. If the String value contain a control character with code < 0x20, a '?' is written. This behavior guarantees, that byte-values < 0x20 can use to detect no-String elements, see getByteNextChild().
Throws:
java.lang.IllegalArgumentException
java.io.UnsupportedEncodingException

addChildString

public final void addChildString(java.lang.String value)
                          throws java.lang.IllegalArgumentException
Adds a child with String value.

Parameters:
value - String to add
Throws:
java.lang.IllegalArgumentException

addChildString

public final void addChildString(java.lang.CharSequence value,
                                 java.lang.String sEncoding)
                          throws java.lang.IllegalArgumentException,
                                 java.io.UnsupportedEncodingException
Adds a child with String value.

Parameters:
value - String to add, @pjava2c=nonPersistent.
Throws:
java.lang.IllegalArgumentException
java.io.UnsupportedEncodingException

addChildString

public final void addChildString(java.lang.CharSequence value)
                          throws java.lang.IllegalArgumentException
Adds a child with String value.

Parameters:
value - String to add, @pjava2c=nonPersistent.
Throws:
java.lang.IllegalArgumentException

addText

public final int addText(java.lang.String ss)
                  throws java.lang.IllegalArgumentException
Deprecated. 

Writes a String into data with given color. The user can overwrite this method and call super.addText(ss) inside if some additional actions should be done.

Parameters:
ss - The String to write
color - The color
Throws:
java.lang.IllegalArgumentException

rewind

public final void rewind()
starts the calling loop of next(). The calling of next() after them supplies the first child element.


next

public final int next()
               throws java.lang.IllegalArgumentException
Sets the data index to the position after the current child element and returns its code.
If more than 1 byte determines the code, the user should call getInt32(idxChild) to get the code. Usage:
 while( (eElement = code.next()) != kEndOfElements)
 { switch(eElement) { ... }
 } 

Returns:
The first byte, that may be the code of the child element.
If this code is between 0x20 to 0xbf, it is assumed that is an character, kText is returned than.
If no more childs are available, kEndOfElements is returned.
Throws:
java.lang.IllegalArgumentException

sufficingBytesForNextChild

public final boolean sufficingBytesForNextChild(int nrofBytes)
                                         throws java.lang.IllegalArgumentException
returns true if the given number of bytes is sufficing in the data from position of next child.

Parameters:
nrofBytes - that should fitting in the given data range from current child position to the end of data determines by calling assingData(...) or by calling addChild() with a known size of child or setLengthElement() .
Returns:
true if it is okay, false if the nrofBytes are negative or to large.
Throws:
java.lang.IllegalArgumentException - see getMaxNrofBytesForNextChild()

getMaxNrofBytesForNextChild

public final int getMaxNrofBytesForNextChild()
                                      throws java.lang.IllegalArgumentException
returns the number number of bytes there are max available from position of a next current child. , the number of bytes to the end of buffer is returned.

Returns:
nrofBytes that should fitting in the given data range from current child position to the end of data determines by calling assingData(...) or by calling addChild() with a known size of child or setLengthElement() .
Throws:
java.lang.IllegalArgumentException - if the length of the current child is not determined yet. Either the method specifyLengthElement() should be overwritten or the method setLengthElement(int) for the child or setLengthCurrentChildElement(int) should be called to prevent this exception.

getMaxNrofBytes

public final int getMaxNrofBytes()
returns the number number of bytes there are max available from position of the current child. , the number of bytes to the end of buffer is returned.

Returns:
nrofBytes that should fitting in the given data range from current child position to the end of data determines by calling assingData(...) or by calling addChild() with a known size of child or setLengthElement() .

getLength

public final int getLength()
Returns the length of the existing actual element.

Returns:
The number of bytes of the actual element in the buffer. It is (idxEnd - idxBegin).

getLengthTotal

public final int getLengthTotal()
Returns the length of the data.

Returns:
The number of bytes of data in the buffer. It is idxEnd.

getData

public final byte[] getData()
Returns the data buffer itself. The actual total length is getted with getLengthTotal().

Returns:
The number of bytes of the data in the buffer.

getPositionInBuffer

public final int getPositionInBuffer()
Returns the position of the Element data in the assigned buffer.

Returns:
index of this element in the data buffer.

getPositionNextChildInBuffer

public final int getPositionNextChildInBuffer()
Returns the position of the current child in the assigned buffer.

Returns:
index of the current child of this element in the data buffer.

getLengthCurrentChildElement

public final int getLengthCurrentChildElement()
                                       throws java.lang.IllegalArgumentException
Returns the length of the current child element. The length may be setted outside by calling setLengthCurrentChildElement() from user level after any calling of next() or after calling getText() if it is a text content. In this case this method returns this length in a simple way.
If the user don't have called setLengthCurrentChildElement() after the last next(), the users defined specifyLengthCurrentChildElement() is called.

Returns:
the length in bytes of the current child element.
Throws:
java.lang.IllegalArgumentException - if the user has not defined a overloaded methode specifyLengthCurrentChildElement() or this method has thrown the exception because the length is not determinable.

setLengthCurrentChildElement

public final void setLengthCurrentChildElement(int lengthOfCurrentChild)
Sets the length of the current child element after calling next(). The user may set the length due to the knowledge of the type and content of the actual child element. So the calling of specifyLengthCurrentChildElement() will be precluded. The idxChildEnd is setted.


setLengthElement

public final void setLengthElement(int length)
Sets the length of the current element, considering all children. If the element is the current child of its parent, the idxChildEnd of parent is set. Therefore a call of next() or addChild uses the idxChildEnd-position for a new child.

Parameters:
length - The length inclusive all children.

isTextByte

public final boolean isTextByte(byte nn)
Returns true if the current child element represents a TEXT(), direct ASCII chars, false if the element is a complex element. Text chararacters are coded with 0x20..0x7f, the standard ASCII code, and with 0x80 to 0xbf, special characters user defined.


getText

public final java.lang.String getText()
Returns the current string or null on end


getString

protected java.lang.String getString(int idx,
                                     int nmax)
Returns a String from the given position inside the actual element . The bytes are interpreted in the given encoding.

Parameters:
idx - The start position inside the child.
nmax - Maximal number of bytes
Returns:
The String representation of the bytes.

setString

protected int setString(int idx,
                        int nmax,
                        java.lang.String ss)
Sets a String to the the given position inside the actual element . The bytes are interpreted in the given encoding.

Parameters:
idx - The start position inside the child.
nmax - Maximal number of bytes
ss - The String representation of the bytes.
Returns:
Number of written chars.
Throws:

setBigEndian

public void setBigEndian(boolean val)
Sets the big or little endian mode. This method is override-able, because a derived class may set the endian of embedded children too.

Parameters:
val - true if big endian, hi byte at lower adress, false if little endian.

_getLong

protected final long _getLong(int idxInChild,
                              int nrofBytesAndSign)
Returns the content of 1 to 8 bytes inside the actual element as a long number, big- or little-endian depending on setBigEndian(). This method is protected because at user level its using is a prone to errors because the idx is free related.

Parameters:
idxInChild - The position of leading byte in the actual element, the data are taken from data[idxBegin+idx].
nrofBytesAndSign - If positiv, than the method returns the unsigned interpretation of the bytes. If negative, than the return value is negative, if the last significant bit of the given number of bytes is set. The value represents the number of bytes to interprete as integer. It may be 1..8 respectively -1...-8.
Returns:
the long value in range adequate nrof bytes.
Since:
2009-09-30: regards negative nrofBytesAndSign. Prior Versions: returns a signed value always.

_setLong

protected final void _setLong(int idx,
                              int nrofBytes,
                              long val)
sets the content of 1 to 8 bytes inside the actual element as a long number, big- or little-endian depending on setBigEndian(). This method is protected because at user level its using is a prone to errors because the idx is free related.

Parameters:
idx - the position of leading byte in the actual element, the data are set to data[idxBegin+idx].
nrofBytes - The number of bytes of the value.
val - the long value in range adequate nrof bytes.

_getString

protected final java.lang.String _getString(int idx,
                                            int nrofBytes)
                                     throws java.io.UnsupportedEncodingException
sets the content inside the acutal element with the character bytes from the given String. No value < 0x20 is setted. If the String value contain a control character with code < 0x20, a '?' is written. This behavior protected, that bytevalues < 0x20 can use to detect no String elements, see getByteNextChild(). This method is protected because at user level its using is a prone to errors because the idx is free related.

Parameters:
idx - the position in the actual element, the data are set to data[idxBegin+idx].
nrofBytes - The length of the byte[] area to set. If the String value is longer as nrofBytes, it will be truncated. No exception occurs. If the String is shorter as nrofBytes, the rest is filled with 0.
value - The String value.
Returns:
The String which is stored at the designated area.
Throws:
java.io.UnsupportedEncodingException

_setString

protected final void _setString(int idx,
                                int nrofBytes,
                                java.lang.String value,
                                java.lang.String sEncoding,
                                boolean preventCtrlChars)
                         throws java.io.UnsupportedEncodingException
sets the content inside the actual element with the character bytes from the given String. This method is protected because at user level its using is a prone to errors because the idx is free related.

Parameters:
idx - the position in the actual element, the data are set to data[idxBegin+idx].
nrofBytes - The length of the byte[] area to set. If the String value is longer as nrofBytes, it will be truncated. No exception occurs. If the String is shorter as nrofBytes, the rest is filled with 0.
value - The String value.
sEncoding - The encoding of the String. null: Use standard encoding.
preventCtrlChars - true then values < 0x20 are not set. If the String value contain a control character with code < 0x20, a '?' is written. This behavior guarantees, that byte-values < 0x20 can use to detect no-String elements, see getByteNextChild().
Throws:
java.io.UnsupportedEncodingException

copyData

public final void copyData(int[] dst)
copies some data to a int[], primarily to debug a content.

Parameters:
dst - This array is field, but only from data of the current element between idxBegin and idxEnd

getFloat

protected final float getFloat(int idx)
Gets a float value from the content of 4 byte. The float value is red according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values, like converted from java.lang.Float.intBitsToFloat().


getDouble

protected final double getDouble(int idx)

getInt64

protected final long getInt64(int idx)

getInt32

public final int getInt32(int idx)
Returns the content of 4 bytes inside the actual element as a integer number between -2147483648 and 2147483647, big- or little-endian depending on setBigEndian().

Parameters:
idx - the position of leading byte in the actual element, the data are raken from data[idxBegin+idx]. This is not the absolute position in data, idxBegin is added.
Returns:
the integer value in range from -2147483648 and 2147483647

getUint32

public final int getUint32(int idx)

getUint16

protected final int getUint16(int idx)
Returns the content of 2 bytes as a positive nr between 0..65535, big-endian inside the actual element.

Parameters:
idx - the position of leading byte in the actual element, the data are raken from data[idxBegin+idx]. This is not the absolute position in data, idxBegin is added.
Returns:
the integer value in range from -32768 to 32767

getInt16

protected final short getInt16(int idx)
Returns the content of 2 bytes as a positive nr between 0..65535 inside the actual element.

Parameters:
idx - the position of leading byte in the actual element, the data are raken from data[idxBegin+idx]. This is not the absolute position in data, idxBegin is added.
Returns:
the integer value in range from -32768 to 32767

getChar

protected final char getChar(int idx)
Returns the content of 1 bytes as ASCII inside the actual element.

Parameters:
idx - the position of char in the actual element, the data are raken from data[idxBegin+idx]. This is not the absolute position in data, idxBegin is added.
Returns:
the ASCII value

getInt8

protected final byte getInt8(int idx)
Returns the content of 1 bytes as a positive or negative nr between -128..127 inside the actual element.

Parameters:
idx - the position of leading byte in the actual element, the data are raken from data[idxBegin+idx]. This is not the absolute position in data, idxBegin is added.
Returns:
the integer value in range from -32768 to 32767

getUint8

protected final int getUint8(int idx)
Returns the content of 1 bytes as a positive or negative nr between -128..127 inside the actual element.

Parameters:
idx - the position of leading byte in the actual element, the data are raken from data[idxBegin+idx]. This is not the absolute position in data, idxBegin is added.
Returns:
the integer value in range from -32768 to 32767

getUint32

protected final int getUint32(int idxBytes,
                              int idxArray,
                              int lengthArray)

getInt32

protected final int getInt32(int idxBytes,
                             int idxArray,
                             int lengthArray)

getInt16

protected final int getInt16(int idxBytes,
                             int idxArray,
                             int lengthArray)

getInt8

protected final int getInt8(int idxBytes,
                            int idxArray,
                            int lengthArray)

getUint16

protected int getUint16(int idxBytes,
                        int idxArray,
                        int lengthArray)

getUint8

protected int getUint8(int idxBytes,
                       int idxArray,
                       int lengthArray)

getFloat

protected float getFloat(int idxBytes,
                         int idxArray,
                         int lengthArray)

setFloat

protected final void setFloat(int idx,
                              float value)
Set the content of 4 byte from a float variable. The float value is stored according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values, like converted from java.lang.Float.floatToRawIntBits().


setDouble

protected final void setDouble(int idx,
                               double value)
Set the content of 8 byte from a double variable. The double value is stored according to the IEEE 754 floating-point "double format" bit layout, preserving Not-a-Number (NaN) values, like converted from java.lang.Double.doubleToRawLongBits().


setInt32

protected final void setInt32(int idx,
                              int value)
Set the content of 4 bytes as a integer number between -2147483648 and 2147483647, big- or little-endian depended from setBigEndian().

Parameters:
idx - The position of leading byte in the current elements data. This is not the absolute position in data, idxBegin is added.
value - The value in range 0..65535. The value is taken modulo 0xffffffff.

setUint8

protected final void setUint8(int idx,
                              int value)
Set the content of 1 bytes as a positive nr between 0..255, big- or little-endian.

Parameters:
idx - The position of leading byte in the current elements data. This is not the absolute position in data, idxBegin is added.
value - The value in range 0..65535. The value is taken modulo 0xff.

setUint16

protected final void setUint16(int idx,
                               int value)
Set the content of 2 bytes as a positive nr between 0..65535, big- or little-endian.

Parameters:
idx - The position of leading byte in the current elements data. This is not the absolute position in data, idxBegin is added.
value - The value in range 0..65535. The value is taken modulo 0xffff.

setUint32

protected final void setUint32(int idx,
                               long value)
Set the content of 4 bytes as a positive nr between 0..2pow32-1, big- or little-endian.

Parameters:
idx - The position of leading byte in the current elements data. This is not the absolute position in data, idxBegin is added.
value - The value as long. The value is taken modulo 0xffffffff.

setInt16

protected final void setInt16(int idx,
                              int value)
Set the content of 2 bytes from an integer between -32768..32768, or from an integer number between 0..65535. The value is interpreted from the input parameter with modulo 0x10000. Big- or little-endian.

Parameters:
idx - The position of leading byte in the current elements data. This is not the absolute position in data, idxBegin is added.
value - The value in range 0..65535. The value is taken modulo 0xffff.

setInt8

protected final void setInt8(int idx,
                             int value)
Set the content of 1 bytes as a positive nr between 0..256.

Parameters:
idx - The position of leading byte in the current elements data. This is not the absolute position in data, idxBegin is added.
value - The value in range 0..65535. The value is taken modulo 0xffff.

setUint32

protected final void setUint32(int idxBytes,
                               int idxArray,
                               int lengthArray,
                               int val)

setInt32

protected final void setInt32(int idxBytes,
                              int idxArray,
                              int lengthArray,
                              int val)

setInt16

protected final void setInt16(int idxBytes,
                              int idxArray,
                              int lengthArray,
                              int val)

setInt8

protected final void setInt8(int idxBytes,
                             int idxArray,
                             int lengthArray,
                             int val)

setUint16

protected final void setUint16(int idxBytes,
                               int idxArray,
                               int lengthArray,
                               int val)

setUint8

protected final void setUint8(int idxBytes,
                              int idxArray,
                              int lengthArray,
                              int val)

setFloat

protected final void setFloat(int idxBytes,
                              int idxArray,
                              int lengthArray,
                              float val)

copyDataFrom

public final void copyDataFrom(ByteDataAccess src)
                        throws java.lang.IllegalArgumentException
Copies the data into a byte[]

Throws:
java.lang.IllegalArgumentException

elementAt

public final void elementAt(int indexObjectArray)
Counts the idxChild by given index, idxChild is ByteCount from idxBegin

Parameters:
indexObjectArray - Index of Array

getCurrentChild

public final ByteDataAccess getCurrentChild()