diff --git a/src/main/java/org/cactoos/io/ReaderAsInput.java b/src/main/java/org/cactoos/io/ReaderAsInput.java new file mode 100644 index 0000000000..94bb513da2 --- /dev/null +++ b/src/main/java/org/cactoos/io/ReaderAsInput.java @@ -0,0 +1,106 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.io; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import org.cactoos.Input; + +/** + * Reader as {@link Input}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.12 + */ +public final class ReaderAsInput implements Input { + + /** + * The reader. + */ + private final Reader reader; + + /** + * The charset. + */ + private final Charset charset; + + /** + * The buffer size. + */ + private final int size; + + /** + * Ctor. + * @param rdr Reader + */ + public ReaderAsInput(final Reader rdr) { + this(rdr, StandardCharsets.UTF_8); + } + + /** + * Ctor. + * @param rdr Reader + * @param cset Charset + */ + public ReaderAsInput(final Reader rdr, final Charset cset) { + // @checkstyle MagicNumber (1 line) + this(rdr, cset, 16 << 10); + } + + /** + * Ctor. + * @param rdr Reader + * @param cset Charset + * @param max Buffer size + */ + public ReaderAsInput(final Reader rdr, final Charset cset, final int max) { + this.reader = rdr; + this.charset = cset; + this.size = max; + } + + @Override + public InputStream stream() throws IOException { + final char[] buffer = new char[this.size]; + final StringBuilder builder = new StringBuilder(); + while (true) { + final int done = this.reader.read(buffer, 0, buffer.length); + if (done < 0) { + break; + } + builder.append(buffer, 0, done); + } + return new ByteArrayInputStream( + builder.toString().getBytes(this.charset) + ); + } + +} diff --git a/src/test/java/org/cactoos/io/ReaderAsInputTest.java b/src/test/java/org/cactoos/io/ReaderAsInputTest.java new file mode 100644 index 0000000000..bf60979c65 --- /dev/null +++ b/src/test/java/org/cactoos/io/ReaderAsInputTest.java @@ -0,0 +1,58 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.io; + +import java.io.StringReader; +import org.cactoos.text.BytesAsText; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link ReaderAsInput}. + * + * @author Kirill (g4s8.public@gmail.com) + * @version $Id$ + * @since 0.12 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class ReaderAsInputTest { + + @Test + public void readsString() throws Exception { + final String source = "hello, друг!"; + MatcherAssert.assertThat( + "Can't read string through a reader", + new BytesAsText( + new InputAsBytes( + new ReaderAsInput( + new StringReader(source) + ) + ) + ).asString(), + Matchers.equalTo(source) + ); + } + +}