*) Adding additional functions to serverByteBuffer so that it

can be used instead of a ByteArrayOutputStream
*) Using a serverByteBuffer for lineBuffering in class httpc
   instead of a ByteArrayOutputStream

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@35 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
(no author) 20 years ago
parent 432e01910b
commit 942914ffd2

@ -87,8 +87,8 @@ public final class httpc {
private long handle; private long handle;
// output and input streams for client control connection // output and input streams for client control connection
public PushbackInputStream clientInput = null; PushbackInputStream clientInput = null;
public OutputStream clientOutput = null; OutputStream clientOutput = null;
private boolean remoteProxyUse = false; private boolean remoteProxyUse = false;
private String savedRemoteHost = null; private String savedRemoteHost = null;
@ -104,8 +104,19 @@ public final class httpc {
java.security.Security.setProperty("networkaddress.cache.negative.ttl" , "0"); java.security.Security.setProperty("networkaddress.cache.negative.ttl" , "0");
} }
/**
* A Object Pool containing all pooled httpc-objects.
* @see httpcPool
*/
private static final httpcPool theHttpcPool; private static final httpcPool theHttpcPool;
/**
* Indicates if the current object was removed from pool because the maximum limit
* was exceeded.
*/
boolean removedFromPool = false; boolean removedFromPool = false;
// Configuring the httpc object pool
static { static {
// implementation of session thread pool // implementation of session thread pool
GenericObjectPool.Config config = new GenericObjectPool.Config(); GenericObjectPool.Config config = new GenericObjectPool.Config();
@ -125,16 +136,25 @@ public final class httpc {
theHttpcPool = new httpcPool(new httpcFactory(),config); theHttpcPool = new httpcPool(new httpcFactory(),config);
} }
private static final ByteArrayOutputStream readLineBuffer = new ByteArrayOutputStream(); /**
* A reusable readline buffer
* @see serverByteBuffer
*/
final serverByteBuffer readLineBuffer = new serverByteBuffer();
public static httpc getInstance(String server, int port, int timeout, boolean ssl, public static httpc getInstance(
String remoteProxyHost, int remoteProxyPort) throws IOException { String server,
int port,
int timeout,
boolean ssl,
String remoteProxyHost,
int remoteProxyPort
) throws IOException {
httpc newHttpc; httpc newHttpc;
try { try {
// fetching a new httpc from the object pool // fetching a new httpc from the object pool
newHttpc = (httpc) httpc.theHttpcPool.borrowObject(); newHttpc = (httpc) httpc.theHttpcPool.borrowObject();
} catch (Exception e) { } catch (Exception e) {
throw new IOException("Unable to initialize a new httpc. " + e.getMessage()); throw new IOException("Unable to initialize a new httpc. " + e.getMessage());
} }

@ -643,22 +643,23 @@ public final class httpd implements serverHandler {
} }
private static String parseArg(String s) { private static String parseArg(String s) {
// this parses a given value-string from a http property // this parses a given value-string from a http property
// we replace all "+" by spaces // we replace all "+" by spaces
// and resolve %-escapes with two-digit hex attributes // and resolve %-escapes with two-digit hex attributes
int pos = 0; int pos = 0;
String result = ""; StringBuffer result = new StringBuffer();
while (pos < s.length()) { while (pos < s.length()) {
if (s.charAt(pos) == '+') { if (s.charAt(pos) == '+') {
result += " "; pos++; result.append(" ");
} else if (s.charAt(pos) == '%') { pos++;
result += (char) Integer.parseInt(s.substring(pos + 1, pos + 3), 16); } else if (s.charAt(pos) == '%') {
pos += 3; result.append((char) Integer.parseInt(s.substring(pos + 1, pos + 3), 16));
} else { pos += 3;
result += s.charAt(pos++); } else {
} result.append(s.charAt(pos++));
} }
return result; }
return result.toString();
} }

@ -59,6 +59,12 @@ public final class serverByteBuffer extends OutputStream {
offset = 0; offset = 0;
} }
public serverByteBuffer(int initLength) {
this.buffer = new byte[initLength];
this.length = 0;
this.offset = 0;
}
public serverByteBuffer(byte[] bb) { public serverByteBuffer(byte[] bb) {
buffer = bb; buffer = bb;
length = bb.length; length = bb.length;
@ -291,5 +297,16 @@ public final class serverByteBuffer extends OutputStream {
for (int i = 0; i < pattern.length; i++) if (buffer[offset + i] != pattern[i]) return false; for (int i = 0; i < pattern.length; i++) if (buffer[offset + i] != pattern[i]) return false;
return true; return true;
} }
public void reset() {
this.length = 0;
this.offset = 0;
}
public byte toByteArray()[] {
byte newbuf[] = new byte[this.length];
System.arraycopy(this.buffer, 0, newbuf, 0, this.length);
return newbuf;
}
} }

@ -504,7 +504,7 @@ public final class serverCore extends serverAbstractThread implements serverThre
public PushbackInputStream in; // on control input stream public PushbackInputStream in; // on control input stream
public OutputStream out; // on control output stream, autoflush public OutputStream out; // on control output stream, autoflush
private final ByteArrayOutputStream readLineBuffer = new ByteArrayOutputStream(256); private final serverByteBuffer readLineBuffer = new serverByteBuffer(256);
public Session(ThreadGroup theThreadGroup) { public Session(ThreadGroup theThreadGroup) {
super(theThreadGroup,"Session"); super(theThreadGroup,"Session");
@ -785,7 +785,7 @@ public final class serverCore extends serverAbstractThread implements serverThre
} }
public static byte[] receive(PushbackInputStream pbis, ByteArrayOutputStream readLineBuffer, long timeout, int maxSize, boolean logerr) { public static byte[] receive(PushbackInputStream pbis, serverByteBuffer readLineBuffer, long timeout, int maxSize, boolean logerr) {
// this is essentially a readln on a PushbackInputStream // this is essentially a readln on a PushbackInputStream
int bufferSize = 0; int bufferSize = 0;
@ -793,7 +793,7 @@ public final class serverCore extends serverAbstractThread implements serverThre
// reuse an existing linebuffer or create a new one ... // reuse an existing linebuffer or create a new one ...
if (readLineBuffer == null) { if (readLineBuffer == null) {
readLineBuffer = new ByteArrayOutputStream(256); readLineBuffer = new serverByteBuffer(256);
} else { } else {
readLineBuffer.reset(); readLineBuffer.reset();
} }

Loading…
Cancel
Save