1616import java .io .ByteArrayOutputStream ;
1717import java .io .IOException ;
1818import java .io .InputStream ;
19- import java .io .OutputStream ;
20- import java .io .StringWriter ;
2119import java .nio .charset .StandardCharsets ;
2220import java .util .ArrayList ;
2321import java .util .Arrays ;
2826import java .util .stream .Stream ;
2927
3028import org .apache .commons .io .IOUtils ;
31- import org .apache .commons .io .function .IOConsumer ;
32- import org .apache .commons .io .output .WriterOutputStream ;
3329import org .apache .commons .lang3 .StringUtils ;
3430import org .junit .jupiter .api .Assertions ;
3531import org .junit .jupiter .params .ParameterizedTest ;
@@ -128,15 +124,11 @@ public static InputStream createTestInputStream(
128124 .iterator ());
129125 }
130126
131- public static String utf8String (
132- final IOConsumer <? super OutputStream > consumer ) throws IOException {
133- try (StringWriter sw = new StringWriter ();
134- WriterOutputStream wos = WriterOutputStream .builder ().setCharset (StandardCharsets .UTF_8 ).setWriter (sw ).get ()) {
135- consumer .accept (wos );
136- wos .flush ();
137- sw .flush ();
138- return sw .toString ();
139- }
127+ public static String trailerUtf8String (
128+ final TrailerInputStream tis ) {
129+ final byte [] trailer = tis .copyTrailer ();
130+ Assertions .assertEquals (trailer .length , tis .getTrailerLength ());
131+ return new String (trailer , 0 , trailer .length , StandardCharsets .UTF_8 );
140132 }
141133
142134 public static void assertDataTrailer (
@@ -157,9 +149,9 @@ public static void assertDataTrailer(
157149 Assertions .assertAll (
158150 () -> Assertions .assertEquals (d , data + trailer , "Generation of expectation" ),
159151 () -> Assertions .assertEquals (trailerLength , trailer .length (), "Trailer length" ),
160- () -> Assertions .assertEquals (data , utf8String ( os :: writeTo ), "Data content" ),
152+ () -> Assertions .assertEquals (data , os . toString ( StandardCharsets . UTF_8 . name () ), "Data content" ),
161153 () -> Assertions .assertEquals (
162- trailer , utf8String (tis :: copyTrailer ), "Trailer content" ));
154+ trailer , trailerUtf8String (tis ), "Trailer content" ));
163155 }
164156
165157 @ ParameterizedTest
@@ -174,7 +166,7 @@ public void testReadBytewise(final int trailerLength) throws IOException {
174166 TrailerInputStream tis = new TrailerInputStream (is , trailerLength );
175167 ByteArrayOutputStream os = new ByteArrayOutputStream ()) {
176168 Assertions .assertEquals (
177- StringUtils .repeat ('a' , trailerLength ), utf8String (tis :: copyTrailer ));
169+ StringUtils .repeat ('a' , trailerLength ), trailerUtf8String (tis ));
178170 int read ;
179171 while ((read = tis .read ()) != IOUtils .EOF ) {
180172 os .write (read );
@@ -195,7 +187,7 @@ public void testReadWholeBlocks(final int trailerLength) throws IOException {
195187 TrailerInputStream tis = new TrailerInputStream (is , trailerLength );
196188 ByteArrayOutputStream os = new ByteArrayOutputStream ()) {
197189 Assertions .assertEquals (
198- StringUtils .repeat ('a' , trailerLength ), utf8String (tis :: copyTrailer ));
190+ StringUtils .repeat ('a' , trailerLength ), trailerUtf8String (tis ));
199191 final byte [] buffer = new byte [chunkLength ];
200192 int read ;
201193 while ((read = tis .read (buffer )) != IOUtils .EOF ) {
@@ -217,7 +209,7 @@ public void testReadLastBlockAlmostFull(final int trailerLength) throws IOExcept
217209 TrailerInputStream tis = new TrailerInputStream (is , trailerLength );
218210 ByteArrayOutputStream os = new ByteArrayOutputStream ()) {
219211 Assertions .assertEquals (
220- StringUtils .repeat ('a' , trailerLength ), utf8String (tis :: copyTrailer ));
212+ StringUtils .repeat ('a' , trailerLength ), trailerUtf8String (tis ));
221213 final byte [] buffer = new byte [chunkLength + 3 * chunks ];
222214 int offset = chunks ;
223215 while (true ) {
@@ -245,7 +237,7 @@ public void testReadLastBlockAlmostEmpty(final int trailerLength) throws IOExcep
245237 TrailerInputStream tis = new TrailerInputStream (is , trailerLength );
246238 ByteArrayOutputStream os = new ByteArrayOutputStream ()) {
247239 Assertions .assertEquals (
248- StringUtils .repeat ('a' , trailerLength ), utf8String (tis :: copyTrailer ));
240+ StringUtils .repeat ('a' , trailerLength ), trailerUtf8String (tis ));
249241 final byte [] buffer = new byte [chunkLength + 3 * chunks ];
250242 int offset = chunks ;
251243 while (true ) {
0 commit comments