codec.asn1
Class DEREncoder

java.lang.Object
  |
  +--java.io.OutputStream
        |
        +--java.io.FilterOutputStream
              |
              +--codec.asn1.AbstractEncoder
                    |
                    +--codec.asn1.DEREncoder
All Implemented Interfaces:
Encoder

public class DEREncoder
extends AbstractEncoder

If ,right after instantiating the encoder, the strict parameter is set to true, the encoder uses the strict encoding rules from X680/X690 for Sets and SetOfs. As long as the flag is not set, the encoder behaves as usual.

Version:
"$Id: DEREncoder.java,v 1.7 2004/08/09 11:10:08 flautens Exp $"
Author:
Volker Roth

Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
DEREncoder(OutputStream out)
          Creates an encoder that writes its output to the given output stream.
 
Method Summary
protected  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.
 void help(byte[] b)
           
 boolean isStrict()
          returns the value of the strict parameter.
 void setStrict(boolean _strictness)
          Sets the value of the strict parameter.
protected  int significantBits(int n)
          Counts the number of significant bits in the given integer.
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
protected  void writeBase128(int n)
          Writes the given integer to the output in base 128 representation with bit 7 of all octets except the last one being set to "1".
protected  void writeBase256(int n)
          Writes the given integer to the output in base 256 with the minimal number of octets.
 void writeBitString(ASN1BitString t)
          Encodes a bitstring.
 void writeBoolean(ASN1Boolean t)
           
 void writeCollection(ASN1Collection t)
           
protected  void writeHeader(ASN1Type t, boolean primitive)
          Encodes the identifier and length octets.
protected  void writeHeader(int tag, int cls, boolean prim, int len)
          This method encodes identifier and length octets.
 void writeInteger(ASN1Integer t)
           
 void writeNull(ASN1Null t)
           
 void writeObjectIdentifier(ASN1ObjectIdentifier t)
           
 void writeOctetString(ASN1OctetString t)
           
 void writeReal(ASN1Type t)
           
protected  void writeStrictSetOf(ASN1SetOf t)
          This method fixes the DER SET OF strict encoding issue brought up by pea-counter implementations of decoders that insist on sorting the SET OF components by encoding -- something no sane implementation requires.
 void writeString(ASN1String t)
           
 void writeTaggedType(ASN1TaggedType t)
           
 void writeTime(ASN1Time t)
           
 void writeType(ASN1Type t)
          Writes an arbitrary ASN1Type.
 void writeTypeIdentifier(ASN1Type t)
           
 
Methods inherited from class java.io.FilterOutputStream
close, flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DEREncoder

public DEREncoder(OutputStream out)
Creates an encoder that writes its output to the given output stream.
Parameters:
out - The output stream to which the encoded ASN.1 objects are written.
Method Detail

isStrict

public boolean isStrict()
returns the value of the strict parameter.

setStrict

public void setStrict(boolean _strictness)
Sets the value of the strict parameter.

writeHeader

protected void writeHeader(ASN1Type t,
                           boolean primitive)
                    throws ASN1Exception,
                           IOException
Encodes the identifier and length octets. If there are no known lengths then this method creates and runs a RunLengthEncoder on the given type in order to establish the length of it and of any contained types. This method must not be called with OPTIONAL types else errors may occur. It is the responsibility of the caller to ascertain this precondition. Only the headers of types that are tagged EXPLICIT are encoded. If the given type is tagged IMPLICIT then this method simply returns.
Parameters:
t - The type of which the header is encoded.
primitive - true if the encoding is PRIMITIVE and false if it is CONSTRUCTED.

writeBoolean

public void writeBoolean(ASN1Boolean t)
                  throws ASN1Exception,
                         IOException

writeInteger

public void writeInteger(ASN1Integer t)
                  throws ASN1Exception,
                         IOException

help

public void help(byte[] b)

writeBitString

public void writeBitString(ASN1BitString t)
                    throws ASN1Exception,
                           IOException
Encodes a bitstring. This method expects that the bitstring instance is already compact. In other words, it does not contain trailing zeroes and the pad count is in the range of zero to seven.

writeOctetString

public void writeOctetString(ASN1OctetString t)
                      throws ASN1Exception,
                             IOException

writeNull

public void writeNull(ASN1Null t)
               throws ASN1Exception,
                      IOException

writeObjectIdentifier

public void writeObjectIdentifier(ASN1ObjectIdentifier t)
                           throws ASN1Exception,
                                  IOException

writeReal

public void writeReal(ASN1Type t)

writeString

public void writeString(ASN1String t)
                 throws ASN1Exception,
                        IOException

writeCollection

public void writeCollection(ASN1Collection t)
                     throws ASN1Exception,
                            IOException

writeStrictSetOf

protected void writeStrictSetOf(ASN1SetOf t)
                         throws ASN1Exception,
                                IOException
This method fixes the DER SET OF strict encoding issue brought up by pea-counter implementations of decoders that insist on sorting the SET OF components by encoding -- something no sane implementation requires. The method of implementation is basically a hack. Don't take this as a good programming pattern. Moreover, this implementation becomes completely and utterly THREAD UNSAFE. Not that this would mater too much.

writeTime

public void writeTime(ASN1Time t)
               throws ASN1Exception,
                      IOException

writeTaggedType

public void writeTaggedType(ASN1TaggedType t)
                     throws ASN1Exception,
                            IOException

writeTypeIdentifier

public void writeTypeIdentifier(ASN1Type t)

write

public void write(byte[] b)
           throws IOException
Overrides:
write in class FilterOutputStream

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class FilterOutputStream

writeType

public void writeType(ASN1Type t)
               throws ASN1Exception,
                      IOException
Writes an arbitrary ASN1Type. The given type is written only if it is not declared OPTIONAL. The type is written by calling its encode method with this as the argument. The called emthod then should invoke the appropriate encoder method of the primitive type to which the given type corresponds.
Overrides:
writeType in class AbstractEncoder
Parameters:
t - The type to write.
Throws:
ASN1Exception - if the given type cannot be encoded.
IOException - if an I/O error occurs.

writeHeader

protected void writeHeader(int tag,
                           int cls,
                           boolean prim,
                           int len)
                    throws IOException
This method encodes identifier and length octets. The given length can be negative in which case 0x80 is written to indicate INDEFINITE LENGTH encoding. Please note that this is appropriate only for a BER encoding or CER encoding (ITU-T Recommenation X.690). Encoders are responsible for writing the end of code octets 0x00 0x00 after encoding the content octets.
Overrides:
writeHeader in class AbstractEncoder
Parameters:
tag - The ASN.1 tag
cls - The ASN.1 tag class.
prim - true if the encoding is PRIMITIVE and false if it is CONSTRUCTED.
len - The number of content octets or -1 to indicate INDEFINITE LENGTH encoding.

getHeaderLength

protected 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. The given length can be negative in which case INDEFINITE LENGTH encoding is assumed.
Overrides:
getHeaderLength in class AbstractEncoder
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.

writeBase128

protected void writeBase128(int n)
                     throws IOException
Writes the given integer to the output in base 128 representation with bit 7 of all octets except the last one being set to "1". The minimum number of octets necessary is used.
Overrides:
writeBase128 in class AbstractEncoder
Parameters:
n - The integer to be written to the output.
Throws:
IOException - Thrown by the underlying output stream.

writeBase256

protected void writeBase256(int n)
                     throws IOException
Writes the given integer to the output in base 256 with the minimal number of octets.
Overrides:
writeBase256 in class AbstractEncoder
Parameters:
n - The integer to be written to the output.
Throws:
IOException - Thrown by the underlying output stream.

significantBits

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


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.