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

@ -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 {