org.jasn
Class BaseBerEncoder

java.lang.Object
  extended byorg.jasn.BaseBerEncoder
All Implemented Interfaces:
Asn1Encoder
Direct Known Subclasses:
BerEncoder, BerIndefiniteEncoder

public abstract class BaseBerEncoder
extends Object
implements Asn1Encoder

This abstract class implements the basic functionality of an ASN.1 encoder using the Basic Encoding Rules (BER).


Field Summary
protected  int implicitTag
           
 
Constructor Summary
BaseBerEncoder()
           
 
Method Summary
 void encodeAny(byte[] encoded)
          Encodes an ASN.1 ANY value.
 void encodeBitString(BitString bits)
          Encodes an ASN.1 BIT STRING value.
 void encodeBitString(byte[] bits)
          Encodes the specified bytes as an ASN.1 BIT STRING value.
 void encodeBMPString(String s)
          Encodes an ASN.1 BMPString value.
 void encodeBoolean(boolean b)
          Encodes an ASN.1 BOOLEAN value.
 void encodeEnumerated(BigInteger e)
          Encodes an ASN.1 ENUMERATED value.
 void encodeEnumerated(int e)
          Encodes an ASN.1 ENUMERATED value.
 void encodeEnumerated(long e)
          Encodes an ASN.1 ENUMERATED value.
abstract  int encodeExplicit(int tag)
          Indicates that the next ASN.1 value must be encoded with an explicit tag equals to the specified tag value.
 void encodeGeneralizedTime(Calendar date)
          Encodes the specified date as an ASN.1 GeneralizedTime.
 void encodeGeneralString(String s)
          Encodes an ASN.1 GeneralString value.
 void encodeGraphicString(String s)
          Encodes an ASN.1 GraphicString value.
 void encodeIA5String(String s)
          Encodes an ASN.1 IA5String value.
 void encodeImplicit(int tag)
          Starts the encoding of an implicitely tagged ASN.1 value.
 void encodeInteger(BigInteger i)
          Encodes an ASN.1 INTEGER value.
 void encodeInteger(int i)
          Encodes an ASN.1 INTEGER value.
 void encodeInteger(long l)
          Encodes an ASN.1 INTEGER value.
protected  void encodeLength(int len)
          Encodes the length part of a BER TLV.
 void encodeNull()
          Encodes an ASN.1 NULL value.
 void encodeNumericString(String s)
          Encodes an ASN.1 NumericString value.
 void encodeObjectIdentifier(ObjectIdentifier oid)
          Encodes an ASN.1 OBJECT IDENTIFIER value.
 void encodeOctetString(byte[] b)
          Encodes the b.length bytes from the specified byte array as an ASN.1 OCTET STRING.
 void encodeOctetString(byte[] b, int offs, int len)
          Encodes an ASN.1 OCTET STRING consisting of len bytes of the specified byte array starting at offset off.
 void encodePrintableString(String s)
          Encodes an ASN.1 PrintableString value.
 void encodeReal(double value)
          Encodes an ASN.1 REAL value.
abstract  int encodeSequence()
          Starts the encoding of an ASN.1 SEQUENCE.
 int encodeSequenceOf()
          Starts the encoding of an ASN.1 SEQUENCE OF.
abstract  int encodeSet()
          Starts the encoding of an ASN.1 SET.
 int encodeSetOf()
          Starts the encoding of an ASN.1 SET OF.
protected  int encodeTag(int tag, boolean constructed)
           
 void encodeTeletexString(String s)
          Encodes an ASN.1 TeletexString value.
 void encodeUniversalString(String s)
          Encodes an ASN.1 UniversalString value.
 void encodeUTCTime(Calendar date)
          Encodes the specified date as an ASN.1 UTCTime.
 void encodeUTF8String(String s)
          Encodes an ASN.1 UTF8String value.
 void encodeVideotexString(String s)
          Encodes an ASN.1 VideotexString value.
 void encodeVisibleString(String s)
          Encodes an ASN.1 VisibleString value.
abstract  void endOf(int id)
          Terminates the encoding of the constructed value identified by id.
protected  byte[] getByteBuffer(int len)
           
protected  char[] getCharBuffer(int len)
          Returns an array of characters whose size is sufficient to hold len characters.
 boolean isDefaultEncoded()
          Indicates wether this encoder encodes DEFAULT values.
 void setName(String name)
           
protected abstract  void writeByte(int b)
           
protected  void writeBytes(byte[] b)
           
protected abstract  void writeBytes(byte[] b, int offs, int len)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jasn.Asn1Encoder
reset
 

Field Detail

implicitTag

protected int implicitTag
Constructor Detail

BaseBerEncoder

public BaseBerEncoder()
Method Detail

encodeAny

public void encodeAny(byte[] encoded)
               throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 ANY value. The specified array of bytes should contains a valid encoded ASN.1 value compatible with this encoder, i.e. if this encoder is a DER encoder, the specified bytes should represent a value that has been DER encoded.

A NullPointerException is thrown if the array is null.

Specified by:
encodeAny in interface Asn1Encoder
Parameters:
encoded - the array of bytes
Throws:
IOException

encodeBoolean

public void encodeBoolean(boolean b)
                   throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 BOOLEAN value.

Specified by:
encodeBoolean in interface Asn1Encoder
Parameters:
b - the boolean value to be encoded.
Throws:
IOException

encodeInteger

public void encodeInteger(BigInteger i)
                   throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 INTEGER value.

Note: For performance reason, it is recommanded to use the other encodeInteger methods with int or long as parameter (if the value to encode may be represented by a int or a long).

If i is null, a NullPointerException is thrown.

Specified by:
encodeInteger in interface Asn1Encoder
Parameters:
i - the arbitrary-precision integer to be encoded.
Throws:
IOException
See Also:
Asn1Encoder.encodeInteger(int), Asn1Encoder.encodeInteger(long)

encodeInteger

public void encodeInteger(int i)
                   throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 INTEGER value.

Specified by:
encodeInteger in interface Asn1Encoder
Parameters:
i - the number to be encoded.
Throws:
IOException

encodeInteger

public void encodeInteger(long l)
                   throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 INTEGER value.

Specified by:
encodeInteger in interface Asn1Encoder
Parameters:
l - the number to be encoded.
Throws:
IOException

encodeBitString

public void encodeBitString(BitString bits)
                     throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 BIT STRING value.

If bits is null, a NullPointerException is thrown.

Specified by:
encodeBitString in interface Asn1Encoder
Parameters:
bits - a BitString representing the sequence of bits to be encoded.
Throws:
IOException

encodeBitString

public void encodeBitString(byte[] bits)
                     throws IOException
Description copied from interface: Asn1Encoder
Encodes the specified bytes as an ASN.1 BIT STRING value.

If bits is null, a NullPointerException is thrown.

Specified by:
encodeBitString in interface Asn1Encoder
Parameters:
bits - an array of bytes representing the sequence of bits to be encoded.
Throws:
IOException

encodeOctetString

public void encodeOctetString(byte[] b,
                              int offs,
                              int len)
                       throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 OCTET STRING consisting of len bytes of the specified byte array starting at offset off.

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:
encodeOctetString in interface Asn1Encoder
Parameters:
b - the byte array containing the bytes to be encoded.
offs - the start offset in the byte array.
len - the number of bytes to encode.
Throws:
IOException

encodeOctetString

public void encodeOctetString(byte[] b)
                       throws IOException
Description copied from interface: Asn1Encoder
Encodes the b.length bytes from the specified byte array as an ASN.1 OCTET STRING.

If b is null, a NullPointerException is thrown.

Specified by:
encodeOctetString in interface Asn1Encoder
Parameters:
b - the byte array to be encoded.
Throws:
IOException
See Also:
Asn1Encoder.encodeOctetString(byte[],int,int)

encodeNull

public void encodeNull()
                throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 NULL value.

Specified by:
encodeNull in interface Asn1Encoder
Throws:
IOException

encodeObjectIdentifier

public void encodeObjectIdentifier(ObjectIdentifier oid)
                            throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 OBJECT IDENTIFIER value.

If oid is null, a NullPointerException is thrown.

Specified by:
encodeObjectIdentifier in interface Asn1Encoder
Parameters:
oid - the ObjectIdentifier to encode.
Throws:
IOException

encodeReal

public void encodeReal(double value)
                throws IOException
Encodes an ASN.1 REAL value.

This method is currently not implemented and throws an UnsupportedOperationException each time it is invoked.

Specified by:
encodeReal in interface Asn1Encoder
Parameters:
value - the number to be encoded.
Throws:
IOException

encodeEnumerated

public void encodeEnumerated(BigInteger e)
                      throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 ENUMERATED value.

Note: This method should be only used in the rare case where the ENUMERATED value to encode cannot be hold in a int or a long. Otherwise the encodeEnumerated methods with int or long parameter should be used.

Specified by:
encodeEnumerated in interface Asn1Encoder
Parameters:
e - the ENUMERATED value represented as a abitrary-precision integer.
Throws:
IOException
See Also:
Asn1Encoder.encodeEnumerated(int), Asn1Encoder.encodeEnumerated(long)

encodeEnumerated

public void encodeEnumerated(int e)
                      throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 ENUMERATED value.

Specified by:
encodeEnumerated in interface Asn1Encoder
Parameters:
e - the ENUMERATED value to encode.
Throws:
IOException

encodeEnumerated

