codec.asn1
Class RunLengthEncoder

java.lang.Object
  |
  +--codec.asn1.RunLengthEncoder
All Implemented Interfaces:
Encoder

public class RunLengthEncoder
extends Object
implements Encoder

This encoder makes one pass through the given ASN.1 type and computes the length of the type encoding according to the DER (ITU-T Recommendation X.690). The result is an array of integers with the length of the individual non-optional and non-implicit type encodings in the reverse order of the order in which the given type is traversed during actual encoding. This array is used by the DEREncoder when encoding a type.

Version:
"$Id: RunLengthEncoder.java,v 1.2 2000/12/06 17:47:29 vroth Exp $"
Author:
Volker Roth

Field Summary
static int INCREMENT
          The number of slots by which the internal buffer is incremented if its capacity is eexceeded.
 
Constructor Summary
RunLengthEncoder()
          Creates an encoder.
 
Method Summary
protected  int accumulate(int pos)
          Accumulates all values on the accumulator stack from the given position to the top of the stack and returns the result.
 int getHeaderLength(int tag, int len)
          This method computes the number of octets needed to encode the identifier and length octets of the ASN.1 type with the given tag and contents length.
 int[] getLengthFields()
          This method brings in the harvest of the encoding procedure.
protected  void push(ASN1Type t, int n)
          Pushes another length integer onto the internal stacks.
protected  void reset()
          Clears the length array and prepares this encoder for a new run.
protected  int significantBits(int n)
          Counts the number of significant bits in the given integer.
protected  int sp()
          Returns the accumulator stack pointer.
 void writeBitString(ASN1BitString t)
           
 void writeBoolean(ASN1Boolean t)
           
 void writeCollection(ASN1Collection t)
           
 void writeCollectionOf(ASN1Collection t)
           
 void writeInteger(ASN1Integer t)
           
 void writeNull(ASN1Null t)
           
 void writeObjectIdentifier(ASN1ObjectIdentifier t)
           
 void writeOctetString(ASN1OctetString t)
           
 void writeReal(ASN1Type t)
           
 void writeString(ASN1String t)
           
 void writeTaggedType(ASN1TaggedType t)
           
 void writeTime(ASN1Time t)
           
 void writeType(ASN1Type o)
          Encodes the length array of the given type.
 void writeTypeIdentifier(ASN1Type t)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INCREMENT

public static final int INCREMENT
The number of slots by which the internal buffer is incremented if its capacity is eexceeded.
Constructor Detail

RunLengthEncoder

public RunLengthEncoder()
Creates an encoder.
Method Detail

getLengthFields

public int[] getLengthFields()
This method brings in the harvest of the encoding procedure. It returns the individual lengths of the DER encodings of the types written to to this encoder. The order of length fields is the reverse order of the pre order parsing of the written types.

If this method is called before a type has been encoded then an array of zero length is returned. Only non-optional types are counted thus the zero length array might be returned also when all encoded types were declared optional.

Returns:
The lengths fields.

writeType

public void writeType(ASN1Type o)
               throws ASN1Exception
Encodes the length array of the given type.
Specified by:
writeType in interface Encoder

getHeaderLength

public int getHeaderLength(int tag,
                           int len)
                    throws ASN1Exception
This method computes the number of octets needed to encode the identifier and length octets of the ASN.1 type with the given tag and contents length. The length must not be negative else an exception is thrown. Since this encoder is meant to work in conjunction with a DEREncoder no indefinite length is supported.
Parameters:
tag - The ASN.1 tag.
len - The number of contents octets of the ASN.1 type with the given tag and length.
Returns:
The number of octets required for encoding the identifier and length octets.
Throws:
ASN1Exception - if the given length is negative.

significantBits

protected int significantBits(int n)
Counts the number of significant bits in the given integer. There is always at least one significant bit.
Parameters:
n - The integer.
Returns:
The number of significant bits in the given integer.

writeBoolean

public void writeBoolean(ASN1Boolean t)
                  throws ASN1Exception
Specified by:
writeBoolean in interface Encoder

writeInteger

public void writeInteger(ASN1Integer t)
                  throws ASN1Exception
Specified by:
writeInteger in interface Encoder

writeBitString

public void writeBitString(ASN1BitString t)
                    throws ASN1Exception
Specified by:
writeBitString in interface Encoder

writeOctetString

public void writeOctetString(ASN1OctetString t)
                      throws ASN1Exception
Specified by:
writeOctetString in interface Encoder

writeNull

public void writeNull(ASN1Null t)
               throws ASN1Exception
Specified by:
writeNull in interface Encoder

writeObjectIdentifier

public void writeObjectIdentifier(ASN1ObjectIdentifier t)
                           throws ASN1Exception
Specified by:
writeObjectIdentifier in interface Encoder

writeReal

public void writeReal(ASN1Type t)
Specified by:
writeReal in interface Encoder

writeString

public void writeString(ASN1String t)
                 throws ASN1Exception
Specified by:
writeString in interface Encoder

writeCollection

public void writeCollection(ASN1Collection t)
                     throws ASN1Exception
Specified by:
writeCollection in interface Encoder

writeCollectionOf

public void writeCollectionOf(ASN1Collection t)
                       throws ASN1Exception

writeTime

public void writeTime(ASN1Time t)
               throws ASN1Exception
Specified by:
writeTime in interface Encoder

writeTaggedType

public void writeTaggedType(ASN1TaggedType t)
                     throws ASN1Exception
Specified by:
writeTaggedType in interface Encoder

writeTypeIdentifier

public void writeTypeIdentifier(ASN1Type t)
Specified by:
writeTypeIdentifier in interface Encoder

reset

protected void reset()
Clears the length array and prepares this encoder for a new run.

push

protected void push(ASN1Type t,
                    int n)
             throws ASN1Exception
Pushes another length integer onto the internal stacks. The value is pushed both on the running stack as well as on the accumulator stack. The stacks increase dynamically in size in chunks of INCREMENT integers and never shrink in capacity.
Parameters:
t - The ASN.1 type.
n - The integer.

sp

protected int sp()
Returns the accumulator stack pointer.
Returns:
The accumulator stack pointer.

accumulate

protected int accumulate(int pos)
Accumulates all values on the accumulator stack from the given position to the top of the stack and returns the result. All accumulated values are popped off the stack.
Parameters:
pos - The position to start from.
Throws:
IllegalStateException - if the given position is atop the top of the stack.


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.