Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I limit reading a line at a time from a SocketChannel InputStream using Java NIO
    primarykey
    data
    text
    <p>I am trying to write a Websockets client and server. Initially the connection is HTTP and the Websockets handshake uses HTTP headers to indicate that an upgrade to a new protocol is necessary on the connection.</p> <p>I want to read in the set of HTTP headers from the SocketChannel and, if an upgrade is indicated, switch over to a different library for handling Websockets and from that point on handle the SocketChannel streams completely differently, as a set of frames rather than lines delimited with \r\n.</p> <p>I know I can read an arbitrary number of bytes into a ByteBuffer, but a Websockets frame may have been sent with the handshake and I don't want to be passing off half-consumed buffers between these sections of code. What I want is to read from the socket only the data up to and including sequence "\r\n\r\n". Any data beyond that I want to leave in the SocketChannel object input stream.</p> <p>What is the recommended way to do this? Get the input stream from the SocketChannel and wrap it in a buffered reader? Would this interact properly with NIO, particularly non-blocking uses? Could I drop the buffered reader from the input stream once the blank line was detected and still have all the frame data available when the channel is passed to the Websockets code?</p> <p>Or perhaps I need to read byte-by-byte (or 4 byte chunks with smaller buffers if some of the target "\r\n\r\n" characters appear at the end of the chunk) and build up my header strings that way.</p> <p>Or maybe some combination of manipulating mark, limit and position would allow the input stream to get back data it had previously read into the ByteBuffer provided the buffer was allocated directly.</p> <p>Any advice would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload