Reformatted classes, added Javadoc headers

This commit is contained in:
Kai S. K. Engelbart 2019-12-28 11:43:48 +02:00
parent 7e3a23e093
commit 58c8e0d98b
21 changed files with 922 additions and 1018 deletions

View File

@ -1,10 +1,13 @@
package com.jenkov.nioserver;
/**
* Created by jjenkov on 16-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>HttpUtilTest.java</strong><br>
* Created: <strong>16 Oct 2015</strong><br>
*
* @author jjenkov
*/
public interface IMessageProcessor {
public void process(Message message, WriteProxy writeProxy);
}

View File

@ -5,7 +5,11 @@ import java.nio.ByteBuffer;
import java.util.List;
/**
* Created by jjenkov on 16-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>HttpUtilTest.java</strong><br>
* Created: <strong>16 Oct 2015</strong><br>
*
* @author jjenkov
*/
public interface IMessageReader {
@ -14,7 +18,4 @@ public interface IMessageReader {
public void read(Socket socket, ByteBuffer byteBuffer) throws IOException;
public List<Message> getMessages();
}

View File

@ -1,10 +1,13 @@
package com.jenkov.nioserver;
/**
* Created by jjenkov on 16-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>HttpUtilTest.java</strong><br>
* Created: <strong>16 Oct 2015</strong><br>
*
* @author jjenkov
*/
public interface IMessageReaderFactory {
public IMessageReader createMessageReader();
}

View File

@ -3,27 +3,31 @@ package com.jenkov.nioserver;
import java.nio.ByteBuffer;
/**
* Created by jjenkov on 16-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>HttpUtilTest.java</strong><br>
* Created: <strong>16 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class Message {
private MessageBuffer messageBuffer = null;
private MessageBuffer messageBuffer;
public long socketId = 0; // the id of source socket or destination socket, depending on whether is going in or out.
public long socketId; // the id of source socket or destination socket, depending on whether is going
// in or out.
public byte[] sharedArray = null;
public int offset = 0; //offset into sharedArray where this message data starts.
public int capacity = 0; //the size of the section in the sharedArray allocated to this message.
public int length = 0; //the number of bytes used of the allocated section.
public byte[] sharedArray;
public int offset; // offset into sharedArray where this message data starts.
public int capacity; // the size of the section in the sharedArray allocated to this message.
public int length; // the number of bytes used of the allocated section.
public Object metaData = null;
public Object metaData;
public Message(MessageBuffer messageBuffer) {
this.messageBuffer = messageBuffer;
}
public Message(MessageBuffer messageBuffer) { this.messageBuffer = messageBuffer; }
/**
* Writes data from the ByteBuffer into this message - meaning into the buffer backing this message.
* Writes data from the ByteBuffer into this message - meaning into the buffer
* backing this message.
*
* @param byteBuffer The ByteBuffer containing the message data to write.
* @return
@ -31,35 +35,28 @@ public class Message {
public int writeToMessage(ByteBuffer byteBuffer) {
int remaining = byteBuffer.remaining();
while(this.length + remaining > capacity){
if(!this.messageBuffer.expandMessage(this)) {
return -1;
}
}
while (this.length + remaining > capacity)
if (!this.messageBuffer.expandMessage(this)) return -1;
int bytesToCopy = Math.min(remaining, this.capacity - this.length);
byteBuffer.get(this.sharedArray, this.offset + this.length, bytesToCopy);
this.length += bytesToCopy;
int bytesToCopy = Math.min(remaining, capacity - length);
byteBuffer.get(sharedArray, offset + length, bytesToCopy);
length += bytesToCopy;
return bytesToCopy;
}
/**
* Writes data from the byte array into this message - meaning into the buffer backing this message.
* Writes data from the byte array into this message - meaning into the buffer
* backing this message.
*
* @param byteArray The byte array containing the message data to write.
* @return
*/
public int writeToMessage(byte[] byteArray){
return writeToMessage(byteArray, 0, byteArray.length);
}
public int writeToMessage(byte[] byteArray) { return writeToMessage(byteArray, 0, byteArray.length); }
/**
* Writes data from the byte array into this message - meaning into the buffer backing this message.
* Writes data from the byte array into this message - meaning into the buffer
* backing this message.
*
* @param byteArray The byte array containing the message data to write.
* @return
@ -67,39 +64,31 @@ public class Message {
public int writeToMessage(byte[] byteArray, int offset, int length) {
int remaining = length;
while(this.length + remaining > capacity){
if(!this.messageBuffer.expandMessage(this)) {
return -1;
}
}
while (this.length + remaining > capacity)
if (!this.messageBuffer.expandMessage(this)) return -1;
int bytesToCopy = Math.min(remaining, this.capacity - this.length);
System.arraycopy(byteArray, offset, this.sharedArray, this.offset + this.length, bytesToCopy);
int bytesToCopy = Math.min(remaining, capacity - length);
System.arraycopy(byteArray, offset, sharedArray, offset + this.length, bytesToCopy);
this.length += bytesToCopy;
return bytesToCopy;
}
/**
* In case the buffer backing the nextMessage contains more than one HTTP message, move all data after the first
* In case the buffer backing the nextMessage contains more than one HTTP
* message, move all data after the first
* message to a new Message object.
*
* @param message The message containing the partial message (after the first message).
* @param endIndex The end index of the first message in the buffer of the message given as parameter.
* @param message The message containing the partial message (after the first
* message).
* @param endIndex The end index of the first message in the buffer of the
* message given as parameter.
*/
public void writePartialMessageToMessage(Message message, int endIndex) {
int startIndexOfPartialMessage = message.offset + endIndex;
int lengthOfPartialMessage = (message.offset + message.length) - endIndex;
int lengthOfPartialMessage = message.offset + message.length - endIndex;
System.arraycopy(message.sharedArray, startIndexOfPartialMessage, this.sharedArray, this.offset, lengthOfPartialMessage);
System.arraycopy(message.sharedArray, startIndexOfPartialMessage, sharedArray, offset, lengthOfPartialMessage);
}
public int writeToByteBuffer(ByteBuffer byteBuffer){
return 0;
}
public int writeToByteBuffer(ByteBuffer byteBuffer) { return 0; }
}

