Index: src/test/org/apache/commons/httpclient/TestStreams.java =================================================================== --- src/test/org/apache/commons/httpclient/TestStreams.java (revision 505859) +++ src/test/org/apache/commons/httpclient/TestStreams.java (working copy) @@ -270,5 +270,52 @@ String[] testCaseName = { TestStreams.class.getName() }; junit.textui.TestRunner.main(testCaseName); } + + + public void testAutoCloseInputStream() throws IOException { + // The purpose of this test is to check EOF handling of ACIS with + // respect to exceptions being thrown. Putting it on top of a + // plain ByteArrayInputStream won't do, since BAIS can't be closed. + ByteArrayInputStream bais = + new ByteArrayInputStream("whatever".getBytes()); + InputStream fbais = new java.io.FilterInputStream(bais) { + private boolean closed = false; + public void close() throws IOException { + closed = true; + super.close(); + } + public int available() throws IOException { + if (closed) + throw new IOException("closed"); + return super.available(); + } + }; + + AutoCloseInputStream acis = new AutoCloseInputStream(fbais, null); + byte[] data = new byte[16]; + int count = 0; + while (count >= 0) { + count = acis.read(data); + } + // We're at EOF. The underlying stream should be closed, + // but the ACIS itself not. + try { + fbais.available(); + fail("underlying stream not auto-closed"); + } catch (IOException x) { + // expected, pis should be closed + } + + // don't want to see an exception being thrown here + acis.available(); + + acis.close(); + try { + acis.available(); + fail("auto-close stream not closed"); + } catch (IOException x) { + // expected, acis should be closed + } + } } Index: src/java/org/apache/commons/httpclient/AutoCloseInputStream.java =================================================================== --- src/java/org/apache/commons/httpclient/AutoCloseInputStream.java (revision 505859) +++ src/java/org/apache/commons/httpclient/AutoCloseInputStream.java (working copy) @@ -131,6 +131,23 @@ } /** + * Obtains the number of bytes that can be read without blocking. + * + * @return the number of bytes available without blocking + * @throws IOException in case of a problem + */ + public int available() throws IOException { + int a = 0; // not -1 + + if (isReadAllowed()) { + a = super.available(); + // no checkClose() here, available() can't trigger EOF + } + + return a; + } + + /** * Close the stream, and also close the underlying stream if it is not * already closed. * @throws IOException If an IO problem occurs.