FEAT StreamUtils.toInputStream & StreamUtils.toString
NClazz/java-commons/pipeline/head This commit looks good
Details
NClazz/java-commons/pipeline/head This commit looks good
Details
parent
f406b6235d
commit
0ab9980583
|
@ -2,8 +2,10 @@ package de.nclazz.commons.io;
|
|||
|
||||
import de.nclazz.commons.util.StringUtils;
|
||||
import lombok.NonNull;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public abstract class StreamUtils {
|
||||
|
@ -63,5 +65,47 @@ public abstract class StreamUtils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts given string to an InputStream.
|
||||
* @param string to convert
|
||||
* @return new InputStream
|
||||
*/
|
||||
public static InputStream toInputStream(@NonNull String string, Charset charset) {
|
||||
charset = charset != null ? charset : Charset.defaultCharset();
|
||||
byte[] bytes = string.getBytes(charset);
|
||||
return new ByteArrayInputStream(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #toInputStream(String, Charset)
|
||||
* @param string to read
|
||||
* @return new InputStream
|
||||
*/
|
||||
public static InputStream toInputStream(@NonNull String string) {
|
||||
return toInputStream(string, Charset.defaultCharset());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the given InputStream and converts it to a String.
|
||||
* The stream does not close after this operation.
|
||||
* @param stream to read
|
||||
* @param charset of the String
|
||||
* @return new String
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static String toString(@NonNull InputStream stream, Charset charset) {
|
||||
charset = charset != null ? charset : Charset.defaultCharset();
|
||||
byte[] bytes = inputStreamToByteArray(stream);
|
||||
return new String(bytes, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #toString(InputStream, Charset)
|
||||
* @param stream to read
|
||||
* @return new String
|
||||
*/
|
||||
public static String toString(@NonNull InputStream stream) {
|
||||
return toString(stream, Charset.defaultCharset());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package de.nclazz.commons.util;
|
|||
import lombok.NonNull;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public abstract class StringUtils {
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class StreamUtilsTests {
|
||||
|
@ -37,4 +38,16 @@ public class StreamUtilsTests {
|
|||
assertTrue(Arrays.equals(alphabetBytes, bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringToInputStreamRoundTrip() throws IOException {
|
||||
String testString = "Hello World!";
|
||||
InputStream stream = StreamUtils.toInputStream(testString);
|
||||
String backString = StreamUtils.toString(stream);
|
||||
|
||||
stream.close();
|
||||
|
||||
assertEquals(testString, backString);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue