From f0b007aa75097241a20bac009f1d3d9a0aee4ae2 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 11 Jan 2020 10:53:01 +0100 Subject: [PATCH] Fixed consecutive message writing --- src/main/java/com/jenkov/nioserver/Message.java | 2 +- .../java/com/jenkov/nioserver/MessageWriter.java | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/jenkov/nioserver/Message.java b/src/main/java/com/jenkov/nioserver/Message.java index 50ba073..4e1cb3d 100644 --- a/src/main/java/com/jenkov/nioserver/Message.java +++ b/src/main/java/com/jenkov/nioserver/Message.java @@ -68,7 +68,7 @@ public class Message { if (!this.messageBuffer.expandMessage(this)) return -1; int bytesToCopy = Math.min(remaining, capacity - length); - System.arraycopy(byteArray, offset, sharedArray, offset + this.length, bytesToCopy); + System.arraycopy(byteArray, offset, sharedArray, this.offset + this.length, bytesToCopy); this.length += bytesToCopy; return bytesToCopy; } diff --git a/src/main/java/com/jenkov/nioserver/MessageWriter.java b/src/main/java/com/jenkov/nioserver/MessageWriter.java index 1b53836..a57a83b 100644 --- a/src/main/java/com/jenkov/nioserver/MessageWriter.java +++ b/src/main/java/com/jenkov/nioserver/MessageWriter.java @@ -19,8 +19,10 @@ public class MessageWriter { private int bytesWritten; public void enqueue(Message message) { - if (messageInProgress == null) messageInProgress = message; - else writeQueue.add(message); + if (messageInProgress == null) { + messageInProgress = message; + bytesWritten = 0; + } else writeQueue.add(message); } public void write(Socket socket, ByteBuffer byteBuffer) throws IOException { @@ -31,8 +33,10 @@ public class MessageWriter { byteBuffer.clear(); if (bytesWritten >= messageInProgress.length) { - if (writeQueue.size() > 0) messageInProgress = writeQueue.remove(0); - else messageInProgress = null; + if (writeQueue.size() > 0) { + messageInProgress = writeQueue.remove(0); + bytesWritten = 0; + } else messageInProgress = null; // TODO: unregister from selector } }