diff --git a/src/main/java/org/kiwiproject/io/KiwiIO.java b/src/main/java/org/kiwiproject/io/KiwiIO.java index 6b7fef2a..b7dfe5a1 100644 --- a/src/main/java/org/kiwiproject/io/KiwiIO.java +++ b/src/main/java/org/kiwiproject/io/KiwiIO.java @@ -4,6 +4,7 @@ import static java.util.Objects.nonNull; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; +import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull; import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; @@ -200,6 +201,40 @@ public static ByteArrayInputStream newByteArrayInputStreamOfLines(String... line return new ByteArrayInputStream(buffer); } + /** + * Creates a new {@link ByteArrayInputStream} containing the bytes of the given string using the + * UTF-8 character set. + *
+ * Note: The UTF-8 character set is widely used and supports a vast range of characters, making it + * suitable for most applications. However, if the string was encoded using a different character + * set, use the other version of this method that accepts a {@link Charset} to specify the character + * set that was used to encode the string. + * + * @param value the string from which to create the ByteArrayInputStream + * @return a new ByteArrayInputStream initialized with bytes from the provided string + * @throws IllegalArgumentException if the input string is null + */ + public static ByteArrayInputStream newByteArrayInputStream(String value) { + return newByteArrayInputStream(value, StandardCharsets.UTF_8); + } + + /** + * Creates a new {@link ByteArrayInputStream} containing the bytes of the given string using the + * specified character set. + * + * @param value the string from which to create the ByteArrayInputStream + * @param charset the character set used to encode the string as bytes + * @return a new ByteArrayInputStream initialized with bytes from the provided string + * @throws IllegalArgumentException if the input string or charset is null + */ + public static ByteArrayInputStream newByteArrayInputStream(String value, Charset charset) { + checkArgumentNotNull(value, "value must not be null"); + checkArgumentNotNull(charset, "charset must not be null"); + + byte[] bytes = value.getBytes(charset); + return new ByteArrayInputStream(bytes); + } + /** * Return a newly constructed, empty {@link ByteArrayInputStream}. * diff --git a/src/test/java/org/kiwiproject/io/KiwiIOTest.java b/src/test/java/org/kiwiproject/io/KiwiIOTest.java index dc35150a..b27e5b25 100644 --- a/src/test/java/org/kiwiproject/io/KiwiIOTest.java +++ b/src/test/java/org/kiwiproject/io/KiwiIOTest.java @@ -3,16 +3,20 @@ import static java.util.stream.Collectors.joining; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import org.joda.time.Instant; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; @@ -312,6 +316,70 @@ private void assertNoMoreLines(BufferedReader reader) throws IOException { } } + @Nested + class NewByteArrayInputStream { + + @Test + void shouldRequireNonNullInputString() { + assertThatIllegalArgumentException() + .isThrownBy(() -> KiwiIO.newByteArrayInputStream(null)) + .withMessage("value must not be null"); + } + + @Test + void shouldAllowEmptyStrings() { + var inputStream = KiwiIO.newByteArrayInputStream(""); + + assertThat(inputStream).isEmpty(); + } + + @Test + void shouldEncodeStringsUsingUTF8() { + var value = "the quick brown fox jumped over the lazy brown dog at " + Instant.now(); + var inputStream = KiwiIO.newByteArrayInputStream(value); + + var expected = new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8)); + + assertThat(inputStream).hasSameContentAs(expected); + } + } + + @Nested + class NewByteArrayInputStreamWtihCharset { + + @Test + void shouldRequireNonNullInputString() { + assertThatIllegalArgumentException() + .isThrownBy(() -> KiwiIO.newByteArrayInputStream(null, StandardCharsets.UTF_8)) + .withMessage("value must not be null"); + } + + @Test + void shouldRequireNonNullCharset() { + assertThatIllegalArgumentException() + .isThrownBy(() -> KiwiIO.newByteArrayInputStream("some string", null)) + .withMessage("charset must not be null"); + } + + @ParameterizedTest + @ValueSource(strings = { + "ISO-8859-1", + "US-ASCII", + "UTF-8", + "UTF-16", + "UTF-32", + }) + void shouldEncodeStringsUsingDifferentCharsets(String charsetName) { + var charset = Charset.forName(charsetName); + var value = "the quick brown fox jumped over the lazy brown dog at " + Instant.now(); + var inputStream = KiwiIO.newByteArrayInputStream(value, charset); + + var expected = new ByteArrayInputStream(value.getBytes(charset)); + + assertThat(inputStream).hasSameContentAs(expected); + } + } + @Nested class TestingProcessArgumentMethods {