com.objsys.asn1j.runtime
Class Asn1PerEncodeBuffer

java.lang.Object
  extended by com.objsys.asn1j.runtime.Asn1MessageBufferBase
      extended by com.objsys.asn1j.runtime.Asn1MessageBuffer
          extended by com.objsys.asn1j.runtime.Asn1EncodeBuffer
              extended by com.objsys.asn1j.runtime.Asn1PerEncodeBuffer
All Implemented Interfaces:
Asn1PerEncoder, Asn1PerMessageBuffer

public class Asn1PerEncodeBuffer
extends Asn1EncodeBuffer
implements Asn1PerMessageBuffer, Asn1PerEncoder

This class handles the encoding of ASN.1 messages as specified in the Packed Encoding Rules (PER) as specified in the ITU-T X.691 standard.


Field Summary
protected  Asn1PerTraceHandler mTraceHandler
           
 
Fields inherited from class com.objsys.asn1j.runtime.Asn1EncodeBuffer
INITIAL_SIZE, mByteIndex, mData
 
Fields inherited from class com.objsys.asn1j.runtime.Asn1MessageBufferBase
context, mTypeCode
 
Constructor Summary
Asn1PerEncodeBuffer(boolean aligned)
          This constructor creates a PER encode buffer object with the default initial size.
Asn1PerEncodeBuffer(boolean aligned, int size)
          This constructor creates a PER encode buffer object with the given initial size.
 
Method Summary
 void binDump(java.io.PrintStream out, java.lang.String varName)
          This method dumps the encoded message in a human-readable format showing a bit trace of all fields to the given print output stream.
 void byteAlign()
          This methods byte-aligns the buffer.
protected  void checkSize(int bytesRequired)
          This method determines if the encode buffer can hold the requested number of bytes.
 void copy(byte value)
          This method is used to copy a single byte to the encode buffer.
 void copy(byte[] value)
          This method copies multiple bytes to the encode buffer
 void encodeBit(boolean value)
          This method encodes a single bit value.
 void encodeBit(boolean value, java.lang.String ident)
          This method encodes a single bit value.
 void encodeBits(byte[] value, int offset, int nbits)
          This method encodes bit values from an array of octets.
 void encodeBits(byte[] value, int offset, int bitOffset, int nbits, java.lang.String ident)
          This method encodes bit values from an array of octets.
 void encodeBits(byte[] value, int offset, int nbits, java.lang.String ident)
          This method encodes bit values from an array of octets.
 void encodeBits(byte value, int nbits)
          This method encodes bit values from an octet.
 void encodeCharString(java.lang.String value, int nchars, int offset, int abpc, int ubpc, Asn1CharSet charSet)
          This method encodes the contents of a known-multiplier character string type.
 void encodeConsWholeNumber(long adjustedValue, long rangeValue)
          This method implements the rules to encode a constrained whole number as specified in section 10.5 of the X.691 standard.
 void encodeConsWholeNumber(long adjustedValue, long rangeValue, java.lang.String ident)
          This method implements the rules to encode a constrained whole number as specified in section 10.5 of the X.691 standard.
 void encodeInt(long value, boolean encodeLen, boolean signExtend)
          This method implements the rules to encode either a non-negative binary integer as specified in section 10.3 or a two's complement binary integer as specified in section 10.4 of the X.691 standard.
 void encodeInt(long value, boolean encodeLen, boolean signExtend, java.lang.String ident)
          This method implements the rules to encode either a non-negative binary integer as specified in section 10.3 or a two's complement binary integer as specified in section 10.4 of the X.691 standard.
 void encodeInt(long value, int nbits)
          This method encodes bit values from an integer value.
 void encodeInt(long value, int nbits, java.lang.String ident)
          This method encodes bit values from an integer value.
 long encodeLength(long value)
          This method encodes a general (unconstrained) length determinant value as described in section 10.9 or the X.691 standard.
 void encodeLength(long value, long lower, long upper)
          This method encodes a constrained length determinant value.
 void encodeLengthEOM(long value)
          This method checks to see if a zero byte needs to be added after a fragmented length has been encoded.
 void encodeOctetString(byte[] value, int offset, int nbytes)
          This method encodes the given array of bytes as an unconstrained octet string value.
 void encodeOIDLengthAndValue(int[] value)
          This method encodes the length and contents of an object identifier value.
 void encodeOpenType(Asn1PerEncodeBuffer buffer, java.lang.String elemName)
          This overloaded version of encodeOpenType will encode the componet in the given PER encode buffer into this PER encode buffer.
 void encodeOpenType(byte[] value, int offset, int nbytes)
          This method encodes the given array of bytes as an open type.
 void encodeRelOIDLengthAndValue(int[] value)
          This method encodes the length and contents of a relative object identifier value.
 void encodeSmallNonNegWholeNumber(int value)
          This method implements the rules to encode a small non-negative whole number as specified in section 10.6 of the X.691 standard.
 byte[] getBuffer()
          This method returns a reference to the byte buffer used to hold the encoded message.
 java.io.ByteArrayInputStream getByteArrayInputStream()
          This method returns a reference to a byte array input stream representing the encoded message.
 int getByteIndex()
          This method returns the current byte index into the encode buffer.
 java.io.InputStream getInputStream()
          This method returns an input stream representing the encoded message.
 int getMsgBitCnt()
          This method returns the number of bits in the encoded PER message.
 int getMsgByteCnt()
          This method returns the number of bytes in the encoded PER message.
 byte[] getMsgCopy()
          This method returns the encoded message in a byte array.
 int getMsgLength()
          This method returns the length (in bytes) of the encoded message component.
 Asn1PerTraceHandler getTraceHandler()
          This method will return a reference to the internal trace handler object used to trace the bit fields within a PER message.
 void hexDump()
          This method dumps the encoded message in hex/ascii format to the standard output stream.
 boolean isAligned()
          This method is used to test if PER aligned encoding has been specified.
 void reset()
          This method resets the buffer object so that it can be reused to encode another PER message.
 void reverseBytes(int offset, int nbytes)
          This method reverses a series of bytes at a given offset within the encode buffer.
 void setAligned(boolean value)
          This method is used to turn PER aligned encoding on or off
 java.lang.String toString()
          This method will return a string representation of the data in the encode buffer.
 void write(java.io.OutputStream out)
          This method writes the encoded record to the given output stream.
 
