Skip to content

Commit

Permalink
New methods in KiwiIO to create a ByteArrayInputStream from a String (#…
Browse files Browse the repository at this point in the history
…1000)

* Add newByteArrayInputStream method in KiwiIO that converts a
  String to a ByteArrayInputStream.
* Add overload that accepts a Charset

Closes #998
  • Loading branch information
sleberknight authored Aug 1, 2023
1 parent e9a98e6 commit aa63783
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/org/kiwiproject/io/KiwiIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
* <p>
* 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}.
*
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/org/kiwiproject/io/KiwiIOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit aa63783

Please sign in to comment.