public byte[] encode() {    if( cachedEncoding != null ) { return cachedEncoding; }  ByteArrayOutputStream cache = new ByteArrayOutputStream();      byte idOctet = 0; idOctet |= tag.getTagClass().toInt() << 6;  if( form == CONSTRUCTED ) { idOctet |= 0x20; }  if( tag.getNum() <= 30 ) {  idOctet |= (tag.getNum() & 0x1f );  cache.write( idOctet ); }  else {  idOctet |= 0x1f; BigInteger tagNum = BigInteger.valueOf(tag.getNum());  cache.write( idOctet );  int bitlength = tagNum.bitLength(); int reps = (bitlength+6)/7;  for( reps = reps-1; reps > 0 ; reps--) { long shifted = tag.getNum() >>> ( 7*reps ); cache.write( (((byte)shifted) & 0x7f) | 0x80 ); }  cache.write( ((byte)tag.getNum()) & 0x7f ); }     if( contentLength == -1 ) {  cache.write( (byte) 0x80 ); } else if( contentLength <= 127 ) {  cache.write( (byte) contentLength ); } else {  byte[] val = unsignedBigIntToByteArray( BigInteger.valueOf(contentLength) ); cache.write( ((byte)val.length) | 0x80 ); cache.write( val, 0, val.length ); }  cachedEncoding = cache.toByteArray();  return cachedEncoding;
public void encode( OutputStream ostream ) throws IOException { ostream.write( encode() );
    public byte[] encode() {        // It's important that we not recompute the encoding if it was        // set by ASN1Header(InputStream), since in that case it represents        // the encoding that was actually read from the InputStream.        if( cachedEncoding != null ) {            return cachedEncoding;        }        ByteArrayOutputStream cache = new ByteArrayOutputStream();                //        // Identifier octet(s)        //        byte idOctet = 0;        idOctet |= tag.getTagClass().toInt() << 6;        if( form == CONSTRUCTED ) {            idOctet |= 0x20;        }        if( tag.getNum() <= 30 ) {            // short form            idOctet |= (tag.getNum() & 0x1f );            cache.write( idOctet );        }  else {            // long form            idOctet |= 0x1f;            BigInteger tagNum = BigInteger.valueOf(tag.getNum());            cache.write( idOctet );            int bitlength = tagNum.bitLength();            int reps = (bitlength+6)/7;            for( reps = reps-1; reps > 0 ; reps--) {                long shifted = tag.getNum() >>> ( 7*reps );                cache.write( (((byte)shifted) & 0x7f) | 0x80 );            }            cache.write( ((byte)tag.getNum()) & 0x7f );        }        //        // Length Octets        //        if( contentLength == -1 ) {            // indefinite form            cache.write( (byte) 0x80 );        } else if( contentLength <= 127 ) {            // short form            cache.write( (byte) contentLength );        } else {            // long form            byte[] val = unsignedBigIntToByteArray(                            BigInteger.valueOf(contentLength) );            cache.write( ((byte)val.length) | 0x80 );            cache.write( val, 0, val.length );        }        cachedEncoding = cache.toByteArray();        return cachedEncoding;    }