Skip to content

Commit 12bf4c2

Browse files
committed
Add validatilon methods to Arrays
1 parent c808b0a commit 12bf4c2

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

core/src/main/java/org/bouncycastle/util/Arrays.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,4 +1240,30 @@ public static boolean isNullOrEmpty(Object[] array)
12401240
{
12411241
return null == array || array.length < 1;
12421242
}
1243+
1244+
public static void validateRange(byte[] buf, int from, int to)
1245+
{
1246+
if (buf == null)
1247+
{
1248+
throw new NullPointerException("'buf' cannot be null");
1249+
}
1250+
if ((from | (buf.length - from) | (to - from) | (buf.length - to)) < 0)
1251+
{
1252+
throw new IndexOutOfBoundsException("buf.length: " + buf.length + ", from: " + from + ", to: " + to);
1253+
}
1254+
}
1255+
1256+
public static void validateSegment(byte[] buf, int off, int len)
1257+
{
1258+
if (buf == null)
1259+
{
1260+
throw new NullPointerException("'buf' cannot be null");
1261+
}
1262+
int available = buf.length - off;
1263+
int remaining = available - len;
1264+
if ((off | len | available | remaining) < 0)
1265+
{
1266+
throw new IndexOutOfBoundsException("buf.length: " + buf.length + ", off: " + off + ", len: " + len);
1267+
}
1268+
}
12431269
}

core/src/main/java/org/bouncycastle/util/io/Streams.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.InputStream;
66
import java.io.OutputStream;
77

8+
import org.bouncycastle.util.Arrays;
9+
810
/**
911
* Utility methods to assist with stream processing.
1012
*/
@@ -160,16 +162,7 @@ public static int readFully(InputStream inStr, byte[] buf, int off, int len)
160162

161163
public static void validateBufferArguments(byte[] buf, int off, int len)
162164
{
163-
if (buf == null)
164-
{
165-
throw new NullPointerException();
166-
}
167-
int available = buf.length - off;
168-
int remaining = available - len;
169-
if ((off | len | available | remaining) < 0)
170-
{
171-
throw new IndexOutOfBoundsException();
172-
}
165+
Arrays.validateSegment(buf, off, len);
173166
}
174167

175168
public static void writeBufTo(ByteArrayOutputStream buf, OutputStream output)

0 commit comments

Comments
 (0)