Refine search
/** * Push a buffer into the queue. There is no mechanism to limit the number of pushed buffers; if such a mechanism * is desired, it must be implemented externally, for example maybe using a {@link Semaphore}. * * @param pooledBuffer the buffer from which more data should be read */ public void push(final Pooled<ByteBuffer> pooledBuffer) { synchronized (this) { if (pooledBuffer.getResource().hasRemaining() && !eof && failure == null) { queue.add(pooledBuffer); notifyAll(); } else { pooledBuffer.free(); } } }
public void free() { if (freedUpdater.compareAndSet(this, 0, 1)) { zero(allocated.getResource()); allocated.free(); } }
/** * Push a buffer back to the head of the stream. Once the buffer data is consumed, it will * be released back to its original pool (if any). * * @param pooledBuffer the buffer to push back */ public void pushBack(Pooled<ByteBuffer> pooledBuffer) { if (pooledBuffer == null) { return; } if (shutdown || ! pooledBuffer.getResource().hasRemaining()) { pooledBuffer.free(); } else { current = new BufferConduit(current, pooledBuffer); } }
public XnioByteBufferPool(Pool<ByteBuffer> pool) { this.pool = pool; Pooled<ByteBuffer> buf = pool.allocate(); bufferSize = buf.getResource().remaining(); direct = !buf.getResource().hasArray(); buf.free(); if(direct) { arrayBackedPool = new DefaultByteBufferPool(false, bufferSize); } else { arrayBackedPool = this; } }
@Override protected void onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) { this.session.handleMessage(Type.BINARY, toMessage(Type.BINARY, message.getData().getResource())); message.getData().free(); }
@Override protected void onFullPongMessage(WebSocketChannel channel, BufferedBinaryMessage message) { this.session.handleMessage(Type.PONG, toMessage(Type.PONG, message.getData().getResource())); message.getData().free(); }
private void asyncCloseMessage(final int id) { Pooled<ByteBuffer> pooled = connection.allocate(); boolean ok = false; try { ByteBuffer byteBuffer = pooled.getResource(); byteBuffer.put(Protocol.MESSAGE_CLOSE); byteBuffer.putInt(channelId); byteBuffer.putShort((short) id); byteBuffer.flip(); ok = true; connection.send(pooled); } finally { if (! ok) pooled.free(); } }
public Pooled<ByteBuffer> getBuffer(boolean firstBuffer) throws IOException { Pooled<ByteBuffer> pooled = allocate(Protocol.MESSAGE_DATA); boolean ok = false; try { ByteBuffer buffer = pooled.getResource(); //Reserve room for the transmit data which is 4 bytes buffer.limit(buffer.limit() - 4); buffer.put(firstBuffer ? Protocol.MSG_FLAG_NEW : 0); // flags // header size plus window size int windowPlusHeader = maximumWindow + 8; if (buffer.remaining() > windowPlusHeader) { // never try to write more than the maximum window size buffer.limit(windowPlusHeader); } ok = true; return pooled; } finally { if (! ok) pooled.free(); } }
protected void shutdownReadsAction(final boolean writeComplete) throws IOException { synchronized (readLock) { log.tracef("Shutting down reads on %s", this); try { receiveBuffer.getResource().clear(); } catch (Throwable t) { } try { receiveBuffer.free(); } catch (Throwable t) { } } channel.shutdownReads(); }
static void sendCloseRequestBody(RemoteConnection remoteConnection) { final Pooled<ByteBuffer> pooled = remoteConnection.allocate(); boolean ok = false; try { final ByteBuffer buffer = pooled.getResource(); buffer.put(Protocol.CONNECTION_CLOSE); buffer.flip(); remoteConnection.send(pooled, true); ok = true; } finally { if (! ok) { pooled.free(); } } }
private boolean flushLocal() throws IOException { try { final ByteBuffer buffer = pooledBuffer.getResource(); if (buffer.position() > 0) { buffer.flip(); try { for (;;) { super.write(buffer); if (! buffer.hasRemaining()) { if (terminate) { pooledBuffer.free(); } return true; } } } finally { buffer.compact(); } } else { return true; } } catch (IllegalStateException ignored) { return true; } }
final ByteBuffer src; try { src = buffer.getResource(); final int pos = src.position(); final int rem = src.remaining(); buffer.free(); } else { return cnt;
public void sendAuthDelete(final int id) throws IOException { log.tracef("Sending authentication delete for ID %08x", id); // todo: allocate small buffer final Pooled<ByteBuffer> pooled = remoteConnection.allocate(); boolean ok = false; try { final ByteBuffer buffer = pooled.getResource(); buffer.put(Protocol.APP_AUTH_DELETE); buffer.putInt(id); buffer.flip(); remoteConnection.send(pooled); ok = true; } finally { if (! ok) pooled.free(); } }
final ByteBuffer src; try { src = pooledBuffer.getResource(); final int pos = src.position(); final int rem = src.remaining(); pooledBuffer.free(); } else { return cnt;
public void sendAuthReject(final int id) throws IOException { log.tracef("Sending authentication reject for ID %08x", id); // todo: allocate small buffer final Pooled<ByteBuffer> pooled = remoteConnection.allocate(); boolean ok = false; try { final ByteBuffer buffer = pooled.getResource(); buffer.put(Protocol.APP_AUTH_REJECT); buffer.putInt(id); buffer.flip(); remoteConnection.send(pooled); ok = true; } finally { if (! ok) pooled.free(); } }
public void writeShutdown() throws IOException { if (closeWrites()) { Pooled<ByteBuffer> pooled = connection.allocate(); boolean ok = false; try { ByteBuffer byteBuffer = pooled.getResource(); byteBuffer.put(Protocol.CHANNEL_SHUTDOWN_WRITE); byteBuffer.putInt(channelId); byteBuffer.flip(); connection.send(pooled); ok = true; } finally { if (! ok) pooled.free(); } } }
void rejectAuthentication(String mechName) { // reject log.rejectedInvalidMechanism(mechName); final Pooled<ByteBuffer> pooled = connection.allocate(); boolean ok = false; try { final ByteBuffer sendBuffer = pooled.getResource(); sendBuffer.put(Protocol.AUTH_REJECTED); sendBuffer.flip(); connection.send(pooled); ok = true; } finally { if (! ok) pooled.free(); } }
public void sendAuthDeleteAck(final int id) throws IOException { log.tracef("Sending authentication delete ack for ID %08x", id); // todo: allocate small buffer final Pooled<ByteBuffer> pooled = remoteConnection.allocate(); boolean ok = false; try { final ByteBuffer buffer = pooled.getResource(); buffer.put(Protocol.APP_AUTH_DELETE_ACK); buffer.putInt(id); buffer.flip(); remoteConnection.send(pooled); ok = true; } finally { if (! ok) pooled.free(); } }
public long read(final ByteBuffer[] dsts, final int offset, final int length) throws IOException { long cnt; try { final ByteBuffer src = buffer.getResource(); cnt = Buffers.copy(dsts, offset, length, src); if (src.hasRemaining()) { return cnt; } final StreamSourceChannel next = channel = this.next; buffer.free(); if (cnt > 0L && next == firstChannel) { // don't hit the main channel until the user wants to return cnt; } } catch (IllegalStateException ignored) { channel = next; cnt = 0; } final long res = next.read(dsts, offset, length); return res > 0 ? res + cnt : cnt > 0 ? cnt : res; }
public int read(final ByteBuffer dst) throws IOException { int cnt; if (! dst.hasRemaining()) { return 0; } final StreamSourceConduit next = this.next; try { final ByteBuffer src = pooledBuffer.getResource(); cnt = Buffers.copy(dst, src); if (src.hasRemaining()) { return cnt; } current = next; pooledBuffer.free(); if (cnt > 0 && next == PushBackStreamSourceConduit.this.next) { // don't hit the main channel until the user wants to return cnt; } } catch (IllegalStateException ignored) { current = next; cnt = 0; } final int res = next.read(dst); return res > 0 ? res + cnt : cnt > 0 ? cnt : res; }