com.objsys.asn1j.runtime
Class Asn1PerOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.objsys.asn1j.runtime.Asn1OutputStream
          extended by com.objsys.asn1j.runtime.Asn1PerOutputStream
All Implemented Interfaces:
Asn1PerEncoder, java.io.Closeable, java.io.Flushable

public class Asn1PerOutputStream
extends Asn1OutputStream
implements Asn1PerEncoder

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


Field Summary
protected  Asn1PerOutputStreamTraceHandler mTraceHandler
           
 
Fields inherited from class com.objsys.asn1j.runtime.Asn1OutputStream
os
 
Constructor Summary
Asn1PerOutputStream(java.io.OutputStream os, boolean aligned)
          This constructor creates a buffered PER output stream object with the default size of buffer.
Asn1PerOutputStream(java.io.OutputStream os, int bufSize, boolean aligned)
          This constructor creates a buffered PER output stream object with the specified size of buffer.
 
Method Summary
 void addCaptureBuffer(java.io.ByteArrayOutputStream buffer)
          This method is used to add a capture buffer to the internal capture buffer list.
 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 binDump(java.lang.String varName)
          This method invokes an overloaded version of binDump to dump the encoded message to standard output.
 void byteAlign()
          This methods byte-aligns the buffer.
 void close()
          Close the stream.
 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(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 encodeSmallLength(int value)
          This method implements the rules to encode a normally small length as specified in section 11.9 of the X.691 standard.
 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.
 void flush()
          Flush the buffer to the stream.
 Asn1PerTraceHandler getTraceHandler()
          This method will return a reference to the internal trace handler object used to trace the bit fields within a PER message.
 boolean isAligned()
          This method is used to test if PER aligned encoding has been specified.
 void removeCaptureBuffer(java.io.ByteArrayOutputStream buffer)
          This method is used to remove a capture buffer from the internal capture buffer list.
 void write(byte[] b)
          Writes b.length bytes from the specified byte array to this output stream.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 void write(int b)
          Writes the specified byte to this output stream.
 
Methods inherited from class com.objsys.asn1j.runtime.Asn1OutputStream
getContext, write2Bytes, write4Bytes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mTraceHandler

protected Asn1PerOutputStreamTraceHandler mTraceHandler
Constructor Detail

Asn1PerOutputStream

public Asn1PerOutputStream(java.io.OutputStream os,
                           boolean aligned)
This constructor creates a buffered PER output stream object with the default size of buffer. Whenever the buffer becomes full, the buffer will be flushed to the stream.

Parameters:
os - The underlying OutputStream object.
aligned - Indicates whether PER aligned or unaligned encoding should be done.

Asn1PerOutputStream

public Asn1PerOutputStream(java.io.OutputStream os,
                           int bufSize,
                           boolean aligned)
This constructor creates a buffered PER output stream object with the specified size of buffer. Whenever the buffer becomes full, the buffer will be flushed to the stream.

Parameters:
os - The underlying OutputStream object.
bufSize - The buffer size.
aligned - Indicates whether PER aligned or unaligned encoding should be done.
Method Detail

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.


binDump

public void binDump(java.lang.String varName)
This method invokes an overloaded version of binDump to dump the encoded message to standard output.


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.


flush

public void flush()
           throws java.io.IOException
Flush the buffer to the stream. It writes whole bytes only.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class Asn1OutputStream
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.

close

public void close()
           throws java.io.IOException
Close the stream. Writes all bytes (even unfinished) before closing.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class Asn1OutputStream
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.

isAligned

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


byteAlign

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


encodeBit

public void encodeBit(boolean value)
               throws java.io.IOException,
                      Asn1Exception
Description copied from interface: Asn1PerEncoder
This method encodes a single bit value.

Specified by:
encodeBit in interface Asn1PerEncoder
Parameters:
value - Boolean value of bit to be encoded.
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeBit

public void encodeBit(boolean value,
                      java.lang.String ident)
               throws java.io.IOException,
                      Asn1Exception
Description copied from interface: Asn1PerEncoder
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.
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeBits

public void encodeBits(byte value,
                       int nbits)
                throws java.io.IOException,
                       Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeBits

public void encodeBits(byte[] value,
                       int offset,
                       int nbits)
                throws java.io.IOException,
                       Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeBits

public void encodeBits(byte[] value,
                       int offset,
                       int nbits,
                       java.lang.String ident)
                throws java.io.IOException,
                       Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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.
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
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 java.io.IOException,
                             Asn1Exception
Description copied from interface: Asn1PerEncoder
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:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeConsWholeNumber

public void encodeConsWholeNumber(long adjustedValue,
                                  long rangeValue,
                                  java.lang.String ident)
                           throws java.io.IOException,
                                  Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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
rangeValue - lower - upper + 1
ident - Bit field identifier name for tracing.
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeConsWholeNumber

public void encodeConsWholeNumber(long adjustedValue,
                                  long rangeValue)
                           throws java.io.IOException,
                                  Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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
rangeValue - upper - lower + 1. Negative values are interpreted as unsigned integers. Zero is used to represent 2^64.
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeInt

public void encodeInt(long value,
                      int nbits,
                      java.lang.String ident)
               throws java.io.IOException,
                      Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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 - Bit field identifier name for tracing.
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeInt

public void encodeInt(long value,
                      int nbits)
               throws java.io.IOException,
                      Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeInt

public void encodeInt(long value,
                      boolean encodeLen,
                      boolean signExtend,
                      java.lang.String ident)
               throws java.io.IOException,
                      Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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 - Bit field identifier name for tracing.
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeInt

public void encodeInt(long value,
                      boolean encodeLen,
                      boolean signExtend)
               throws java.io.IOException,
                      Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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.
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeLength

public long encodeLength(long value)
                  throws java.io.IOException,
                         Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeLength

public void encodeLength(long value,
                         long lower,
                         long upper)
                  throws java.io.IOException,
                         Asn1Exception
Description copied from interface: Asn1PerEncoder
This method encodes a constrained length determinant value.

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:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeLengthEOM

public void encodeLengthEOM(long value)
                     throws java.io.IOException,
                            Asn1Exception
Description copied from interface: Asn1PerEncoder
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.
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeOctetString

public void encodeOctetString(byte[] value,
                              int offset,
                              int nbytes)
                       throws java.io.IOException,
                              Asn1Exception
Description copied from interface: Asn1PerEncoder
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:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeOIDLengthAndValue

public void encodeOIDLengthAndValue(int[] value)
                             throws java.io.IOException,
                                    Asn1Exception
Description copied from interface: Asn1PerEncoder
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:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeRelOIDLengthAndValue

public void encodeRelOIDLengthAndValue(int[] value)
                                throws java.io.IOException,
                                       Asn1Exception
Description copied from interface: Asn1PerEncoder
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:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeOpenType

public void encodeOpenType(byte[] value,
                           int offset,
                           int nbytes)
                    throws java.io.IOException,
                           Asn1Exception
Description copied from interface: Asn1PerEncoder
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:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1Exception - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeSmallLength

public void encodeSmallLength(int value)
                       throws java.io.IOException,
                              Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
This method implements the rules to encode a normally small length as specified in section 11.9 of the X.691 standard.

Specified by:
encodeSmallLength in interface Asn1PerEncoder
Parameters:
value - Value to be encoded
Throws:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

encodeSmallNonNegWholeNumber

public void encodeSmallNonNegWholeNumber(int value)
                                  throws java.io.IOException,
                                         Asn1InvalidArgException
Description copied from interface: Asn1PerEncoder
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:
java.io.IOException - Any exception thrown by the underlying OutputStream.
Asn1InvalidArgException - Any exception thrown by the underlying Asn1PerEncodeBuffer.

addCaptureBuffer

public void addCaptureBuffer(java.io.ByteArrayOutputStream buffer)
This method is used to add a capture buffer to the internal capture buffer list. A capture buffer is used to capture all bytes read from this position forward from the input stream.

Parameters:
buffer - Buffer into which captured bytes are to be stored

removeCaptureBuffer

public void removeCaptureBuffer(java.io.ByteArrayOutputStream buffer)
This method is used to remove a capture buffer from the internal capture buffer list. The add and remove methods can be used to get a set of raw bytes from the input stream for further processing.

Parameters:
buffer - Buffer in which captured bytes stored

write

public void write(byte[] b)
           throws java.io.IOException
Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Overrides:
write in class Asn1OutputStream
Parameters:
b - the data.
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes from the specified byte array starting at offset off to this output stream.

Overrides:
write in class Asn1OutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
java.io.IOException - if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.

write

public void write(int b)
           throws java.io.IOException
Writes the specified byte to this output stream.

Overrides:
write in class Asn1OutputStream
Parameters:
b - the byte.
Throws:
java.io.IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.