www.pudn.com > cryptix-asn1-0.1.11.zip > BitString.java


/* $Id: BitString.java,v 1.4 2001/05/26 07:10:24 raif Exp $ 
 * 
 * Copyright (C) 1997-2001 The Cryptix Foundation Limited. All rights reserved. 
 * 
 * Use, modification, copying and distribution of this software is subject to 
 * the terms and conditions of the Cryptix General Licence. You should have 
 * received a copy of the Cryptix General Licence along with this library; if 
 * not, you can download a copy from http://www.cryptix.org/ 
 */ 
package cryptix.asn1.lang; 
 
import cryptix.asn1.io.ASNReader; 
import cryptix.asn1.io.ASNWriter; 
import cryptix.asn1.io.BlankElementException; 
import cryptix.asn1.io.ElementNotFoundException; 
 
import org.apache.log4j.Category; 
 
import java.io.EOFException; 
import java.io.IOException; 
import java.math.BigInteger; 
 
/** 
 * The basic implementation of the ASN.1 BIT STRING type.

* * @version $Revision: 1.4 $ * @author Raif S. Naffah */ public class BitString extends Type implements IType { // Constants and vars // ....................................................................... static Category cat = Category.getInstance(BitString.class.getName()); // Constructor(s) //........................................................................ public BitString() { super("", new Tag(Tag.BIT_STRING)); } public BitString(String name) { super(name, new Tag(Tag.BIT_STRING)); } public BitString(String name, Tag tag) { super(name, tag); } public BitString(String name, Object value) { this(name, new Tag(Tag.BIT_STRING), value); } /** * Constructs a new instance of a BIT_STRING type, given a designated Tag * and a designated initial value. * * @param name the name of this instance. * @param tag the designated Tag value. * @param value the designated initial value. * @exception ClassCastException if the designated value is not a byte array. */ public BitString(String name, Tag tag, Object value) { super(name, tag); if (value != null) this.value = (byte[]) ((byte[]) value).clone(); if (this.value != null) defaultValue(this.value); } // Class methods // ....................................................................... /** * Returns a new instance of this type with a trivial name and the * designated value. * * @param value a designated initial value for the new instance. * @return a new instance with the designated value. */ public static BitString getInstance(byte[] value) { return new BitString("", value); } // Redefinition of methods in superclass Type // ....................................................................... /** * Decodes a BIT_STRING from an input stream. * * @param is the ASN.1 stream to read from. * @exception IOException if an exception occurs during the operation. */ public void decode(ASNReader is) throws IOException { String cn = this.getClass().getName(); cat.debug("==> "+cn+".decode()"); is.mark(Integer.MAX_VALUE); try { byte[] result = is.decodeBitString(this); if (result == null) throw new ElementNotFoundException(cn); else this.value(result); } catch (IOException x) { cat.warn("Exception ("+String.valueOf(x)+") encountered while decoding a "+cn); if (x instanceof ASNException || x instanceof EOFException) { cat.warn("Resetting stream..."); is.reset(); } throw x; } cat.debug("<== "+cn+".decode()"); } /** * Encodes a BIT_STRING to an output stream. * * @param os the ASN.1 stream to write to. * @exception IOException if an exception occurs during the operation. */ public void encode(ASNWriter os) throws IOException { String cn = this.getClass().getName(); cat.debug("==> "+cn+".encode()"); Object val = this.value(); if (val != null) os.encodeBitString(this, (byte[]) val); else { val = this.defaultValue(); if (val != null) { cat.info("Encoding default value for "+cn); os.encodeBitString(this, (byte[]) val); } else if (!this.isOptional()) throw new BlankElementException(cn); // else { // cat.warn("Encoding a NULL for "+cn); // new Null(this.name()).encode(os); // } } cat.debug("<== "+cn+".encode()"); } // other instance methods // ....................................................................... /** * Convenience method to facilitate type casting. * * @return the current value as a byte array. */ public byte[] byteArrayValue() { return (byte[]) this.value; } /** * Returns true if the designated value is equal to the value of * this instance.

* * @param obj a value to compare with the value of this instance. * @return true if this instance has an equal, non-null value to * the designated one; false otherwise. */ protected boolean sameValue(Object obj) { if (obj == null) return false; byte[] other; try { other = (byte[]) obj; } catch (ClassCastException x) { return false; } byte[] val = this.byteArrayValue(); if (val == null) return false; if (val.length != other.length) return false; for (int i = 0; i < val.length; i++) if (val[i] != other[i]) return false; return true; } // java.lang.Object overloaded methods // ....................................................................... /** * Returns a string representation of this instance in the binary system. * * @return a string representation of this instance. */ public String toString() { StringBuffer sb = new StringBuffer("-- "); if (value != null) sb.append(new BigInteger(1, byteArrayValue()).toString(2)); else sb.append("N/A"); return sb.toString(); } }