From 019f965527dd64c9496072f7b2983fc2be60cd89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Mon, 28 Dec 2020 16:34:41 +0100 Subject: [PATCH] Fix IterableOfBytes to be traversable multiple times --- .../org/cactoos/iterable/IterableOfBytes.java | 28 +++++++++---------- .../org/cactoos/iterable/IterableOfChars.java | 16 +++++------ .../org/cactoos/iterator/IteratorOfBytes.java | 28 +++++++++---------- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/cactoos/iterable/IterableOfBytes.java b/src/main/java/org/cactoos/iterable/IterableOfBytes.java index 62b27fad27..d2ac7835dc 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfBytes.java +++ b/src/main/java/org/cactoos/iterable/IterableOfBytes.java @@ -23,9 +23,9 @@ */ package org.cactoos.iterable; -import java.nio.charset.StandardCharsets; -import java.util.Iterator; +import org.cactoos.Bytes; import org.cactoos.Text; +import org.cactoos.io.BytesOf; import org.cactoos.iterator.IteratorOfBytes; /** @@ -37,33 +37,33 @@ public final class IterableOfBytes extends IterableEnvelope { /** * Ctor. - * @param bytes Bytes + * @param txt Text */ - public IterableOfBytes(final byte... bytes) { - this(new IteratorOfBytes(bytes)); + public IterableOfBytes(final Text txt) { + this(new BytesOf(txt)); } /** * Ctor. - * @param bytes Iterator + * @param str String */ - public IterableOfBytes(final Iterator bytes) { - super(new IterableOf<>(bytes)); + public IterableOfBytes(final String str) { + this(new BytesOf(str)); } /** * Ctor. - * @param str String + * @param bytes Bytes */ - public IterableOfBytes(final String str) { - this(str.getBytes(StandardCharsets.UTF_8)); + public IterableOfBytes(final byte... bytes) { + this(new BytesOf(bytes)); } /** * Ctor. - * @param txt Text + * @param bytes Bytes to iterate */ - public IterableOfBytes(final Text txt) { - this(txt.toString()); + public IterableOfBytes(final Bytes bytes) { + super(new IterableOf<>(() -> new IteratorOfBytes(bytes))); } } diff --git a/src/main/java/org/cactoos/iterable/IterableOfChars.java b/src/main/java/org/cactoos/iterable/IterableOfChars.java index 526cc97621..837e647510 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfChars.java +++ b/src/main/java/org/cactoos/iterable/IterableOfChars.java @@ -33,14 +33,6 @@ */ public final class IterableOfChars extends IterableEnvelope { - /** - * Ctor. - * @param chars Characters - */ - public IterableOfChars(final char... chars) { - super(new IterableOf<>(() -> new IteratorOfChars(chars))); - } - /** * Ctor. * @param str String @@ -56,4 +48,12 @@ public IterableOfChars(final String str) { public IterableOfChars(final Text txt) { this(txt.toString()); } + + /** + * Ctor. + * @param chars Characters + */ + public IterableOfChars(final char... chars) { + super(new IterableOf<>(() -> new IteratorOfChars(chars))); + } } diff --git a/src/main/java/org/cactoos/iterator/IteratorOfBytes.java b/src/main/java/org/cactoos/iterator/IteratorOfBytes.java index e0c5d4cd2a..a46a2e6e6b 100644 --- a/src/main/java/org/cactoos/iterator/IteratorOfBytes.java +++ b/src/main/java/org/cactoos/iterator/IteratorOfBytes.java @@ -29,6 +29,7 @@ import org.cactoos.Bytes; import org.cactoos.Text; import org.cactoos.io.BytesOf; +import org.cactoos.io.UncheckedBytes; /** * Iterator that returns a set of bytes. @@ -51,38 +52,35 @@ public final class IteratorOfBytes implements Iterator { /** * Ctor. - * @param itms Items to iterate + * @param txt Text to iterate */ - public IteratorOfBytes(final byte... itms) { - this.items = itms; - this.position = new AtomicInteger(0); + public IteratorOfBytes(final Text txt) { + this(new BytesOf(txt)); } /** * Ctor. - * @param txt Text to iterate - * @throws Exception If fails + * @param str String to iterate */ - public IteratorOfBytes(final Text txt) throws Exception { - this(new BytesOf(txt)); + public IteratorOfBytes(final String str) { + this(new BytesOf(str)); } /** * Ctor. * @param bytes Bytes to iterate - * @throws Exception If fails */ - public IteratorOfBytes(final Bytes bytes) throws Exception { - this(bytes.asBytes()); + public IteratorOfBytes(final Bytes bytes) { + this(new UncheckedBytes(bytes).asBytes()); } /** * Ctor. - * @param str String to iterate - * @throws Exception If fails + * @param itms Items to iterate */ - public IteratorOfBytes(final String str) throws Exception { - this(new BytesOf(str)); + public IteratorOfBytes(final byte... itms) { + this.items = itms; + this.position = new AtomicInteger(0); } @Override