Javolution 6.0.0 java
javolution.io.Union Class Referenceabstract
Inheritance diagram for javolution.io.Union:
[legend]
Collaboration diagram for javolution.io.Union:
[legend]

Public Member Functions

 Union ()
 
final boolean isUnion ()
 
final int size ()
 
Struct outer ()
 
final ByteBuffer getByteBuffer ()
 
final Struct setByteBuffer (ByteBuffer byteBuffer, int position)
 
final Struct setByteBufferPosition (int position)
 
final int getByteBufferPosition ()
 
int read (InputStream in) throws IOException
 
void write (OutputStream out) throws IOException
 
final long address ()
 
String toString ()
 
ByteOrder byteOrder ()
 
boolean isPacked ()
 
long readBits (int bitOffset, int bitSize)
 
void writeBits (long value, int bitOffset, int bitSize)
 

Static Public Attributes

static final LocalContext.Parameter< Integer > MAXIMUM_ALIGNMENT
 

Protected Member Functions

UTF8String[] array (UTF8String[] array, int stringLength)
 

Package Functions

protected< S extends Struct > S inner (S struct)
 
protected< S extends Struct > S[] array (S[] structs)
 
protected< S extends Struct > S[][] array (S[][] structs)
 
protected< S extends Struct > S[][][] array (S[][][] structs)
 
protected< M extends Member > M[] array (M[] arrayMember)
 
protected< M extends Member > M[][] array (M[][] arrayMember)
 
protected< M extends Member > M[][][] array (M[][][] arrayMember)
 

Package Attributes

Struct _outer
 
ByteBuffer _byteBuffer
 
int _outerOffset
 
int _alignment = 1
 
int _length
 
int _index
 
int _wordSize
 
int _bitsUsed
 
boolean _resetIndex
 
byte[] _bytes
 

Private Member Functions

synchronized ByteBuffer newBuffer ()
 
long readByteBufferLong (int index)
 
void writeByteBufferLong (int index, long value)
 

Static Private Member Functions

static byte readByte (int index, ByteBuffer byteBuffer)
 
static void writeByte (int index, ByteBuffer byteBuffer, byte value)
 

Static Private Attributes

static final char[] HEXA
 
static final Class<? extends Bool[]> BOOL = new Bool[0].getClass()
 
static final Class<? extends Signed8[]> SIGNED_8
 
static final Class<? extends Unsigned8[]> UNSIGNED_8
 
static final Class<? extends Signed16[]> SIGNED_16
 
static final Class<? extends Unsigned16[]> UNSIGNED_16
 
static final Class<? extends Signed32[]> SIGNED_32
 
static final Class<? extends Unsigned32[]> UNSIGNED_32
 
static final Class<? extends Signed64[]> SIGNED_64
 
static final Class<? extends Float32[]> FLOAT_32
 
static final Class<? extends Float64[]> FLOAT_64
 

Detailed Description

Equivalent to C/C++ union; this class works in the same way as Struct (sub-class) except that all members are mapped to the same location in memory.

Here is an example of C union: [code] union Number { int asInt; float asFloat; char asString[12]; };[/code] And its Java equivalent:[code] public class Number extends Union { Signed32 asInt = new Signed32(); Float32 asFloat = new Float32(); Utf8String asString = new Utf8String(12); }[/code] As for any Struct, fields are directly accessible:[code] Number num = new Number(); num.asInt.set(23); num.asString.set("23"); // Null terminated (C compatible) float f = num.asFloat.get();[/code]

Author
Jean-Marie Dautelle
Version
1.0, October 4, 2004

Definition at line 36 of file Union.java.

Constructor & Destructor Documentation

◆ Union()

javolution.io.Union.Union ( )

Default constructor.

Definition at line 41 of file Union.java.

41 {}

Member Function Documentation

◆ address()

final long javolution.io.Struct.address ( )
inherited

Returns this struct address. This method allows for structs to be referenced (e.g. pointer) from other structs.

Returns
the struct memory address.
Exceptions
UnsupportedOperationExceptionif the struct buffer is not a direct buffer.
See also
Reference32
Reference64

Definition at line 393 of file Struct.java.

393  {
394  ByteBuffer thisBuffer = this.getByteBuffer();
395  if (thisBuffer instanceof sun.nio.ch.DirectBuffer)
396  return ((sun.nio.ch.DirectBuffer)thisBuffer).address();
397  throw new UnsupportedOperationException();
398  }

References javolution.io.Struct.getByteBuffer().

Referenced by javolution.io.Struct.Reference32< S extends Struct >.set(), and javolution.io.Struct.Reference64< S extends Struct >.set().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ array() [1/7]

protected<M extends Member> M [] javolution.io.Struct.array ( M[]  arrayMember)
packageinherited

Defines the specified array member. For predefined members, the array is populated when empty; custom members should use literal (populated) arrays.

Parameters
arrayMemberthe array member.
Returns
the specified array member.
Exceptions
UnsupportedOperationExceptionif the specified array is empty and the member type is unknown.

Definition at line 618 of file Struct.java.