Methods inherited from class com.objsys.asn1j.runtime.Asn1EncodeBuffer
binDump, hexDump, initBuffer, trimBitString
 
Methods inherited from class com.objsys.asn1j.runtime.Asn1MessageBuffer
addNamedEventHandler, getEventHandlerListCount, hasEventHandlers, invokeCharacters, invokeEndElement, invokeStartElement, setEventHandlerList
 
Methods inherited from class com.objsys.asn1j.runtime.Asn1MessageBufferBase
getContext, hexDump, hexDump, setKey, setTypeCode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

mTraceHandler

protected Asn1PerTraceHandler mTraceHandler
Constructor Detail

Asn1PerEncodeBuffer

public Asn1PerEncodeBuffer(boolean aligned)
This constructor creates a PER encode buffer object with the default initial size. Whenever the buffer becomes full, it will be expanded.

Parameters:
aligned - Indicates whether PER aligned or unaligned encoding should be done.

Asn1PerEncodeBuffer

public Asn1PerEncodeBuffer(boolean aligned,
                           int size)
This constructor creates a PER encode buffer object with the given initial size. Whenever the buffer becomes full, it will be expanded. For best performance, this size should be large enough to prevent resizing in normal operation.

Parameters:
aligned - Indicates whether PER aligned or unaligned encoding should be done.
size - The initial size in bytes of an encode buffer.
Method Detail

binDump

public void binDump(java.io.PrintStream out,
                    java.lang.String varName)
This method dumps the encoded message in a human-readable format showing a bit trace of all fields to the given print output stream.

Specified by:
binDump in class Asn1EncodeBuffer

byteAlign

public void byteAlign()
This methods byte-aligns the buffer.

Specified by:
byteAlign in interface Asn1PerMessageBuffer

checkSize

protected void checkSize(int bytesRequired)
This method determines if the encode buffer can hold the requested number of bytes. If not, the buffer is expanded. This implementation assumes mByteIndex is used, and that the array is filling from 0..n.

Overrides:
checkSize in class Asn1EncodeBuffer
Parameters:
bytesRequired - Number of required bytes.

copy

public void copy(byte value)
This method is used to copy a single byte to the encode buffer.

Specified by:
copy in class Asn1EncodeBuffer
Parameters:
value - The byte value to copy

copy

public void copy(byte[] value)
          throws Asn1Exception
