You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/04/02 19:18:40 UTC
svn commit: r159778 - in jakarta/httpclient/trunk/http-common/src:
java/org/apache/http/io/ test/org/apache/http/io/
Author: olegk
Date: Sat Apr 2 09:18:39 2005
New Revision: 159778
URL: http://svn.apache.org/viewcvs?view=rev&rev=159778
Log:
Added InputStreamHttpDataReceiver and OutputStreamHttpDataTransmitter wrapper classes (provided for compatibility with old the IO model)
Added:
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java (with props)
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java (with props)
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java (with props)
Modified:
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java
Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java?view=diff&r1=159777&r2=159778
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java Sat Apr 2 09:18:39 2005
@@ -35,12 +35,9 @@
import org.apache.http.Header;
import org.apache.http.HttpException;
-import org.apache.http.params.HttpParams;
-import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EncodingUtil;
import org.apache.http.util.ExceptionUtil;
import org.apache.http.util.HeadersParser;
-import org.apache.http.util.HttpLineParser;
/**
* <p>Transparently coalesces chunks of a HTTP stream that uses
@@ -85,7 +82,7 @@
private Header[] footers = new Header[] {};
- public ChunkedInputStream(final HttpDataReceiver in) throws IOException {
+ public ChunkedInputStream(final HttpDataReceiver in) {
super();
if (in == null) {
throw new IllegalArgumentException("InputStream parameter may not be null");
@@ -343,43 +340,4 @@
}
}
- static class InputStreamHttpDataReceiver implements HttpDataReceiver {
-
- private final InputStream instream;
-
- private String charset = "US-ASCII";
-
- public InputStreamHttpDataReceiver(final InputStream instream) {
- super();
- if (instream == null) {
- throw new IllegalArgumentException("Input stream may not be null");
- }
- this.instream = instream;
- }
-
- public boolean isDataAvailable(int timeout) throws IOException {
- return this.instream.available() > 0;
- }
-
- public int read() throws IOException {
- return this.instream.read();
- }
-
- public int read(final byte[] b, int off, int len) throws IOException {
- return this.instream.read(b, off, len);
- }
-
- public int read(final byte[] b) throws IOException {
- return this.instream.read(b);
- }
-
- public String readLine() throws IOException {
- return HttpLineParser.readLine(this.instream, this.charset);
- }
-
- public void reset(final HttpParams params) {
- HttpProtocolParams protocolParams = new HttpProtocolParams(params);
- this.charset = protocolParams.getHttpElementCharset();
- }
- }
}
Added: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java?view=auto&rev=159778
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java (added)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java Sat Apr 2 09:18:39 2005
@@ -0,0 +1,88 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * 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 implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * 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
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.util.HttpLineParser;
+
+/**
+ * <p>Old IO Compatibility wrapper</p>
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ */
+public class InputStreamHttpDataReceiver implements HttpDataReceiver {
+
+ private final InputStream instream;
+
+ private String charset = "US-ASCII";
+
+ public InputStreamHttpDataReceiver(final InputStream instream) {
+ super();
+ if (instream == null) {
+ throw new IllegalArgumentException("Input stream may not be null");
+ }
+ this.instream = instream;
+ }
+
+ public InputStream getInputStream() {
+ return this.instream;
+ }
+
+ public boolean isDataAvailable(int timeout) throws IOException {
+ return this.instream.available() > 0;
+ }
+
+ public int read() throws IOException {
+ return this.instream.read();
+ }
+
+ public int read(final byte[] b, int off, int len) throws IOException {
+ return this.instream.read(b, off, len);
+ }
+
+ public int read(final byte[] b) throws IOException {
+ return this.instream.read(b);
+ }
+
+ public String readLine() throws IOException {
+ return HttpLineParser.readLine(this.instream, this.charset);
+ }
+
+ public void reset(final HttpParams params) {
+ HttpProtocolParams protocolParams = new HttpProtocolParams(params);
+ this.charset = protocolParams.getHttpElementCharset();
+ }
+
+}
Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/InputStreamHttpDataReceiver.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java?view=auto&rev=159778
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java (added)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java Sat Apr 2 09:18:39 2005
@@ -0,0 +1,95 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * 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 implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * 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
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+
+/**
+ * <p>Old IO Compatibility wrapper</p>
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ */
+public class OutputStreamHttpDataTransmitter implements HttpDataTransmitter {
+
+ private static final int CR = 13;
+ private static final int LF = 10;
+ private static final byte[] CRLF = new byte[] {CR, LF};
+
+ private final OutputStream outstream;
+
+ private String charset = "US-ASCII";
+
+ public OutputStreamHttpDataTransmitter(final OutputStream outstream) {
+ super();
+ if (outstream == null) {
+ throw new IllegalArgumentException("Input stream may not be null");
+ }
+ this.outstream = outstream;
+ }
+
+ public OutputStream getOutputStream() {
+ return this.outstream;
+ }
+
+ public void flush() throws IOException {
+ this.outstream.flush();
+ }
+
+ public void write(final byte[] b, int off, int len) throws IOException {
+ this.outstream.write(b, off, len);
+ }
+
+ public void write(final byte[] b) throws IOException {
+ this.outstream.write(b);
+ }
+
+ public void write(int b) throws IOException {
+ this.outstream.write(b);
+ }
+
+ public void writeLine(final String s) throws IOException {
+ if (s == null) {
+ return;
+ }
+ this.outstream.write(s.getBytes(this.charset));
+ this.outstream.write(CRLF);
+ }
+
+ public void reset(final HttpParams params) {
+ HttpProtocolParams protocolParams = new HttpProtocolParams(params);
+ this.charset = protocolParams.getHttpElementCharset();
+ }
+
+}
Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/OutputStreamHttpDataTransmitter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java?view=diff&r1=159777&r2=159778
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java Sat Apr 2 09:18:39 2005
@@ -41,6 +41,7 @@
suite.addTest(TestHttpDataInputStream.suite());
suite.addTest(TestHttpDataOutputStream.suite());
suite.addTest(TestChunkCoding.suite());
+ suite.addTest(TestOldIOHttpDataReceiverAndTransmitter.suite());
return suite;
}
Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java?view=diff&r1=159777&r2=159778
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java Sat Apr 2 09:18:39 2005
@@ -35,7 +35,6 @@
import java.io.OutputStream;
import org.apache.http.Header;
-import org.apache.http.impl.DefaultHttpParams;
import org.apache.http.util.EncodingUtil;
import junit.framework.Test;
@@ -273,27 +272,6 @@
}
assertEquals(0, out.size());
}
-
- public void testInputStreamHttpDataReceiver() throws IOException {
- String s = "aaaaa";
- InputStream in = new ByteArrayInputStream(
- EncodingUtil.getBytes(s, CONTENT_CHARSET));
- ChunkedInputStream.InputStreamHttpDataReceiver datareceiver =
- new ChunkedInputStream.InputStreamHttpDataReceiver(in);
- assertTrue(datareceiver.isDataAvailable(1));
- assertEquals('a', datareceiver.read());
- byte[] tmp = new byte[2];
- datareceiver.read(tmp);
- assertEquals('a', tmp[0]);
- assertEquals('a', tmp[1]);
- datareceiver.read(tmp, 0, tmp.length);
- assertEquals('a', tmp[0]);
- assertEquals('a', tmp[1]);
- assertEquals(-1, datareceiver.read());
- datareceiver.reset(new DefaultHttpParams(null));
- }
-
-
public void testContentLengthInputStream() throws IOException {
String correct = "1234567890123456";
Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java?view=auto&rev=159778
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java (added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java Sat Apr 2 09:18:39 2005
@@ -0,0 +1,112 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ * 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 implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * 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
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.io;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.http.impl.DefaultHttpParams;
+import org.apache.http.util.EncodingUtil;
+
+public class TestOldIOHttpDataReceiverAndTransmitter extends TestCase {
+
+ public TestOldIOHttpDataReceiverAndTransmitter(String testName) {
+ super(testName);
+ }
+
+ // ------------------------------------------------------- TestCase Methods
+
+ public static Test suite() {
+ return new TestSuite(TestOldIOHttpDataReceiverAndTransmitter.class);
+ }
+
+ // ------------------------------------------------------------------- Main
+ public static void main(String args[]) {
+ String[] testCaseName = { TestOldIOHttpDataReceiverAndTransmitter.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ public void testConstructors() {
+ String s = "aaaaa";
+ InputStream in = new ByteArrayInputStream(EncodingUtil.getAsciiBytes(s));
+ new InputStreamHttpDataReceiver(in);
+ try {
+ new InputStreamHttpDataReceiver(null);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ new OutputStreamHttpDataTransmitter(new ByteArrayOutputStream());
+ try {
+ new OutputStreamHttpDataTransmitter(null);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testInputStreamHttpDataReceiver() throws IOException {
+ String s = "aaaaa";
+ InputStream in = new ByteArrayInputStream(EncodingUtil.getAsciiBytes(s));
+ HttpDataReceiver datareceiver = new InputStreamHttpDataReceiver(in);
+ assertTrue(datareceiver.isDataAvailable(1));
+ assertEquals('a', datareceiver.read());
+ byte[] tmp = new byte[2];
+ datareceiver.read(tmp);
+ assertEquals('a', tmp[0]);
+ assertEquals('a', tmp[1]);
+ datareceiver.read(tmp, 0, tmp.length);
+ assertEquals('a', tmp[0]);
+ assertEquals('a', tmp[1]);
+ assertEquals(-1, datareceiver.read());
+ datareceiver.reset(new DefaultHttpParams(null));
+ }
+
+ public void testOutputStreamHttpDataTransmitter() throws IOException {
+ String s = "aaaaa\r\n";
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ HttpDataTransmitter datatransmitter = new OutputStreamHttpDataTransmitter(out);
+ datatransmitter.reset(new DefaultHttpParams(null));
+
+ datatransmitter.write('a');
+ datatransmitter.write(new byte[] {'a'});
+ datatransmitter.write(new byte[] {'a'}, 0, 1);
+ datatransmitter.writeLine("aa");
+ datatransmitter.flush();
+
+ assertEquals(s, out.toString("US-ASCII"));
+ }
+}
+
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain