You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2016/12/01 21:43:59 UTC
[1/2] commons-io git commit: IO-493: Add infinite circular input
stream (closes #8)
Repository: commons-io
Updated Branches:
refs/heads/master e95134229 -> fcc636194
IO-493: Add infinite circular input stream (closes #8)
Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/699d6f0e
Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/699d6f0e
Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/699d6f0e
Branch: refs/heads/master
Commit: 699d6f0eca65837501d7ab7a92ae2c614f8e6cbf
Parents: e951342
Author: Piotr Turski <pi...@gmail.com>
Authored: Wed Dec 2 00:22:59 2015 +0100
Committer: pascalschumacher <pa...@gmx.net>
Committed: Thu Dec 1 22:40:50 2016 +0100
----------------------------------------------------------------------
.../io/input/InfiniteCircularInputStream.java | 52 +++++++++++++++++
.../input/InfiniteCircularInputStreamTest.java | 61 ++++++++++++++++++++
2 files changed, 113 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-io/blob/699d6f0e/src/main/java/org/apache/commons/io/input/InfiniteCircularInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/io/input/InfiniteCircularInputStream.java b/src/main/java/org/apache/commons/io/input/InfiniteCircularInputStream.java
new file mode 100644
index 0000000..2b2841c
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/input/InfiniteCircularInputStream.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.input;
+
+import java.io.InputStream;
+
+/**
+ *
+ * An {@link InputStream} that infinitely repeats provided bytes.
+ * <p>
+ * Closing a <tt>InfiniteCircularInputStream</tt> has no effect. The methods in
+ * this class can be called after the stream has been closed without generating
+ * an <tt>IOException</tt>.
+ *
+ */
+public class InfiniteCircularInputStream extends InputStream {
+
+ final private byte[] repeatedContent;
+ private int position = -1;
+
+ /**
+ * Creates a InfiniteCircularStream from the specified array of chars.
+ *
+ * @param repeatedContent
+ * Input buffer to be repeated (not copied)
+ */
+ public InfiniteCircularInputStream(byte[] repeatedContent) {
+ this.repeatedContent = repeatedContent;
+ }
+
+ @Override
+ public int read() {
+ position = (position + 1) % repeatedContent.length;
+ return repeatedContent[position] & 0xff; // copied from
+ // java.io.ByteArrayInputStream.read()
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-io/blob/699d6f0e/src/test/java/org/apache/commons/io/input/InfiniteCircularInputStreamTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/input/InfiniteCircularInputStreamTest.java b/src/test/java/org/apache/commons/io/input/InfiniteCircularInputStreamTest.java
new file mode 100644
index 0000000..711a3f0
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/input/InfiniteCircularInputStreamTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.input;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class InfiniteCircularInputStreamTest {
+
+ @Test
+ public void should_cycle_bytes() throws IOException {
+ byte[] input = new byte[] { 1, 2 };
+ byte[] expected = new byte[] { 1, 2, 1, 2, 1 };
+
+ assertStreamOutput(input, expected);
+ }
+
+ @Test
+ public void should_handle_whole_range_of_bytes() throws IOException {
+ int size = Byte.MAX_VALUE - Byte.MIN_VALUE + 1;
+ byte[] contentToCycle = new byte[size];
+ byte value = Byte.MIN_VALUE;
+ for (int i = 0; i < contentToCycle.length; i++) {
+ contentToCycle[i] = value++;
+ }
+
+ byte[] expectedOutput = Arrays.copyOf(contentToCycle, size);
+
+ assertStreamOutput(contentToCycle, expectedOutput);
+ }
+
+ private void assertStreamOutput(byte[] toCycle, byte[] expected) throws IOException {
+ byte[] actual = new byte[expected.length];
+
+ try (InputStream infStream = new InfiniteCircularInputStream(toCycle)) {
+ int actualReadBytes = infStream.read(actual);
+
+ Assert.assertArrayEquals(expected, actual);
+ Assert.assertEquals(expected.length, actualReadBytes);
+ }
+ }
+
+}
[2/2] commons-io git commit: IO-493: Add infinite circular input
stream
Posted by pa...@apache.org.
IO-493: Add infinite circular input stream
add changes.xml entry
Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/fcc63619
Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/fcc63619
Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/fcc63619
Branch: refs/heads/master
Commit: fcc6361946b1809771e136af566518320006c01e
Parents: 699d6f0
Author: pascalschumacher <pa...@gmx.net>
Authored: Thu Dec 1 22:43:43 2016 +0100
Committer: pascalschumacher <pa...@gmx.net>
Committed: Thu Dec 1 22:43:43 2016 +0100
----------------------------------------------------------------------
src/changes/changes.xml | 3 +++
1 file changed, 3 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-io/blob/fcc63619/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9b93a64..5130ba0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -77,6 +77,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-504" dev="ggregory" type="update" due-to="Christian Schulte">
Deprecated of all IOUtils.closeQuietly() methods and use try-with-resources internally.
</action>
+ <action issue="IO-493" dev="pschumacher" type="add" due-to="Piotr Turski">
+ Add infinite circular input stream
+ </action>
<action issue="IO-507" dev="ggregory" type="add">
Add a ByteOrderFactory class.
</action>