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:
keinhaar
2015-12-20 12:42:36 +01:00
parent 0c71ba0763
commit 524f034bac
4 changed files with 205 additions and 34 deletions

View File

@ -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();
}
}