You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2007/12/17 15:36:38 UTC
svn commit: r604883 - in /xmlgraphics/commons/trunk:
src/java/org/apache/xmlgraphics/util/io/SubInputStream.java
test/java/org/apache/xmlgraphics/util/io/SubInputStreamTestCase.java
Author: jeremias
Date: Mon Dec 17 06:36:37 2007
New Revision: 604883
URL: http://svn.apache.org/viewvc?rev=604883&view=rev
Log:
Add option to SubInputStream that allows to close the underlying stream.
Fixed a bug in read() method.
Added a non-exhaustive test case for SubInputStream.
Added:
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/SubInputStreamTestCase.java (with props)
Modified:
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/io/SubInputStream.java
Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/io/SubInputStream.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/io/SubInputStream.java?rev=604883&r1=604882&r2=604883&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/io/SubInputStream.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/io/SubInputStream.java Mon Dec 17 06:36:37 2007
@@ -33,25 +33,44 @@
*/
public class SubInputStream extends FilterInputStream {
- /** Indicates the number of bytes remaning to be read from the underlying InputStream. */
+ /** Indicates the number of bytes remaining to be read from the underlying InputStream. */
private long bytesToRead;
/**
+ * Indicates whether the underlying stream should be closed when the {@link #close()} method
+ * is called.
+ */
+ private boolean closeUnderlying = false;
+
+ /**
* Creates a new SubInputStream.
* @param in the InputStream to read from
* @param maxLen the maximum number of bytes to read from the underlying InputStream until
* the end-of-file is signalled.
+ * @param closeUnderlying true if the underlying stream should be closed when the
+ * {@link #close()} method is called.
*/
- public SubInputStream(InputStream in, long maxLen) {
+ public SubInputStream(InputStream in, long maxLen, boolean closeUnderlying) {
super(in);
this.bytesToRead = maxLen;
+ this.closeUnderlying = closeUnderlying;
+ }
+
+ /**
+ * Creates a new SubInputStream. The underlying stream is not closed, when close() is called.
+ * @param in the InputStream to read from
+ * @param maxLen the maximum number of bytes to read from the underlying InputStream until
+ * the end-of-file is signalled.
+ */
+ public SubInputStream(InputStream in, long maxLen) {
+ this(in, maxLen, false);
}
- /** @see java.io.InputStream#read() */
+ /** {@inheritDoc} */
public int read() throws IOException {
if (bytesToRead > 0) {
int result = super.read();
- if (result <= 0) {
+ if (result >= 0) {
bytesToRead--;
return result;
} else {
@@ -62,7 +81,7 @@
}
}
- /** @see java.io.InputStream#read(byte[], int, int) */
+ /** {@inheritDoc} */
public int read(byte[] b, int off, int len) throws IOException {
if (bytesToRead == 0) {
return -1;
@@ -77,7 +96,7 @@
return result;
}
- /** @see java.io.InputStream#skip(long) */
+ /** {@inheritDoc} */
public long skip(long n) throws IOException {
long effRead = Math.min(bytesToRead, n);
long result = super.skip(effRead);
@@ -85,9 +104,11 @@
return result;
}
- /** @see java.io.InputStream#close() */
+ /** {@inheritDoc} */
public void close() throws IOException {
- //Don't close the underlying InputStream!!!
this.bytesToRead = 0;
+ if (this.closeUnderlying) {
+ super.close();
+ }
}
}
Added: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/SubInputStreamTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/SubInputStreamTestCase.java?rev=604883&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/SubInputStreamTestCase.java (added)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/SubInputStreamTestCase.java Mon Dec 17 06:36:37 2007
@@ -0,0 +1,81 @@
+/*
+ * 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.
+ */
+
+/* $Id$ */
+
+package org.apache.xmlgraphics.util.io;
+
+import java.io.ByteArrayInputStream;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for SubInputStream.
+ */
+public class SubInputStreamTestCase extends TestCase {
+
+ /**
+ * Main constructor.
+ * @param name the test case's name
+ * @see junit.framework.TestCase#TestCase(String)
+ */
+ public SubInputStreamTestCase(String name) {
+ super(name);
+ }
+
+ /**
+ * Tests SubInputStream.
+ * @throws Exception if an error occurs
+ */
+ public void testMain() throws Exception {
+ //Initialize test data
+ byte[] data = new byte[256];
+ for (int i = 0; i < data.length; i++) {
+ data[i] = (byte)(i & 0xff);
+ }
+
+ int v, c;
+ byte[] buf;
+ String s;
+
+ SubInputStream subin = new SubInputStream(new ByteArrayInputStream(data), 10);
+ v = subin.read();
+ assertEquals(0, v);
+ v = subin.read();
+ assertEquals(1, v);
+
+ buf = new byte[4];
+ c = subin.read(buf);
+ assertEquals(4, c);
+ s = new String(buf, "US-ASCII");
+ assertEquals("\u0002\u0003\u0004\u0005", s);
+
+ Arrays.fill(buf, (byte)0);
+ c = subin.read(buf, 2, 2);
+ assertEquals(2, c);
+ s = new String(buf, "US-ASCII");
+ assertEquals("\u0000\u0000\u0006\u0007", s);
+
+ Arrays.fill(buf, (byte)0);
+ c = subin.read(buf);
+ assertEquals(2, c);
+ s = new String(buf, "US-ASCII");
+ assertEquals("\u0008\u0009\u0000\u0000", s);
+ }
+
+}
Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/SubInputStreamTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/SubInputStreamTestCase.java
------------------------------------------------------------------------------
svn:keywords = Id
---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org