Skip to content

Commit b093c24

Browse files
committed
Add ASN1RelativeOID cache
1 parent 1c34996 commit b093c24

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

core/src/main/java/org/bouncycastle/asn1/ASN1InputStream.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,8 @@ static ASN1Primitive createPrimitiveDERObject(
553553
case PRINTABLE_STRING:
554554
return ASN1PrintableString.createPrimitive(defIn.toByteArray());
555555
case RELATIVE_OID:
556-
return ASN1RelativeOID.createPrimitive(defIn.toByteArray(), false);
556+
// TODO Ideally only clone if we used a buffer
557+
return ASN1RelativeOID.createPrimitive(getBuffer(defIn, tmpBuffers), true);
557558
case T61_STRING:
558559
return ASN1T61String.createPrimitive(defIn.toByteArray());
559560
case UNIVERSAL_STRING:

core/src/main/java/org/bouncycastle/asn1/ASN1ObjectIdentifier.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public ASN1ObjectIdentifier intern()
421421
return oid;
422422
}
423423

424-
private static class OidHandle
424+
static class OidHandle
425425
{
426426
private final int key;
427427
private final byte[] contents;
@@ -452,10 +452,10 @@ static ASN1ObjectIdentifier createPrimitive(byte[] contents, boolean clone)
452452
{
453453
final OidHandle hdl = new OidHandle(contents);
454454
ASN1ObjectIdentifier oid = pool.get(hdl);
455-
if (oid == null)
455+
if (oid != null)
456456
{
457-
return new ASN1ObjectIdentifier(contents, clone);
457+
return oid;
458458
}
459-
return oid;
459+
return new ASN1ObjectIdentifier(contents, clone);
460460
}
461461
}

core/src/main/java/org/bouncycastle/asn1/ASN1RelativeOID.java

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
55
import java.math.BigInteger;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
import java.util.concurrent.ConcurrentMap;
68

79
import org.bouncycastle.util.Arrays;
810

@@ -78,6 +80,9 @@ public static ASN1RelativeOID tryFromID(String identifier)
7880

7981
private static final long LONG_LIMIT = (Long.MAX_VALUE >> 7) - 0x7F;
8082

83+
private static final ConcurrentMap<ASN1ObjectIdentifier.OidHandle, ASN1RelativeOID> pool =
84+
new ConcurrentHashMap<ASN1ObjectIdentifier.OidHandle, ASN1RelativeOID>();
85+
8186
private final byte[] contents;
8287
private String identifier;
8388

@@ -180,6 +185,12 @@ boolean encodeConstructed()
180185

181186
static ASN1RelativeOID createPrimitive(byte[] contents, boolean clone)
182187
{
188+
final ASN1ObjectIdentifier.OidHandle hdl = new ASN1ObjectIdentifier.OidHandle(contents);
189+
ASN1RelativeOID oid = pool.get(hdl);
190+
if (oid != null)
191+
{
192+
return oid;
193+
}
183194
return new ASN1RelativeOID(contents, clone);
184195
}
185196

0 commit comments

Comments
 (0)