Code example for ByteArrayOutputStream

Methods: toByteArray, write

0
     * 
     *  @since 0.8.7 
     */ 
    private static byte[] sigBytesToASN1(byte[] sig) {
        //System.out.println("pre TO asn1\n" + net.i2p.util.HexDump.dump(sig)); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(48);
        baos.write(0x30);
        baos.write(0);  // length to be filled in below
 
        byte[] tmp = new byte[20];
        baos.write(2);
        System.arraycopy(sig, 0, tmp, 0, 20);
        BigInteger r = new BigInteger(1, tmp);
        byte[] b = r.toByteArray();
        baos.write(b.length);
        baos.write(b, 0, b.length);
 
        baos.write(2);
        System.arraycopy(sig, 20, tmp, 0, 20);
        BigInteger s = new BigInteger(1, tmp);
        b = s.toByteArray();
        baos.write(b.length);
        baos.write(b, 0, b.length);
        byte[] rv = baos.toByteArray();
        rv[1] = (byte) (rv.length - 2);
        //System.out.println("post TO asn1\n" + net.i2p.util.HexDump.dump(rv)); 
        return rv;
    }