This method copies multiple bytes to the encode buffer

Specified by:
copy in class Asn1EncodeBuffer
Parameters:
value - Array of bytes to copy to the encode buffer
Throws:
Asn1Exception

encodeBit

public void encodeBit(boolean value,
                      java.lang.String ident)
This method encodes a single bit value.

Specified by:
encodeBit in interface Asn1PerEncoder
Parameters:
value - Boolean value of bit to be encoded.
ident - Bit field identifier name for tracing.

encodeBit

public void encodeBit(boolean value)
This method encodes a single bit value. The ident argument which is used for tracing is defaulted to 'value'.

Specified by:
encodeBit in interface Asn1PerEncoder
Parameters:
value - Boolean value of bit to be encoded.

encodeBits

public void encodeBits(byte value,
                       int nbits)
                throws Asn1InvalidArgException
This method encodes bit values from an octet. The most significant bits from the octet are encoded.

Specified by:
encodeBits in interface Asn1PerEncoder
Parameters:
value - Octet containing bits to be encoded
nbits - Number of bits to encode
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeBits

public void encodeBits(byte[] value,
                       int offset,
                       int nbits,
                       java.lang.String ident)
                throws Asn1InvalidArgException
This method encodes bit values from an array of octets.

Specified by:
encodeBits in interface Asn1PerEncoder
Parameters:
value - Octet array containing bits to be encoded
offset - Starting byte offset in value
nbits - Number of bits to encode
ident - Bit field identifier name for tracing, or null.
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeBits

public void encodeBits(byte[] value,
                       int offset,
                       int bitOffset,
                       int nbits,
                       java.lang.String ident)
                throws Asn1InvalidArgException
This method encodes bit values from an array of octets.

Parameters:
value - Octet array containing bits to be encoded
offset - Starting byte offset in value
bitOffset - Starting bit offset in first byte. Pass 0 to begin encoding with the most significant bit. Pass 7 to begin with the least significant bit.
nbits - Number of bits to encode
ident - Bit field identifier name for tracing, or null.
Throws:
Asn1InvalidArgException

encodeBits

public void encodeBits(byte[] value,
                       int offset,
                       int nbits)
                throws Asn1InvalidArgException
This method encodes bit values from an array of octets. The ident argument which is used for tracing is defaulted to 'value'.

Specified by:
encodeBits in interface Asn1PerEncoder
Parameters:
value - Octet array containing bits to be encoded
offset - Starting byte offset in value
nbits - Number of bits to encode
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeCharString

public void encodeCharString(java.lang.String value,
                             int nchars,
                             int offset,
                             int abpc,
                             int ubpc,
                             Asn1CharSet charSet)
                      throws Asn1Exception
This method encodes the contents of a known-multiplier character string type. This version assumes a permitted alphabet constraint was specified.

Specified by:
encodeCharString in interface Asn1PerEncoder
Parameters:
value - String containing characters to encode
nchars - Number of characters from string to encode
offset - Offset to first char in string to encode
abpc - Number of bits per character (aligned)
ubpc - Number of bits per character (unaligned)
charSet - Object representing permitted alphabet constraint character set (optional)
Throws:
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeConsWholeNumber

public void encodeConsWholeNumber(long adjustedValue,
                                  long rangeValue,
                                  java.lang.String ident)
                           throws Asn1InvalidArgException
This method implements the rules to encode a constrained whole number as specified in section 10.5 of the X.691 standard.

Specified by:
encodeConsWholeNumber in interface Asn1PerEncoder
Parameters:
adjustedValue - Adjusted value to be encoded = value - lower range endpoint value. Negative values are interpreted as unsigned integers.
rangeValue - upper - lower + 1. Negative values are interpreted as unsigned integers. Zero is used to represent 2^64.
ident - Tracing identifier
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeConsWholeNumber

public void encodeConsWholeNumber(long adjustedValue,
                                  long rangeValue)
                           throws Asn1InvalidArgException
This method implements the rules to encode a constrained whole number as specified in section 10.5 of the X.691 standard. The ident argument which is used for tracing is defaulted to 'value'.

Specified by:
encodeConsWholeNumber in interface Asn1PerEncoder
Parameters:
adjustedValue - Adjusted value to be encoded = value - lower range endpoint value
rangeValue - upper - lower + 1. Negative values are interpreted as unsigned integers. Zero is used to represent 2^64.
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeInt

