FEAT StreamUtils: inputStreamToByteArray + pipeStream
de.nclazz/java-commons/pipeline/head This commit looks good Details

pull/1/head
Niclas Thobaben 2020-12-01 01:33:36 +01:00
parent 65b223e7ca
commit 7e8ddb9aa8
5 changed files with 89 additions and 2 deletions

View File

@ -1,4 +1,4 @@
# de.nclazz Java-Commons
# java-commons
[![Build Status](https://jenkins.niclas-thobaben.de/buildStatus/icon?job=de.nclazz%2Fjava-commons%2Fmaster)](https://jenkins.niclas-thobaben.de/job/de.nclazz/job/java-commons/job/master/)

View File

@ -7,7 +7,7 @@
<groupId>de.nclazz</groupId>
<artifactId>commons</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>de.nclazz</groupId>
<artifactId>maven-boot</artifactId>

View File

@ -0,0 +1,46 @@
package de.nclazz.commons.io;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public abstract class StreamUtils {
private StreamUtils() { /* no-op */ }
/**
* Pipes all the input from {@code streamIn} to {@code streamOut}.
* This method does not close any of the streams!
*
* @param streamIn InputStream
* @param streamOut OutputStream
* @throws IOException
*/
public static void pipeStream(InputStream streamIn, OutputStream streamOut) throws IOException {
byte[] bytes = new byte[4096]; //TODO make configurable? maybe as a sys property?
int length;
while ((length = streamIn.read(bytes)) >= 0) {
streamOut.write(bytes, 0, length);
}
}
/**
* Loads an InputStream into a new byte array.
* This method does not close the stream after processing!
* @param stream InputStream
* @return new {@code byte[]}
*/
public static byte[] inputStreamToByteArray(InputStream stream) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[4096]; //TODO make configurable? maybe as a sys property?
int length;
while((length = stream.read(buffer)) != -1) {
bos.write(buffer, 0, length);
}
return bos.toByteArray();
}
}

View File

@ -0,0 +1,40 @@
package de.nclazz.commons.io;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StreamUtilsTests {
byte[] alphabetBytes = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', };
@Test
void testInputStreamToByteArray() throws IOException {
InputStream stream = StreamUtilsTests.class.getResourceAsStream("/test-files/alphabet-utf8.txt");
byte[] bytes = StreamUtils.inputStreamToByteArray(stream);
assertTrue(Arrays.equals(alphabetBytes, bytes));
}
@Test
void testStreamPiping() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream stream = StreamUtilsTests.class.getResourceAsStream("/test-files/alphabet-utf8.txt");
StreamUtils.pipeStream(stream, bos);
byte[] bytes = bos.toByteArray();
assertTrue(Arrays.equals(alphabetBytes, bytes));
}
}

View File

@ -0,0 +1 @@
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