View File

@ -1,12 +1,16 @@
package com.jenkov.nioserver;
/**
* A shared buffer which can contain many messages inside. A message gets a section of the buffer to use. If the
* message outgrows the section in size, the message requests a larger section and the message is copied to that
* larger section. The smaller section is then freed again.
* A shared buffer which can contain many messages inside. A message gets a
* section of the buffer to use. If the message outgrows the section in size,
* the message requests a larger section and the message is copied to that
* larger section. The smaller section is then freed again.<br>
* <br>
* Project: <strong>java-nio-server</strong><br>
* File: <strong>MessageBuffer.java</strong><br>
* Created: <strong>18 Oct 2015</strong><br>
*
*
* Created by jjenkov on 18-10-2015.
* @author jjenkov
*/
public class MessageBuffer {
@ -15,7 +19,7 @@ public class MessageBuffer {
private static final int CAPACITY_SMALL = 4 * KB;
private static final int CAPACITY_MEDIUM = 128 * KB;
private static final int CAPACITY_LARGE = 1024 * KB;
private static final int CAPACITY_LARGE = 1 * MB;
// package scope (default) - so they can be accessed from unit tests.
byte[] smallMessageBuffer = new byte[1024 * 4 * KB]; // 1024 x 4KB messages = 4MB.
@ -26,20 +30,18 @@ public class MessageBuffer {
QueueIntFlip mediumMessageBufferFreeBlocks = new QueueIntFlip(128); // 128 free sections
QueueIntFlip largeMessageBufferFreeBlocks = new QueueIntFlip(16); // 16 free sections
//todo make all message buffer capacities and block sizes configurable
//todo calculate free block queue sizes based on capacity and block size of buffers.
// TODO: make all message buffer capacities and block sizes configurable
// TODO: calculate free block queue sizes based on capacity and block size of
// buffers.
public MessageBuffer() {
// add all free sections to all free section queues.
for(int i=0; i<smallMessageBuffer.length; i+= CAPACITY_SMALL){
this.smallMessageBufferFreeBlocks.put(i);
}
for(int i=0; i<mediumMessageBuffer.length; i+= CAPACITY_MEDIUM){
this.mediumMessageBufferFreeBlocks.put(i);
}
for(int i=0; i<largeMessageBuffer.length; i+= CAPACITY_LARGE){
this.largeMessageBufferFreeBlocks.put(i);
}
for (int i = 0; i < smallMessageBuffer.length; i += CAPACITY_SMALL)
smallMessageBufferFreeBlocks.put(i);
for (int i = 0; i < mediumMessageBuffer.length; i += CAPACITY_MEDIUM)
mediumMessageBufferFreeBlocks.put(i);
for (int i = 0; i < largeMessageBuffer.length; i += CAPACITY_LARGE)
largeMessageBufferFreeBlocks.put(i);
}
public Message getMessage() {
@ -47,7 +49,7 @@ public class MessageBuffer {
if (nextFreeSmallBlock == -1) return null;
Message message = new Message(this); //todo get from Message pool - caps memory usage.
Message message = new Message(this); // TODO: get from Message pool - caps memory usage.
message.sharedArray = this.smallMessageBuffer;
message.capacity = CAPACITY_SMALL;
@ -58,13 +60,11 @@ public class MessageBuffer {
}
public boolean expandMessage(Message message) {
if(message.capacity == CAPACITY_SMALL){
return moveMessage(message, this.smallMessageBufferFreeBlocks, this.mediumMessageBufferFreeBlocks, this.mediumMessageBuffer, CAPACITY_MEDIUM);
} else if(message.capacity == CAPACITY_MEDIUM){
return moveMessage(message, this.mediumMessageBufferFreeBlocks, this.largeMessageBufferFreeBlocks, this.largeMessageBuffer, CAPACITY_LARGE);
} else {
return false;
}
if (message.capacity == CAPACITY_SMALL)
return moveMessage(message, smallMessageBufferFreeBlocks, mediumMessageBufferFreeBlocks, mediumMessageBuffer, CAPACITY_MEDIUM);
else if (message.capacity == CAPACITY_MEDIUM)
return moveMessage(message, mediumMessageBufferFreeBlocks, largeMessageBufferFreeBlocks, largeMessageBuffer, CAPACITY_LARGE);
else return false;
}
private boolean moveMessage(Message message, QueueIntFlip srcBlockQueue, QueueIntFlip destBlockQueue, byte[] dest, int newCapacity) {
@ -80,9 +80,4 @@ public class MessageBuffer {
message.capacity = newCapacity;
return true;
}
}

View File

@ -6,44 +6,36 @@ import java.util.ArrayList;
import java.util.List;
/**
* Created by jjenkov on 21-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>MessageWriter.java</strong><br>
* Created: <strong>21 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class MessageWriter {
private List<Message> writeQueue = new ArrayList<>();
private Message messageInProgress = null;
private int bytesWritten = 0;
public MessageWriter() {
}
private Message messageInProgress;
private int bytesWritten;
public void enqueue(Message message) {
if(this.messageInProgress == null){
this.messageInProgress = message;
} else {
this.writeQueue.add(message);
}
if (messageInProgress == null) messageInProgress = message;
else writeQueue.add(message);
}
public void write(Socket socket, ByteBuffer byteBuffer) throws IOException {
byteBuffer.put(this.messageInProgress.sharedArray, this.messageInProgress.offset + this.bytesWritten, this.messageInProgress.length - this.bytesWritten);
byteBuffer.put(messageInProgress.sharedArray, messageInProgress.offset + bytesWritten, messageInProgress.length - bytesWritten);
byteBuffer.flip();
this.bytesWritten += socket.write(byteBuffer);
bytesWritten += socket.write(byteBuffer);
byteBuffer.clear();
if(bytesWritten >= this.messageInProgress.length){
if(this.writeQueue.size() > 0){
this.messageInProgress = this.writeQueue.remove(0);
} else {
this.messageInProgress = null;
//todo unregister from selector
}
if (bytesWritten >= messageInProgress.length) {
if (writeQueue.size() > 0) messageInProgress = writeQueue.remove(0);
else messageInProgress = null;
// TODO: unregister from selector
}
}
public boolean isEmpty() {
return this.writeQueue.isEmpty() && this.messageInProgress == null;
}
public boolean isEmpty() { return writeQueue.isEmpty() && messageInProgress == null; }
}

View File

@ -1,47 +1,43 @@
package com.jenkov.nioserver;
/**
* Same as QueueFillCount, except that QueueFlip uses a flip flag to keep track of when the internal writePos has
* "overflowed" (meaning it goes back to 0). Other than that, the two implementations are very similar in functionality.
* Same as QueueFillCount, except that QueueFlip uses a flip flag to keep track
* of when the internal writePos has "overflowed" (meaning it goes back to 0).
* Other than that, the two implementations are very similar in
* functionality.<br>
* <br>
* One additional difference is that QueueFlip has an available() method, where
* this is a public variable in QueueFillCount.<br>
* <br>
* Project: <strong>java-nio-server</strong><br>
* File: <strong>QueueIntFlip.java</strong><br>
* Created: <strong>18 Oct 2015</strong><br>
*
* One additional difference is that QueueFlip has an available() method, where this is a public variable in
* QueueFillCount.
*
* Created by jjenkov on 18-09-2015.
* @author jjenkov
*/
public class QueueIntFlip {
public int[] elements = null;
public int[] elements;
public int capacity = 0;
public int writePos = 0;
public int readPos = 0;
public boolean flipped = false;
public int capacity;
public int writePos;
public int readPos;
public boolean flipped;
public QueueIntFlip(int capacity) {
this.capacity = capacity;
this.elements = new int[capacity]; //todo get from TypeAllocator ?
elements = new int[capacity]; // TODO: get from TypeAllocator ?
}
public void reset() {
this.writePos = 0;
this.readPos = 0;
this.flipped = false;
writePos = 0;
readPos = 0;
flipped = false;
}
public int available() {
if(!flipped){
return writePos - readPos;
}
return capacity - readPos + writePos;
}
public int available() { return flipped ? capacity - readPos + writePos : writePos - readPos; }
public int remainingCapacity() {
if(!flipped){
return capacity - writePos;
}
return readPos - writePos;
}
public int remainingCapacity() { return flipped ? readPos - writePos : capacity - writePos; }
public boolean put(int element) {
if (!flipped) {
@ -52,9 +48,7 @@ public class QueueIntFlip {
if (writePos < readPos) {
elements[writePos++] = element;
return true;
} else {
return false;
}
} else return false;
} else {
elements[writePos++] = element;
return true;
@ -63,9 +57,7 @@ public class QueueIntFlip {
if (writePos < readPos) {
elements[writePos++] = element;
return true;
} else {
return false;
}
} else return false;
}
}
@ -78,27 +70,23 @@ public class QueueIntFlip {
if (length <= capacity - writePos) {
// new elements fit into top of elements array - copy directly
for(; newElementsReadPos < length; newElementsReadPos++){
this.elements[this.writePos++] = newElements[newElementsReadPos];
}
for (; newElementsReadPos < length; newElementsReadPos++)
elements[writePos++] = newElements[newElementsReadPos];
return newElementsReadPos;
} else {
// new elements must be divided between top and bottom of elements array
// writing to top
for(;this.writePos < capacity; this.writePos++){
this.elements[this.writePos] = newElements[newElementsReadPos++];
}
for (; writePos < capacity; writePos++)
elements[writePos] = newElements[newElementsReadPos++];
// writing to bottom
this.writePos = 0;
this.flipped = true;
int endPos = Math.min(this.readPos, length - newElementsReadPos);
for(; this.writePos < endPos; this.writePos++){
int endPos = Math.min(readPos, length - newElementsReadPos);
for (; writePos < endPos; writePos++)
this.elements[writePos] = newElements[newElementsReadPos++];
}
return newElementsReadPos;
}
@ -107,37 +95,24 @@ public class QueueIntFlip {
// readPos higher than writePos - free sections are:
// 1) from writePos to readPos
int endPos = Math.min(this.readPos, this.writePos + length);
int endPos = Math.min(readPos, writePos + length);
for(; this.writePos < endPos; this.writePos++){
this.elements[this.writePos] = newElements[newElementsReadPos++];
}
for (; writePos < endPos; writePos++)
elements[writePos] = newElements[newElementsReadPos++];
return newElementsReadPos;
}
}
public int take() {
if(!flipped){
if(readPos < writePos){
return elements[readPos++];
} else {
return -1;
}
} else {
if (!flipped) return readPos < writePos ? elements[readPos++] : -1;
else {
if (readPos == capacity) {
readPos = 0;
flipped = false;
if(readPos < writePos){
return elements[readPos++];
} else {
return -1;
}
} else {
return elements[readPos++];
}
return readPos < writePos ? elements[readPos++] : -1;
} else return elements[readPos++];
}
}
@ -146,19 +121,19 @@ public class QueueIntFlip {
if (!flipped) {
// writePos higher than readPos - available section is writePos - readPos
int endPos = Math.min(this.writePos, this.readPos + length);
for(; this.readPos < endPos; this.readPos++){
into[intoWritePos++] = this.elements[this.readPos];
}
int endPos = Math.min(writePos, readPos + length);
for (; readPos < endPos; readPos++)
into[intoWritePos++] = elements[readPos];
return intoWritePos;
} else {
//readPos higher than writePos - available sections are top + bottom of elements array
// readPos higher than writePos - available sections are top + bottom of
// elements array
if (length <= capacity - readPos) {
//length is lower than the elements available at the top of the elements array - copy directly
for(; intoWritePos < length; intoWritePos++){
into[intoWritePos] = this.elements[this.readPos++];
}
// length is lower than the elements available at the top of the elements array
// - copy directly
for (; intoWritePos < length; intoWritePos++)
into[intoWritePos] = elements[readPos++];
return intoWritePos;
} else {
@ -166,21 +141,18 @@ public class QueueIntFlip {
// split copy into a copy from both top and bottom of elements array.
// copy from top
for(; this.readPos < capacity; this.readPos++){
into[intoWritePos++] = this.elements[this.readPos];
}
for (; readPos < capacity; readPos++)
into[intoWritePos++] = elements[readPos];
// copy from bottom
this.readPos = 0;
this.flipped = false;
int endPos = Math.min(this.writePos, length - intoWritePos);
for(; this.readPos < endPos; this.readPos++){
into[intoWritePos++] = this.elements[this.readPos];
}
readPos = 0;
flipped = false;
int endPos = Math.min(writePos, length - intoWritePos);
for (; readPos < endPos; readPos++)
into[intoWritePos++] = elements[readPos];
return intoWritePos;
}
}
}
}

View File

@ -3,19 +3,22 @@ package com.jenkov.nioserver;
import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
* Created by jjenkov on 24-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>Server.java</strong><br>
* Created: <strong>24 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class Server {
private SocketAccepter socketAccepter = null;
private SocketProcessor socketProcessor = null;
private SocketAcceptor socketAccepter;
private SocketProcessor socketProcessor;
private int tcpPort = 0;
private IMessageReaderFactory messageReaderFactory = null;
private IMessageProcessor messageProcessor = null;
private int tcpPort;
private IMessageReaderFactory messageReaderFactory;
private IMessageProcessor messageProcessor;
public Server(int tcpPort, IMessageReaderFactory messageReaderFactory, IMessageProcessor messageProcessor) {
this.tcpPort = tcpPort;
@ -25,22 +28,19 @@ public class Server {
public void start() throws IOException {
Queue socketQueue = new ArrayBlockingQueue(1024); //move 1024 to ServerConfig
this.socketAccepter = new SocketAccepter(tcpPort, socketQueue);
Queue<Socket> socketQueue = new ArrayBlockingQueue<>(1024); // TODO: move 1024 to ServerConfig
socketAccepter = new SocketAcceptor(tcpPort, socketQueue);
MessageBuffer readBuffer = new MessageBuffer();
MessageBuffer writeBuffer = new MessageBuffer();
this.socketProcessor = new SocketProcessor(socketQueue, readBuffer, writeBuffer, this.messageReaderFactory, this.messageProcessor);
socketProcessor = new SocketProcessor(socketQueue, readBuffer, writeBuffer, this.messageReaderFactory, this.messageProcessor);
Thread accepterThread = new Thread(this.socketAccepter);
Thread processorThread = new Thread(this.socketProcessor);
Thread accepterThread = new Thread(socketAccepter);
Thread processorThread = new Thread(socketProcessor);
accepterThread.start();
processorThread.start();
}
}

View File

@ -5,51 +5,47 @@ import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
/**
* Created by jjenkov on 16-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>Socket.java</strong><br>
* Created: <strong>16 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class Socket {
public long socketId;
public SocketChannel socketChannel = null;
public IMessageReader messageReader = null;
public MessageWriter messageWriter = null;
public SocketChannel socketChannel;
public IMessageReader messageReader;
public MessageWriter messageWriter;
public boolean endOfStreamReached = false;
public boolean endOfStreamReached;
public Socket() {
}
public Socket(SocketChannel socketChannel) {
this.socketChannel = socketChannel;
}
public Socket(SocketChannel socketChannel) { this.socketChannel = socketChannel; }
public int read(ByteBuffer byteBuffer) throws IOException {
int bytesRead = this.socketChannel.read(byteBuffer);
int bytesRead = socketChannel.read(byteBuffer);
int totalBytesRead = bytesRead;
while (bytesRead > 0) {
bytesRead = this.socketChannel.read(byteBuffer);
bytesRead = socketChannel.read(byteBuffer);
totalBytesRead += bytesRead;
}
if(bytesRead == -1){
this.endOfStreamReached = true;
}
if (bytesRead == -1) endOfStreamReached = true;
return totalBytesRead;
}
public int write(ByteBuffer byteBuffer) throws IOException {
int bytesWritten = this.socketChannel.write(byteBuffer);
int bytesWritten = socketChannel.write(byteBuffer);
int totalBytesWritten = bytesWritten;
while (bytesWritten > 0 && byteBuffer.hasRemaining()) {
bytesWritten = this.socketChannel.write(byteBuffer);
bytesWritten = socketChannel.write(byteBuffer);
totalBytesWritten += bytesWritten;
}
return totalBytesWritten;
}
}

View File

@ -2,52 +2,50 @@ package com.jenkov.nioserver;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Queue;
/**
* Created by jjenkov on 19-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>SocketAcceptor.java</strong><br>
* Created: <strong>19 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class SocketAccepter implements Runnable{
public class SocketAcceptor implements Runnable {
private int tcpPort = 0;
private ServerSocketChannel serverSocket = null;
private int tcpPort;
private ServerSocketChannel serverSocket;
private Queue socketQueue = null;
private Queue<Socket> socketQueue;
public SocketAccepter(int tcpPort, Queue socketQueue) {
public SocketAcceptor(int tcpPort, Queue<Socket> socketQueue) {
this.tcpPort = tcpPort;
this.socketQueue = socketQueue;
}
public void run() {
try {
this.serverSocket = ServerSocketChannel.open();
this.serverSocket.bind(new InetSocketAddress(tcpPort));
serverSocket = ServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress(tcpPort));
} catch (IOException e) {
e.printStackTrace();
return;
}
while (true) {
try {
SocketChannel socketChannel = this.serverSocket.accept();
SocketChannel socketChannel = serverSocket.accept();
System.out.println("Socket accepted: " + socketChannel);
//todo check if the queue can even accept more sockets.
// TODO: check if the queue can even accept more sockets.
this.socketQueue.add(new Socket(socketChannel));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

View File

@ -5,101 +5,108 @@ import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
/**
* Created by jjenkov on 16-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>SocketProcessor.java</strong><br>
* Created: <strong>16 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class SocketProcessor implements Runnable {
private Queue<Socket> inboundSocketQueue = null;
private Queue<Socket> inboundSocketQueue;
private MessageBuffer readMessageBuffer = null; //todo Not used now - but perhaps will be later - to check for space in the buffer before reading from sockets
private MessageBuffer writeMessageBuffer = null; //todo Not used now - but perhaps will be later - to check for space in the buffer before reading from sockets (space for more to write?)
private MessageBuffer readMessageBuffer; // TODO: Not used now - but perhaps will be later - to check for space in the
// buffer before reading from sockets
@SuppressWarnings("unused")
private MessageBuffer writeMessageBuffer; // TODO: Not used now - but perhaps will be later - to check for space in the
// buffer before reading from sockets (space for more to write?)
private IMessageReaderFactory messageReaderFactory = null;
private IMessageReaderFactory messageReaderFactory;
private Queue<Message> outboundMessageQueue = new LinkedList<>(); //todo use a better / faster queue.
private Queue<Message> outboundMessageQueue = new LinkedList<>(); // TODO: use a better / faster queue.
private Map<Long, Socket> socketMap = new HashMap<>();
private ByteBuffer readByteBuffer = ByteBuffer.allocate(1024 * 1024);
private ByteBuffer writeByteBuffer = ByteBuffer.allocate(1024 * 1024);
private Selector readSelector = null;
private Selector writeSelector = null;
private Selector readSelector;
private Selector writeSelector;
private IMessageProcessor messageProcessor = null;
private WriteProxy writeProxy = null;
private IMessageProcessor messageProcessor;
private WriteProxy writeProxy;
private long nextSocketId = 16 * 1024; //start incoming socket ids from 16K - reserve bottom ids for pre-defined sockets (servers).
private long nextSocketId = 16 * 1024; // start incoming socket ids from 16K - reserve bottom ids for pre-defined
// sockets (servers).
private Set<Socket> emptyToNonEmptySockets = new HashSet<>();
private Set<Socket> nonEmptyToEmptySockets = new HashSet<>();
public SocketProcessor(Queue<Socket> inboundSocketQueue, MessageBuffer readMessageBuffer, MessageBuffer writeMessageBuffer, IMessageReaderFactory messageReaderFactory, IMessageProcessor messageProcessor) throws IOException {
public SocketProcessor(Queue<Socket> inboundSocketQueue, MessageBuffer readMessageBuffer, MessageBuffer writeMessageBuffer,
IMessageReaderFactory messageReaderFactory, IMessageProcessor messageProcessor) throws IOException {
this.inboundSocketQueue = inboundSocketQueue;
this.readMessageBuffer = readMessageBuffer;
this.writeMessageBuffer = writeMessageBuffer;
this.writeProxy = new WriteProxy(writeMessageBuffer, this.outboundMessageQueue);
writeProxy = new WriteProxy(writeMessageBuffer, this.outboundMessageQueue);
this.messageReaderFactory = messageReaderFactory;
this.messageProcessor = messageProcessor;
this.readSelector = Selector.open();
this.writeSelector = Selector.open();
readSelector = Selector.open();
writeSelector = Selector.open();
}
public void run() {
while (true) {
try {
executeCycle();
} catch(IOException e){
e.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
public void executeCycle() throws IOException {
takeNewSockets();
readFromSockets();
writeToSockets();
}
public void takeNewSockets() throws IOException {
Socket newSocket = this.inboundSocketQueue.poll();
Socket newSocket = inboundSocketQueue.poll();
while (newSocket != null) {
newSocket.socketId = this.nextSocketId++;
newSocket.socketId = nextSocketId++;
newSocket.socketChannel.configureBlocking(false);
newSocket.messageReader = this.messageReaderFactory.createMessageReader();
newSocket.messageReader.init(this.readMessageBuffer);
newSocket.messageReader = messageReaderFactory.createMessageReader();
newSocket.messageReader.init(readMessageBuffer);
newSocket.messageWriter = new MessageWriter();
this.socketMap.put(newSocket.socketId, newSocket);
socketMap.put(newSocket.socketId, newSocket);
SelectionKey key = newSocket.socketChannel.register(this.readSelector, SelectionKey.OP_READ);
SelectionKey key = newSocket.socketChannel.register(readSelector, SelectionKey.OP_READ);
key.attach(newSocket);
newSocket = this.inboundSocketQueue.poll();
newSocket = inboundSocketQueue.poll();
}
}
public void readFromSockets() throws IOException {
int readReady = this.readSelector.selectNow();
int readReady = readSelector.selectNow();
if (readReady > 0) {
Set<SelectionKey> selectedKeys = this.readSelector.selectedKeys();
@ -107,9 +114,7 @@ public class SocketProcessor implements Runnable {
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
readFromSocket(key);
keyIterator.remove();
}
selectedKeys.clear();
@ -124,21 +129,21 @@ public class SocketProcessor implements Runnable {
if (fullMessages.size() > 0) {
for (Message message : fullMessages) {
message.socketId = socket.socketId;
this.messageProcessor.process(message, this.writeProxy); //the message processor will eventually push outgoing messages into an IMessageWriter for this socket.
messageProcessor.process(message, writeProxy); // the message processor will eventually push outgoing messages into an
// IMessageWriter for this socket.
}
fullMessages.clear();
}
if (socket.endOfStreamReached) {
System.out.println("Socket closed: " + socket.socketId);
this.socketMap.remove(socket.socketId);
socketMap.remove(socket.socketId);
key.attach(null);
key.cancel();
key.channel().close();
}
}
public void writeToSockets() throws IOException {
// Take all new messages from outboundMessageQueue
@ -164,38 +169,32 @@ public class SocketProcessor implements Runnable {
socket.messageWriter.write(socket, this.writeByteBuffer);
if(socket.messageWriter.isEmpty()){
this.nonEmptyToEmptySockets.add(socket);
}
if (socket.messageWriter.isEmpty()) { this.nonEmptyToEmptySockets.add(socket); }
keyIterator.remove();
}
selectionKeys.clear();
}
}
private void registerNonEmptySockets() throws ClosedChannelException {
for(Socket socket : emptyToNonEmptySockets){
socket.socketChannel.register(this.writeSelector, SelectionKey.OP_WRITE, socket);
}
for (Socket socket : emptyToNonEmptySockets)
socket.socketChannel.register(writeSelector, SelectionKey.OP_WRITE, socket);
emptyToNonEmptySockets.clear();
}
private void cancelEmptySockets() {
for (Socket socket : nonEmptyToEmptySockets) {
SelectionKey key = socket.socketChannel.keyFor(this.writeSelector);
key.cancel();
}
nonEmptyToEmptySockets.clear();
}
private void takeNewOutboundMessages() {
Message outMessage = this.outboundMessageQueue.poll();
Message outMessage = outboundMessageQueue.poll();
while (outMessage != null) {
Socket socket = this.socketMap.get(outMessage.socketId);
Socket socket = socketMap.get(outMessage.socketId);
if (socket != null) {
MessageWriter messageWriter = socket.messageWriter;
@ -203,13 +202,10 @@ public class SocketProcessor implements Runnable {
messageWriter.enqueue(outMessage);
nonEmptyToEmptySockets.remove(socket);
emptyToNonEmptySockets.add(socket); // not necessary if removed from nonEmptyToEmptySockets in prev. statement.
} else{
messageWriter.enqueue(outMessage);
}
} else messageWriter.enqueue(outMessage);
}
outMessage = this.outboundMessageQueue.poll();
outMessage = outboundMessageQueue.poll();
}
}
}

View File

@ -3,24 +3,23 @@ package com.jenkov.nioserver;
import java.util.Queue;
/**
* Created by jjenkov on 22-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>WriteProxy.java</strong><br>
* Created: <strong>22 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class WriteProxy {
private MessageBuffer messageBuffer = null;
private Queue writeQueue = null;
private MessageBuffer messageBuffer;
private Queue<Message> writeQueue;
public WriteProxy(MessageBuffer messageBuffer, Queue writeQueue) {
public WriteProxy(MessageBuffer messageBuffer, Queue<Message> writeQueue) {
this.messageBuffer = messageBuffer;
this.writeQueue = writeQueue;
}
public Message getMessage(){
return this.messageBuffer.getMessage();
}
public boolean enqueue(Message message){
return this.writeQueue.offer(message);
}
public Message getMessage() { return messageBuffer.getMessage(); }
public boolean enqueue(Message message) { return writeQueue.offer(message); }
}

View File

@ -1,24 +1,25 @@
package com.jenkov.nioserver.example;
import com.jenkov.nioserver.*;
import java.io.IOException;
import com.jenkov.nioserver.IMessageProcessor;
import com.jenkov.nioserver.Message;
import com.jenkov.nioserver.Server;
import com.jenkov.nioserver.http.HttpMessageReaderFactory;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
* Created by jjenkov on 19-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>Main.java</strong><br>
* Created: <strong>19 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class Main {
public static void main(String[] args) throws IOException {
String httpResponse = "HTTP/1.1 200 OK\r\n" +
"Content-Length: 38\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<html><body>Hello World!</body></html>";
String httpResponse = "HTTP/1.1 200 OK\r\n" + "Content-Length: 38\r\n" + "Content-Type: text/html\r\n" + "\r\n"
+ "<html><body>Hello World!</body></html>";
byte[] httpResponseBytes = httpResponse.getBytes("UTF-8");
@ -35,8 +36,5 @@ public class Main {
Server server = new Server(9999, new HttpMessageReaderFactory(), messageProcessor);
server.start();
}
}

View File

@ -1,7 +1,11 @@
package com.jenkov.nioserver.http;
/**
* Created by jjenkov on 19-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>HttpHeaders.java</strong><br>
* Created: <strong>19 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class HttpHeaders {
@ -20,8 +24,4 @@ public class HttpHeaders {
public int bodyStartIndex = 0;
public int bodyEndIndex = 0;
}

View File

@ -1,38 +1,39 @@
package com.jenkov.nioserver.http;
import com.jenkov.nioserver.IMessageReader;
import com.jenkov.nioserver.Message;
import com.jenkov.nioserver.MessageBuffer;
import com.jenkov.nioserver.Socket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import com.jenkov.nioserver.IMessageReader;
import com.jenkov.nioserver.Message;
import com.jenkov.nioserver.MessageBuffer;
import com.jenkov.nioserver.Socket;
/**
* Created by jjenkov on 18-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>HttpMessageReader.java</strong><br>
* Created: <strong>18 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class HttpMessageReader implements IMessageReader {
private MessageBuffer messageBuffer = null;
private MessageBuffer messageBuffer;
private List<Message> completeMessages = new ArrayList<Message>();
private Message nextMessage = null;
public HttpMessageReader() {
}
private List<Message> completeMessages = new ArrayList<>();
private Message nextMessage;
@Override
public void init(MessageBuffer readMessageBuffer) {
this.messageBuffer = readMessageBuffer;
this.nextMessage = messageBuffer.getMessage();
this.nextMessage.metaData = new HttpHeaders();
messageBuffer = readMessageBuffer;
nextMessage = messageBuffer.getMessage();
nextMessage.metaData = new HttpHeaders();
}
@Override
public void read(Socket socket, ByteBuffer byteBuffer) throws IOException {
int bytesRead = socket.read(byteBuffer);
socket.read(byteBuffer);
byteBuffer.flip();
if (byteBuffer.remaining() == 0) {
@ -40,11 +41,14 @@ public class HttpMessageReader implements IMessageReader {
return;
}
this.nextMessage.writeToMessage(byteBuffer);
nextMessage.writeToMessage(byteBuffer);
int endIndex = HttpUtil.parseHttpRequest(this.nextMessage.sharedArray, this.nextMessage.offset, this.nextMessage.offset + this.nextMessage.length, (HttpHeaders) this.nextMessage.metaData);
int endIndex = HttpUtil.parseHttpRequest(nextMessage.sharedArray,
nextMessage.offset,
nextMessage.offset + nextMessage.length,
(HttpHeaders) nextMessage.metaData);
if (endIndex != -1) {
Message message = this.messageBuffer.getMessage();
Message message = messageBuffer.getMessage();
message.metaData = new HttpHeaders();
message.writePartialMessageToMessage(nextMessage, endIndex);
@ -55,10 +59,6 @@ public class HttpMessageReader implements IMessageReader {
byteBuffer.clear();
}
@Override
public List<Message> getMessages() {
return this.completeMessages;
}
public List<Message> getMessages() { return completeMessages; }
}

View File

@ -2,18 +2,16 @@ package com.jenkov.nioserver.http;
import com.jenkov.nioserver.IMessageReader;
import com.jenkov.nioserver.IMessageReaderFactory;
import com.jenkov.nioserver.MessageBuffer;
/**
* Created by jjenkov on 18-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>HttpMessageReaderFactory.java</strong><br>
* Created: <strong>18 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class HttpMessageReaderFactory implements IMessageReaderFactory {
public HttpMessageReaderFactory() {
}
@Override
public IMessageReader createMessageReader() {
return new HttpMessageReader();
}
public IMessageReader createMessageReader() { return new HttpMessageReader(); }
}

View File

@ -3,7 +3,11 @@ package com.jenkov.nioserver.http;
import java.io.UnsupportedEncodingException;
/**
* Created by jjenkov on 19-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>HttpUtil.java</strong><br>
* Created: <strong>19 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class HttpUtil {
@ -13,23 +17,22 @@ public class HttpUtil {
private static final byte[] HEAD = new byte[] { 'H', 'E', 'A', 'D' };
private static final byte[] DELETE = new byte[] { 'D', 'E', 'L', 'E', 'T', 'E' };
@SuppressWarnings("unused")
private static final byte[] HOST = new byte[] { 'H', 'o', 's', 't' };
private static final byte[] CONTENT_LENGTH = new byte[] { 'C', 'o', 'n', 't', 'e', 'n', 't', '-', 'L', 'e', 'n', 'g', 't', 'h' };
public static int parseHttpRequest(byte[] src, int startIndex, int endIndex, HttpHeaders httpHeaders) {
/*
int endOfHttpMethod = findNext(src, startIndex, endIndex, (byte) ' ');
if(endOfHttpMethod == -1) return false;
resolveHttpMethod(src, startIndex, httpHeaders);
* int endOfHttpMethod = findNext(src, startIndex, endIndex, (byte) ' ');
* if(endOfHttpMethod == -1) return false;
* resolveHttpMethod(src, startIndex, httpHeaders);
*/
// parse HTTP request line
int endOfFirstLine = findNextLineBreak(src, startIndex, endIndex);
if (endOfFirstLine == -1) return -1;
// parse HTTP headers
int prevEndOfHeader = endOfFirstLine + 1;
int endOfHeader = findNextLineBreak(src, prevEndOfHeader, endIndex);
@ -48,9 +51,7 @@ public class HttpUtil {
endOfHeader = findNextLineBreak(src, prevEndOfHeader, endIndex);
}
if(endOfHeader == -1){
return -1;
}
if (endOfHeader == -1) { return -1; }
// check that byte array contains full HTTP message.
int bodyStartIndex = endOfHeader + 1;
@ -63,7 +64,6 @@ public class HttpUtil {
return bodyEndIndex;
}
return -1;
}
@ -82,44 +82,35 @@ public class HttpUtil {
while (index < endIndex && !endOfValueFound) {
switch (src[index]) {
case '0' : ;
case '1' : ;
case '2' : ;
case '3' : ;
case '4' : ;
case '5' : ;
case '6' : ;
case '7' : ;
case '8' : ;
case '9' : { index++; break; }
default: {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
index++;
break;
default:
endOfValueFound = true;
valueEndIndex = index;
}
}
}
httpHeaders.contentLength = Integer.parseInt(new String(src, valueStartIndex, valueEndIndex - valueStartIndex, "UTF-8"));
}
public static int findNext(byte[] src, int startIndex, int endIndex, byte value) {
for(int index = startIndex; index < endIndex; index++){
for (int index = startIndex; index < endIndex; index++)
if (src[index] == value) return index;
}
return -1;
}
public static int findNextLineBreak(byte[] src, int startIndex, int endIndex) {
for(int index = startIndex; index < endIndex; index++){
if(src[index] == '\n'){
if(src[index - 1] == '\r'){
return index;
}
};
}
for (int index = startIndex; index < endIndex; index++)
if (src[index] == '\n') if (src[index - 1] == '\r') return index;
return -1;
}
@ -147,9 +138,8 @@ public class HttpUtil {
}
public static boolean matches(byte[] src, int offset, byte[] value) {
for(int i=offset, n=0; n < value.length; i++, n++){
for (int i = offset, n = 0; n < value.length; i++, n++)
if (src[i] != value[n]) return false;
}
return true;
}
}

View File

@ -1,15 +1,19 @@
package com.jenkov.nioserver;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import org.junit.jupiter.api.Test;
/**
* Created by jjenkov on 18-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>MessageBufferTest.java</strong><br>
* Created: <strong>18 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class MessageBufferTest {
@ -32,11 +36,9 @@ public class MessageBufferTest {
assertEquals(0, message2.length);
assertEquals(4 * 1024, message2.capacity);
//todo test what happens if the small buffer space is depleted of messages.
// TODO: test what happens if the small buffer space is depleted of messages.
}
@Test
public void testExpandMessage() {
MessageBuffer messageBuffer = new MessageBuffer();
@ -73,8 +75,5 @@ public class MessageBufferTest {
assertEquals(0, message.length);
assertEquals(1024 * 1024, message.capacity);
assertSame(message.sharedArray, largeSharedArray);
}
}

View File

@ -1,22 +1,21 @@
package com.jenkov.nioserver;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import java.nio.ByteBuffer;
import org.junit.jupiter.api.Test;
/**
* Created by jjenkov on 18-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>MessageTest.java</strong><br>
* Created: <strong>18 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class MessageTest {
@Test
public void testWriteToMessage() {
MessageBuffer messageBuffer = new MessageBuffer();
@ -46,14 +45,12 @@ public class MessageTest {
fill(byteBuffer, 1);
written = message.writeToMessage(byteBuffer);
assertEquals(-1, written);
}
private void fill(ByteBuffer byteBuffer, int length) {
byteBuffer.clear();
for(int i=0; i<length; i++){
for (int i = 0; i < length; i++)
byteBuffer.put((byte) (i % 128));
}
byteBuffer.flip();
}
}

View File

@ -1,15 +1,19 @@
package com.jenkov.nioserver;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import org.junit.jupiter.api.Test;
/**
* Created by jjenkov on 21-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>SelectorTest.java</strong><br>
* Created: <strong>21 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class SelectorTest {
@ -27,10 +31,5 @@ public class SelectorTest {
SelectionKey key2 = socketChannel.register(selector, SelectionKey.OP_WRITE);
key2.cancel();
}
}

View File

@ -1,18 +1,17 @@
package com.jenkov.nioserver.http;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.UnsupportedEncodingException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import org.junit.jupiter.api.Test;
/**
* Created by jjenkov on 19-10-2015.
* Project: <strong>java-nio-server</strong><br>
* File: <strong>HttpUtilTest.java</strong><br>
* Created: <strong>19 Oct 2015</strong><br>
*
* @author jjenkov
*/
public class HttpUtilTest {
@ -33,12 +32,9 @@ public class HttpUtilTest {
assertEquals(httpMethod, httpHeaders.httpMethod);
}
@Test
public void testParseHttpRequest() throws UnsupportedEncodingException {
String httpRequest =
"GET / HTTP/1.1\r\n\r\n";
String httpRequest = "GET / HTTP/1.1\r\n\r\n";
byte[] source = httpRequest.getBytes("UTF-8");
HttpHeaders httpHeaders = new HttpHeaders();
@ -47,33 +43,19 @@ public class HttpUtilTest {
assertEquals(0, httpHeaders.contentLength);
httpRequest =
"GET / HTTP/1.1\r\n" +
"Content-Length: 5\r\n" +
"\r\n1234";
httpRequest = "GET / HTTP/1.1\r\n" + "Content-Length: 5\r\n" + "\r\n1234";
source = httpRequest.getBytes("UTF-8");
assertEquals(-1, HttpUtil.parseHttpRequest(source, 0, source.length, httpHeaders));
assertEquals(5, httpHeaders.contentLength);
httpRequest =
"GET / HTTP/1.1\r\n" +
"Content-Length: 5\r\n" +
"\r\n12345";
httpRequest = "GET / HTTP/1.1\r\n" + "Content-Length: 5\r\n" + "\r\n12345";
source = httpRequest.getBytes("UTF-8");
assertEquals(42, HttpUtil.parseHttpRequest(source, 0, source.length, httpHeaders));
assertEquals(5, httpHeaders.contentLength);
httpRequest =
"GET / HTTP/1.1\r\n" +
"Content-Length: 5\r\n" +
"\r\n12345" +
"GET / HTTP/1.1\r\n" +
"Content-Length: 5\r\n" +
"\r\n12345";
httpRequest = "GET / HTTP/1.1\r\n" + "Content-Length: 5\r\n" + "\r\n12345" + "GET / HTTP/1.1\r\n" + "Content-Length: 5\r\n" + "\r\n12345";
source = httpRequest.getBytes("UTF-8");
@ -82,7 +64,4 @@ public class HttpUtilTest {
assertEquals(37, httpHeaders.bodyStartIndex);
assertEquals(42, httpHeaders.bodyEndIndex);
}
}