summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/com/isode/stroke/base/ByteArray.java6
-rw-r--r--src/com/isode/stroke/network/JavaConnection.java36
2 files changed, 22 insertions, 20 deletions
diff --git a/src/com/isode/stroke/base/ByteArray.java b/src/com/isode/stroke/base/ByteArray.java
index 22d81b7..d3942ce 100644
--- a/src/com/isode/stroke/base/ByteArray.java
+++ b/src/com/isode/stroke/base/ByteArray.java
@@ -77,7 +77,7 @@ public class ByteArray {
* @return array copy of internal data, will never be null, but may
* contain zero elements.
*/
- public synchronized byte[] getData() {
+ public byte[] getData() {
if (dataCopy_ == null) {
dataCopy_ = new byte[getSize()];
for (int i = 0; i < data_.size(); i++) {
@@ -167,7 +167,7 @@ public class ByteArray {
* @param b a single byte
* @return a reference to the updated object
*/
- public synchronized ByteArray append(byte b) {
+ public ByteArray append(byte b) {
dataCopy_ = null; /* Invalidate cache */
data_.add(Byte.valueOf(b));
return this;
@@ -278,7 +278,7 @@ public class ByteArray {
/**
* Clears the contents of this ByteArray, leaving it with zero elements.
*/
- public synchronized void clear() {
+ public void clear() {
data_ = new Vector<Byte>();
dataCopy_ = null;
}
diff --git a/src/com/isode/stroke/network/JavaConnection.java b/src/com/isode/stroke/network/JavaConnection.java
index 31b07a0..3f768dc 100644
--- a/src/com/isode/stroke/network/JavaConnection.java
+++ b/src/com/isode/stroke/network/JavaConnection.java
@@ -8,6 +8,7 @@
*/
package com.isode.stroke.network;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
@@ -16,6 +17,7 @@ import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -29,7 +31,7 @@ public class JavaConnection extends Connection implements EventOwner {
private class Worker implements Runnable {
private final HostAddressPort address_;
- private final List<ByteArray> writeBuffer_ = Collections.synchronizedList(new ArrayList<ByteArray>());
+ private final List<byte[]> writeBuffer_ = Collections.synchronizedList(new ArrayList<byte[]>());
public Worker(HostAddressPort address) {
address_ = address;
@@ -156,8 +158,7 @@ public class JavaConnection extends Connection implements EventOwner {
return;
}
- ByteArray data = writeBuffer_.get(0);
- byte[] bytes = data.getData();
+ byte[] bytes = writeBuffer_.get(0);
int bytesToWrite = bytes.length;
if (bytesToWrite == 0) {
@@ -167,8 +168,8 @@ public class JavaConnection extends Connection implements EventOwner {
writeBuffer_.remove(0);
return;
}
+
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
-
/*
* Because the SocketChannel is non-blocking, we have to
* be prepared to cope with the write operation not
@@ -190,15 +191,13 @@ public class JavaConnection extends Connection implements EventOwner {
}
/* The buffer was *partly* written. This means we have to
- * remove that part. We do this by creating a new ByteArray
+ * remove that part. We do this by creating a new byte[]
* with the remaining bytes in, and replacing the first
* element in the list with that.
*/
byte[] remainingBytes = new byte[bytesToWrite - bytesWritten];
- System.arraycopy(bytes, bytesWritten,remainingBytes,0, remainingBytes.length);
- ByteArray leftOver = new ByteArray(remainingBytes);
-
- writeBuffer_.set(0, leftOver);
+ remainingBytes = Arrays.copyOfRange(bytes, bytesWritten, bytes.length);
+ writeBuffer_.set(0, remainingBytes);
return;
}
@@ -210,21 +209,21 @@ public class JavaConnection extends Connection implements EventOwner {
private ByteArray doRead() throws IOException {
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
- ByteArray data = new ByteArray();
int count = socketChannel_.read(byteBuffer);
if (count == 0) {
- return data;
+ return new ByteArray();
}
+
+ ByteArrayOutputStream byteArrayOutputStream =
+ new ByteArrayOutputStream(1024);
+
while (count > 0) {
byteBuffer.flip();
byte[] result = new byte[byteBuffer.remaining()];
byteBuffer.get(result);
byteBuffer.compact();
- for (int i=0; i<result.length; i++) {
- data.append(result[i]);
- }
-
+ byteArrayOutputStream.write(result);
count = socketChannel_.read(byteBuffer);
}
if (count == -1) {
@@ -233,7 +232,10 @@ public class JavaConnection extends Connection implements EventOwner {
*/
throw new IOException("socketChannel_.read returned -1");
}
- return data;
+
+ /* There is no need to close the ByteArrayOutputStream */
+ return new ByteArray(byteArrayOutputStream.toByteArray());
+
}
private void handleConnected(final boolean error) {
@@ -299,7 +301,7 @@ public class JavaConnection extends Connection implements EventOwner {
@Override
public void write(ByteArray data) {
- worker_.writeBuffer_.add(data);
+ worker_.writeBuffer_.add(data.getData());
if (selector_ != null) {
selector_.wakeup();
}