Skip to content

Commit dc5a764

Browse files
committed
StreamUtils: support for arrays of BigIntegers
1 parent 0cea27b commit dc5a764

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ A Java library containing math, stream, ... util classes that I used to work wit
33
<br>This project mainly exists to make my life easier (so that I don't have to copy-paste code), but feel free to use it if you want.
44

55
[![Project Status: Active](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
6-
[![Coverage](https://badgen.net/badge/coverage/96%25/green)](https://badgen.net/badge/coverage/96%25/green)
6+
[![Coverage](https://badgen.net/badge/coverage/97%25/green)](https://badgen.net/badge/coverage/97%25/green)
77

88
***
99

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = 'com.github.pvriel'
7-
version = '1.3.0'
7+
version = '1.3.1'
88

99
repositories {
1010
mavenLocal()

src/main/java/com/github/pvriel/mpcutils4j/stream/StreamUtils.java

+37
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,42 @@ public static BigInteger readBigIntegerFromInputStream(InputStream inputStream)
4040
return new BigInteger(receiveInputWithLengthPrefix(inputStream));
4141
}
4242

43+
/**
44+
* Method to write a {@link BigInteger} array to an {@link OutputStream}.
45+
* @param bigIntegers
46+
* The {@link BigInteger} array to write. No null values are allowed.
47+
* @param outputStream
48+
* The {@link OutputStream} to write the instance to. No null values are allowed.
49+
* @throws IOException
50+
* If the {@link OutputStream} threw an exception while writing the data.
51+
*/
52+
public static void writeArrayOfBigIntegersToOutputStream(BigInteger[] bigIntegers, OutputStream outputStream) throws IOException {
53+
outputStream.write(ByteBuffer.allocate(4).putInt(bigIntegers.length).array());
54+
for (BigInteger bigInteger : bigIntegers) {
55+
writeBigIntegerToOutputStream(bigInteger, outputStream);
56+
}
57+
}
58+
59+
/**
60+
* Method to read a {@link BigInteger} array from an {@link OutputStream}.
61+
* @param inputStream
62+
* The {@link InputStream} to read the instance from. No null values are allowed.
63+
* @return The resulting {@link BigInteger} array.
64+
* @throws IOException
65+
* If the {@link OutputStream} threw an exception while reading the data.
66+
*/
67+
public static BigInteger[] readArrayOfBigIntegersFromInputStream(InputStream inputStream) throws IOException {
68+
ByteBuffer byteBuffer = ByteBuffer.wrap(inputStream.readNBytes(4));
69+
int length = byteBuffer.getInt();
70+
byteBuffer.clear();
71+
72+
BigInteger[] bigIntegers = new BigInteger[length];
73+
for (int i = 0; i < length; i++) {
74+
bigIntegers[i] = readBigIntegerFromInputStream(inputStream);
75+
}
76+
return bigIntegers;
77+
}
78+
4379
/**
4480
* Method to read a byte array from an {@link OutputStream}.
4581
* This method first reads a 4-byte long length prefix at the beginning of the resulting array.
@@ -48,6 +84,7 @@ public static BigInteger readBigIntegerFromInputStream(InputStream inputStream)
4884
* The {@link InputStream} to read the instance from. No null values are allowed.
4985
* @throws IOException
5086
* If the {@link OutputStream} threw an exception while reading the data.
87+
* @return The resulting byte array.
5188
*/
5289
public static byte[] readByteArrayFromInputStream(InputStream inputStream) throws IOException {
5390
return receiveInputWithLengthPrefix(inputStream);

src/test/java/com/github/pvriel/mpcutils4j/stream/StreamUtilsTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class StreamUtilsTest {
1919

2020
@BeforeEach
2121
void setUp() throws IOException {
22-
PipedInputStream pipedInputStream = new PipedInputStream(10_000);
22+
PipedInputStream pipedInputStream = new PipedInputStream(100_000);
2323
PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
2424

2525
inputStream = pipedInputStream;
@@ -45,6 +45,22 @@ void writeBigIntegerToOutputStream() throws IOException {
4545
}
4646
}
4747

48+
@Test
49+
@DisplayName("Check if an array of BigIntegers can be correctly written to an OutputStream and read from an InputStream.")
50+
void writeArrayOfBigIntegersToOutputStream() throws IOException {
51+
int amountOfValues = 100;
52+
int bitLengthValues = 2048;
53+
54+
BigInteger[] randomValues = new BigInteger[amountOfValues];
55+
for (int j = 0; j < randomValues.length; j++) {
56+
randomValues[j] = new BigInteger(bitLengthValues, random);
57+
}
58+
StreamUtils.writeArrayOfBigIntegersToOutputStream(randomValues, outputStream);
59+
BigInteger[] readValues = StreamUtils.readArrayOfBigIntegersFromInputStream(inputStream);
60+
assertArrayEquals(randomValues, readValues);
61+
62+
}
63+
4864
@Test
4965
@DisplayName("Check if a byte array can be correctly written to an OutputStream and read from an InputStream.")
5066
void writeByteArrayToOutputStream() throws IOException {

0 commit comments

Comments
 (0)