618  {
619  boolean resetIndexSaved = _resetIndex;
620  if (_resetIndex) {
621  _index = 0;
622  _resetIndex = false; // Ensures the array elements are sequential.
623  }
624  if (BOOL.isInstance(arrayMember)) {
625  for (int i = 0; i < arrayMember.length;) {
626  arrayMember[i++] = (M) this.new Bool();
627  }
628  } else if (SIGNED_8.isInstance(arrayMember)) {
629  for (int i = 0; i < arrayMember.length;) {
630  arrayMember[i++] = (M) this.new Signed8();
631  }
632  } else if (UNSIGNED_8.isInstance(arrayMember)) {
633  for (int i = 0; i < arrayMember.length;) {
634  arrayMember[i++] = (M) this.new Unsigned8();
635  }
636  } else if (SIGNED_16.isInstance(arrayMember)) {
637  for (int i = 0; i < arrayMember.length;) {
638  arrayMember[i++] = (M) this.new Signed16();
639  }
640  } else if (UNSIGNED_16.isInstance(arrayMember)) {
641  for (int i = 0; i < arrayMember.length;) {
642  arrayMember[i++] = (M) this.new Unsigned16();
643  }
644  } else if (SIGNED_32.isInstance(arrayMember)) {
645  for (int i = 0; i < arrayMember.length;) {
646  arrayMember[i++] = (M) this.new Signed32();
647  }
648  } else if (UNSIGNED_32.isInstance(arrayMember)) {
649  for (int i = 0; i < arrayMember.length;) {
650  arrayMember[i++] = (M) this.new Unsigned32();
651  }
652  } else if (SIGNED_64.isInstance(arrayMember)) {
653  for (int i = 0; i < arrayMember.length;) {
654  arrayMember[i++] = (M) this.new Signed64();
655  }
656  } else if (FLOAT_32.isInstance(arrayMember)) {
657  for (int i = 0; i < arrayMember.length;) {
658  arrayMember[i++] = (M) this.new Float32();
659  }
660  } else if (FLOAT_64.isInstance(arrayMember)) {
661  for (int i = 0; i < arrayMember.length;) {
662  arrayMember[i++] = (M) this.new Float64();
663  }
664  } else {
665  throw new UnsupportedOperationException(
666  "Cannot create member elements, the arrayMember should "
667  + "contain the member instances instead of null");
668  }
669  _resetIndex = resetIndexSaved;
670  return (M[]) arrayMember;
671  }

References javolution.io.Struct._index, javolution.io.Struct._resetIndex, javolution.io.Struct.BOOL, javolution.io.Struct.FLOAT_32, javolution.io.Struct.FLOAT_64, javolution.io.Struct.SIGNED_16, javolution.io.Struct.SIGNED_32, javolution.io.Struct.SIGNED_64, javolution.io.Struct.SIGNED_8, javolution.io.Struct.UNSIGNED_16, javolution.io.Struct.UNSIGNED_32, and javolution.io.Struct.UNSIGNED_8.

◆ array() [2/7]

protected<M extends Member> M [][] javolution.io.Struct.array ( arrayMember[][])
packageinherited

Defines the specified two-dimensional array member. For predefined members, the array is populated when empty; custom members should use literal (populated) arrays.

Parameters
arrayMemberthe two-dimensional array member.
Returns
the specified array member.
Exceptions
UnsupportedOperationExceptionif the specified array is empty and the member type is unknown.

Definition at line 703 of file Struct.java.

703  {
704  boolean resetIndexSaved = _resetIndex;
705  if (_resetIndex) {
706  _index = 0;
707  _resetIndex = false; // Ensures the array elements are sequential.
708  }
709  for (int i = 0; i < arrayMember.length; i++) {
710  array(arrayMember[i]);
711  }
712  _resetIndex = resetIndexSaved;
713  return (M[][]) arrayMember;
714  }

References javolution.io.Struct._index, javolution.io.Struct._resetIndex, and javolution.io.Struct.array().

Here is the call graph for this function:

◆ array() [3/7]

protected<M extends Member> M [][][] javolution.io.Struct.array ( arrayMember[][][])
packageinherited

Defines the specified three-dimensional array member. For predefined members, the array is populated when empty; custom members should use literal (populated) arrays.

Parameters
arrayMemberthe three-dimensional array member.
Returns
the specified array member.
Exceptions
UnsupportedOperationExceptionif the specified array is empty and the member type is unknown.

Definition at line 726 of file Struct.java.

726  {
727  boolean resetIndexSaved = _resetIndex;
728  if (_resetIndex) {
729  _index = 0;
730  _resetIndex = false; // Ensures the array elements are sequential.
731  }
732  for (int i = 0; i < arrayMember.length; i++) {
733  array(arrayMember[i]);
734  }
735  _resetIndex = resetIndexSaved;
736  return (M[][][]) arrayMember;
737  }

References javolution.io.Struct._index, javolution.io.Struct._resetIndex, and javolution.io.Struct.array().

Here is the call graph for this function:

◆ array() [4/7]

protected<S extends Struct> S [] javolution.io.Struct.array ( S[]  structs)
packageinherited

Defines the specified array of structs as inner structs. The array is populated if necessary using the struct component default constructor (which must be public).

Parameters
structsthe struct array.
Returns
the specified struct array.
Exceptions
IllegalArgumentExceptionif the specified array contains inner structs.

Definition at line 532 of file Struct.java.

532  {
533  Class<?> structClass = null;
534  boolean resetIndexSaved = _resetIndex;
535  if (_resetIndex) {
536  _index = 0;
537  _resetIndex = false; // Ensures the array elements are sequential.
538  }
539  for (int i = 0; i < structs.length;) {
540  S struct = structs[i];
541  if (struct == null) {
542  try {
543  if (structClass == null) {
544  String arrayName = structs.getClass().getName();
545  String structName = arrayName.substring(2,
546  arrayName.length() - 1);
547  structClass = Class.forName(structName);
548  if (structClass == null) { throw new IllegalArgumentException(
549  "Struct class: " + structName + " not found"); }
550  }
551  struct = (S) structClass.newInstance();
552  } catch (Exception e) {
553  throw new RuntimeException(e.getMessage());
554  }
555  }
556  structs[i++] = inner(struct);
557  }
558  _resetIndex = resetIndexSaved;
559  return (S[]) structs;
560  }

References javolution.io.Struct._index, javolution.io.Struct._resetIndex, and javolution.io.Struct.inner().

Referenced by javolution.io.Struct.array().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ array() [5/7]

protected<S extends Struct> S [][] javolution.io.Struct.array ( structs[][])
packageinherited

Defines the specified two-dimensional array of structs as inner structs. The array is populated if necessary using the struct component default constructor (which must be public).

Parameters
structsthe two dimensional struct array.
Returns
the specified struct array.
Exceptions
IllegalArgumentExceptionif the specified array contains inner structs.

Definition at line 572 of file Struct.java.

572  {
573  boolean resetIndexSaved = _resetIndex;
574  if (_resetIndex) {
575  _index = 0;
576  _resetIndex = false; // Ensures the array elements are sequential.
577  }
578  for (int i = 0; i < structs.length; i++) {
579  array(structs[i]);
580  }
581  _resetIndex = resetIndexSaved;
582  return (S[][]) structs;
583  }

References javolution.io.Struct._index, javolution.io.Struct._resetIndex, and javolution.io.Struct.array().

Here is the call graph for this function:

◆ array() [6/7]

protected<S extends Struct> S [][][] javolution.io.Struct.array ( structs[][][])
packageinherited

Defines the specified three dimensional array of structs as inner structs. The array is populated if necessary using the struct component default constructor (which must be public).

Parameters
structsthe three dimensional struct array.
Returns
the specified struct array.
Exceptions
IllegalArgumentExceptionif the specified array contains inner structs.

Definition at line 595 of file Struct.java.

595  {
596  boolean resetIndexSaved = _resetIndex;
597  if (_resetIndex) {
598  _index = 0;
599  _resetIndex = false; // Ensures the array elements are sequential.
600  }
601  for (int i = 0; i < structs.length; i++) {
602  array(structs[i]);
603  }
604  _resetIndex = resetIndexSaved;
605  return (S[][][]) structs;
606  }

References javolution.io.Struct._index, javolution.io.Struct._resetIndex, and javolution.io.Struct.array().

Here is the call graph for this function:

◆ array() [7/7]

UTF8String [] javolution.io.Struct.array ( UTF8String[]  array,
int  stringLength 
)
protectedinherited

Defines the specified array of UTF-8 strings, all strings having the specified length (convenience method).

Parameters
arraythe string array.
stringLengththe length of the string elements.
Returns
the specified string array.

Definition at line 747 of file Struct.java.

747  {
748  boolean resetIndexSaved = _resetIndex;
749  if (_resetIndex) {
750  _index = 0;
751  _resetIndex = false; // Ensures the array elements are sequential.
752  }
753  for (int i = 0; i < array.length; i++) {
754  array[i] = new UTF8String(stringLength);
755  }
756  _resetIndex = resetIndexSaved;
757  return array;
758  }

References javolution.io.Struct._index, javolution.io.Struct._resetIndex, and javolution.io.Struct.array().

Here is the call graph for this function:

◆ byteOrder()

ByteOrder javolution.io.Struct.byteOrder ( )
inherited

