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 2006/08/05 23:35:51 UTC
svn commit: r429060 - in /jakarta/httpcomponents/httpnio/trunk/src:
java/org/apache/http/nio/ test/org/apache/http/nio/mockup/
Author: olegk
Date: Sat Aug 5 14:35:50 2006
New Revision: 429060
URL: http://svn.apache.org/viewvc?rev=429060&view=rev
Log:
Made NIO HTTP data receiver and transmitter thread-safe. Instances of both classes may be accessed by worker threads that cosume or produce data, while I/O operations are performed by the I/O selector thread
Modified:
jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataReceiver.java
jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataTransmitter.java
jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataReceiver.java
jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataTransmitter.java
jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java
jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java
Modified: jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataReceiver.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataReceiver.java?rev=429060&r1=429059&r2=429060&view=diff
==============================================================================
--- jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataReceiver.java (original)
+++ jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataReceiver.java Sat Aug 5 14:35:50 2006
@@ -41,6 +41,7 @@
import org.apache.http.io.HttpDataReceiver;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.HTTP;
/**
* <p>
@@ -53,9 +54,6 @@
*/
public abstract class NIOHttpDataReceiver implements HttpDataReceiver {
- private static final int CR = 13;
- private static final int LF = 10;
-
private ByteBuffer buffer = null;
private Charset charset = null;
@@ -70,7 +68,7 @@
this.chbuffer = CharBuffer.allocate(1024);
}
- public void reset(final HttpParams params) {
+ public synchronized void reset(final HttpParams params) {
this.charset = Charset.forName(HttpProtocolParams.getHttpElementCharset(params));
this.chardecoder = createCharDecoder();
}
@@ -82,26 +80,26 @@
return chardecoder;
}
- protected abstract int readFromChannel(ByteBuffer dst) throws IOException;
-
- protected int fillBuffer() throws IOException {
+ private int doFillBuffer() throws IOException {
this.buffer.compact();
- int i = readFromChannel(this.buffer);
+ int i = fillBuffer(this.buffer);
this.buffer.flip();
return i;
}
- protected boolean hasDataInBuffer() {
+ protected abstract int fillBuffer(ByteBuffer dst) throws IOException;
+
+ protected synchronized boolean hasDataInBuffer() {
return this.buffer.hasRemaining();
}
- public int read(final byte[] b, int off, int len) throws IOException {
+ public synchronized int read(final byte[] b, int off, int len) throws IOException {
if (b == null) {
return 0;
}
int noRead = 0;
if (!this.buffer.hasRemaining()) {
- noRead = fillBuffer();
+ noRead = doFillBuffer();
if (noRead == -1) {
return -1;
}
@@ -114,17 +112,17 @@
return chunk;
}
- public int read(final byte[] b) throws IOException {
+ public synchronized int read(final byte[] b) throws IOException {
if (b == null) {
return 0;
}
return read(b, 0, b.length);
}
- public int read() throws IOException {
+ public synchronized int read() throws IOException {
int noRead = 0;
if (!this.buffer.hasRemaining()) {
- noRead = fillBuffer();
+ noRead = doFillBuffer();
if (noRead == -1) {
return -1;
}
@@ -139,14 +137,14 @@
private int locateLF() {
for (int i = this.buffer.position(); i < this.buffer.limit(); i++) {
int b = this.buffer.get(i);
- if (b == LF) {
+ if (b == HTTP.LF) {
return i;
}
}
return -1;
}
- public int readLine(final CharArrayBuffer charbuffer) throws IOException {
+ public synchronized int readLine(final CharArrayBuffer charbuffer) throws IOException {
if (charbuffer == null) {
throw new IllegalArgumentException("Char array buffer may not be null");
}
@@ -184,7 +182,7 @@
this.chardecoder.decode(this.buffer, this.chbuffer, false);
}
// discard the decoded content
- noRead = fillBuffer();
+ noRead = doFillBuffer();
if (noRead == -1) {
retry = false;
// terminate the decoding process
@@ -214,13 +212,13 @@
// discard LF if found
int l = charbuffer.length();
if (l > 0) {
- if (charbuffer.charAt(l - 1) == LF) {
+ if (charbuffer.charAt(l - 1) == HTTP.LF) {
l--;
charbuffer.setLength(l);
}
// discard CR if found
if (l > 0) {
- if (charbuffer.charAt(l - 1) == CR) {
+ if (charbuffer.charAt(l - 1) == HTTP.CR) {
l--;
charbuffer.setLength(l);
}
Modified: jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataTransmitter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataTransmitter.java?rev=429060&r1=429059&r2=429060&view=diff
==============================================================================
--- jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataTransmitter.java (original)
+++ jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOHttpDataTransmitter.java Sat Aug 5 14:35:50 2006
@@ -41,6 +41,7 @@
import org.apache.http.io.HttpDataTransmitter;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.HTTP;
/**
* <p>
@@ -53,9 +54,7 @@
*/
public abstract class NIOHttpDataTransmitter implements HttpDataTransmitter {
- private static final int CR = 13;
- private static final int LF = 10;
- private static final byte[] CRLF = new byte[] {CR, LF};
+ private static final byte[] CRLF = new byte[] {HTTP.CR, HTTP.LF};
private ByteBuffer buffer = null;
@@ -70,23 +69,23 @@
this.chbuffer = CharBuffer.allocate(1024);
}
- public void reset(final HttpParams params) {
+ public synchronized void reset(final HttpParams params) {
this.charset = Charset.forName(HttpProtocolParams.getHttpElementCharset(params));
this.charencoder = createCharEncoder();
}
- protected abstract void writeToChannel(ByteBuffer src) throws IOException;
-
- protected void flushBuffer() throws IOException {
+ private void doFlushBuffer() throws IOException {
this.buffer.flip();
- writeToChannel(this.buffer);
+ flushBuffer(this.buffer);
this.buffer.compact();
}
- public void flush() throws IOException {
+ protected abstract void flushBuffer(ByteBuffer src) throws IOException;
+
+ public synchronized void flush() throws IOException {
this.buffer.flip();
while (this.buffer.hasRemaining()) {
- writeToChannel(this.buffer);
+ flushBuffer(this.buffer);
}
this.buffer.clear();
}
@@ -98,21 +97,7 @@
return charencoder;
}
- private void write(
- final CharsetEncoder charencoder,
- final CharBuffer charbuffer,
- boolean endOfInput) throws IOException {
- boolean retry = true;
- while (retry) {
- CoderResult result = charencoder.encode(charbuffer, this.buffer, endOfInput);
- if (result.isOverflow()) {
- flushBuffer();
- }
- retry = !result.isUnderflow();
- }
- }
-
- public void write(final byte[] b, int off, int len) throws IOException {
+ public synchronized void write(final byte[] b, int off, int len) throws IOException {
if (b == null) {
return;
}
@@ -130,7 +115,7 @@
off += chunk;
remaining -= chunk;
} else {
- flushBuffer();
+ doFlushBuffer();
}
}
}
@@ -146,25 +131,14 @@
write(CRLF);
}
- public void write(int b) throws IOException {
+ public synchronized void write(int b) throws IOException {
if (!this.buffer.hasRemaining()) {
- flushBuffer();
+ doFlushBuffer();
}
this.buffer.put((byte)b);
}
- private void flushCharEncoder(final CharsetEncoder charencoder) throws IOException {
- boolean retry = true;
- while (retry) {
- CoderResult result = charencoder.flush(this.buffer);
- if (result.isOverflow()) {
- flushBuffer();
- }
- retry = !result.isUnderflow();
- }
- }
-
- public void writeLine(final CharArrayBuffer buffer) throws IOException {
+ public synchronized void writeLine(final CharArrayBuffer buffer) throws IOException {
if (buffer == null) {
return;
}
@@ -184,13 +158,29 @@
}
this.chbuffer.put(buffer.buffer(), offset, offset + l);
this.chbuffer.flip();
- write(this.charencoder, this.chbuffer, eol);
+
+ boolean retry = true;
+ while (retry) {
+ CoderResult result = this.charencoder.encode(this.chbuffer, this.buffer, eol);
+ if (result.isOverflow()) {
+ doFlushBuffer();
+ }
+ retry = !result.isUnderflow();
+ }
+
this.chbuffer.compact();
offset += l;
remaining -= l;
}
// flush the encoder
- flushCharEncoder(this.charencoder);
+ boolean retry = true;
+ while (retry) {
+ CoderResult result = this.charencoder.flush(this.buffer);
+ if (result.isOverflow()) {
+ doFlushBuffer();
+ }
+ retry = !result.isUnderflow();
+ }
}
writeCRLF();
}
Modified: jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataReceiver.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataReceiver.java?rev=429060&r1=429059&r2=429060&view=diff
==============================================================================
--- jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataReceiver.java (original)
+++ jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataReceiver.java Sat Aug 5 14:35:50 2006
@@ -75,7 +75,7 @@
super.reset(params);
}
- protected int readFromChannel(final ByteBuffer dst) throws IOException {
+ protected int fillBuffer(final ByteBuffer dst) throws IOException {
if (dst == null) {
throw new IllegalArgumentException("Byte buffer may not be null");
}
Modified: jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataTransmitter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataTransmitter.java?rev=429060&r1=429059&r2=429060&view=diff
==============================================================================
--- jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataTransmitter.java (original)
+++ jakarta/httpcomponents/httpnio/trunk/src/java/org/apache/http/nio/NIOSocketHttpDataTransmitter.java Sat Aug 5 14:35:50 2006
@@ -66,7 +66,7 @@
initBuffer(buffersize);
}
- protected void writeToChannel(final ByteBuffer src) throws IOException {
+ protected void flushBuffer(final ByteBuffer src) throws IOException {
this.channel.write(src);
}
Modified: jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java?rev=429060&r1=429059&r2=429060&view=diff
==============================================================================
--- jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java (original)
+++ jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java Sat Aug 5 14:35:50 2006
@@ -50,7 +50,7 @@
}
- protected int readFromChannel(final ByteBuffer dst) throws IOException {
+ protected int fillBuffer(final ByteBuffer dst) throws IOException {
if (dst == null) {
throw new IllegalArgumentException("Byte buffer may not be null");
}
Modified: jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java?rev=429060&r1=429059&r2=429060&view=diff
==============================================================================
--- jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java (original)
+++ jakarta/httpcomponents/httpnio/trunk/src/test/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java Sat Aug 5 14:35:50 2006
@@ -36,7 +36,7 @@
initBuffer(BUFFER_SIZE);
}
- protected void writeToChannel(final ByteBuffer src) throws IOException {
+ protected void flushBuffer(final ByteBuffer src) throws IOException {
this.channel.write(src);
}