package org.bouncycastle.asn1;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;
public abstract class ASN1OctetString
extends ASN1Object
implements ASN1OctetStringParser
{
byte[] string;
public static ASN1OctetString getInstance(
ASN1TaggedObject obj,
boolean explicit)
{
DERObject o = obj.getObject();
if (explicit || o instanceof ASN1OctetString)
{
return getInstance(o);
}
else
{
return BERConstructedOctetString.fromSequence(ASN1Sequence.getInstance(o));
}
}
public static ASN1OctetString getInstance(
Object obj)
{
if (obj == null || obj instanceof ASN1OctetString)
{
return (ASN1OctetString)obj;
}
if (obj instanceof ASN1TaggedObject)
{
return getInstance(((ASN1TaggedObject)obj).getObject());
}
throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
}
public ASN1OctetString(
byte[] string)
{
if (string == null)
{
throw new NullPointerException("string cannot be null");
}
this.string = string;
}
public ASN1OctetString(
DEREncodable obj)
{
try
{
this.string = obj.getDERObject().getEncoded(ASN1Encodable.DER);
}
catch (IOException e)
{
throw new IllegalArgumentException("Error processing object : " + e.toString());
}
}
public InputStream getOctetStream()
{
return new ByteArrayInputStream(string);
}
public ASN1OctetStringParser parser()
{
return this;
}
public byte[] getOctets()
{
return string;
}
public int hashCode()
{
return Arrays.hashCode(this.getOctets());
}
boolean asn1Equals(
DERObject o)
{
if (!(o instanceof ASN1OctetString))
{
return false;
}
ASN1OctetString other = (ASN1OctetString)o;
return Arrays.areEqual(string, other.string);
}
public DERObject getLoadedObject()
{
return this.getDERObject();
}
abstract void encode(DEROutputStream out)
throws IOException;
public String toString()
{
return "#"+new String(Hex.encode(string));
}
}