com.objsys.asn1j.runtime
Class Asn1PerEncodeBuffer

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

public class Asn1PerEncodeBuffer
extends Asn1EncodeBuffer
implements Asn1PerMessageBuffer

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
mByteIndex, mData, mSizeIncrement, SIZE_INCREMENT
 
Constructor Summary
Asn1PerEncodeBuffer(boolean aligned)
          This constructor creates a PER encode buffer object with the default size increment.
Asn1PerEncodeBuffer(boolean aligned, int sizeIncrement)
          This constructor creates a PER encode buffer object with the given size increment.
 
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 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
 
Methods inherited from class com.objsys.asn1j.runtime.Asn1MessageBuffer
hexDump, hexDump, setKey
 
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 size increment. Whenever the buffer becomes full, the buffer will be expanded by the sizeIncrement size.

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

Asn1PerEncodeBuffer

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

Parameters:
aligned - Indicates whether PER aligned or unaligned encoding should be done.
sizeIncrement - The initial size in bytes of an encode buffer. If the buffer becomes full, it will be expanded by the amount.
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.

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.

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'.

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.

Parameters:
value - Octet containing bits to be encoded
nbits - Number of bits to encode
Throws:
Asn1InvalidArgException

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.

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.
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'.

Parameters:
value - Octet array containing bits to be encoded
offset - Starting byte offset in value
nbits - Number of bits to encode
Throws:
Asn1InvalidArgException

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.

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

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.

Parameters:
adjustedValue - Adjusted value to be encoded = value - lower range endpoint value
rangeValue - lower - upper + 1
ident - Tracing identifier
Throws:
Asn1InvalidArgException

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'.

Parameters:
adjustedValue - Adjusted value to be encoded = value - lower range endpoint value
rangeValue - lower - upper + 1
Throws:
Asn1InvalidArgException

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.

Parameters:
value - Integer containing bits to be encoded
nbits - Number of bits to encode
ident - Tracing identifier
Throws:
Asn1InvalidArgException

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'.

Parameters:
value - Integer containing bits to be encoded
nbits - Number of bits to encode
Throws:
Asn1InvalidArgException

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.

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

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'.

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

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.

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

encodeLength

public void encodeLength(long value,
                         long lower,
                         long upper)
                  throws Asn1Exception
This method encodes a constrained length determinant value.

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

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.

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.

Parameters:
value - Integer array containing arcs to encode
Throws:
Asn1Exception

encodeRelOIDLengthAndValue

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

Parameters:
value - Integer array containing arcs to encode
Throws:
Asn1Exception

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.

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

encodeOpenType

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

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

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.

Parameters:
value - Value to be encoded
Throws:
Asn1InvalidArgException

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