Commit 3e626cd7 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Resize outAppData byte buffer if the data to write is bigger than the buffer capacity.

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@2801 b35dd754-fafc-0310-a699-88a17e54d16e
parent 9ad1f9dd
...@@ -56,10 +56,11 @@ public class TLSStreamWriter { ...@@ -56,10 +56,11 @@ public class TLSStreamWriter {
} }
} }
private void tlsWrite(final ByteBuffer buf) throws IOException { private void tlsWrite(ByteBuffer buf) throws IOException {
ByteBuffer tlsBuffer = null; ByteBuffer tlsBuffer = null;
ByteBuffer tlsOutput = null; ByteBuffer tlsOutput = null;
do { do {
// TODO Consider optimizing by not creating new instances each time
tlsBuffer = ByteBuffer.allocate(Math.min(buf.remaining(), wrapper.getAppBuffSize())); tlsBuffer = ByteBuffer.allocate(Math.min(buf.remaining(), wrapper.getAppBuffSize()));
tlsOutput = ByteBuffer.allocate(wrapper.getNetBuffSize()); tlsOutput = ByteBuffer.allocate(wrapper.getNetBuffSize());
...@@ -81,7 +82,7 @@ public class TLSStreamWriter { ...@@ -81,7 +82,7 @@ public class TLSStreamWriter {
* Writes outNetData to the SocketChannel. <P> Returns true when the ByteBuffer has no remaining * Writes outNetData to the SocketChannel. <P> Returns true when the ByteBuffer has no remaining
* data. * data.
*/ */
private boolean writeToSocket(final ByteBuffer outNetData) throws IOException { private boolean writeToSocket(ByteBuffer outNetData) throws IOException {
wbc.write(outNetData); wbc.write(outNetData);
return !outNetData.hasRemaining(); return !outNetData.hasRemaining();
} }
...@@ -104,6 +105,7 @@ public class TLSStreamWriter { ...@@ -104,6 +105,7 @@ public class TLSStreamWriter {
} }
public synchronized void write(byte[] bytes, int off, int len) throws IOException { public synchronized void write(byte[] bytes, int off, int len) throws IOException {
outAppData = resizeApplicationBuffer(bytes.length);
outAppData.put(bytes, off, len); outAppData.put(bytes, off, len);
outAppData.flip(); outAppData.flip();
doWrite(outAppData); doWrite(outAppData);
...@@ -112,4 +114,17 @@ public class TLSStreamWriter { ...@@ -112,4 +114,17 @@ public class TLSStreamWriter {
}; };
} }
private ByteBuffer resizeApplicationBuffer(int increment) {
// TODO Creating new buffers and copying over old one may not scale. Consider using views. Thanks to Noah for the tip.
if (outAppData.remaining() < increment) {
System.out.println("resizing writer");
ByteBuffer bb = ByteBuffer.allocate(outAppData.capacity() + wrapper.getAppBuffSize());
outAppData.flip();
bb.put(outAppData);
return bb;
} else {
return outAppData;
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment