Java Code Examples for io.netty.buffer.ByteBuf#isWritable()
The following examples show how to use
io.netty.buffer.ByteBuf#isWritable() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BSTIndex.java From tajo with Apache License 2.0 | 6 votes |
private void writeRootIndex(ByteBuf byteBuf, Tuple tuple, long offset) throws IOException { byte[] buf = rowStoreEncoder.toBytes(tuple); int size = buf.length + 12; if (!byteBuf.isWritable(size)) { byteBuf.ensureWritable(size); } // key writing byteBuf.writeInt(buf.length); byteBuf.writeBytes(buf); // leaf offset writing byteBuf.writeLong(offset); rootEntrySize++; // flush to file and reset buffer if (byteBuf.writerIndex() >= BUFFER_SIZE) { flushBuffer(byteBuf, rootOutChannel, rootOut); } }
Example 2
Source File: MessageEncoder.java From ServerCore with Apache License 2.0 | 6 votes |
@Override protected void encode(ChannelHandlerContext ctx, AbstractMessage msg, ByteBuf out) { int messageId = msgPool.getMessageId(msg); if (messageId == 0) { LOGGER.error("编码到未知的消息{}", messageId); } byte[] bytes = msg.toByteArray(); int length = Integer.BYTES + bytes.length; boolean writeAble = out.isWritable(length); if (!writeAble) { LOGGER.error("消息过大,编码失败 {} -> {}", messageId, length); return; } // int->4 out.writeInt(messageId); // ->20(假设) out.writeBytes(bytes); }
Example 3
Source File: NativeVelocityCompressor.java From Velocity with MIT License | 6 votes |
@Override public void deflate(ByteBuf source, ByteBuf destination) throws DataFormatException { ensureNotDisposed(); source.memoryAddress(); destination.memoryAddress(); while (!deflate.finished) { if (!destination.isWritable()) { destination.ensureWritable(ZLIB_BUFFER_SIZE); } int produced = deflate.process(deflateCtx, source.memoryAddress() + source.readerIndex(), source.readableBytes(), destination.memoryAddress() + destination.writerIndex(), destination.writableBytes(), true); source.readerIndex(source.readerIndex() + deflate.consumed); destination.writerIndex(destination.writerIndex() + produced); } deflate.reset(deflateCtx); deflate.consumed = 0; deflate.finished = false; }
Example 4
Source File: NativeVelocityCompressor.java From Velocity with MIT License | 6 votes |
@Override public void inflate(ByteBuf source, ByteBuf destination, int max) throws DataFormatException { ensureNotDisposed(); source.memoryAddress(); destination.memoryAddress(); while (!inflate.finished && source.isReadable()) { if (!destination.isWritable()) { ensureMaxSize(destination, max); destination.ensureWritable(ZLIB_BUFFER_SIZE); } int produced = inflate.process(inflateCtx, source.memoryAddress() + source.readerIndex(), source.readableBytes(), destination.memoryAddress() + destination.writerIndex(), destination.writableBytes()); source.readerIndex(source.readerIndex() + inflate.consumed); destination.writerIndex(destination.writerIndex() + produced); } inflate.reset(inflateCtx); inflate.consumed = 0; inflate.finished = false; }
Example 5
Source File: IndexLog.java From qmq with Apache License 2.0 | 6 votes |
private void partialCopy(ByteBuffer src, ByteBuf to) { while (to.isWritable(Long.BYTES)) { src.mark(); to.markWriterIndex(); if (!to.isWritable(Long.BYTES)) break; to.writeLong(src.getLong()); if (!to.isWritable(Long.BYTES)) { src.reset(); to.resetWriterIndex(); break; } to.writeLong(src.getLong()); // subject if (!writeString(src, to)) break; // msgId if (!writeString(src, to)) break; } }
Example 6
Source File: SslHandler.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
private static boolean attemptCopyToCumulation(ByteBuf cumulation, ByteBuf next, int wrapDataSize) { final int inReadableBytes = next.readableBytes(); final int cumulationCapacity = cumulation.capacity(); if (wrapDataSize - cumulation.readableBytes() >= inReadableBytes && // Avoid using the same buffer if next's data would make cumulation exceed the wrapDataSize. // Only copy if there is enough space available and the capacity is large enough, and attempt to // resize if the capacity is small. (cumulation.isWritable(inReadableBytes) && cumulationCapacity >= wrapDataSize || cumulationCapacity < wrapDataSize && ensureWritableSuccess(cumulation.ensureWritable(inReadableBytes, false)))) { cumulation.writeBytes(next); next.release(); return true; } return false; }
Example 7
Source File: ZstdEncoder.java From x-pipe with Apache License 2.0 | 5 votes |
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { if (finished) { if (!out.isWritable(in.readableBytes())) { // out should be EMPTY_BUFFER because we should have allocated enough space above in allocateBuffer. throw ENCODE_FINSHED_EXCEPTION; } out.writeBytes(in); return; } final ByteBuf buffer = this.buffer; int length; while ((length = in.readableBytes()) > 0) { final int nextChunkSize = Math.min(length, buffer.writableBytes()); in.readBytes(buffer, nextChunkSize); if(nextChunkSize <= MIN_BLOCK_SIZE) { writeUnCompressedData(out); return; } if (!buffer.isWritable()) { flushBufferedData(out); } } if(buffer.isReadable()) { flushBufferedData(out); } }
Example 8
Source File: JdkZlibEncoder.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelPromise promise) { if (finished) { promise.setSuccess(); return promise; } finished = true; ByteBuf footer = ctx.alloc().heapBuffer(); if (writeHeader && wrapper == ZlibWrapper.GZIP) { // Write the GZIP header first if not written yet. (i.e. user wrote nothing.) writeHeader = false; footer.writeBytes(gzipHeader); } deflater.finish(); while (!deflater.finished()) { deflate(footer); if (!footer.isWritable()) { // no more space so write it to the channel and continue ctx.write(footer); footer = ctx.alloc().heapBuffer(); } } if (wrapper == ZlibWrapper.GZIP) { int crcValue = (int) crc.getValue(); int uncBytes = deflater.getTotalIn(); footer.writeByte(crcValue); footer.writeByte(crcValue >>> 8); footer.writeByte(crcValue >>> 16); footer.writeByte(crcValue >>> 24); footer.writeByte(uncBytes); footer.writeByte(uncBytes >>> 8); footer.writeByte(uncBytes >>> 16); footer.writeByte(uncBytes >>> 24); } deflater.end(); return ctx.writeAndFlush(footer, promise); }
Example 9
Source File: NettyHandlerTestBase.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
@Test public void windowUpdateMatchesTarget() throws Exception { manualSetUp(); Http2Stream connectionStream = connection().connectionStream(); Http2LocalFlowController localFlowController = connection().local().flowController(); makeStream(); AbstractNettyHandler handler = (AbstractNettyHandler) handler(); handler.setAutoTuneFlowControl(true); ByteBuf data = ctx().alloc().buffer(1024); while (data.isWritable()) { data.writeLong(1111); } int length = data.readableBytes(); ByteBuf frame = dataFrame(3, false, data.copy()); channelRead(frame); int accumulator = length; // 40 is arbitrary, any number large enough to trigger a window update would work for (int i = 0; i < 40; i++) { channelRead(dataFrame(3, false, data.copy())); accumulator += length; } long pingData = handler.flowControlPing().payload(); channelRead(pingFrame(true, pingData)); assertEquals(accumulator, handler.flowControlPing().getDataSincePing()); assertEquals(2 * accumulator, localFlowController.initialWindowSize(connectionStream)); }
Example 10
Source File: UndertowOutputStream.java From quarkus-http with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public void write(final byte[] b, final int off, final int len) throws IOException { if (len < 1) { return; } if (exchange.getIoThread().inEventLoop()) { throw new IllegalStateException("Cannot do blocking IO from IO thread"); } if (closed) { throw new IOException("Stream is closed"); } int rem = len; int idx = off; ByteBuf buffer = pooledBuffer; try { if (buffer == null) { pooledBuffer = buffer = exchange.getBufferAllocator().allocateBuffer(); } while (rem > 0) { int toWrite = Math.min(rem, buffer.writableBytes()); buffer.writeBytes(b, idx, toWrite); rem -= toWrite; idx += toWrite; if (!buffer.isWritable()) { writeStarted = true; ByteBuf tempBuffer = buffer; this.pooledBuffer = buffer = exchange.getBufferAllocator().allocateBuffer(); exchange.getOutputChannel().writeBlocking(tempBuffer, false); } } } catch (Exception e) { if (buffer != null) { buffer.release(); } throw new IOException(e); } updateWritten(len); }
Example 11
Source File: JdkZlibEncoder.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelPromise promise) { if (finished) { promise.setSuccess(); return promise; } finished = true; ByteBuf footer = ctx.alloc().heapBuffer(); if (writeHeader && wrapper == ZlibWrapper.GZIP) { // Write the GZIP header first if not written yet. (i.e. user wrote nothing.) writeHeader = false; footer.writeBytes(gzipHeader); } deflater.finish(); while (!deflater.finished()) { deflate(footer); if (!footer.isWritable()) { // no more space so write it to the channel and continue ctx.write(footer); footer = ctx.alloc().heapBuffer(); } } if (wrapper == ZlibWrapper.GZIP) { int crcValue = (int) crc.getValue(); int uncBytes = deflater.getTotalIn(); footer.writeByte(crcValue); footer.writeByte(crcValue >>> 8); footer.writeByte(crcValue >>> 16); footer.writeByte(crcValue >>> 24); footer.writeByte(uncBytes); footer.writeByte(uncBytes >>> 8); footer.writeByte(uncBytes >>> 16); footer.writeByte(uncBytes >>> 24); } deflater.end(); return ctx.writeAndFlush(footer, promise); }
Example 12
Source File: MessageIndexSyncWorker.java From qmq with Apache License 2.0 | 5 votes |
private boolean writeLong(long value, ByteBuf to) { if (!to.isWritable(Long.BYTES)) { to.resetWriterIndex(); return false; } to.writeLong(value); return true; }
Example 13
Source File: MessageIndexSyncWorker.java From qmq with Apache License 2.0 | 5 votes |
private Control copyString(ByteBuffer from, ByteBuf to) { short len = from.getShort(); if (len <= 0) { to.resetWriterIndex(); return Control.INVALID; } byte[] str = new byte[len]; from.get(str); if (!to.isWritable(Short.BYTES + len)) { to.resetWriterIndex(); return Control.NOSPACE; } PayloadHolderUtils.writeString(str, to); return Control.OK; }
Example 14
Source File: ServletOutputStreamImpl.java From quarkus-http with Apache License 2.0 | 5 votes |
private void writeAsync(byte[] b, int off, int len) throws IOException { ByteBuf buffer = pooledBuffer; try { if (buffer == null) { pooledBuffer = buffer = exchange.allocateBuffer(); } int toWrite = Math.min(len, buffer.writableBytes()); buffer.writeBytes(b, off, toWrite); if (!buffer.isWritable()) { setFlags(FLAG_PENDING_DATA | FLAG_WRITE_STARTED); this.pooledBuffer = null; if (toWrite < len) { ByteBuf remainder = Unpooled.wrappedBuffer(b, off + toWrite, len - toWrite); buffer = Unpooled.wrappedBuffer(buffer, remainder); } exchange.writeAsync(buffer, false, listenerCallback, null); } } catch (Exception e) { if (buffer != null) { buffer.release(); this.pooledBuffer = null; } throw new IOException(e); } updateWrittenAsync(len); }
Example 15
Source File: VertxOutputStream.java From quarkus with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public void write(final byte[] b, final int off, final int len) throws IOException { if (len < 1) { return; } if (closed) { throw new IOException("Stream is closed"); } int rem = len; int idx = off; ByteBuf buffer = pooledBuffer; try { if (buffer == null) { pooledBuffer = buffer = allocator.allocateBuffer(); } while (rem > 0) { int toWrite = Math.min(rem, buffer.writableBytes()); buffer.writeBytes(b, idx, toWrite); rem -= toWrite; idx += toWrite; if (!buffer.isWritable()) { ByteBuf tmpBuf = buffer; this.pooledBuffer = buffer = allocator.allocateBuffer(); response.writeBlocking(tmpBuf, false); } } } catch (Exception e) { if (buffer != null && buffer.refCnt() > 0) { buffer.release(); } throw new IOException(e); } updateWritten(len); }
Example 16
Source File: MultipartParser.java From quarkus-http with Apache License 2.0 | 4 votes |
@Override public void handle(final PartHandler handler, final ByteBuf rawData) throws IOException { boolean equalsSeen = this.equalsSeen; byte firstCharacter = this.firstCharacter; ByteBuf buf = bufferPool.allocateBuffer(); try { while (rawData.isReadable()) { byte b = rawData.readByte(); if (equalsSeen) { if (firstCharacter == 0) { if (b == '\n' || b == '\r') { //soft line break //ignore equalsSeen = false; } else { firstCharacter = b; } } else { int result = Character.digit((char) firstCharacter, 16); result <<= 4; //shift it 4 bytes and then add the next value to the end result += Character.digit((char) b, 16); buf.writeByte((byte) result); equalsSeen = false; firstCharacter = 0; } } else if (b == '=') { equalsSeen = true; } else { buf.writeByte(b); if (!buf.isWritable()) { handler.data(buf); buf.clear(); } } } handler.data(buf); } finally { buf.release(); this.equalsSeen = equalsSeen; this.firstCharacter = firstCharacter; } }
Example 17
Source File: AbstractOioByteChannel.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
@Override protected void doRead() { final ChannelConfig config = config(); if (isInputShutdown() || !readPending) { // We have to check readPending here because the Runnable to read could have been scheduled and later // during the same read loop readPending was set to false. return; } // In OIO we should set readPending to false even if the read was not successful so we can schedule // another read on the event loop if no reads are done. readPending = false; final ChannelPipeline pipeline = pipeline(); final ByteBufAllocator allocator = config.getAllocator(); final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); allocHandle.reset(config); ByteBuf byteBuf = null; boolean close = false; boolean readData = false; try { byteBuf = allocHandle.allocate(allocator); do { allocHandle.lastBytesRead(doReadBytes(byteBuf)); if (allocHandle.lastBytesRead() <= 0) { if (!byteBuf.isReadable()) { // nothing was read. release the buffer. byteBuf.release(); byteBuf = null; close = allocHandle.lastBytesRead() < 0; if (close) { // There is nothing left to read as we received an EOF. readPending = false; } } break; } else { readData = true; } final int available = available(); if (available <= 0) { break; } // Oio collects consecutive read operations into 1 ByteBuf before propagating up the pipeline. if (!byteBuf.isWritable()) { final int capacity = byteBuf.capacity(); final int maxCapacity = byteBuf.maxCapacity(); if (capacity == maxCapacity) { allocHandle.incMessagesRead(1); readPending = false; pipeline.fireChannelRead(byteBuf); byteBuf = allocHandle.allocate(allocator); } else { final int writerIndex = byteBuf.writerIndex(); if (writerIndex + available > maxCapacity) { byteBuf.capacity(maxCapacity); } else { byteBuf.ensureWritable(available); } } } } while (allocHandle.continueReading()); if (byteBuf != null) { // It is possible we allocated a buffer because the previous one was not writable, but then didn't use // it because allocHandle.continueReading() returned false. if (byteBuf.isReadable()) { readPending = false; pipeline.fireChannelRead(byteBuf); } else { byteBuf.release(); } byteBuf = null; } if (readData) { allocHandle.readComplete(); pipeline.fireChannelReadComplete(); } if (close) { closeOnRead(pipeline); } } catch (Throwable t) { handleReadException(pipeline, byteBuf, t, close, allocHandle); } finally { if (readPending || config.isAutoRead() || !readData && isActive()) { // Reading 0 bytes could mean there is a SocketTimeout and no data was actually read, so we // should execute read() again because no data may have been read. read(); } } }
Example 18
Source File: IndexLog.java From qmq with Apache License 2.0 | 4 votes |
private void fillZero(ByteBuf buffer) { while (buffer.isWritable(Byte.BYTES)) { buffer.writeByte(0); } }
Example 19
Source File: ServletOutputStreamImpl.java From quarkus-http with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ public void write(final byte[] b, final int off, final int len) throws IOException { if (len < 1) { return; } if (anyAreSet(state, FLAG_CLOSED)) { throw UndertowMessages.MESSAGES.streamIsClosed(); } if (listener == null) { if (exchange.getIoThread().inEventLoop()) { throw UndertowMessages.MESSAGES.blockingIoFromIOThread(); } int rem = len; int idx = off; ByteBuf buffer = pooledBuffer; try { if (buffer == null) { // TODO too ugly ... is there any other way? for now I'm not replicating this throughout the class if (bufferSize > 0) { pooledBuffer = buffer = exchange.allocateBuffer(bufferSize); } else { pooledBuffer = buffer = exchange.allocateBuffer(); } } while (rem > 0) { int toWrite = Math.min(rem, buffer.writableBytes()); buffer.writeBytes(b, idx, toWrite); rem -= toWrite; idx += toWrite; if (!buffer.isWritable()) { setFlags(FLAG_WRITE_STARTED); ByteBuf tmpBuf = buffer; this.pooledBuffer = buffer = exchange.allocateBuffer(); exchange.writeBlocking(tmpBuf, false); } } } catch (Exception e) { if (buffer != null) { buffer.release(); this.pooledBuffer = null; } throw new IOException(e); } updateWritten(len); } else { writeAsync(b, off, len); } }
Example 20
Source File: ServletPrintWriter.java From quarkus-http with Apache License 2.0 | 4 votes |
public void close() { if (outputStream.getServletRequestContext().getOriginalRequest().getDispatcherType() == DispatcherType.INCLUDE) { return; } if (closed) { return; } closed = true; try { boolean done = false; CharBuffer buffer; if (underflow == null) { buffer = CharBuffer.wrap(EMPTY_CHAR); } else { buffer = CharBuffer.wrap(underflow); underflow = null; } if (charsetEncoder != null) { do { ByteBuf out = outputStream.underlyingBuffer(); if (out == null) { //servlet output stream has already been closed error = true; return; } CoderResult result = doEncoding(buffer, out, true); if (result.isOverflow()) { out = outputStream.flushInternal(); if (!out.isWritable()) { outputStream.close(); error = true; return; } } else { done = true; } } while (!done); } outputStream.close(); } catch (IOException e) { error = true; } }