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 <piotr.tur...@gmail.com>
Authored: Wed Dec 2 00:22:59 2015 +0100
Committer: pascalschumacher <pascalschumac...@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);
+        }
+    }
+
+}

Reply via email to