Returns the byte order for this struct (configurable). The byte order is inherited by inner structs. Sub-classes may change the byte order by overriding this method. For example:[code] public class TopStruct extends Struct { ... // Members initialization. public ByteOrder byteOrder() { // TopStruct and its inner structs use hardware byte order. return ByteOrder.nativeOrder(); } }}[/code]

Returns
the byte order when reading/writing multibyte values (default: network byte order, BIG_ENDIAN).

Definition at line 480 of file Struct.java.

480  {
481  return (_outer != null) ? _outer.byteOrder() : ByteOrder.BIG_ENDIAN;
482  }

References javolution.io.Struct._outer, and javolution.io.Struct.byteOrder().

Referenced by javolution.io.Struct.byteOrder(), javolution.io.Struct.Member.get(), javolution.io.Struct.newBuffer(), javolution.io.Struct.readBits(), javolution.io.Struct.Member.set(), javolution.io.Struct.setByteBuffer(), and javolution.io.Struct.writeBits().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getByteBuffer()

final ByteBuffer javolution.io.Struct.getByteBuffer ( )
inherited

Returns the byte buffer for this struct. This method will allocate a new direct buffer if none has been set.

Changes to the buffer's content are visible in this struct, and vice versa.

The buffer of an inner struct is the same as its parent struct.

If no byte buffer has been set, a direct buffer is allocated with a capacity equals to this struct's size.

Returns
the current byte buffer or a new direct buffer if none set.
See also
setByteBuffer

Definition at line 254 of file Struct.java.

254  {
255  if (_outer != null) return _outer.getByteBuffer();
256  return (_byteBuffer != null) ? _byteBuffer : newBuffer();
257  }

References javolution.io.Struct._byteBuffer, javolution.io.Struct._outer, javolution.io.Struct.getByteBuffer(), and javolution.io.Struct.newBuffer().

Referenced by javolution.io.Struct.address(), javolution.io.Struct.UTF8String.get(), javolution.io.Struct.Bool.get(), javolution.io.Struct.Signed8.get(), javolution.io.Struct.Unsigned8.get(), javolution.io.Struct.Signed16.get(), javolution.io.Struct.Unsigned16.get(), javolution.io.Struct.Signed32.get(), javolution.io.Struct.Unsigned32.get(), javolution.io.Struct.Signed64.get(), javolution.io.Struct.Float32.get(), javolution.io.Struct.Float64.get(), javolution.io.Struct.Enum8< T extends Enum< T >.get(), javolution.io.Struct.Enum16< T extends Enum< T >.get(), javolution.io.Struct.Enum32< T extends Enum< T >.get(), javolution.io.Struct.Enum64< T extends Enum< T >.get(), javolution.io.Struct.getByteBuffer(), javolution.io.Struct.Reference32< S extends Struct >.isUpToDate(), javolution.io.Struct.Reference64< S extends Struct >.isUpToDate(), javolution.io.Struct.read(), javolution.io.Struct.readByteBufferLong(), javolution.io.Struct.UTF8String.set(), javolution.io.Struct.Bool.set(), javolution.io.Struct.Signed8.set(), javolution.io.Struct.Unsigned8.set(), javolution.io.Struct.Signed16.set(), javolution.io.Struct.Unsigned16.set(), javolution.io.Struct.Signed32.set(), javolution.io.Struct.Unsigned32.set(), javolution.io.Struct.Signed64.set(), javolution.io.Struct.Float32.set(), javolution.io.Struct.Float64.set(), javolution.io.Struct.Reference32< S extends Struct >.set(), javolution.io.Struct.Reference64< S extends Struct >.set(), javolution.io.Struct.Enum8< T extends Enum< T >.set(), javolution.io.Struct.Enum16< T extends Enum< T >.set(), javolution.io.Struct.Enum32< T extends Enum< T >.set(), javolution.io.Struct.Enum64< T extends Enum< T >.set(), javolution.io.Struct.setByteBufferPosition(), javolution.io.Struct.toString(), javolution.io.Struct.Reference32< S extends Struct >.value(), javolution.io.Struct.Reference64< S extends Struct >.value(), javolution.io.Struct.write(), and javolution.io.Struct.writeByteBufferLong().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getByteBufferPosition()

final int javolution.io.Struct.getByteBufferPosition ( )
inherited

Returns the absolute byte position of this struct within its associated byte buffer.

Returns
the absolute position of this struct (can be an inner struct) in the byte buffer.

Definition at line 312 of file Struct.java.

312  {
313  return (_outer != null) ? _outer.getByteBufferPosition() + _outerOffset
314  : _outerOffset;
315  }

References javolution.io.Struct._outer, javolution.io.Struct._outerOffset, and javolution.io.Struct.getByteBufferPosition().

Referenced by javolution.io.Struct.UTF8String.get(), javolution.io.Struct.Bool.get(), javolution.io.Struct.Signed8.get(), javolution.io.Struct.Unsigned8.get(), javolution.io.Struct.Signed16.get(), javolution.io.Struct.Unsigned16.get(), javolution.io.Struct.Signed32.get(), javolution.io.Struct.Unsigned32.get(), javolution.io.Struct.Signed64.get(), javolution.io.Struct.Float32.get(), javolution.io.Struct.Float64.get(), javolution.io.Struct.Enum8< T extends Enum< T >.get(), javolution.io.Struct.Enum16< T extends Enum< T >.get(), javolution.io.Struct.Enum32< T extends Enum< T >.get(), javolution.io.Struct.Enum64< T extends Enum< T >.get(), javolution.io.Struct.getByteBufferPosition(), javolution.io.Struct.Reference32< S extends Struct >.isUpToDate(), javolution.io.Struct.Reference64< S extends Struct >.isUpToDate(), javolution.io.Struct.read(), javolution.io.Struct.readBits(), javolution.io.Struct.UTF8String.set(), javolution.io.Struct.Bool.set(), javolution.io.Struct.Signed8.set(), javolution.io.Struct.Unsigned8.set(), javolution.io.Struct.Signed16.set(), javolution.io.Struct.Unsigned16.set(), javolution.io.Struct.Signed32.set(), javolution.io.Struct.Unsigned32.set(), javolution.io.Struct.Signed64.set(), javolution.io.Struct.Float32.set(), javolution.io.Struct.Float64.set(), javolution.io.Struct.Reference32< S extends Struct >.set(), javolution.io.Struct.Reference64< S extends Struct >.set(), javolution.io.Struct.Enum8< T extends Enum< T >.set(), javolution.io.Struct.Enum16< T extends Enum< T >.set(), javolution.io.Struct.Enum32< T extends Enum< T >.set(), javolution.io.Struct.Enum64< T extends Enum< T >.set(), javolution.io.Struct.toString(), javolution.io.Struct.Reference32< S extends Struct >.value(), javolution.io.Struct.Reference64< S extends Struct >.value(), javolution.io.Struct.write(), and javolution.io.Struct.writeBits().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ inner()

protected<S extends Struct> S javolution.io.Struct.inner ( struct)
packageinherited

Defines the specified struct as inner of this struct.

Parameters
structthe inner struct.
Returns
the specified struct.
Exceptions
IllegalArgumentExceptionif the specified struct is already an inner struct.

Definition at line 513 of file Struct.java.

513  {
514  if (struct._outer != null) throw new IllegalArgumentException(
515  "struct: Already an inner struct");
516  Member inner = new Member(struct.size() << 3, struct._alignment); // Update indexes.
517  struct._outer = this;
518  struct._outerOffset = inner.offset();
519  return (S) struct;
520  }

References javolution.io.Struct._alignment, javolution.io.Struct._outer, and javolution.io.Struct.size().

Referenced by javolution.io.Struct.array().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isPacked()

boolean javolution.io.Struct.isPacked ( )
inherited

Indicates if this struct is packed (configurable). By default, members of a struct are aligned on the boundary corresponding to the member base type; padding is performed if necessary. This directive is not inherited by inner structs. Sub-classes may change the packing directive by overriding this method. For example:[code] public class MyStruct extends Struct { ... // Members initialization. public boolean isPacked() { return true; // MyStruct is packed. } }}[/code]

Returns
true if word size requirements are ignored. false otherwise (default).

Definition at line 501 of file Struct.java.

501  {
502  return false;
503  }

Referenced by javolution.io.Struct.Member.Member().

Here is the caller graph for this function:

◆ isUnion()

final boolean javolution.io.Union.isUnion ( )

Returns true.

Returns
true

Reimplemented from javolution.io.Struct.

Definition at line 48 of file Union.java.

48  {
49  return true;
50  }

◆ newBuffer()

synchronized ByteBuffer javolution.io.Struct.newBuffer ( )
privateinherited

Definition at line 259 of file Struct.java.

259  {
260  if (_byteBuffer != null) return _byteBuffer; // Synchronized check.
261  ByteBuffer bf = ByteBuffer.allocateDirect(size());
262  bf.order(byteOrder());
263  setByteBuffer(bf, 0);
264  return _byteBuffer;
265  }

References javolution.io.Struct._byteBuffer, javolution.io.Struct.byteOrder(), javolution.io.Struct.setByteBuffer(), and javolution.io.Struct.size().

Referenced by javolution.io.Struct.getByteBuffer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ outer()

Struct javolution.io.Struct.outer ( )
inherited

Returns the outer of this struct or null if this struct is not an inner struct.

Returns
the outer struct or null.

Definition at line 236 of file Struct.java.

236  {
237  return _outer;
238  }

References javolution.io.Struct._outer.

◆ read()

int javolution.io.Struct.read ( InputStream  in) throws IOException
inherited

Reads this struct from the specified input stream (convenience method when using Stream I/O). For better performance, use of Block I/O (e.g. java.nio.channels.*) is recommended. This method behaves appropriately when not all of the data is available from the input stream. Incomplete data is extremely common when the input stream is associated with something like a TCP connection. The typical usage pattern in those scenarios is to repeatedly call read() until the entire message is received.

Parameters
inthe input stream being read from.
Returns
the number of bytes read (typically the size of this struct.
Exceptions
IOExceptionif an I/O error occurs.

Definition at line 332 of file Struct.java.

332  {
333  ByteBuffer buffer = getByteBuffer();
334  int size = size();
335  int remaining = size - buffer.position();
336  if (remaining == 0) remaining = size;// at end so move to beginning
337  int alreadyRead = size - remaining; // typically 0
338  if (buffer.hasArray()) {
339  int offset = buffer.arrayOffset() + getByteBufferPosition();
340  int bytesRead = in
341  .read(buffer.array(), offset + alreadyRead, remaining);
342  buffer.position(getByteBufferPosition() + alreadyRead + bytesRead
343  - offset);
344  return bytesRead;
345  } else {
346  synchronized (buffer) {
347  if (_bytes == null) {
348  _bytes = new byte[size()];
349  }
350  int bytesRead = in.read(_bytes, 0, remaining);
351  buffer.position(getByteBufferPosition() + alreadyRead);
352  buffer.put(_bytes, 0, bytesRead);
353  return bytesRead;
354  }
355  }
356  }

References javolution.io.Struct._bytes, javolution.io.Struct.getByteBuffer(), javolution.io.Struct.getByteBufferPosition(), and javolution.io.Struct.size().

Here is the call graph for this function:

◆ readBits()

long javolution.io.Struct.readBits ( int  bitOffset,
int  bitSize 
)
inherited

Reads the specified bits from this Struct as an long (signed) integer value.

Parameters
bitOffsetthe bit start position in the Struct.
bitSizethe number of bits.
Returns
the specified bits read as a signed long.
Exceptions
IllegalArgumentExceptionif (bitOffset + bitSize - 1) / 8 >= this.size()

Definition at line 770 of file Struct.java.

770  {
771  if ((bitOffset + bitSize - 1) >> 3 >= this.size()) throw new IllegalArgumentException(
772  "Attempt to read outside the Struct");
773  int offset = bitOffset >> 3;
774  int bitStart = bitOffset - (offset << 3);
775  bitStart = (byteOrder() == ByteOrder.BIG_ENDIAN) ? bitStart : 64
776  - bitSize - bitStart;
777  int index = getByteBufferPosition() + offset;
778  long value = readByteBufferLong(index);
779  value <<= bitStart; // Clears preceding bits.
780  value >>= (64 - bitSize); // Signed shift.
781  return value;
782  }

References javolution.io.Struct.byteOrder(), javolution.io.Struct.getByteBufferPosition(), javolution.io.Struct.readByteBufferLong(), and javolution.io.Struct.size().

Referenced by javolution.io.Struct.BitField.longValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readByte()

static byte javolution.io.Struct.readByte ( int  index,
ByteBuffer  byteBuffer 
)
staticprivateinherited

Definition at line 809 of file Struct.java.

809  {
810  return (index < byteBuffer.limit()) ? byteBuffer.get(index) : 0;
811  }

Referenced by javolution.io.Struct.readByteBufferLong().

Here is the caller graph for this function:

◆ readByteBufferLong()

long javolution.io.Struct.readByteBufferLong ( int  index)
privateinherited

Definition at line 784 of file Struct.java.

784  {
785  ByteBuffer byteBuffer = getByteBuffer();
786  if (index + 8 < byteBuffer.limit()) return byteBuffer.getLong(index);
787  // Else possible buffer overflow.
788  if (byteBuffer.order() == ByteOrder.LITTLE_ENDIAN) {
789  return (readByte(index, byteBuffer) & 0xff)
790  + ((readByte(++index, byteBuffer) & 0xff) << 8)
791  + ((readByte(++index, byteBuffer) & 0xff) << 16)
792  + ((readByte(++index, byteBuffer) & 0xffL) << 24)
793  + ((readByte(++index, byteBuffer) & 0xffL) << 32)
794  + ((readByte(++index, byteBuffer) & 0xffL) << 40)
795  + ((readByte(++index, byteBuffer) & 0xffL) << 48)
796  + ((readByte(++index, byteBuffer) & 0xffL) << 56);
797  } else {
798  return (((long) readByte(index, byteBuffer)) << 56)
799  + ((readByte(++index, byteBuffer) & 0xffL) << 48)
800  + ((readByte(++index, byteBuffer) & 0xffL) << 40)
801  + ((readByte(++index, byteBuffer) & 0xffL) << 32)
802  + ((readByte(++index, byteBuffer) & 0xffL) << 24)
803  + ((readByte(++index, byteBuffer) & 0xff) << 16)
804  + ((readByte(++index, byteBuffer) & 0xff) << 8)
805  + (readByte(++index, byteBuffer) & 0xffL);
806  }
807  }

References javolution.io.Struct.getByteBuffer(), and javolution.io.Struct.readByte().

Referenced by javolution.io.Struct.readBits(), and javolution.io.Struct.writeBits().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setByteBuffer()

final Struct javolution.io.Struct.setByteBuffer ( ByteBuffer  byteBuffer,
int  position 
)
inherited

Sets the current byte buffer for this struct. The specified byte buffer can be mapped to memory for direct memory access or can wrap a shared byte array for I/O purpose (e.g. DatagramPacket). The capacity of the specified byte buffer should be at least the size of this struct plus the offset position.

Parameters
byteBufferthe new byte buffer.
positionthe position of this struct in the specified byte buffer.
Returns
this
Exceptions
IllegalArgumentExceptionif the specified byteBuffer has a different byte order than this struct.
UnsupportedOperationExceptionif this struct is an inner struct.
See also
byteOrder()

Definition at line 283 of file Struct.java.

283  {
284  if (byteBuffer.order() != byteOrder()) throw new IllegalArgumentException(
285  "The byte order of the specified byte buffer"
286  + " is different from this struct byte order");
287  if (_outer != null) throw new UnsupportedOperationException(
288  "Inner struct byte buffer is inherited from outer");
289  _byteBuffer = byteBuffer;
290  _outerOffset = position;
291  return this;
292  }

References javolution.io.Struct._byteBuffer, javolution.io.Struct._outer, javolution.io.Struct._outerOffset, and javolution.io.Struct.byteOrder().

Referenced by javolution.io.Struct.newBuffer(), and javolution.io.Struct.setByteBufferPosition().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setByteBufferPosition()

final Struct javolution.io.Struct.setByteBufferPosition ( int  position)
inherited

Sets the byte position of this struct within its byte buffer.

Parameters
positionthe position of this struct in its byte buffer.
Returns
this
Exceptions
UnsupportedOperationExceptionif this struct is an inner struct.

Definition at line 301 of file Struct.java.

301  {
302  return setByteBuffer(this.getByteBuffer(), position);
303  }

References javolution.io.Struct.getByteBuffer(), and javolution.io.Struct.setByteBuffer().

Here is the call graph for this function:

◆ size()

final int javolution.io.Struct.size ( )
inherited

Returns the size in bytes of this struct. The size includes tail padding to satisfy the struct word size requirement (defined by the largest word size of its members).

Returns
the C/C++ sizeof(this).

Definition at line 225 of file Struct.java.

225  {
226  return (_alignment <= 1) ? _length
227  : ((_length + _alignment - 1) / _alignment) * _alignment;
228  }

References javolution.io.Struct._alignment, and javolution.io.Struct._length.

Referenced by javolution.io.Struct.inner(), javolution.io.Struct.newBuffer(), javolution.io.Struct.read(), javolution.io.Struct.readBits(), javolution.io.Struct.toString(), javolution.io.Struct.write(), and javolution.io.Struct.writeBits().

Here is the caller graph for this function:

◆ toString()

String javolution.io.Struct.toString ( )
inherited

Returns the String representation of this struct in the form of its constituing bytes (hexadecimal). For example:[code] public static class Student extends Struct { Utf8String name = new Utf8String(16); Unsigned16 year = new Unsigned16(); Float32 grade = new Float32(); } Student student = new Student(); student.name.set("John Doe"); student.year.set(2003); student.grade.set(12.5f); System.out.println(student);

4A 6F 68 6E 20 44 6F 65 00 00 00 00 00 00 00 00 07 D3 00 00 41 48 00 00[/code]

Returns
a hexadecimal representation of the bytes content for this struct.

Definition at line 420 of file Struct.java.

420  {
421  TextBuilder tmp = new TextBuilder();
422  final int size = size();
423  final ByteBuffer buffer = getByteBuffer();
424  final int start = getByteBufferPosition();
425  for (int i = 0; i < size; i++) {
426  int b = buffer.get(start + i) & 0xFF;
427  tmp.append(HEXA[b >> 4]);
428  tmp.append(HEXA[b & 0xF]);
429  tmp.append(((i & 0xF) == 0xF) ? '\n' : ' ');
430  }
431  return tmp.toString();
432  }

References javolution.text.TextBuilder.append(), javolution.io.Struct.getByteBuffer(), javolution.io.Struct.getByteBufferPosition(), javolution.io.Struct.HEXA, javolution.io.Struct.size(), and javolution.text.TextBuilder.toString().

Here is the call graph for this function:

◆ write()

void javolution.io.Struct.write ( OutputStream  out) throws IOException
inherited

Writes this struct to the specified output stream (convenience method when using Stream I/O). For better performance, use of Block I/O (e.g. java.nio.channels.*) is recommended.

Parameters
outthe output stream to write to.
Exceptions
IOExceptionif an I/O error occurs.

Definition at line 366 of file Struct.java.

366  {
367  ByteBuffer buffer = getByteBuffer();
368  if (buffer.hasArray()) {
369  int offset = buffer.arrayOffset() + getByteBufferPosition();
370  out.write(buffer.array(), offset, size());
371  } else {
372  synchronized (buffer) {
373  if (_bytes == null) {
374  _bytes = new byte[size()];
375  }
376  buffer.position(getByteBufferPosition());
377  buffer.get(_bytes);
378  out.write(_bytes);
379  }
380  }
381  }

References javolution.io.Struct._bytes, javolution.io.Struct.getByteBuffer(), javolution.io.Struct.getByteBufferPosition(), and javolution.io.Struct.size().

Here is the call graph for this function:

◆ writeBits()

void javolution.io.Struct.writeBits ( long  value,
int  bitOffset,
int  bitSize 
)
inherited

Writes the specified bits into this Struct.

Parameters
valuethe bits value as a signed long.
bitOffsetthe bit start position in the Struct.
bitSizethe number of bits.
Exceptions
IllegalArgumentExceptionif (bitOffset + bitSize - 1) / 8 >= this.size()

Definition at line 822 of file Struct.java.

822  {
823  if ((bitOffset + bitSize - 1) >> 3 >= this.size()) throw new IllegalArgumentException(
824  "Attempt to write outside the Struct");
825  int offset = bitOffset >> 3;
826  int bitStart = (byteOrder() == ByteOrder.BIG_ENDIAN) ? bitOffset
827  - (offset << 3) : 64 - bitSize - (bitOffset - (offset << 3));
828  long mask = -1L;
829  mask <<= bitStart; // Clears preceding bits
830  mask >>>= (64 - bitSize); // Unsigned shift.
831  mask <<= 64 - bitSize - bitStart;
832  value <<= (64 - bitSize - bitStart);
833  value &= mask; // Protects against out of range values.
834  int index = getByteBufferPosition() + offset;
835  long oldValue = readByteBufferLong(index);
836  long resetValue = oldValue & (~mask);
837  long newValue = resetValue | value;
838  writeByteBufferLong(index, newValue);
839  }

References javolution.io.Struct.byteOrder(), javolution.io.Struct.getByteBufferPosition(), javolution.io.Struct.readByteBufferLong(), javolution.io.Struct.size(), and javolution.io.Struct.writeByteBufferLong().

Referenced by javolution.io.Struct.BitField.set().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeByte()

static void javolution.io.Struct.writeByte ( int  index,
ByteBuffer  byteBuffer,
byte  value 
)
staticprivateinherited

Definition at line 869 of file Struct.java.

869  {
870  if (index < byteBuffer.limit()) {
871  byteBuffer.put(index, value);
872  }
873  }

Referenced by javolution.io.Struct.writeByteBufferLong().

Here is the caller graph for this function:

◆ writeByteBufferLong()

void javolution.io.Struct.writeByteBufferLong ( int  index,
long  value 
)
privateinherited

Definition at line 841 of file Struct.java.

841  {
842  ByteBuffer byteBuffer = getByteBuffer();
843  if (index + 8 < byteBuffer.limit()) {
844  byteBuffer.putLong(index, value);
845  return;
846  }
847  // Else possible buffer overflow.
848  if (byteBuffer.order() == ByteOrder.LITTLE_ENDIAN) {
849  writeByte(index, byteBuffer, (byte) value);
850  writeByte(++index, byteBuffer, (byte) (value >> 8));
851  writeByte(++index, byteBuffer, (byte) (value >> 16));
852  writeByte(++index, byteBuffer, (byte) (value >> 24));
853  writeByte(++index, byteBuffer, (byte) (value >> 32));
854  writeByte(++index, byteBuffer, (byte) (value >> 40));
855  writeByte(++index, byteBuffer, (byte) (value >> 48));
856  writeByte(++index, byteBuffer, (byte) (value >> 56));
857  } else {
858  writeByte(index, byteBuffer, (byte) (value >> 56));
859  writeByte(++index, byteBuffer, (byte) (value >> 48));
860  writeByte(++index, byteBuffer, (byte) (value >> 40));
861  writeByte(++index, byteBuffer, (byte) (value >> 32));
862  writeByte(++index, byteBuffer, (byte) (value >> 24));
863  writeByte(++index, byteBuffer, (byte) (value >> 16));
864  writeByte(++index, byteBuffer, (byte) (value >> 8));
865  writeByte(++index, byteBuffer, (byte) value);
866  }
867  }

References javolution.io.Struct.getByteBuffer(), and javolution.io.Struct.writeByte().

Referenced by javolution.io.Struct.writeBits().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _alignment

int javolution.io.Struct._alignment = 1
packageinherited

Holds this struct alignment in bytes (largest word size of its members).

Definition at line 181 of file Struct.java.

Referenced by javolution.io.Struct.inner(), javolution.io.Struct.Member.Member(), and javolution.io.Struct.size().

◆ _bitsUsed

int javolution.io.Struct._bitsUsed
packageinherited

Holds the bits used in the word during construction (for bit fields). This is the number of bits used in the last word.

Definition at line 200 of file Struct.java.

Referenced by javolution.io.Struct.Member.Member().

◆ _byteBuffer

ByteBuffer javolution.io.Struct._byteBuffer
packageinherited

Holds the byte buffer backing the struct (top struct).

Definition at line 172 of file Struct.java.

Referenced by javolution.io.Struct.getByteBuffer(), javolution.io.Struct.newBuffer(), and javolution.io.Struct.setByteBuffer().

◆ _bytes

byte [] javolution.io.Struct._bytes
packageinherited

Holds bytes array for Stream I/O when byteBuffer has no intrinsic array.

Definition at line 209 of file Struct.java.

Referenced by javolution.io.Struct.read(), and javolution.io.Struct.write().

◆ _index

int javolution.io.Struct._index
packageinherited

Holds the index position during construction. This is the index a the first unused byte available.

Definition at line 190 of file Struct.java.

Referenced by javolution.io.Struct.array(), and javolution.io.Struct.Member.Member().

◆ _length

int javolution.io.Struct._length
packageinherited

Holds this struct's length.

Definition at line 185 of file Struct.java.

Referenced by javolution.io.Struct.Member.Member(), and javolution.io.Struct.size().

◆ _outer

◆ _outerOffset

int javolution.io.Struct._outerOffset
packageinherited

Holds the offset of this struct relative to the outer struct or to the byte buffer if there is no outer.

Definition at line 177 of file Struct.java.

Referenced by javolution.io.Struct.getByteBufferPosition(), and javolution.io.Struct.setByteBuffer().

◆ _resetIndex

boolean javolution.io.Struct._resetIndex
packageinherited

Indicates if the index has to be reset for each new field ( true only for Union subclasses).

Definition at line 205 of file Struct.java.

Referenced by javolution.io.Struct.array(), javolution.io.Struct.Member.Member(), and javolution.io.Struct.Struct().

◆ _wordSize

int javolution.io.Struct._wordSize
packageinherited

Holds the word size during construction (for bit fields). This is the size of the last word used.

Definition at line 195 of file Struct.java.

Referenced by javolution.io.Struct.Member.Member().

◆ BOOL

final Class<? extends Bool[]> javolution.io.Struct.BOOL = new Bool[0].getClass()
staticprivateinherited

Definition at line 673 of file Struct.java.

Referenced by javolution.io.Struct.array().

◆ FLOAT_32

final Class<? extends Float32[]> javolution.io.Struct.FLOAT_32
staticprivateinherited
Initial value:
= new Float32[0]
.getClass()

Definition at line 688 of file Struct.java.

Referenced by javolution.io.Struct.array().

◆ FLOAT_64

final Class<? extends Float64[]> javolution.io.Struct.FLOAT_64
staticprivateinherited
Initial value:
= new Float64[0]
.getClass()

Definition at line 690 of file Struct.java.

Referenced by javolution.io.Struct.array().

◆ HEXA

final char [] javolution.io.Struct.HEXA
staticprivateinherited
Initial value:
= { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }

Definition at line 434 of file Struct.java.

Referenced by javolution.io.Struct.toString().

◆ MAXIMUM_ALIGNMENT

final LocalContext.Parameter<Integer> javolution.io.Struct.MAXIMUM_ALIGNMENT
staticinherited
Initial value:
= new LocalContext.Parameter<Integer>() {
@Override
protected Integer getDefault() {
return 4;
}
}

Configurable holding the maximum wordSize in bytes (default 4). Should be a value greater or equal to 1.

Definition at line 158 of file Struct.java.

◆ SIGNED_16

final Class<? extends Signed16[]> javolution.io.Struct.SIGNED_16
staticprivateinherited
Initial value:
= new Signed16[0]
.getClass()

Definition at line 678 of file Struct.java.

Referenced by javolution.io.Struct.array().

◆ SIGNED_32

final Class<? extends Signed32[]> javolution.io.Struct.SIGNED_32
staticprivateinherited
Initial value:
= new Signed32[0]
.getClass()

Definition at line 682 of file Struct.java.

Referenced by javolution.io.Struct.array().

◆ SIGNED_64

final Class<? extends Signed64[]> javolution.io.Struct.SIGNED_64
staticprivateinherited
Initial value:
= new Signed64[0]
.getClass()

Definition at line 686 of file Struct.java.

Referenced by javolution.io.Struct.array().

◆ SIGNED_8

final Class<? extends Signed8[]> javolution.io.Struct.SIGNED_8
staticprivateinherited
Initial value:
= new Signed8[0]
.getClass()

Definition at line 674 of file Struct.java.

Referenced by javolution.io.Struct.array().

◆ UNSIGNED_16

final Class<? extends Unsigned16[]> javolution.io.Struct.UNSIGNED_16
staticprivateinherited
Initial value:
= new Unsigned16[0]
.getClass()

Definition at line 680 of file Struct.java.

Referenced by javolution.io.Struct.array().

◆ UNSIGNED_32

final Class<? extends Unsigned32[]> javolution.io.Struct.UNSIGNED_32
staticprivateinherited
Initial value:
= new Unsigned32[0]
.getClass()

Definition at line 684 of file Struct.java.

Referenced by javolution.io.Struct.array().

◆ UNSIGNED_8

final Class<? extends Unsigned8[]> javolution.io.Struct.UNSIGNED_8
staticprivateinherited
Initial value:
= new Unsigned8[0]
.getClass()

Definition at line 676 of file Struct.java.

Referenced by javolution.io.Struct.array().


The documentation for this class was generated from the following file:
javolution.io.Struct._length
int _length
Definition: Struct.java:185
javolution.io.Struct.writeByte
static void writeByte(int index, ByteBuffer byteBuffer, byte value)
Definition: Struct.java:869
javolution.io.Struct._outerOffset
int _outerOffset
Definition: Struct.java:177
javolution.io.Struct._bytes
byte[] _bytes
Definition: Struct.java:209
javolution.io.Struct.BOOL
static final Class<? extends Bool[]> BOOL
Definition: Struct.java:673
javolution.io.Struct._index
int _index
Definition: Struct.java:190
javolution.io.Struct.SIGNED_8
static final Class<? extends Signed8[]> SIGNED_8
Definition: Struct.java:674
javolution.io.Struct._alignment
int _alignment
Definition: Struct.java:181
javolution.io.Struct.UNSIGNED_8
static final Class<? extends Unsigned8[]> UNSIGNED_8
Definition: Struct.java:676
javolution.io.Struct.SIGNED_64
static final Class<? extends Signed64[]> SIGNED_64
Definition: Struct.java:686
javolution.io.Struct._resetIndex
boolean _resetIndex
Definition: Struct.java:205
javolution.io.Struct.UNSIGNED_32
static final Class<? extends Unsigned32[]> UNSIGNED_32
Definition: Struct.java:684
javolution.io.Struct.FLOAT_64
static final Class<? extends Float64[]> FLOAT_64
Definition: Struct.java:690
javolution.io.Struct.FLOAT_32
static final Class<? extends Float32[]> FLOAT_32
Definition: Struct.java:688
javolution.io.Struct.SIGNED_32
static final Class<? extends Signed32[]> SIGNED_32
Definition: Struct.java:682
Exception
javolution.io.Struct.getByteBufferPosition
final int getByteBufferPosition()
Definition: Struct.java:312
javolution.io.Struct.readByte
static byte readByte(int index, ByteBuffer byteBuffer)
Definition: Struct.java:809
javolution.io.Struct.size
final int size()
Definition: Struct.java:225
javolution.io.Struct.byteOrder
ByteOrder byteOrder()
Definition: Struct.java:480
javolution.io.Struct.writeByteBufferLong
void writeByteBufferLong(int index, long value)
Definition: Struct.java:841
javolution.io.Struct.UNSIGNED_16
static final Class<? extends Unsigned16[]> UNSIGNED_16
Definition: Struct.java:680
javolution.io.Struct.HEXA
static final char[] HEXA
Definition: Struct.java:434
javolution.io.Struct.setByteBuffer
final Struct setByteBuffer(ByteBuffer byteBuffer, int position)
Definition: Struct.java:283
javolution.io.Struct.SIGNED_16
static final Class<? extends Signed16[]> SIGNED_16
Definition: Struct.java:678
javolution.io.Struct.readByteBufferLong
long readByteBufferLong(int index)
Definition: Struct.java:784
javolution.io.Struct.inner
protected< S extends Struct > S inner(S struct)
Definition: Struct.java:513
javolution.io.Struct.newBuffer
synchronized ByteBuffer newBuffer()
Definition: Struct.java:259
javolution.io.Struct.getByteBuffer
final ByteBuffer getByteBuffer()
Definition: Struct.java:254
javolution.io.Struct._outer
Struct _outer
Definition: Struct.java:168
javolution.io.Struct.array
protected< S extends Struct > S[] array(S[] structs)
Definition: Struct.java:532
javolution.io.Struct._byteBuffer
ByteBuffer _byteBuffer
Definition: Struct.java:172