Skip to content

Commit 1bda45c

Browse files
committed
Port OctetString utility methods from bc-csharp
1 parent 66af7df commit 1bda45c

2 files changed

Lines changed: 91 additions & 3 deletions

File tree

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.IOException;
44

5+
import org.bouncycastle.util.Arrays;
6+
57
/**
68
* ASN.1 OctetStrings, with indefinite length rules, and <i>constructed form</i> support.
79
* <p>
@@ -19,10 +21,37 @@
1921
public class BEROctetString
2022
extends ASN1OctetString
2123
{
22-
private static final int DEFAULT_SEGMENT_LIMIT = 1000;
24+
public static final BEROctetString EMPTY = new BEROctetString(EMPTY_OCTETS);
2325

24-
private final int segmentLimit;
25-
private final ASN1OctetString[] elements;
26+
public static BEROctetString fromContents(byte[] contents)
27+
{
28+
if (contents == null)
29+
{
30+
throw new NullPointerException("'contents' cannot be null");
31+
}
32+
33+
return internalFromContents(contents);
34+
}
35+
36+
public static BEROctetString fromContentsOptional(byte[] contents)
37+
{
38+
return contents == null ? null : internalFromContents(contents);
39+
}
40+
41+
public static BEROctetString withContents(byte[] contents)
42+
{
43+
if (contents == null)
44+
{
45+
throw new NullPointerException("'contents' cannot be null");
46+
}
47+
48+
return internalWithContents(contents);
49+
}
50+
51+
public static BEROctetString withContentsOptional(byte[] contents)
52+
{
53+
return contents == null ? null : internalWithContents(contents);
54+
}
2655

2756
/**
2857
* Convert a vector of octet strings into a single byte string
@@ -58,6 +87,21 @@ static byte[] flattenOctetStrings(ASN1OctetString[] octetStrings)
5887
}
5988
}
6089

90+
static BEROctetString internalFromContents(byte[] contents)
91+
{
92+
return contents.length < 1 ? EMPTY : new BEROctetString(Arrays.clone(contents));
93+
}
94+
95+
static BEROctetString internalWithContents(byte[] contents)
96+
{
97+
return contents.length < 1 ? EMPTY : new BEROctetString(contents);
98+
}
99+
100+
private static final int DEFAULT_SEGMENT_LIMIT = 1000;
101+
102+
private final int segmentLimit;
103+
private final ASN1OctetString[] elements;
104+
61105
/**
62106
* Create an OCTET-STRING object from a byte[]
63107
* @param string the octets making up the octet string.

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,56 @@
22

33
import java.io.IOException;
44

5+
import org.bouncycastle.util.Arrays;
6+
57
/**
68
* Carrier class for a DER encoding OCTET STRING
79
*/
810
public class DEROctetString
911
extends ASN1OctetString
1012
{
13+
public static final DEROctetString EMPTY = new DEROctetString(EMPTY_OCTETS);
14+
15+
public static DEROctetString fromContents(byte[] contents)
16+
{
17+
if (contents == null)
18+
{
19+
throw new NullPointerException("'contents' cannot be null");
20+
}
21+
22+
return internalFromContents(contents);
23+
}
24+
25+
public static DEROctetString fromContentsOptional(byte[] contents)
26+
{
27+
return contents == null ? null : internalFromContents(contents);
28+
}
29+
30+
public static DEROctetString withContents(byte[] contents)
31+
{
32+
if (contents == null)
33+
{
34+
throw new NullPointerException("'contents' cannot be null");
35+
}
36+
37+
return internalWithContents(contents);
38+
}
39+
40+
public static DEROctetString withContentsOptional(byte[] contents)
41+
{
42+
return contents == null ? null : internalWithContents(contents);
43+
}
44+
45+
static DEROctetString internalFromContents(byte[] contents)
46+
{
47+
return contents.length < 1 ? EMPTY : new DEROctetString(Arrays.clone(contents));
48+
}
49+
50+
static DEROctetString internalWithContents(byte[] contents)
51+
{
52+
return contents.length < 1 ? EMPTY : new DEROctetString(contents);
53+
}
54+
1155
/**
1256
* Base constructor.
1357
*

0 commit comments

Comments
 (0)