public void encodeEnumerated(long e)
                      throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 ENUMERATED value.

Specified by:
encodeEnumerated in interface Asn1Encoder
Parameters:
e - the ENUMERATED value to encode.
Throws:
IOException

encodeUTF8String

public void encodeUTF8String(String s)
                      throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 UTF8String value.

UTF8String is synonymous with UniversalString but the encoding uses the UTF-8 transformation format described in the RFC 2279.

Specified by:
encodeUTF8String in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeSequence

public abstract 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
Returns:
an identifier for the SEQUENCE that must be passed to the endOf method to terminate the encoding of the SEQUENCE.
Throws:
IOException

encodeSequenceOf

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

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

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 encoding of SEQUENCE OF. The corresponding ASN.1 definition is:

 MySequence ::= SEQUENCE OF PrintableString
 

 List mySequence = ...; // contains a list of String
 

int id = enc.encodeSequenceOf(); Iterator it = mySequence.iterator(); while (it.hasNext()) { enc.encodePrintableString((String)it.next()); }

enc.endOf(id);

Specified by:
encodeSequenceOf in interface Asn1Encoder
Returns:
a identifier that must be passed to the endOf method to terminate the decoding of the SEQUENCE OF.
Throws:
IOException

encodeSet

public abstract 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
Returns:
a identifier that must be passed to the endOf method to terminate the decoding of the SET.
Throws:
IOException

encodeSetOf

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

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

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 encoding of SET OF. The corresponding ASN.1 definition is:

 MySet ::= SET OF PrintableString
 

 Set mySet = ...; // contains a set of String
 

int id = enc.encodeSetOf(); Iterator it = mySet.iterator(); while (it.hasNext()) { enc.encodePrintableString((String)it.next()); }

enc.endOf(id);

Specified by:
encodeSetOf in interface Asn1Encoder
Returns:
a identifier that must be passed to the endOf method to terminate the decoding of the SET OF.
Throws:
IOException

encodeNumericString

public void encodeNumericString(String s)
                         throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 NumericString value.

The only characters that can appear in a NumericString are the digits (0,1,...,9) and the space character(ASCII code 32).

Specified by:
encodeNumericString in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodePrintableString

public void encodePrintableString(String s)
                           throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 PrintableString value.

The authorized characters in a PrintableString are listed below:

 Name              Graphic
 ---------------   -----------
 Capital letters   A, B, ... Z
 Small letters     a, b, ... z
 Digits            0, 1, ... 9
 Space             (space)
 Apostrophe        '
 Left Parenthesis  (
 Right Parenthesis )
 Plus sign         +
 Comma             ,
 Hyphen            -
 Full stop         .
 Solidus           /
 Colon             :
 Equal sign        =
 Question mark     ?
 

Specified by:
encodePrintableString in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeTeletexString

public void encodeTeletexString(String s)
                         throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 TeletexString value.

A TeletexString (or T.61) is a string of multibyte character primarily used for European languages. It uses combining diacritical marks and base characters to represent accented characters, e.g. "a with accute accent" is represented as "accute accent" followed by "a". This is similar to Unicode, except that in Unicode, the accents come after the base character. Also unlike Unicode, the possible combinations of base characters and diacritics is fixed.

A TeletexString must contains characters from the following character sets defined in the ISO 2375:

87, 102, 103, 106, 107 + SPACE + DELETE.

The ITU-T Rec. X.680 has added the following character sets:
6, 126, 144, 150, 153, 156, 164, 165, 168

Specified by:
encodeTeletexString in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeVideotexString

public void encodeVideotexString(String s)
                          throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 VideotexString value.

The ASN.1 VideotexString type is support multibyte characters used by video text system like the french minitel. VideotextString is deprecated and should not be used anymore.

A VideotexString must contains characters from the following character sets defined in the ISO 2375:

1, 13, 72, 73, 87, 89, 102, 108, 126, 128, 129, 144, 150, 153, 164, 165, 168 + SPACE + DELETE.

Specified by:
encodeVideotexString in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeIA5String

public void encodeIA5String(String s)
                     throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 IA5String value.

The characters allowed in an IA5String are:
SPACE + DELETE + all characters defined in the register 6 and all control characters defined in the register 1 (i.e. the the ASCII zone of Unicode).

Specified by:
encodeIA5String in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeUTCTime

public void encodeUTCTime(Calendar date)
                   throws IOException
Description copied from interface: Asn1Encoder
Encodes the specified date as an ASN.1 UTCTime.

Specified by:
encodeUTCTime in interface Asn1Encoder
Parameters:
date - the date to encode.
Throws:
IOException

encodeGeneralizedTime

public void encodeGeneralizedTime(Calendar date)
                           throws IOException
Description copied from interface: Asn1Encoder
Encodes the specified date as an ASN.1 GeneralizedTime.