public void encodeInt(long value,
                      int nbits,
                      java.lang.String ident)
               throws Asn1InvalidArgException
This method encodes bit values from an integer value. The least significant bits from the integer are encoded.

Specified by:
encodeInt in interface Asn1PerEncoder
Parameters:
value - Integer containing bits to be encoded
nbits - Number of bits to encode
ident - Tracing identifier
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeInt

public void encodeInt(long value,
                      int nbits)
               throws Asn1InvalidArgException
This method encodes bit values from an integer value. The least significant bits from the integer are encoded. The ident argument which is used for tracing is defaulted to 'value'.

Specified by:
encodeInt in interface Asn1PerEncoder
Parameters:
value - Integer containing bits to be encoded
nbits - Number of bits to encode
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeInt

public void encodeInt(long value,
                      boolean encodeLen,
                      boolean signExtend,
                      java.lang.String ident)
               throws Asn1InvalidArgException
This method implements the rules to encode either a non-negative binary integer as specified in section 10.3 or a two's complement binary integer as specified in section 10.4 of the X.691 standard.

Specified by:
encodeInt in interface Asn1PerEncoder
Parameters:
value - Integer value to be encoded
encodeLen - Flag indicating length determinant should be encoded before encoding integer value.
signExtend - Flag indicating if sign extension should be performed.
ident - Tracing identifier
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeInt

public void encodeInt(long value,
                      boolean encodeLen,
                      boolean signExtend)
               throws Asn1InvalidArgException
This method implements the rules to encode either a non-negative binary integer as specified in section 10.3 or a two's complement binary integer as specified in section 10.4 of the X.691 standard. The ident argument which is used for tracing is defaulted to 'value'.

Specified by:
encodeInt in interface Asn1PerEncoder
Parameters:
value - Integer value to be encoded
encodeLen - Flag indicating length determinant should be encoded before encoding integer value.
signExtend - Flag indicating if sign extension should be performed.
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeLength

public long encodeLength(long value)
                  throws Asn1InvalidArgException
This method encodes a general (unconstrained) length determinant value as described in section 10.9 or the X.691 standard.

Specified by:
encodeLength in interface Asn1PerEncoder
Parameters:
value - Length value to be encoded
Returns:
Value that was actually encoded. This may be less than the value that was passed in if fragementation was done (i.e the value was >= 16k).
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeLength

public void encodeLength(long value,
                         long lower,
                         long upper)
                  throws Asn1Exception
This method encodes a constrained length determinant value. This method should not be used when fragmentation is called for (ie, upper >= 64K and value >= 16K). See encodeLength(value) instead.

Specified by:
encodeLength in interface Asn1PerEncoder
Parameters:
value - Length value to be encoded
lower - Lower bound (inclusive) of length value range
upper - Upper bound (inclusive) of length value range
Throws:
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeLengthEOM

public void encodeLengthEOM(long value)
This method checks to see if a zero byte needs to be added after a fragmented length has been encoded. It will add it to the byte stream if necessary.

Specified by:
encodeLengthEOM in interface Asn1PerEncoder
Parameters:
value - Original length value that was encoded.

encodeOIDLengthAndValue

public void encodeOIDLengthAndValue(int[] value)
                             throws Asn1Exception
This method encodes the length and contents of an object identifier value.

Specified by:
encodeOIDLengthAndValue in interface Asn1PerEncoder
Parameters:
value - Integer array containing arcs to encode
Throws:
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeRelOIDLengthAndValue

public void encodeRelOIDLengthAndValue(int[] value)
                                throws Asn1Exception
This method encodes the length and contents of a relative object identifier value.

Specified by:
encodeRelOIDLengthAndValue in interface Asn1PerEncoder
Parameters:
value - Integer array containing arcs to encode
Throws:
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeOctetString

public void encodeOctetString(byte[] value,
                              int offset,
                              int nbytes)
                       throws Asn1Exception
This method encodes the given array of bytes as an unconstrained octet string value.

Specified by:
encodeOctetString in interface Asn1PerEncoder
Parameters:
value - Byte array containing data to encode. This is assumed to contain a previously encoded PER component.
offset - Starting offset in byte array value
nbytes - Number of bytes to encode
Throws:
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeOpenType

public void encodeOpenType(byte[] value,
                           int offset,
                           int nbytes)
                    throws Asn1Exception
This method encodes the given array of bytes as an open type.

