Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New methods in KiwiIO to create a ByteArrayInputStream from a String #1000

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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