Return-Path: Delivered-To: apmail-jakarta-httpclient-commits-archive@www.apache.org Received: (qmail 53791 invoked from network); 4 Apr 2005 18:50:10 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 4 Apr 2005 18:50:10 -0000 Received: (qmail 11613 invoked by uid 500); 4 Apr 2005 18:50:09 -0000 Mailing-List: contact httpclient-commits-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: httpclient-dev@jakarta.apache.org Delivered-To: mailing list httpclient-commits@jakarta.apache.org Received: (qmail 11599 invoked by uid 500); 4 Apr 2005 18:50:09 -0000 Delivered-To: apmail-jakarta-httpclient-cvs@jakarta.apache.org Received: (qmail 11596 invoked by uid 99); 4 Apr 2005 18:50:09 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Mon, 04 Apr 2005 11:50:09 -0700 Received: (qmail 53778 invoked by uid 65534); 4 Apr 2005 18:50:08 -0000 Message-ID: <20050404185008.53777.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Mailer: svnmailer-1.0.0-dev Date: Mon, 04 Apr 2005 18:50:07 -0000 Subject: svn commit: r160082 - in jakarta/httpclient/trunk/http-common/src: java/org/apache/http/impl/AutoCloseInputStream.java test/org/apache/http/impl/TestAutoCloseInputStream.java To: httpclient-cvs@jakarta.apache.org From: olegk@apache.org X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: olegk Date: Mon Apr 4 11:50:07 2005 New Revision: 160082 URL: http://svn.apache.org/viewcvs?view=3Drev&rev=3D160082 Log: Simplified implementation + 100% test coverage Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/Test= AutoCloseInputStream.java (with props) Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/Auto= CloseInputStream.java Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/imp= l/AutoCloseInputStream.java URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src= /java/org/apache/http/impl/AutoCloseInputStream.java?view=3Ddiff&r1=3D16008= 1&r2=3D160082 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/Auto= CloseInputStream.java (original) +++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/Auto= CloseInputStream.java Mon Apr 4 11:50:07 2005 @@ -46,20 +46,11 @@ class AutoCloseInputStream extends FilterInputStream { =20 /**=20 - * True if this stream is open. Assume that the underlying stream=20 - * is open until we get an EOF indication. - */ - private boolean streamOpen =3D true; - - /** True if the stream closed itself. */ - private boolean selfClosed =3D false; - - /**=20 * The watcher is notified when the contents of the stream have * been exhausted */=20 private ResponseConsumedWatcher watcher =3D null; - + private boolean watcherNotified =3D false; /** * Create a new auto closing stream for the provided connection * @@ -80,14 +71,8 @@ * @return the character read, or -1 for EOF */ public int read() throws IOException { - int l =3D -1; - - if (isReadAllowed()) { - // underlying stream not closed, go ahead and read. - l =3D super.read(); - checkClose(l); - } - + int l =3D super.read(); + checkEndOfStream(l); return l; } =20 @@ -101,13 +86,8 @@ * @throws IOException if there are errors reading */ public int read(byte[] b, int off, int len) throws IOException { - int l =3D -1; - - if (isReadAllowed()) { - l =3D super.read(b, off, len); - checkClose(l); - } - + int l =3D super.read(b, off, len); + checkEndOfStream(l); return l; } =20 @@ -120,12 +100,8 @@ * @throws IOException if there are errors reading */ public int read(byte[] b) throws IOException { - int l =3D -1; - - if (isReadAllowed()) { - l =3D super.read(b); - checkClose(l); - } + int l =3D super.read(b); + checkEndOfStream(l); return l; } =20 @@ -135,10 +111,8 @@ * @throws IOException If an IO problem occurs. */ public void close() throws IOException { - if (!selfClosed) { - selfClosed =3D true; - notifyWatcher(); - } + super.close(); + ensureWatcherNotified(); } =20 /** @@ -147,35 +121,19 @@ * @param readResult The result of the read operation to check. * @throws IOException If an IO problem occurs. */ - private void checkClose(int readResult) throws IOException { + private void checkEndOfStream(int readResult) throws IOException { if (readResult =3D=3D -1) { - notifyWatcher(); - } - } - - /** - * See whether a read of the underlying stream should be allowed, and = if - * not, check to see whether our stream has already been closed! - * - * @return true if it is still OK to read from the stream. - * @throws IOException If an IO problem occurs. - */ - private boolean isReadAllowed() throws IOException { - if (!streamOpen && selfClosed) { - throw new IOException("Attempted read on closed stream."); + close(); } - return streamOpen; } =20 /** * Notify the watcher that the contents have been consumed. * @throws IOException If an IO problem occurs. */ - private void notifyWatcher() throws IOException { - if (streamOpen) { - super.close(); - streamOpen =3D false; - + private void ensureWatcherNotified() throws IOException { + if (!this.watcherNotified) { + this.watcherNotified =3D true; if (watcher !=3D null) { watcher.responseConsumed(); } Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/T= estAutoCloseInputStream.java URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src= /test/org/apache/http/impl/TestAutoCloseInputStream.java?view=3Dauto&rev=3D= 160082 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/Test= AutoCloseInputStream.java (added) +++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/Test= AutoCloseInputStream.java Mon Apr 4 11:50:07 2005 @@ -0,0 +1,113 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + * + * Copyright 2002-2004 The Apache Software Foundation + * + * Licensed 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 implie= d=2E + * See the License for the specific language governing permissions and + * limitations under the License. + * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + * + * 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.http.impl; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class TestAutoCloseInputStream extends TestCase { + + public TestAutoCloseInputStream(String testName) { + super(testName); + } + + // ------------------------------------------------------- TestCase Me= thods + + public static Test suite() { + return new TestSuite(TestAutoCloseInputStream.class); + } + + // -------------------------------------------------------------------= Main + public static void main(String args[]) { + String[] testCaseName =3D { TestAutoCloseInputStream.class.getName= () }; + junit.textui.TestRunner.main(testCaseName); + } + + class TestResponseConsumedWatcher implements ResponseConsumedWatcher { + =20 + private int count; + =20 + public TestResponseConsumedWatcher() { + super(); + count =3D 0; + } + =20 + public void responseConsumed() { + count++; + } + =20 + public int getCount() { + return this.count; + } + }; + =20 + =20 + public void testConstructors() throws Exception { + InputStream instream =3D new ByteArrayInputStream(new byte[] {}); + ResponseConsumedWatcher watcher =3D new TestResponseConsumedWatche= r(); + new AutoCloseInputStream(instream, watcher); + new AutoCloseInputStream(instream, null); + } + + public void testBasics() throws IOException { + byte[] input =3D "0123456789ABCDEF".getBytes("US-ASCII"); + InputStream instream =3D new ByteArrayInputStream(input); + TestResponseConsumedWatcher watcher =3D new TestResponseConsumedWa= tcher(); + instream =3D new AutoCloseInputStream(instream, watcher); + byte[] tmp =3D new byte[input.length]; + int ch =3D instream.read(); + assertTrue(ch !=3D -1); + tmp[0] =3D (byte)ch; + assertTrue(instream.read(tmp, 1, tmp.length - 1) !=3D -1); + assertTrue(instream.read(tmp) =3D=3D -1); + for (int i =3D 0; i < input.length; i++) { + assertEquals(input[i], tmp[i]); + } =20 + assertTrue(instream.read() =3D=3D -1); + instream.close(); + instream.close(); + // Has been triggered once only + assertEquals(1, watcher.getCount()); + } + + public void testNullWatcher() throws IOException { + byte[] input =3D "0".getBytes("US-ASCII"); + InputStream instream =3D new ByteArrayInputStream(input); + instream =3D new AutoCloseInputStream(instream, null); + assertTrue(instream.read() !=3D -1); + assertTrue(instream.read() =3D=3D -1); + assertTrue(instream.read() =3D=3D -1); + } +} + Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/i= mpl/TestAutoCloseInputStream.java ---------------------------------------------------------------------------= --- svn:eol-style =3D native Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/i= mpl/TestAutoCloseInputStream.java ---------------------------------------------------------------------------= --- svn:keywords =3D Date Author Id Revision HeadURL Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/i= mpl/TestAutoCloseInputStream.java ---------------------------------------------------------------------------= --- svn:mime-type =3D text/plain