WebSocket
Simone Bordet
simone.bordet at gmail.com
Tue Feb 20 18:31:00 UTC 2018
Hi,
On Tue, Feb 20, 2018 at 4:32 PM, Chuck Davis <cjgunzel at gmail.com> wrote:
> Simone, please, please tell me how this is done.
The WebSocket.Listener APIs return a CompletableFuture, indicating
when the application is done with the processing of the parameters
passed to the listener method (in particular the ByteBuffer).
We can leverage this to avoid to copy the bytes in the ByteBuffer - we
will just store the ByteBuffers in the ByteBufferInputStream for later
use, and we return a CompletableFuture that we will complete when we
have consumed the ByteBuffer bytes.
When the listener tells you that you have the LAST (or WHOLE) part,
then you can wrap the ByteBufferInputStream with your
ObjectInputStream.
The code is attached and shows the bare minimum implementation of
ByteBufferInputStream - just implementing `int read()`.
Implementing `int read(byte[], int, int)` is left as exercise :)
Just to be paranoid, the attached code license is public domain as
explained in https://creativecommons.org/publicdomain/zero/1.0/.
Just to be clear, the only copy that happens is when you have to
bridge from the byte[] format used by InputStream to the ByteBuffer
format.
The same copy happens when you use ByteArrayInputStream (that you used
in the "good" code in your first email of this thread), so this
version is on par with your "good" code.
This version has the advantage that guarantees that no other copy is
done, not even by the implementation (hopefully, right Pavel ?), while
with JDK 8 there may have been hidden copies to provide a full
ByteBuffer.
--
Simone Bordet
---
Finally, no matter how good the architecture and design are,
to deliver bug-free software with optimal performance and reliability,
the implementation technique must be flawless. Victoria Livschitz
-------------- next part --------------
A non-text attachment was scrubbed...
Name: WebSocketTest.java
Type: text/x-java
Size: 1981 bytes
Desc: not available
URL: <http://mail.openjdk.java.net/pipermail/net-dev/attachments/20180220/be319034/WebSocketTest-0001.java>
More information about the net-dev
mailing list