Specified by:
encodeOpenType in interface Asn1PerEncoder
Parameters:
value - Byte array containing data to encode. This is assumed to contain a previously encoded PER component.
offset - Starting offset in byte array value
nbytes - Number of bytes to encode
Throws:
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeOpenType

public void encodeOpenType(Asn1PerEncodeBuffer buffer,
                           java.lang.String elemName)
                    throws Asn1Exception
This overloaded version of encodeOpenType will encode the componet in the given PER encode buffer into this PER encode buffer.

Parameters:
buffer - PER encode buffer containing encoded message component.
elemName - Name of element being encoded.
Throws:
Asn1Exception

encodeSmallNonNegWholeNumber

public void encodeSmallNonNegWholeNumber(int value)
                                  throws Asn1InvalidArgException
This method implements the rules to encode a small non-negative whole number as specified in section 10.6 of the X.691 standard.

Specified by:
encodeSmallNonNegWholeNumber in interface Asn1PerEncoder
Parameters:
value - Value to be encoded
Throws:
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

getBuffer

public byte[] getBuffer()
This method returns a reference to the byte buffer used to hold the encoded message.

Returns:
Byte buffer reference

getByteIndex

public int getByteIndex()
This method returns the current byte index into the encode buffer.

Returns:
Byte index value

getByteArrayInputStream

public java.io.ByteArrayInputStream getByteArrayInputStream()
This method returns a reference to a byte array input stream representing the encoded message. This is the preferred way to access the contents of the encoded message as it is the most efficient.

Returns:
byte array input stream containing encoded message

getInputStream

public java.io.InputStream getInputStream()
This method returns an input stream representing the encoded message. This method is defined as abstract in the base class and must be implemented by all derived classes. In this case, a byte array input stream is returned.

Specified by:
getInputStream in interface Asn1PerMessageBuffer
Specified by:
getInputStream in class Asn1MessageBuffer
Returns:
Input stream containing encoded message

getMsgBitCnt

public int getMsgBitCnt()
This method returns the number of bits in the encoded PER message.

Specified by:
getMsgBitCnt in interface Asn1PerMessageBuffer
Returns:
Count of bits in encoded message

getMsgByteCnt

public int getMsgByteCnt()
This method returns the number of bytes in the encoded PER message. The number is rounded up to include the last byte if one or more bits have been set in that byte.

Returns:
Count of bytes in encoded message

getMsgCopy

public byte[] getMsgCopy()
This method returns the encoded message in a byte array. This is less efficient than the getByteArrayInputStream method because the message contents must be copied to a newly created byte array.

Specified by:
getMsgCopy in class Asn1EncodeBuffer
Returns:
byte array containing encoded message

getMsgLength

public int getMsgLength()
This method returns the length (in bytes) of the encoded message component.

Specified by:
getMsgLength in class Asn1EncodeBuffer
Returns:
length of encoded message component

hexDump

public void hexDump()
This method dumps the encoded message in hex/ascii format to the standard output stream.

Overrides:
hexDump in class Asn1EncodeBuffer

isAligned

public boolean isAligned()
This method is used to test if PER aligned encoding has been specified.

Specified by:
isAligned in interface Asn1PerMessageBuffer

reset

public void reset()
This method resets the buffer object so that it can be reused to encode another PER message. Any previously encoded data is lost.

Specified by:
reset in class Asn1EncodeBuffer

reverseBytes

public void reverseBytes(int offset,
                         int nbytes)
This method reverses a series of bytes at a given offset within the encode buffer.

Parameters:
offset - Starting byte offset within the buffer
nbytes - Number of bytes to reverse

setAligned

public void setAligned(boolean value)
This method is used to turn PER aligned encoding on or off


write

public void write(java.io.OutputStream out)
           throws java.io.IOException
This method writes the encoded record to the given output stream.

Specified by:
write in class Asn1EncodeBuffer
Parameters:
out - Output stream to which record is to be written
Throws:
java.io.IOException

getTraceHandler

public Asn1PerTraceHandler getTraceHandler()
This method will return a reference to the internal trace handler object used to trace the bit fields within a PER message.

Specified by:
getTraceHandler in interface Asn1PerMessageBuffer

toString

public java.lang.String toString()
This method will return a string representation of the data in the encode buffer. The format is hex characters.

Overrides:
toString in class java.lang.Object
Returns:
Stringified representation of the value