Repository: yegor256/cactoos
Pull request #48 (2017-05-29 13:03:35)

#46: ThrowableAsBytes
as per #46 

  * Added `ThrowableAsBytes`
  * Removed `InputAsText`
  * Added `InputAsBytes`
  * Added `BytesAsText`


Patch:
diff --git a/README.md b/README.md
index 4cf15adb..c7d169a9 100644
--- a/README.md
+++ b/README.md
@@ -55,12 +55,11 @@ Java version required: 1.8+.
 To read a file:
 
 ```java
-import java.io.File;
-import org.cactoos.io.FileAsInput;
-import org.cactoos.io.InputAsText;
-String text = new InputAsText(
-  new FileAsInput(
-    new File("/code/a.txt")
+String text = new BytesAsText(
+  new InputAsBytes(
+    new FileAsInput(
+      new File("/code/a.txt")
+    )
   )
 ).asString();
 ```
diff --git a/src/main/java/org/cactoos/Input.java b/src/main/java/org/cactoos/Input.java
index 0c9a2a69..56822327 100644
--- a/src/main/java/org/cactoos/Input.java
+++ b/src/main/java/org/cactoos/Input.java
@@ -33,8 +33,10 @@ import org.cactoos.io.FileAsInput;
  * <p>Here is for example how {@link Input} can be used
  * in order to read the content of a text file:</p>
  *
- * <pre> String content = new InputAsText(
- *   new FileAsInput(new File("/tmp/names.txt"))
+ * <pre> String content = new BytesAsText(
+ *   new InputAsBytes(
+ *     new FileAsInput(new File("/tmp/names.txt"))
+ *   )
  * ).asString();</pre>
  *
  * <p>Here {@link FileAsInput} implements {@link Input} and behaves like
diff --git a/src/main/java/org/cactoos/text/ArrayAsBytes.java b/src/main/java/org/cactoos/text/ArrayAsBytes.java
new file mode 100644
index 00000000..6f545703
--- /dev/null
+++ b/src/main/java/org/cactoos/text/ArrayAsBytes.java
@@ -0,0 +1,59 @@
+/**
+ * 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.text;
+
+import org.cactoos.Bytes;
+
+/**
+ * Array as Bytes.
+ *
+ * <p>There is no thread-safety guarantee.
+ *
+ * @author Yegor Bugayenko (yegor256@gmail.com)
+ * @version $Id$
+ * @since 0.2
+ */
+public final class ArrayAsBytes implements Bytes {
+
+    /**
+     * The bytes.
+     */
+    private final byte[] bytes;
+
+    /**
+     * Ctor.
+     * @param bts Bytes to encapsulate
+     */
+    @SuppressWarnings("PMD.ArrayIsStoredDirectly")
+    public ArrayAsBytes(final byte[] bts) {
+        this.bytes = bts;
+    }
+
+    @Override
+    @SuppressWarnings("PMD.MethodReturnsInternalArray")
+    public byte[] asBytes() {
+        return this.bytes;
+    }
+
+}
diff --git a/src/main/java/org/cactoos/text/BytesAsText.java b/src/main/java/org/cactoos/text/BytesAsText.java
new file mode 100644
index 00000000..b76b4994
--- /dev/null
+++ b/src/main/java/org/cactoos/text/BytesAsText.java
@@ -0,0 +1,96 @@
+/**
+ * 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.text;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import org.cactoos.Bytes;
+import org.cactoos.Text;
+
+/**
+ * Bytes as Text.
+ *
+ * <p>This class is doing something similar to what
+ * ExceptionUtils are doing from Apache Commons.</p>
+ *
+ * <p>There is no thread-safety guarantee.
+ *
+ * @author Yegor Bugayenko (yegor256@gmail.com)
+ * @version $Id$
+ * @since 0.2
+ */
+public final class BytesAsText implements Text {
+
+    /**
+     * The bytes.
+     */
+    private final Bytes bytes;
+
+    /**
+     * The charset.
+     */
+    private final Charset charset;
+
+    /**
+     * Ctor.
+     * @param bts Bytes to encapsulate
+     */
+    public BytesAsText(final byte[] bts) {
+        this(bts, StandardCharsets.UTF_8);
+    }
+
+    /**
+     * Ctor.
+     * @param bts Bytes to encapsulate
+     */
+    public BytesAsText(final Bytes bts) {
+        this(bts, StandardCharsets.UTF_8);
+    }
+
+    /**
+     * Ctor.
+     * @param bts Bytes to encapsulate
+     * @param cset Charset
+     */
+    public BytesAsText(final byte[] bts, final Charset cset) {
+        this(new ArrayAsBytes(bts), cset);
+    }
+
+    /**
+     * Ctor.
+     * @param bts Bytes to encapsulate
+     * @param cset Charset
+     */
+    public BytesAsText(final Bytes bts, final Charset cset) {
+        this.bytes = bts;
+        this.charset = cset;
+    }
+
+    @Override
+    public String asString() throws IOException {
+        return new String(this.bytes.asBytes(), this.charset);
+    }
+
+}
diff --git a/src/main/java/org/cactoos/io/InputAsText.java b/src/main/java/org/cactoos/text/ThrowableAsBytes.java
similarity index 59%
rename from src/main/java/org/cactoos/io/InputAsText.java
rename to src/main/java/org/cactoos/text/ThrowableAsBytes.java
index 1b2095b2..7fd1dfd8 100644
--- a/src/main/java/org/cactoos/io/InputAsText.java
+++ b/src/main/java/org/cactoos/text/ThrowableAsBytes.java
@@ -21,59 +21,46 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-package org.cactoos.io;
+package org.cactoos.text;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.nio.charset.Charset;
-import org.cactoos.Input;
-import org.cactoos.Text;
+import java.io.PrintStream;
+import org.cactoos.Bytes;
 
 /**
- * Input as Text.
+ * Throwable as Text.
+ *
+ * <p>This class is doing something similar to what
+ * ExceptionUtils are doing from Apache Commons.</p>
  *
  * <p>There is no thread-safety guarantee.
  *
  * @author Yegor Bugayenko (yegor256@gmail.com)
  * @version $Id$
- * @since 0.1
+ * @since 0.2
  */
-public final class InputAsText implements Text {
+public final class ThrowableAsBytes implements Bytes {
 
     /**
-     * The input.
+     * The throwable.
      */
-    private final Input source;
+    private final Throwable throwable;
 
     /**
-     * Text encoding.
+     * Ctor.
+     * @param error The exception to serialize
      */
-    private final Charset charset;
-
-    /**
-     * New {@link InputAsText} with default charset.
-     *
-     * @param input The input
-     */
-    public InputAsText(final Input input) {
-        this(input, Charset.defaultCharset());
-    }
-
-    /**
-     * New {@link InputAsText} with specified charset.
-     *
-     * @param input The input
-     * @param encoding Text charset
-     */
-    public InputAsText(final Input input, final Charset encoding) {
-        this.source = input;
-        this.charset = encoding;
+    public ThrowableAsBytes(final Throwable error) {
+        this.throwable = error;
     }
 
     @Override
-    public String asString() throws IOException {
-        return new String(
-            new InputAsBytes(this.source).asBytes(),
-            this.charset
-        );
+    public byte[] asBytes() throws IOException {
+        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            this.throwable.printStackTrace(new PrintStream(baos));
+            return baos.toByteArray();
+        }
     }
+
 }


Test patch:
diff --git a/src/test/java/org/cactoos/io/FileAsInputTest.java b/src/test/java/org/cactoos/io/FileAsInputTest.java
index 1f236cdf..bccd5879 100644
--- a/src/test/java/org/cactoos/io/FileAsInputTest.java
+++ b/src/test/java/org/cactoos/io/FileAsInputTest.java
@@ -26,6 +26,7 @@ package org.cactoos.io;
 import java.io.File;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
+import org.cactoos.text.BytesAsText;
 import org.cactoos.text.StringAsText;
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
@@ -50,21 +51,23 @@ public final class FileAsInputTest {
         final File temp = File.createTempFile("cactoos", "txt");
         temp.deleteOnExit();
         MatcherAssert.assertThat(
-            new InputAsText(
-                new TeeInput(
-                    new TextAsInput(
-                        new StringAsText("Hello, друг!"),
-                        StandardCharsets.UTF_8
-                    ),
-                    new FileAsOutput(temp)
-                ),
-                StandardCharsets.UTF_8
+            new BytesAsText(
+                new InputAsBytes(
+                    new TeeInput(
+                        new TextAsInput(
+                            new StringAsText("Hello, друг!"),
+                            StandardCharsets.UTF_8
+                        ),
+                        new FileAsOutput(temp)
+                    )
+                )
             ).asString(),
             Matchers.allOf(
                 Matchers.equalTo(
-                    new InputAsText(
-                        new FileAsInput(temp),
-                        StandardCharsets.UTF_8
+                    new BytesAsText(
+                        new InputAsBytes(
+                            new FileAsInput(temp)
+                        )
                     ).asString()
                 ),
                 Matchers.startsWith("Hello, "),
diff --git a/src/test/java/org/cactoos/io/TeeInputTest.java b/src/test/java/org/cactoos/io/TeeInputTest.java
index c426646d..9d4ac744 100644
--- a/src/test/java/org/cactoos/io/TeeInputTest.java
+++ b/src/test/java/org/cactoos/io/TeeInputTest.java
@@ -25,6 +25,7 @@ package org.cactoos.io;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import org.cactoos.text.BytesAsText;
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
 import org.junit.Test;
@@ -45,10 +46,12 @@ public final class TeeInputTest {
     public void copiesContent() throws IOException {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
         MatcherAssert.assertThat(
-            new InputAsText(
-                new TeeInput(
-                    new TextAsInput("Hello, world!"),
-                    new OutputStreamAsOutput(baos)
+            new BytesAsText(
+                new InputAsBytes(
+                    new TeeInput(
+                        new TextAsInput("Hello, world!"),
+                        new OutputStreamAsOutput(baos)
+                    )
                 )
             ).asString(),
             Matchers.allOf(
diff --git a/src/test/java/org/cactoos/io/UrlAsInputTest.java b/src/test/java/org/cactoos/io/UrlAsInputTest.java
index 2c511422..65c44157 100644
--- a/src/test/java/org/cactoos/io/UrlAsInputTest.java
+++ b/src/test/java/org/cactoos/io/UrlAsInputTest.java
@@ -24,6 +24,7 @@
 package org.cactoos.io;
 
 import java.io.IOException;
+import org.cactoos.text.BytesAsText;
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
 import org.junit.Test;
@@ -64,9 +65,11 @@ public final class UrlAsInputTest {
     public void readsRealUrl() throws IOException {
         new FtRemote(new TkHtml("<html>How are you?</html>")).exec(
             home -> MatcherAssert.assertThat(
-                new InputAsText(
-                    new UrlAsInput(
-                        home.toURL()
+                new BytesAsText(
+                    new InputAsBytes(
+                        new UrlAsInput(
+                            home.toURL()
+                        )
                     )
                 ).asString(),
                 Matchers.allOf(
diff --git a/src/test/java/org/cactoos/text/BytesAsTextTest.java b/src/test/java/org/cactoos/text/BytesAsTextTest.java
new file mode 100644
index 00000000..b5c32455
--- /dev/null
+++ b/src/test/java/org/cactoos/text/BytesAsTextTest.java
@@ -0,0 +1,55 @@
+/**
+ * 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.text;
+
+import java.io.IOException;
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+/**
+ * Test case for {@link BytesAsText}.
+ *
+ * @author Yegor Bugayenko (yegor256@gmail.com)
+ * @version $Id$
+ * @since 0.2
+ */
+public final class BytesAsTextTest {
+
+    /**
+     * BytesAsText converts bytes to String.
+     * @throws IOException If fails
+     */
+    @Test
+    public void convertsBytesToText() throws IOException {
+        MatcherAssert.assertThat(
+            new BytesAsText(
+                new ArrayAsBytes(
+                    new byte[]{'H', 'e', 'l', 'l', 'o'}
+                )
+            ).asString(),
+            Matchers.equalTo("Hello")
+        );
+    }
+}
diff --git a/src/test/java/org/cactoos/io/InputAsTextTest.java b/src/test/java/org/cactoos/text/ThrowableAsBytesTest.java
similarity index 67%
rename from src/test/java/org/cactoos/io/InputAsTextTest.java
rename to src/test/java/org/cactoos/text/ThrowableAsBytesTest.java
index 6a827ec4..f6bc0fcb 100644
--- a/src/test/java/org/cactoos/io/InputAsTextTest.java
+++ b/src/test/java/org/cactoos/text/ThrowableAsBytesTest.java
@@ -21,43 +21,42 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-package org.cactoos.io;
+package org.cactoos.text;
 
 import java.io.IOException;
-import java.nio.charset.Charset;
-import org.cactoos.text.StringAsText;
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
 import org.junit.Test;
 
 /**
- * Test case for {@link InputAsText}.
+ * Test case for {@link ThrowableAsBytes}.
  *
  * @author Yegor Bugayenko (yegor256@gmail.com)
  * @version $Id$
- * @since 0.1
+ * @since 0.2
  */
-public final class InputAsTextTest {
+public final class ThrowableAsBytesTest {
 
     /**
-     * InputAsText can read with specified encoding.
-     *
-     * @throws IOException If some problem inside
+     * ThrowableAsBytes prints stacktrace.
+     * @throws IOException If fails
      */
     @Test
-    public void readsInputWithCharsetIntoText() throws IOException {
-        final Charset charset = Charset.forName("KOI8-R");
+    public void printsStackTrace() throws IOException {
         MatcherAssert.assertThat(
-            new InputAsText(
-                new TextAsInput(
-                    new StringAsText("Hello, друг!"),
-                    charset
-                ),
-                charset
+            new BytesAsText(
+                new ThrowableAsBytes(
+                    new IOException(
+                        "It doesn't work at all"
+                    )
+                )
             ).asString(),
             Matchers.allOf(
-                Matchers.startsWith("Hello, "),
-                Matchers.endsWith("друг!")
+                Matchers.containsString("java.io.IOException"),
+                Matchers.containsString("doesn't work at all"),
+                Matchers.containsString(
+                    "\tat org.cactoos.text.ThrowableAsBytesTest"
+                )
             )
         );
     }