mirror of
https://github.com/corda/corda.git
synced 2025-06-13 12:48:18 +00:00
Added support for HTTP URL connections, and fixed SocketInputStream and BufferedInputStream.
Did some cleanup as proposed by the main developers. - Bigger HTTP Header Buffer - Exception if Header is anyway exceeded. - Linebreaks on HTTP Request fixed to standard. - Only stop header reading on \r\n\r\n and no longer on \n\n\n\n - Simplyfied the code to stop if buffer could not be filled. - Handle special case if buffer has length 0, like specified in the Java API - Socket will no longer fill the buffer completely
This commit is contained in:
@ -7,9 +7,11 @@
|
||||
|
||||
There is NO WARRANTY for this software. See license.txt for
|
||||
details. */
|
||||
|
||||
package java.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class BufferedInputStream extends InputStream {
|
||||
private final InputStream in;
|
||||
private final byte[] buffer;
|
||||
@ -25,17 +27,16 @@ public class BufferedInputStream extends InputStream {
|
||||
this(in, 4096);
|
||||
}
|
||||
|
||||
private void fill() throws IOException {
|
||||
private int fill() throws IOException {
|
||||
position = 0;
|
||||
limit = in.read(buffer);
|
||||
|
||||
return limit;
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
if (position >= limit) {
|
||||
fill();
|
||||
if (limit == -1) {
|
||||
return -1;
|
||||
}
|
||||
if (position >= limit && fill() == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return buffer[position++] & 0xFF;
|
||||
@ -43,7 +44,9 @@ public class BufferedInputStream extends InputStream {
|
||||
|
||||
public int read(byte[] b, int offset, int length) throws IOException {
|
||||
int count = 0;
|
||||
|
||||
if (position >= limit && fill() == -1) {
|
||||
return -1;
|
||||
}
|
||||
if (position < limit) {
|
||||
int remaining = limit - position;
|
||||
if (remaining > length) {
|
||||
@ -57,8 +60,8 @@ public class BufferedInputStream extends InputStream {
|
||||
offset += remaining;
|
||||
length -= remaining;
|
||||
}
|
||||
|
||||
while (length > 0) {
|
||||
while (length > 0 && in.available() > 0)
|
||||
{
|
||||
int c = in.read(b, offset, length);
|
||||
if (c == -1) {
|
||||
if (count == 0) {
|
||||
@ -69,13 +72,8 @@ public class BufferedInputStream extends InputStream {
|
||||
offset += c;
|
||||
count += c;
|
||||
length -= c;
|
||||
|
||||
if (in.available() <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
@ -87,3 +85,4 @@ public class BufferedInputStream extends InputStream {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,18 +86,16 @@ public class Socket implements Closeable, AutoCloseable {
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer) throws IOException {
|
||||
if(buffer.length == 0) return 0; //spec says return 0 if buffer length is zero.
|
||||
int fullSize = buffer.length;
|
||||
int index = 0;
|
||||
int size;
|
||||
do {
|
||||
size = recv(sock, buffer, index, Math.min(fullSize, Socket.BUFFER_SIZE));
|
||||
fullSize -= size;
|
||||
index += size;
|
||||
} while (fullSize != 0 && size != 0);
|
||||
return index;
|
||||
size = recv(sock, buffer, 0, Math.min(fullSize, Socket.BUFFER_SIZE));
|
||||
fullSize -= size;
|
||||
//removed loop, because otherwise interactive protocols will not work.
|
||||
if(size < 0) throw new IOException("Error while reading stream"); //as the manpage of recv says, a value below zero indicates an error.
|
||||
if(size == 0) return -1; // if the stream is closed (size == 0), then return -1 to indicate end of stream.
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private class SocketOutputStream extends OutputStream {
|
||||
|
Reference in New Issue
Block a user