org.jasn
Class BerEncoder

java.lang.Object
  extended byorg.jasn.BaseBerEncoder
      extended byorg.jasn.BerEncoder
All Implemented Interfaces:
Asn1Encoder

public class BerEncoder
extends BaseBerEncoder

An ASN.1 BER encoder.


Field Summary
 
Fields inherited from class org.jasn.BaseBerEncoder
implicitTag
 
Constructor Summary
BerEncoder()
          Creates a new encoder that will write to the given output stream using the BER encoding.
 
Method Summary
 int encodeConstructed(int tag)
           
 int encodeExplicit(int tag)
          Starts the encoding of an explicitly tagged value.
protected  void encodeLength(int len)
          Encodes the length part of a BER TLV.
 int encodeSequence()
          Starts the encoding of an ASN.1 SEQUENCE.
 int encodeSet()
          Starts the encoding of an ASN.1 SET.
 void endOf(int id)
          Terminates the encoding of the constructed value identified by id.
 void reset()
          Resets the state of this encoder.
protected  void writeByte(int b)
          Writes the specified byte to this output stream.
protected  void writeBytes(byte[] b, int offs, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 void writeTo(OutputStream out)
           
 
Methods inherited from class org.jasn.BaseBerEncoder
encodeAny, encodeBitString, encodeBitString, encodeBMPString, encodeBoolean, encodeEnumerated, encodeEnumerated, encodeEnumerated, encodeGeneralizedTime, encodeGeneralString, encodeGraphicString, encodeIA5String, encodeImplicit, encodeInteger, encodeInteger, encodeInteger, encodeNull, encodeNumericString, encodeObjectIdentifier, encodeOctetString, encodeOctetString, encodePrintableString, encodeReal, encodeSequenceOf, encodeSetOf, encodeTag, encodeTeletexString, encodeUniversalString, encodeUTCTime, encodeUTF8String, encodeVideotexString, encodeVisibleString, getByteBuffer, getCharBuffer, isDefaultEncoded, setName, writeBytes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BerEncoder

public BerEncoder()
Creates a new encoder that will write to the given output stream using the BER encoding.

Method Detail

encodeSequence

public int encodeSequence()
                   throws IOException
Description copied from interface: Asn1Encoder
Starts the encoding of an ASN.1 SEQUENCE.

An ASN.1 SEQUENCE is an ordered collection of various data element. The decoding of a SEQUENCE must always start with this method; then the elements defined in the SEQUENCE should be decoded with their corresponding decode methods. Finally, the endOf method must be invoked to terminate the decoding of the SEQUENCE.

The returned int value must be passed to the endOf method that then checks if the current nesting and the passed int do match.

The code samples below show the decoding of two nested sequences. The corresponding ASN.1 definition is:

 SEQUENCE {
    s IA5String,
    a SEQUENCE {
        i INTEGER
    }
 }
 

 int id1 = enc.encodeSequence();
     enc.decodeIA5String("Organization");
     int id2 = enc.encodeSequence();
         enc.encodeInteger(124);
     enc.endOf(id2);
 enc.endOf(id1);
 

Specified by:
encodeSequence in interface Asn1Encoder
Specified by:
encodeSequence in class BaseBerEncoder
Throws:
IOException

endOf

public void endOf(int id)
           throws IOException
Description copied from interface: Asn1Encoder
Terminates the encoding of the constructed value identified by id.

Specified by:
endOf in interface Asn1Encoder
Specified by:
endOf in class BaseBerEncoder
Throws:
IOException

encodeSet

public int encodeSet()
              throws IOException
Description copied from interface: Asn1Encoder
Starts the encoding of an ASN.1 SET.

An ASN.1 SET is an unordered collection of various data element. The encoding of a SET must always start with this method; then the elements defined in the SET should be encoded with their corresponding encode methods. Finally, the endOf method must be invoked to terminate the encoding of the SET.

The returned int value must be passed to the endOf method that then checks if the current nesting and the passed int do match.

The code samples below show the decoding of two nested sequences. The corresponding ASN.1 definition is:

 SET {
    s IA5String,
    a SET {
        i INTEGER
    }
 }
 

 int id1 = enc.encodeSet();
     enc.decodeIA5String("Organization");
     int id2 = enc.encodeSet();
         enc.encodeInteger(124);
     enc.endOf(id2);
 enc.endOf(id1);
 

Specified by:
encodeSet in interface Asn1Encoder
Specified by:
encodeSet in class BaseBerEncoder
Throws:
IOException

encodeExplicit

public int encodeExplicit(int tag)
                   throws IOException
Starts the encoding of an explicitly tagged value. This tells the encoder to encode the next ASN.1 value explicitly.

Specified by:
encodeExplicit in interface Asn1Encoder
Specified by:
encodeExplicit in class BaseBerEncoder
Parameters:
tag - the tag used to explicitly tagged the next encoded value.
Throws:
IOException

encodeConstructed

public int encodeConstructed(int tag)
                      throws IOException
Throws:
IOException

writeTo

public void writeTo(OutputStream out)
             throws IOException
Throws:
IOException

encodeLength

protected void encodeLength(int len)
                     throws IOException
Description copied from class: BaseBerEncoder
Encodes the length part of a BER TLV.

Overrides:
encodeLength in class BaseBerEncoder
Throws:
IOException

writeByte

protected void writeByte(int b)
                  throws IOException
Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Specified by:
writeByte in class BaseBerEncoder
Parameters:
b - the byte.
Throws:
IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.

writeBytes

protected void writeBytes(byte[] b,
                          int offs,
                          int len)
                   throws IOException
Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

The write method of OutputStream calls the write method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Specified by:
writeBytes in class BaseBerEncoder
Parameters:
b - the data.
offs - the start offset in the data.
len - the number of bytes to write.
Throws:
IOException - if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.

reset

public void reset()
Resets the state of this encoder. This method should normally be called if some error have been thrown during encoding.



Copyright © 2004 Nicolas Vraux. All Rights Reserved.