diff --git a/src/java/org/apache/commons/collections/primitives/adapters/io/InputStreamByteIterator.java b/src/java/org/apache/commons/collections/primitives/adapters/io/InputStreamByteIterator.java new file mode 100644 index 000000000..a04e2df46 --- /dev/null +++ b/src/java/org/apache/commons/collections/primitives/adapters/io/InputStreamByteIterator.java @@ -0,0 +1,125 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/primitives/adapters/io/Attic/InputStreamByteIterator.java,v 1.1 2003/04/15 03:07:50 rwaldhoff Exp $ + * ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.commons.collections.primitives.adapters.io; + +import java.io.IOException; +import java.io.InputStream; +import java.util.NoSuchElementException; + +import org.apache.commons.collections.primitives.ByteIterator; + +/** + * Adapts an {@link InputStream} to the {@link ByteIterator} interface. + * + * @version $Revision: 1.1 $ $Date: 2003/04/15 03:07:50 $ + * @author Rodney Waldhoff + */ +public class InputStreamByteIterator implements ByteIterator { + + public InputStreamByteIterator(InputStream in) { + this.stream = in; + } + + public boolean hasNext() { + ensureNextAvailable(); + return (-1 != next); + } + + public byte next() { + if(!hasNext()) { + throw new NoSuchElementException("No next element"); + } else { + nextAvailable = false; + return (byte)next; + } + } + + /** + * Not supported. + * @throws UnsupportedOperationException + */ + public void remove() throws UnsupportedOperationException { + throw new UnsupportedOperationException("remove() is not supported here"); + } + + public static ByteIterator adapt(InputStream in) { + return null == in ? null : new InputStreamByteIterator(in); + } + + private void ensureNextAvailable() { + if(!nextAvailable) { + readNext(); + } + } + + private void readNext() { + try { + next = stream.read(); + nextAvailable = true; + } catch(IOException e) { + // TODO: Fix me using tunneled exception, see + // http://radio.weblogs.com/0122027/2003/04/01.html#a7 + // for example + throw new RuntimeException(e.toString()); + } + } + + private InputStream stream = null; + private boolean nextAvailable = false; + private int next; +} diff --git a/src/test/org/apache/commons/collections/primitives/adapters/TestAll.java b/src/test/org/apache/commons/collections/primitives/adapters/TestAll.java index 6ddb469d4..e7709af16 100644 --- a/src/test/org/apache/commons/collections/primitives/adapters/TestAll.java +++ b/src/test/org/apache/commons/collections/primitives/adapters/TestAll.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/adapters/Attic/TestAll.java,v 1.7 2003/04/15 01:55:21 rwaldhoff Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/adapters/Attic/TestAll.java,v 1.8 2003/04/15 03:07:50 rwaldhoff Exp $ * ==================================================================== * The Apache Software License, Version 1.1 * @@ -62,7 +62,7 @@ import junit.framework.TestCase; import junit.framework.TestSuite; /** - * @version $Revision: 1.7 $ $Date: 2003/04/15 01:55:21 $ + * @version $Revision: 1.8 $ $Date: 2003/04/15 03:07:50 $ * @author Rodney Waldhoff */ public class TestAll extends TestCase { @@ -132,6 +132,8 @@ public class TestAll extends TestCase { suite.addTest(TestDoubleIteratorIterator.suite()); suite.addTest(TestDoubleListIteratorListIterator.suite()); + suite.addTest(org.apache.commons.collections.primitives.adapters.io.TestAll.suite()); + return suite; } } diff --git a/src/test/org/apache/commons/collections/primitives/adapters/io/TestAll.java b/src/test/org/apache/commons/collections/primitives/adapters/io/TestAll.java new file mode 100644 index 000000000..11fdc1a4f --- /dev/null +++ b/src/test/org/apache/commons/collections/primitives/adapters/io/TestAll.java @@ -0,0 +1,81 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/adapters/io/Attic/TestAll.java,v 1.1 2003/04/15 03:07:49 rwaldhoff Exp $ + * ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.commons.collections.primitives.adapters.io; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * @version $Revision: 1.1 $ $Date: 2003/04/15 03:07:49 $ + * @author Rodney Waldhoff + */ +public class TestAll extends TestCase { + public TestAll(String testName) { + super(testName); + } + + public static Test suite() { + TestSuite suite = new TestSuite(); + + suite.addTest(TestInputStreamByteIterator.suite()); + + return suite; + } +} + diff --git a/src/test/org/apache/commons/collections/primitives/adapters/io/TestInputStreamByteIterator.java b/src/test/org/apache/commons/collections/primitives/adapters/io/TestInputStreamByteIterator.java new file mode 100644 index 000000000..2524edb95 --- /dev/null +++ b/src/test/org/apache/commons/collections/primitives/adapters/io/TestInputStreamByteIterator.java @@ -0,0 +1,110 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/adapters/io/Attic/TestInputStreamByteIterator.java,v 1.1 2003/04/15 03:07:49 rwaldhoff Exp $ + * ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.commons.collections.primitives.adapters.io; + +import java.io.ByteArrayInputStream; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.commons.collections.primitives.ByteIterator; +import org.apache.commons.collections.primitives.TestByteIterator; + +/** + * @version $Revision: 1.1 $ $Date: 2003/04/15 03:07:49 $ + * @author Rodney Waldhoff + */ +public class TestInputStreamByteIterator extends TestByteIterator { + + // conventional + // ------------------------------------------------------------------------ + + public TestInputStreamByteIterator(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(TestInputStreamByteIterator.class); + } + + // ------------------------------------------------------------------------ + + public boolean supportsRemove() { + return false; + } + + protected ByteIterator makeEmptyByteIterator() { + return new InputStreamByteIterator(new ByteArrayInputStream(new byte[0])); + } + + protected ByteIterator makeFullByteIterator() { + return new InputStreamByteIterator(new ByteArrayInputStream(getFullElements())); + } + + protected byte[] getFullElements() { + byte[] bytes = new byte[256]; + for(int i=0; i < 256; i++) { + bytes[i] = (byte)(i-128); + } + return bytes; + } + + + // ------------------------------------------------------------------------ + +}