Specified by:
encodeGeneralizedTime in interface Asn1Encoder
Parameters:
date - the date to encode.
Throws:
IOException

encodeGraphicString

public void encodeGraphicString(String s)
                         throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 GraphicString value.

The characters allowed in a GraphicString are SPACE + all characters defined in all G sets.

Specified by:
encodeGraphicString in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeVisibleString

public void encodeVisibleString(String s)
                         throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 VisibleString value.

The characters allowed in a VisibleString are:
SPACE + all characters defined in the register 6 (i.e. the visible characters in the ASCII zone of Unicode).

Note: The CCITT Rec. X.208 (ancestor of X.680) referenced the register 2. The main difference is that the register 2 defines some diaresis characters that may be combined with BACKSPACE and letter to create accents.

Specified by:
encodeVisibleString in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeGeneralString

public void encodeGeneralString(String s)
                         throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 GeneralString value.

The characters allowed in a GeneralString are SPACE + DELETE + all characters defined in all G sets and all C sets.

Specified by:
encodeGeneralString in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeUniversalString

public void encodeUniversalString(String s)
                           throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 UniversalString value.

A UniversalString comprises the characters defined in the ISO10646-1 standard or in the Unicode Standard. It is a set of fixed-length characters: each character of the UniversalString type encoded on 4 bytes has only one possible interpretation.

Specified by:
encodeUniversalString in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeBMPString

public void encodeBMPString(String s)
                     throws IOException
Description copied from interface: Asn1Encoder
Encodes an ASN.1 BMPString value.

BMPString is a subtype of UniversalString that models the Basic Multilingual Plane (the first 64K-2 cells) of ISO/IEC 10646-1.

Specified by:
encodeBMPString in interface Asn1Encoder
Parameters:
s - the string value to encode.
Throws:
IOException

encodeImplicit

public void encodeImplicit(int tag)
                    throws IOException
Starts the encoding of an implicitely tagged ASN.1 value.

The next encoded value will use the specified implicit tag. Once a value has been implicitely tagged, the tag is reset. All preceding calls of this method are ignored.

Specified by:
encodeImplicit in interface Asn1Encoder
Parameters:
tag - the implicit tag used to encode the next ASN.1 value.
Throws:
IOException

encodeExplicit

public abstract int encodeExplicit(int tag)
                            throws IOException
Description copied from interface: Asn1Encoder
Indicates that the next ASN.1 value must be encoded with an explicit tag equals to the specified tag value.

Explicit tags create a separate (tag, length, value) triplet where the tagged element is encoded as value.
This method must be called before the ASN.1 value is encoded; then the value itself is handled as usual. Finally, the endOf method must be invoked to terminate the explicitly encoding.

The encoding of the following ASN.1 type

 version [0] EXPLICIT Version
 
looks like:
 int id = enc.encodeExplicit(Tags.makeTag(Tags.CONTEXT, 0));
    enc.encodeInteger(version);
 dec.endOf(id);
 

Specified by:
encodeExplicit in interface Asn1Encoder
Parameters:
tag - the explicit tag of the next encoded ASN.1 value. The tag must be a valid tag such as one that has been created with the Tags.createTag(int, int) method.
Returns:
a sequence identifier that must be passed to the endOf method to terminate the encoding of the explicit tagging.
Throws:
IOException
See Also:
Asn1Encoder.endOf(int), Tags.createTag(int, int)

endOf

public abstract 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
Parameters:
id - the reference to a constructed value.
Throws:
IOException

setName

public void setName(String name)
             throws IOException
Throws:
IOException

isDefaultEncoded

public boolean isDefaultEncoded()
Description copied from interface: Asn1Encoder
Indicates wether this encoder encodes DEFAULT values.

DEFAULT values are optional values in

Specified by:
isDefaultEncoded in interface Asn1Encoder
Returns:
true if this encoder encodes DEFAULT values.

encodeTag

protected int encodeTag(int tag,
                        boolean constructed)
                 throws IOException
Throws:
IOException

encodeLength

protected void encodeLength(int len)
                     throws IOException
Encodes the length part of a BER TLV.

Throws:
IOException

writeByte

protected abstract void writeByte(int b)
                           throws IOException
Throws:
IOException

writeBytes

protected abstract void writeBytes(byte[] b,
                                   int offs,
                                   int len)
                            throws IOException
Throws:
IOException

writeBytes

protected void writeBytes(byte[] b)
                   throws IOException
Throws:
IOException

getCharBuffer

protected char[] getCharBuffer(int len)
Returns an array of characters whose size is sufficient to hold len characters.

Parameters:
len - the minimum length of the array to return.
Returns:
an

getByteBuffer

protected byte[] getByteBuffer(int len)


Copyright © 2004 Nicolas Vraux. All Rights Reserved.