Java Code Examples for io.netty.buffer.ByteBuf#writerIndex()
The following examples show how to use
io.netty.buffer.ByteBuf#writerIndex() .
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: MessageDecoder.java From consulo with Apache License 2.0 | 6 votes |
public static boolean readUntil(char what, @Nonnull ByteBuf buffer, @Nonnull StringBuilder builder) { int i = buffer.readerIndex(); //noinspection ForLoopThatDoesntUseLoopVariable for (int n = buffer.writerIndex(); i < n; i++) { char c = (char)buffer.getByte(i); if (c == what) { buffer.readerIndex(i + 1); return true; } else { builder.append(c); } } buffer.readerIndex(i); return false; }
Example 2
Source File: ConnectedDataItemRequest.java From ethernet-ip with Apache License 2.0 | 6 votes |
public static ByteBuf encode(ConnectedDataItemRequest item, ByteBuf buffer) { buffer.writeShort(item.getTypeId()); // Length placeholder... int lengthStartIndex = buffer.writerIndex(); buffer.writeShort(0); // Encode the encapsulated data... int dataStartIndex = buffer.writerIndex(); item.getEncoder().accept(buffer); // Go back and update the length. int bytesWritten = buffer.writerIndex() - dataStartIndex; buffer.markWriterIndex(); buffer.writerIndex(lengthStartIndex); buffer.writeShort(bytesWritten); buffer.resetWriterIndex(); return buffer; }
Example 3
Source File: ZstdEncoder.java From x-pipe with Apache License 2.0 | 6 votes |
private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelPromise promise) { if (finished) { promise.setSuccess(); return promise; } finished = true; final ByteBuf footer = ctx.alloc().ioBuffer( (int) Zstd.compressBound(buffer.readableBytes()) + HEADER_LENGTH); flushBufferedData(footer); final int idx = footer.writerIndex(); footer.setInt(idx, MAGIC_NUMBER); footer.setByte(idx + TOKEN_OFFSET, (byte) (BLOCK_TYPE_NON_COMPRESSED | compressionLevel)); footer.setInt(idx + COMPRESSED_LENGTH_OFFSET, 0); footer.setInt(idx + DECOMPRESSED_LENGTH_OFFSET, 0); footer.setInt(idx + CHECKSUM_OFFSET, 0); footer.writerIndex(idx + HEADER_LENGTH); return ctx.writeAndFlush(footer, promise); }
Example 4
Source File: DiscoveryServiceTest.java From pulsar with Apache License 2.0 | 6 votes |
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { try { ByteBuf buffer = (ByteBuf) msg; buffer.readUnsignedInt(); // discard frame length int cmdSize = (int) buffer.readUnsignedInt(); buffer.writerIndex(buffer.readerIndex() + cmdSize); ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(buffer); BaseCommand.Builder cmdBuilder = BaseCommand.newBuilder(); BaseCommand cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build(); cmdInputStream.recycle(); cmdBuilder.recycle(); buffer.release(); promise.complete(cmd); } catch (Exception e) { promise.completeExceptionally(e); } ctx.close(); }
Example 5
Source File: DataTypeCodec.java From vertx-sql-client with Apache License 2.0 | 6 votes |
private static <T> void binaryEncodeArray(T[] values, DataType type, ByteBuf buff){ int startIndex = buff.writerIndex(); buff.writeInt(1); // ndim buff.writeInt(0); // dataoffset buff.writeInt(type.id); // elemtype buff.writeInt(values.length); // dimension buff.writeInt(1); // lower bnds boolean hasNulls = false; for (T value : values) { if (value == null) { hasNulls = true; buff.writeInt(-1); } else { int idx = buff.writerIndex(); buff.writeInt(0); encodeBinary(type, value, buff); buff.setInt(idx, buff.writerIndex() - idx - 4); } } if (hasNulls) { buff.setInt(startIndex + 4, 1); } }
Example 6
Source File: CompressionCodecSnappyJNI.java From pulsar with Apache License 2.0 | 6 votes |
@Override public ByteBuf encode(ByteBuf source) { int uncompressedLength = source.readableBytes(); int maxLength = Snappy.maxCompressedLength(uncompressedLength); ByteBuffer sourceNio = source.nioBuffer(source.readerIndex(), source.readableBytes()); ByteBuf target = PooledByteBufAllocator.DEFAULT.buffer(maxLength, maxLength); ByteBuffer targetNio = target.nioBuffer(0, maxLength); int compressedLength = 0; try { compressedLength = Snappy.compress(sourceNio, targetNio); } catch (IOException e) { log.error("Failed to compress to Snappy: {}", e.getMessage()); } target.writerIndex(compressedLength); return target; }
Example 7
Source File: NextHopUtil.java From bgpcep with Eclipse Public License 1.0 | 5 votes |
/** * Parses CNextHop IP address from given ByteBuf. * * @param buffer contains byte array representation of CNextHop * @return CNexthop object */ public static CNextHop parseNextHop(final ByteBuf buffer) { switch (buffer.writerIndex()) { case Ipv4Util.IP4_LENGTH: return parseNextHopIpv4(buffer); case Ipv6Util.IPV6_LENGTH: return parseNextHopIpv6(buffer); case Ipv6Util.IPV6_LENGTH * 2: return parseNextHopFullIpv6(buffer); default: throw new IllegalArgumentException("Cannot parse NEXT_HOP attribute. Wrong bytes length: " + buffer.writerIndex()); } }
Example 8
Source File: ChunkDecoder.java From opc-ua-stack with Apache License 2.0 | 5 votes |
private void decryptChunk(Delegate delegate, SecureChannel channel, ByteBuf chunkBuffer) throws UaException { int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel); int blockCount = chunkBuffer.readableBytes() / cipherTextBlockSize; int plainTextBufferSize = cipherTextBlockSize * blockCount; ByteBuf plainTextBuffer = BufferUtil.buffer(plainTextBufferSize); ByteBuffer plainTextNioBuffer = plainTextBuffer .writerIndex(plainTextBufferSize) .nioBuffer(); ByteBuffer chunkNioBuffer = chunkBuffer.nioBuffer(); try { Cipher cipher = delegate.getCipher(channel); assert (chunkBuffer.readableBytes() % cipherTextBlockSize == 0); if (delegate instanceof AsymmetricDelegate) { for (int blockNumber = 0; blockNumber < blockCount; blockNumber++) { chunkNioBuffer.limit(chunkNioBuffer.position() + cipherTextBlockSize); cipher.doFinal(chunkNioBuffer, plainTextNioBuffer); } } else { cipher.doFinal(chunkNioBuffer, plainTextNioBuffer); } } catch (GeneralSecurityException e) { throw new UaException(StatusCodes.Bad_SecurityChecksFailed, e); } /* Write plainTextBuffer back into the chunk buffer we decrypted from. */ plainTextNioBuffer.flip(); // limit = pos, pos = 0 chunkBuffer.writerIndex(chunkBuffer.readerIndex()); chunkBuffer.writeBytes(plainTextNioBuffer); plainTextBuffer.release(); }
Example 9
Source File: ProxyProtocolHandler.java From nomulus with Apache License 2.0 | 5 votes |
/** * Returns the index in the buffer of the end of line found. Returns -1 if no end of line was * found in the buffer. */ private static int findEndOfLine(final ByteBuf buffer) { final int n = buffer.writerIndex(); for (int i = buffer.readerIndex(); i < n; i++) { final byte b = buffer.getByte(i); if (b == '\r' && i < n - 1 && buffer.getByte(i + 1) == '\n') { return i; // \r\n } } return -1; // Not found. }
Example 10
Source File: AMQPMessageTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
private AMQPStandardMessage encodeAndDecodeMessage(MessageImpl message) { ByteBuf nettyBuffer = Unpooled.buffer(1500); message.encode(new NettyWritable(nettyBuffer)); byte[] bytes = new byte[nettyBuffer.writerIndex()]; nettyBuffer.readBytes(bytes); return new AMQPStandardMessage(0, bytes, null); }
Example 11
Source File: Snappy.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
/** * Reads a compressed reference offset and length from the supplied input * buffer, seeks back to the appropriate place in the input buffer and * writes the found data to the supplied output stream. * * @param tag The tag used to identify this as a copy is also used to encode * the length and part of the offset * @param in The input buffer to read from * @param out The output buffer to write to * @return The number of bytes appended to the output buffer, or -1 to indicate * "try again later" * @throws DecompressionException If the read offset is invalid */ private static int decodeCopyWith1ByteOffset(byte tag, ByteBuf in, ByteBuf out, int writtenSoFar) { if (!in.isReadable()) { return NOT_ENOUGH_INPUT; } int initialIndex = out.writerIndex(); int length = 4 + ((tag & 0x01c) >> 2); int offset = (tag & 0x0e0) << 8 >> 5 | in.readUnsignedByte(); validateOffset(offset, writtenSoFar); out.markReaderIndex(); if (offset < length) { int copies = length / offset; for (; copies > 0; copies--) { out.readerIndex(initialIndex - offset); out.readBytes(out, offset); } if (length % offset != 0) { out.readerIndex(initialIndex - offset); out.readBytes(out, length % offset); } } else { out.readerIndex(initialIndex - offset); out.readBytes(out, length); } out.resetReaderIndex(); return length; }
Example 12
Source File: ProtonWritableBufferImplTest.java From vertx-proton with Apache License 2.0 | 5 votes |
@Test public void testHasRemaining() { ByteBuf buffer = Unpooled.buffer(100, 100); ProtonWritableBufferImpl writable = new ProtonWritableBufferImpl(buffer); assertTrue(writable.hasRemaining()); writable.put((byte) 0); assertTrue(writable.hasRemaining()); buffer.writerIndex(buffer.maxCapacity()); assertFalse(writable.hasRemaining()); }
Example 13
Source File: SnappyFramedEncoder.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
private static void setChunkLength(ByteBuf out, int lengthIdx) { int chunkLength = out.writerIndex() - lengthIdx - 3; if (chunkLength >>> 24 != 0) { throw new CompressionException("compressed data too large: " + chunkLength); } out.setMedium(lengthIdx, ByteBufUtil.swapMedium(chunkLength)); }
Example 14
Source File: Kcp.java From kcp-netty with MIT License | 5 votes |
private static int encodeSeg(ByteBuf buf, Segment seg) { int offset = buf.writerIndex(); buf.writeIntLE(seg.conv); buf.writeByte(seg.cmd); buf.writeByte(seg.frg); buf.writeShortLE(seg.wnd); buf.writeIntLE((int) seg.ts); buf.writeIntLE((int) seg.sn); buf.writeIntLE((int) seg.una); buf.writeIntLE(seg.data.readableBytes()); return buf.writerIndex() - offset; }
Example 15
Source File: FastestSerializer.java From rpc-benchmark with Apache License 2.0 | 5 votes |
public static void writeResponse(ByteBuf byteBuf, Response response) throws IOException { int beginWriterIndex = byteBuf.writerIndex(); byteBuf.writeInt(0); responseSerializer.write(byteBuf, response); int finishWriterIndex = byteBuf.writerIndex(); int length = finishWriterIndex - beginWriterIndex - 4; byteBuf.setInt(beginWriterIndex, length); }
Example 16
Source File: PullMessageResultCommandV4.java From hermes with Apache License 2.0 | 5 votes |
private void writeBatchDatas(ByteBuf buf, HermesPrimitiveCodec codec, List<TppConsumerMessageBatch> batches) { for (TppConsumerMessageBatch batch : batches) { // placeholder for len int start = buf.writerIndex(); codec.writeInt(-1); int indexBeforeData = buf.writerIndex(); batch.getTransferCallback().transfer(buf); int indexAfterData = buf.writerIndex(); buf.writerIndex(start); codec.writeInt(indexAfterData - indexBeforeData); buf.writerIndex(indexAfterData); } }
Example 17
Source File: FetchCodec.java From joyqueue with Apache License 2.0 | 4 votes |
@Override public void encode(FetchResponse payload, ByteBuf buffer) throws Exception { short version = payload.getVersion(); if (version >= 1) { buffer.writeInt(payload.getThrottleTimeMs()); } Map<String, List<FetchResponse.PartitionResponse>> partitionResponseMap = payload.getPartitionResponses(); buffer.writeInt(partitionResponseMap.size()); for (Map.Entry<String, List<FetchResponse.PartitionResponse>> entry : partitionResponseMap.entrySet()) { try { Serializer.write(entry.getKey(), buffer, Serializer.SHORT_SIZE); } catch (Exception e) { throw new TransportException.CodecException(e); } List<FetchResponse.PartitionResponse> partitionResponses = entry.getValue(); buffer.writeInt(partitionResponses.size()); for (FetchResponse.PartitionResponse partitionResponse : partitionResponses) { buffer.writeInt(partitionResponse.getPartition()); buffer.writeShort(partitionResponse.getError()); buffer.writeLong(partitionResponse.getHighWater()); // not fully supported, just make it compatible if (version >= 4) { // last_stable_offset buffer.writeLong(partitionResponse.getLastStableOffset()); // log_start_offset if (version >= 5) { buffer.writeLong(partitionResponse.getLogStartOffset()); } // aborted_transactions // size buffer.writeInt(0); // producer_id // first_offset } int startIndex = buffer.writerIndex(); buffer.writeInt(0); // length KafkaMessageSerializer.writeMessages(buffer, partitionResponse.getMessages(), payload.getVersion()); int length = buffer.writerIndex() - startIndex - 4; buffer.setInt(startIndex, length); } } }
Example 18
Source File: GCMCryptoService.java From ambry with Apache License 2.0 | 4 votes |
/** * Helper function to encrypt ByteBuf with the given key. When useFixedIv is true, don't use a random iv byte * array, using a all zero byte array instead. Only set it to be true in test. * @param toEncrypt {@link ByteBuf} that needs to be encrypted * @param key the secret key (of type T) to use to encrypt * @param iv If null, will create a random byte array serve as iv bytes. * @return the {@link ByteBuf} containing the encrypted content. Ensure the result has all * the information like the IV along with the encrypted content, in order to decrypt the content with a given key * @throws {@link GeneralSecurityException} on any exception with encryption */ public ByteBuf encrypt(ByteBuf toEncrypt, SecretKeySpec key, byte[] iv) throws GeneralSecurityException { try { Cipher encrypter = Cipher.getInstance(GCM_CRYPTO_INSTANCE, "BC"); if (iv == null) { iv = new byte[ivValSize]; random.nextBytes(iv); } encrypter.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); int outputSize = encrypter.getOutputSize(toEncrypt.readableBytes()); // stick with heap memory for now so to compare with the java.nio.ByteBuffer. ByteBuf encryptedContent = ByteBufAllocator.DEFAULT.heapBuffer(IVRecord_Format_V1.getIVRecordSize(iv) + outputSize); IVRecord_Format_V1.serializeIVRecord(encryptedContent, iv); boolean toRelease = false; if (toEncrypt.nioBufferCount() != 1) { toRelease = true; ByteBuf temp = ByteBufAllocator.DEFAULT.heapBuffer(toEncrypt.readableBytes()); temp.writeBytes(toEncrypt); toEncrypt = temp; } try { ByteBuffer toEncryptBuffer = toEncrypt.nioBuffer(); ByteBuffer encryptedContentBuffer = encryptedContent.nioBuffer(encryptedContent.writerIndex(), encryptedContent.capacity() - encryptedContent.writerIndex()); int n = encrypter.doFinal(toEncryptBuffer, encryptedContentBuffer); encryptedContent.writerIndex(encryptedContent.writerIndex() + n); return encryptedContent; } finally { if (toRelease) { toEncrypt.release(); } else { toEncrypt.skipBytes(toEncrypt.readableBytes()); } } } catch (Exception e) { throw new GeneralSecurityException("Exception thrown while encrypting data", e); } }
Example 19
Source File: CachedResource.java From quarkus-http with Apache License 2.0 | 4 votes |
@Override public void serveRangeAsync(OutputChannel sender, HttpServerExchange exchange, long start, long end) { final DirectBufferCache dataCache = cachingResourceManager.getDataCache(); if (dataCache == null) { ((RangeAwareResource) underlyingResource).serveRangeAsync(sender, exchange, start, end); return; } final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey); final Long length = getContentLength(); //if it is not eligible to be served from the cache if (length == null || length > cachingResourceManager.getMaxFileSize()) { ((RangeAwareResource) underlyingResource).serveRangeAsync(sender, exchange, start, end); return; } //it is not cached yet, just serve it directly if (existing == null || !existing.enabled() || !existing.reference()) { //it is not cached yet, we can't use a range request to establish the cached item //so we just serve it ((RangeAwareResource) underlyingResource).serveRangeAsync(sender, exchange, start, end); } else { //serve straight from the cache ByteBuf[] buffers; boolean ok = false; try { LimitedBufferSlicePool.PooledByteBuffer[] pooled = existing.buffers(); buffers = new ByteBuf[pooled.length]; for (int i = 0; i < buffers.length; i++) { // Keep position from mutating buffers[i] = pooled[i].getBuffer().duplicate(); } ok = true; } finally { if (!ok) { existing.dereference(); } } if (start > 0) { long startDec = start; long endCount = 0; //handle the start of the range for (ByteBuf b : buffers) { if (endCount == end) { b.clear(); continue; } else if (endCount + b.readableBytes() < end) { endCount += b.readableBytes(); } else { b.writerIndex((int) (b.readerIndex() + (end - endCount))); endCount = end; } if (b.readableBytes() >= startDec) { startDec = 0; b.readerIndex((int) (b.readerIndex() + startDec)); } else { startDec -= b.readableBytes(); b.clear(); } } } sender.writeAsync(Unpooled.wrappedBuffer(buffers), true, new DereferenceCallback(existing, IoCallback.END_EXCHANGE), null); } }
Example 20
Source File: MySQLBinlogEventPacketDecoder.java From shardingsphere with Apache License 2.0 | 4 votes |
private void removeChecksum(final int eventType, final ByteBuf in) { if (0 < binlogContext.getChecksumLength() && MySQLBinlogEventType.FORMAT_DESCRIPTION_EVENT.getValue() != eventType) { in.writerIndex(in.writerIndex() - binlogContext.getChecksumLength()); } }