@Override public long read(ByteBuffer[] dsts, int offset, int length) throws IOException { Bounds[] old = new Bounds[length]; for (int i = offset; i < length; i++) { ByteBuffer dst = dsts[i]; old[i - offset] = new Bounds(dst.position(), dst.limit()); } long b = super.read(dsts, offset, length); if (b > 0) { for (int i = offset; i < length; i++) { ByteBuffer dst = dsts[i]; int oldPos = old[i - offset].position; afterRead(dst, oldPos, dst.position() - oldPos); } } else if(b == -1){ checkComplete(); } return b; }
public void readBlocking(StreamSourceFrameChannel channel) throws IOException { if (current == null) { current = channel.getWebSocketChannel().getBufferPool().allocate(); } for (; ; ) { int res = channel.read(current.getBuffer()); if (res == -1) { complete = true; return; } else if (res == 0) { channel.awaitReadable(); } checkMaxSize(channel, res); if (bufferFullMessage) { dealWithFullBuffer(channel); } else if (!current.getBuffer().hasRemaining()) { return; } } }
@Override public int read(ByteBuffer dst) throws IOException { int position = dst.position(); int r = super.read(dst); if (r > 0) { checker(dst, position, dst.position() - position, false); } else if(r == -1) { checkComplete(); } return r; }
/** * Called after data was read into the {@link ByteBuffer} * * @param buffer the {@link ByteBuffer} into which the data was read * @param position the position it was written to * @param length the number of bytes there were written * @throws IOException thrown if an error occurs */ protected void afterRead(ByteBuffer buffer, int position, int length) throws IOException { try { for (ChannelFunction func : functions) { func.afterRead(buffer, position, length); } if (isComplete()) { checkComplete(); } } catch (UnsupportedEncodingException e) { getFramedChannel().markReadsBroken(e); throw e; } }
for (; ; ) { if (current == null) { current = channel.getWebSocketChannel().getBufferPool().allocate(); int res = channel.read(current.getBuffer()); if (res == -1) { this.complete = true; callback.complete(channel.getWebSocketChannel(), this); return; } else if (res == 0) { if(!bufferFullMessage) { callback.complete(channel.getWebSocketChannel(), BufferedBinaryMessage.this); channel.getReadSetter().set(new ChannelListener<StreamSourceFrameChannel>() { @Override public void handleEvent(StreamSourceFrameChannel channel) { channel.resumeReads(); return; dealWithFullBuffer(channel); } else if (!current.getBuffer().hasRemaining()) { callback.complete(channel.getWebSocketChannel(), BufferedBinaryMessage.this); callback.onError(channel.getWebSocketChannel(), this, e);
@Override public synchronized PooledByteBuffer transformForRead(PooledByteBuffer pooledBuffer, StreamSourceFrameChannel channel, boolean lastFragmentOfMessage) throws IOException { if ((channel.getRsv() & 4) == 0) { PooledByteBuffer output = allocateBufferWithArray(channel.getWebSocketChannel(), 0); // first pass PooledByteBuffer inputBuffer = null; if (currentReadChannel != null && currentReadChannel != channel) { output = decompress(channel.getWebSocketChannel(), output); decompress.setInput(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { inputBuffer = toArrayBacked(buffer, channel.getWebSocketChannel().getBufferPool()); decompress.setInput(inputBuffer.getBuffer().array(), inputBuffer.getBuffer().arrayOffset() + inputBuffer.getBuffer().position(), inputBuffer.getBuffer().remaining()); output = decompress(channel.getWebSocketChannel(), output); } finally { output = decompress(channel.getWebSocketChannel(), output); currentReadChannel = null; } else {
public WebSocketChannel getWebSocketChannel() { return getFramedChannel(); }
@Override public StreamSourceFrameChannel getChannel(PooledByteBuffer pooled) { StreamSourceFrameChannel channel = createChannel(pooled); if (frameFinalFlag) { channel.finalFrame(); } else { fragmentedChannel = channel; } return channel; }
int getWebSocketFrameCount() { return getReadFrameCount(); }
public void read(final StreamSourceFrameChannel channel, final WebSocketCallback<BufferedTextMessage> callback) { PooledByteBuffer pooled = channel.getWebSocketChannel().getBufferPool().allocate(); final ByteBuffer buffer = pooled.getBuffer(); try { try { for (; ; ) { int res = channel.read(buffer); if (res == -1) { this.complete = true; buffer.flip(); data.write(buffer); callback.complete(channel.getWebSocketChannel(), this); return; } else if (res == 0) { data.write(buffer); if (!bufferFullMessage) { callback.complete(channel.getWebSocketChannel(), this); channel.getReadSetter().set(new ChannelListener<StreamSourceFrameChannel>() { @Override public void handleEvent(StreamSourceFrameChannel channel) { channel.resumeReads(); return; buffer.clear(); if (!bufferFullMessage) { callback.complete(channel.getWebSocketChannel(), this);
/** * Called after data was read into the {@link ByteBuffer} * * @param buffer the {@link ByteBuffer} into which the data was read * @param position the position it was written to * @param length the number of bytes there were written * @throws IOException thrown if an error occurs */ protected void afterRead(ByteBuffer buffer, int position, int length) throws IOException { try { for (ChannelFunction func : functions) { func.afterRead(buffer, position, length); } if (isComplete()) { checkComplete(); } } catch (UnsupportedEncodingException e) { getFramedChannel().markReadsBroken(e); throw e; } }
@Override public synchronized PooledByteBuffer transformForRead(PooledByteBuffer pooledBuffer, StreamSourceFrameChannel channel, boolean lastFragmentOfMessage) throws IOException { if ((channel.getRsv() & 4) == 0) { PooledByteBuffer output = allocateBufferWithArray(channel.getWebSocketChannel(), 0); // first pass PooledByteBuffer inputBuffer = null; if (currentReadChannel != null && currentReadChannel != channel) { output = decompress(channel.getWebSocketChannel(), output); decompress.setInput(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { inputBuffer = toArrayBacked(buffer, channel.getWebSocketChannel().getBufferPool()); decompress.setInput(inputBuffer.getBuffer().array(), inputBuffer.getBuffer().arrayOffset() + inputBuffer.getBuffer().position(), inputBuffer.getBuffer().remaining()); output = decompress(channel.getWebSocketChannel(), output); } finally { output = decompress(channel.getWebSocketChannel(), output); currentReadChannel = null; } else {
private void checkComplete() throws IOException { try { for (ChannelFunction func : functions) { func.complete(); } } catch (UnsupportedEncodingException e) { getFramedChannel().markReadsBroken(e); throw e; } }
@Override protected void handleHeaderData(FrameHeaderData headerData) { super.handleHeaderData(headerData); if (((WebSocketFrame) headerData).isFinalFragment()) { finalFrame(); } if(masker != null) { masker.newFrame(headerData); } if(functions != null) { for(ChannelFunction func : functions) { func.newFrame(headerData); } } }
int getWebSocketFrameCount() { return getReadFrameCount(); }
for (; ; ) { if (current == null) { current = channel.getWebSocketChannel().getBufferPool().allocate(); int res = channel.read(current.getBuffer()); if (res == -1) { this.complete = true; callback.complete(channel.getWebSocketChannel(), this); return; } else if (res == 0) { if(!bufferFullMessage) { callback.complete(channel.getWebSocketChannel(), BufferedBinaryMessage.this); channel.getReadSetter().set(new ChannelListener<StreamSourceFrameChannel>() { @Override public void handleEvent(StreamSourceFrameChannel channel) { channel.resumeReads(); return; dealWithFullBuffer(channel); } else if (!current.getBuffer().hasRemaining()) { callback.complete(channel.getWebSocketChannel(), BufferedBinaryMessage.this); callback.onError(channel.getWebSocketChannel(), this, e);
public void readBlocking(StreamSourceFrameChannel channel) throws IOException { PooledByteBuffer pooled = channel.getWebSocketChannel().getBufferPool().allocate(); final ByteBuffer buffer = pooled.getBuffer(); try { for (; ; ) { int res = channel.read(buffer); if (res == -1) { buffer.flip(); data.write(buffer); this.complete = true; return; } else if (res == 0) { channel.awaitReadable(); } checkMaxSize(channel, res); if (!buffer.hasRemaining()) { buffer.flip(); data.write(buffer); buffer.compact(); if (!bufferFullMessage) { //if we are not reading the full message we return return; } } } } finally { pooled.close(); } }
/** * Called after data was read into the {@link ByteBuffer} * * @param buffer the {@link ByteBuffer} into which the data was read * @param position the position it was written to * @param length the number of bytes there were written * @throws IOException thrown if an error occurs */ protected void afterRead(ByteBuffer buffer, int position, int length) throws IOException { try { for (ChannelFunction func : functions) { func.afterRead(buffer, position, length); } if (isComplete()) { checkComplete(); } } catch (UnsupportedEncodingException e) { getFramedChannel().markReadsBroken(e); throw e; } }