You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2013/04/22 23:30:55 UTC
svn commit: r1470725 - in /commons/proper/io/trunk/src: changes/changes.xml
main/java/org/apache/commons/io/input/CharSequenceInputStream.java
test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
Author: sebb
Date: Mon Apr 22 21:30:55 2013
New Revision: 1470725
URL: http://svn.apache.org/r1470725
Log:
IO-379 CharSequenceInputStream - add tests for available()
Fix code so it really does reflect a minimum available.
Modified:
commons/proper/io/trunk/src/changes/changes.xml
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1470725&r1=1470724&r2=1470725&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Mon Apr 22 21:30:55 2013
@@ -47,6 +47,10 @@ The <action> type attribute can be add,u
<body>
<!-- The release date is the date RC is cut -->
<release version="2.5" date="2013-??-??" description="New features and bug fixes.">
+ <action issue="IO-379" dev="sebb" type="add">
+ CharSequenceInputStream - add tests for available()
+ Fix code so it really does reflect a minimum available.
+ </action>
<action issue="IO-328" dev="sebb" type="update">
getPrefixLength returns null if filename has leading slashes
Javadoc: add examples to show correct behaviour; add unit tests
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java?rev=1470725&r1=1470724&r2=1470725&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java Mon Apr 22 21:30:55 2013
@@ -187,9 +187,19 @@ public class CharSequenceInputStream ext
return skipped;
}
+ /**
+ * Return an estimate of the number of bytes remaining in the byte stream.
+ * @return the count of bytes that can be read without blocking (or returning EOF).
+ *
+ * @throws IOException if an error occurs (probably not possible)
+ */
@Override
public int available() throws IOException {
- return this.cbuf.remaining();
+ // The cached entries are in bbuf; since encoding always creates at least one byte
+ // per character, we can add the two to get a better estimate (e.g. if bbuf is empty)
+ // Note that the previous implementation (2.4) could return zero even though there were
+ // encoded bytes still available.
+ return this.bbuf.remaining() + this.cbuf.remaining();
}
@Override
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java?rev=1470725&r1=1470724&r2=1470725&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java Mon Apr 22 21:30:55 2013
@@ -379,4 +379,50 @@ public class CharSequenceInputStreamTest
public void testSkip_UTF8() throws Exception {
testSkip("UTF-8");
}
+
+ private int checkAvail(InputStream is, int min) throws Exception {
+ int available = is.available();
+ assertTrue("avail should be >= " + min + ", but was " + available, available >= min);
+ return available;
+ }
+
+ private void testAvailableSkip(final String csName) throws Exception {
+ final String input = "test";
+ final InputStream r = new CharSequenceInputStream(input, csName);
+ try {
+ int available = checkAvail(r, input.length());
+ assertEquals(available - 1, r.skip(available-1)); // skip all but one
+ available = checkAvail(r, 1);
+ assertEquals(1, r.skip(1));
+ available = checkAvail(r, 0);
+ } finally {
+ r.close();
+ }
+ }
+
+ private void testAvailableRead(final String csName) throws Exception {
+ final String input = "test";
+ final InputStream r = new CharSequenceInputStream(input, csName);
+ try {
+ int available = checkAvail(r, input.length());
+ byte buff[] = new byte[available];
+ assertEquals(available - 1, r.skip(available-1)); // skip all but one
+ available = checkAvail(r, 1);
+ buff = new byte[available];
+ assertEquals(available, r.read(buff, 0, available));
+ } finally {
+ r.close();
+ }
+ }
+
+ @Test
+ public void testAvailable() throws Exception {
+ for (final String csName : Charset.availableCharsets().keySet()) {
+ // prevent java.lang.UnsupportedOperationException at sun.nio.cs.ext.ISO2022_CN.newEncoder.
+ if (Charset.forName(csName).canEncode()) {
+ testAvailableSkip(csName);
+ testAvailableRead(csName);
+ }
+ }
+ }
}