io.netty.buffer.ByteBuf Java Examples

The following examples show how to use io.netty.buffer.ByteBuf. 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: AbstractPsync.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf getRequest() {

	Pair<String, Long> requestInfo = getRequestMasterInfo();

	replIdRequest = requestInfo.getKey();
	offsetRequest = requestInfo.getValue();

	if (replIdRequest == null) {
		replIdRequest = "?";
		offsetRequest = -1;
	}
	RequestStringParser requestString = new RequestStringParser(getName(), replIdRequest,
			String.valueOf(offsetRequest));
	if (getLogger().isDebugEnabled()) {
		getLogger().debug("[doRequest]{}, {}", this, StringUtil.join(" ", requestString.getPayload()));
	}
	return requestString.format();
}
 
Example #2
Source File: ZeroQueueConsumerImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
void receiveIndividualMessagesFromBatch(MessageMetadata msgMetadata, int redeliveryCount,
        List<Long> ackSet,
        ByteBuf uncompressedPayload, MessageIdData messageId, ClientCnx cnx) {
    log.warn(
            "Closing consumer [{}]-[{}] due to unsupported received batch-message with zero receiver queue size",
            subscription, consumerName);
    // close connection
    closeAsync().handle((ok, e) -> {
        // notify callback with failure result
        notifyPendingReceivedCallback(null,
                new PulsarClientException.InvalidMessageException(
                        format("Unsupported Batch message with 0 size receiver queue for [%s]-[%s] ",
                                subscription, consumerName)));
        return null;
    });
}
 
Example #3
Source File: GenericFrameCodecTest.java    From rsocket-java with Apache License 2.0 6 votes vote down vote up
@Test
void requestResponseData() {
  ByteBuf request =
      RequestResponseFrameCodec.encode(
          ByteBufAllocator.DEFAULT,
          1,
          false,
          null,
          Unpooled.copiedBuffer("d", StandardCharsets.UTF_8));

  String data = RequestResponseFrameCodec.data(request).toString(StandardCharsets.UTF_8);
  ByteBuf metadata = RequestResponseFrameCodec.metadata(request);

  assertFalse(FrameHeaderCodec.hasMetadata(request));
  assertEquals("d", data);
  assertNull(metadata);
  request.release();
}
 
Example #4
Source File: BeatsFrameDecoderTest.java    From graylog-plugin-beats with GNU General Public License v3.0 6 votes vote down vote up
private ByteBuf buildCompressedFrame(byte[] payload, int compressionLevel) {
    final Deflater deflater = new Deflater(compressionLevel);
    deflater.setInput(payload);
    deflater.finish();

    final byte[] compressedPayload = new byte[1024];
    final int compressedPayloadLength = deflater.deflate(compressedPayload);
    deflater.end();

    final ByteBuf buffer = Unpooled.buffer(6 + compressedPayloadLength);
    buffer.writeByte('2');
    buffer.writeByte('C');
    // Compressed payload length
    buffer.writeInt(compressedPayloadLength);
    // Compressed payload
    buffer.writeBytes(compressedPayload, 0, compressedPayloadLength);
    return buffer;
}
 
Example #5
Source File: DefaultNettyDecoder.java    From PeonyFramwork with Apache License 2.0 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> list) throws Exception {
    try {
        int readAble = in.readableBytes();
        if (readAble < headSize) {
            return;
        }

        in.markReaderIndex();
        int size = in.readInt();
        if (readAble < (size + headSize)) {
            in.resetReaderIndex();
            return;
        }

        // FIXME wjm 最好改了
        try (ObjectInputStream oin = new ObjectInputStream(new ByteBufInputStream(in.readBytes(size), true))) {
            list.add(oin.readObject());
        }
    } catch (Throwable e) {
        logger.error("decode error ", e);
    }
}
 
Example #6
Source File: AltsTsiFrameProtector.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
void protectFlush(
    List<ByteBuf> unprotectedBufs, Consumer<ByteBuf> ctxWrite, ByteBufAllocator alloc)
    throws GeneralSecurityException {
  checkState(crypter != null, "Cannot protectFlush after destroy.");
  ByteBuf protectedBuf;
  try {
    protectedBuf = handleUnprotected(unprotectedBufs, alloc);
  } finally {
    for (ByteBuf buf : unprotectedBufs) {
      buf.release();
    }
  }
  if (protectedBuf != null) {
    ctxWrite.accept(protectedBuf);
  }
}
 
Example #7
Source File: KcpRttExampleClient.java    From java-Kcp with Apache License 2.0 6 votes vote down vote up
@Override
public void onConnected(Ukcp ukcp) {
    future = scheduleSrv.scheduleWithFixedDelay(() -> {
        ByteBuf byteBuf = rttMsg(++count);
        ukcp.writeOrderedReliableMessage(byteBuf);
        byteBuf.release();
        if (count >= rtts.length) {
            // finish
            future.cancel(true);
            byteBuf = rttMsg(-1);
            ukcp.writeOrderedReliableMessage(byteBuf);
            byteBuf.release();

        }
    }, 20, 20, TimeUnit.MILLISECONDS);
}
 
Example #8
Source File: ReqOrderAction.java    From ftdc with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf write(ByteBuf buffer) {
	buffer.writeBytes(brokerID);
	buffer.writeBytes(investorID);
	buffer.writeInt(orderActionRef);
	buffer.writeBytes(orderRef);
	buffer.writeInt(requestID);
	buffer.writeInt(frontID);
	buffer.writeInt(sessionID);
	buffer.writeBytes(exchangeID);
	buffer.writeBytes(orderSysID);
	buffer.writeBytes(actionFlag);
	buffer.writeDouble(limitPrice);
	buffer.writeInt(volumeChange);
	buffer.writeBytes(userID);
	buffer.writeBytes(instrumentID);
	buffer.writeBytes(investUnitID);
	buffer.writeBytes(iPAddress);
	buffer.writeBytes(macAddress);

	return buffer;
}
 
Example #9
Source File: AbstractTwoOctetAsExtendedCommunityTest.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testGetType() {
    final AbstractTwoOctetAsExtendedCommunity abstractTwoOctetAsExtendedCommunity =
            new AbstractTwoOctetAsExtendedCommunity() {
        @Override
        public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity,
                final ByteBuf byteAggregator) {
        }

        @Override
        public int getSubType() {
            return 0;
        }

        @Override
        public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer)  {
            return null;
        }
    };
    Assert.assertEquals(0, abstractTwoOctetAsExtendedCommunity.getType(true));
    Assert.assertEquals(64, abstractTwoOctetAsExtendedCommunity.getType(false));
}
 
Example #10
Source File: MessageDecoder.java    From consulo with Apache License 2.0 6 votes vote down vote up
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 #11
Source File: PendingWriteQueueTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static void assertWrite(ChannelHandler handler, int count) {
    final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.US_ASCII);
    final EmbeddedChannel channel = new EmbeddedChannel(handler);
    channel.config().setWriteBufferLowWaterMark(1);
    channel.config().setWriteBufferHighWaterMark(3);

    ByteBuf[] buffers = new ByteBuf[count];
    for (int i = 0; i < buffers.length; i++) {
        buffers[i] = buffer.retainedDuplicate();
    }
    assertTrue(channel.writeOutbound(buffers));
    assertTrue(channel.finish());
    channel.closeFuture().syncUninterruptibly();

    for (int i = 0; i < buffers.length; i++) {
        assertBuffer(channel, buffer);
    }
    buffer.release();
    assertNull(channel.readOutbound());
}
 
Example #12
Source File: AddPlayerSerializer_v361.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, AddPlayerPacket packet) {
    BedrockUtils.writeUuid(buffer, packet.getUuid());
    BedrockUtils.writeString(buffer, packet.getUsername());
    VarInts.writeLong(buffer, packet.getUniqueEntityId());
    VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
    BedrockUtils.writeString(buffer, packet.getPlatformChatId());
    BedrockUtils.writeVector3f(buffer, packet.getPosition());
    BedrockUtils.writeVector3f(buffer, packet.getMotion());
    BedrockUtils.writeVector3f(buffer, packet.getRotation());
    BedrockUtils.writeItemData(buffer, packet.getHand());
    BedrockUtils.writeEntityData(buffer, packet.getMetadata());
    AdventureSettingsSerializer_v361.INSTANCE.serialize(buffer, packet.getAdventureSettings());
    BedrockUtils.writeArray(buffer, packet.getEntityLinks(), BedrockUtils::writeEntityLink);
    BedrockUtils.writeString(buffer, packet.getDeviceId());
}
 
Example #13
Source File: S3AsyncByteReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> readFully(long offset, ByteBuf dst, int dstOffset, int len) {
  if(len == 0) {
    throw new IllegalArgumentException("Empty reads not allowed.");
  }
  logger.debug("Starting read of {}.{} for range {}..{}", bucket, path, offset, offset + len);
  Stopwatch w = Stopwatch.createStarted();
  numOutstandingReads.incrementAndGet();
  CompletableFuture<Void> future = client.getObject(
    GetObjectRequest.builder()
      .range(range(offset, len))
      .bucket(bucket)
      .key(path)
      .build(),
    new ByteRangeReader(dst, dstOffset));

  return future.whenComplete((a,b) -> {
    int numOutstanding = numOutstandingReads.decrementAndGet();
    if (b == null) {
      // no exception
      logger.debug("Finished read of {}.{} for range {}..{} in {}ms.", bucket, path, offset, offset + len, w.elapsed(TimeUnit.MILLISECONDS));
      return;
    }

    // exception
    logger.warn("Async read of {}.{} for length {} failed in {}ms when there are {} outstanding reads. Error {}", bucket, path, len, w.elapsed(TimeUnit.MILLISECONDS), numOutstanding, b);
  });
}
 
Example #14
Source File: ClientCommandPacket.java    From ProtocolSupportBungee with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Collection<ByteBuf> toData(ClientStatus packet) {
	if (packet.getPayload() == 1) {
		ByteBuf data = Allocator.allocateBuffer();
		data.writeByte(LegacyPacketId.Serverbound.LOGIN_PLAY_CLIENT_COMMAND);
		data.writeByte(packet.getPayload());
		return Collections.singletonList(data);
	} else {
		return Collections.emptyList();
	}
}
 
Example #15
Source File: GSVRoutingMetadata.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf getContent() {
    List<String> tags = new ArrayList<>();
    tags.add(assembleRoutingKey());
    //method included in routing key
    /*if (method != null && !method.isEmpty()) {
        tags.add("m=" + method);
    }*/
    if (endpoint != null && !endpoint.isEmpty()) {
        tags.add("e=" + endpoint);
    }
    return TaggingMetadataFlyweight.createTaggingContent(PooledByteBufAllocator.DEFAULT, tags);
}
 
Example #16
Source File: ModbusResponseEncoder.java    From modbus with Apache License 2.0 5 votes vote down vote up
private ByteBuf encodeWriteSingleCoil(WriteSingleCoilResponse response, ByteBuf buffer) {
    buffer.writeByte(response.getFunctionCode().getCode());
    buffer.writeShort(response.getAddress());
    buffer.writeShort(response.getValue());

    return buffer;
}
 
Example #17
Source File: AddEntitySerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, AddEntityPacket packet) {
    VarInts.writeLong(buffer, packet.getUniqueEntityId());
    VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
    BedrockUtils.writeString(buffer, packet.getIdentifier());
    BedrockUtils.writeVector3f(buffer, packet.getPosition());
    BedrockUtils.writeVector3f(buffer, packet.getMotion());
    BedrockUtils.writeVector3f(buffer, packet.getRotation());
    BedrockUtils.writeArray(buffer, packet.getAttributes(), BedrockUtils::writeEntityAttribute);
    BedrockUtils.writeEntityData(buffer, packet.getMetadata());
    BedrockUtils.writeArray(buffer, packet.getEntityLinks(), BedrockUtils::writeEntityLink);
}
 
Example #18
Source File: CallFrame.java    From tchannel-java with MIT License 5 votes vote down vote up
public final ByteBuf encodePayload(@NotNull ByteBufAllocator allocator, @NotNull List<ByteBuf> args) {
    ByteBuf payload = CodecUtils.writeArgs(allocator, encodeHeader(allocator), args);

    if (args.isEmpty()) {
        this.flags = 0;
        payload.setByte(0, 0);
    } else {
        this.flags = 1;
        payload.setByte(0, 1);
    }

    this.payload = payload;
    return payload;
}
 
Example #19
Source File: ChunkedReadWriteHandler.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Get expected size encoded as offset + 4 bytes of data.
 */
private int getSize(final byte[] data, final int offset) {

  if (data.length - offset < INT_SIZE) {
    return 0;
  }

  final ByteBuf intBuffer = Unpooled.wrappedBuffer(data, offset, INT_SIZE).order(Unpooled.LITTLE_ENDIAN);
  final int ret = intBuffer.readInt();
  intBuffer.release();

  return ret;
}
 
Example #20
Source File: MySQLJsonValueDecoderTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private ByteBuf mockJsonObjectByteBufValue(final List<JsonEntry> jsonEntries, final boolean isSmall) {
    ByteBuf result = Unpooled.buffer();
    writeInt(result, jsonEntries.size(), isSmall);
    writeInt(result, 0, isSmall);
    int startOffset = isSmall
            ? 1 + SMALL_JSON_INT_LENGTH + SMALL_JSON_INT_LENGTH + jsonEntries.size() * SMALL_JSON_KEY_META_DATA_LENGTH + jsonEntries.size() * SMALL_JSON_VALUE_META_DATA_LENGTH - 1
            : 1 + LARGE_JSON_INT_LENGTH + LARGE_JSON_INT_LENGTH + jsonEntries.size() * LARGE_JSON_KEY_META_DATA_LENGTH + jsonEntries.size() * LARGE_JSON_VALUE_META_DATA_LENGTH - 1;
    ByteBuf keyByteBuf = writeKeys(result, jsonEntries, startOffset, isSmall);
    startOffset += keyByteBuf.readableBytes();
    ByteBuf valueByteBuf = writeValues(result, jsonEntries, startOffset, isSmall);
    result.writeBytes(keyByteBuf);
    result.writeBytes(valueByteBuf);
    return result;
}
 
Example #21
Source File: NettyWritableTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemaining() {
   ByteBuf buffer = Unpooled.buffer(1024);
   NettyWritable writable = new NettyWritable(buffer);

   assertEquals(buffer.maxCapacity(), writable.remaining());
   writable.put((byte) 0);
   assertEquals(buffer.maxCapacity() - 1, writable.remaining());
}
 
Example #22
Source File: HttpPostMultipartRequestDecoder.java    From dorado with Apache License 2.0 5 votes vote down vote up
/**
 * Load the field value or file data from a Multipart request
 *
 * @return {@code true} if the last chunk is loaded (boundary delimiter found),
 *         {@code false} if need more chunks
 * @throws ErrorDataDecoderException
 */
private static boolean loadDataMultipartStandard(ByteBuf undecodedChunk, String delimiter, HttpData httpData) {
	final int startReaderIndex = undecodedChunk.readerIndex();
	final int delimeterLength = delimiter.length();
	int index = 0;
	int lastPosition = startReaderIndex;
	byte prevByte = HttpConstants.LF;
	boolean delimiterFound = false;
	while (undecodedChunk.isReadable()) {
		final byte nextByte = undecodedChunk.readByte();
		// Check the delimiter
		if (prevByte == HttpConstants.LF && nextByte == delimiter.codePointAt(index)) {
			index++;
			if (delimeterLength == index) {
				delimiterFound = true;
				break;
			}
			continue;
		}
		lastPosition = undecodedChunk.readerIndex();
		if (nextByte == HttpConstants.LF) {
			index = 0;
			lastPosition -= (prevByte == HttpConstants.CR) ? 2 : 1;
		}
		prevByte = nextByte;
	}
	if (prevByte == HttpConstants.CR) {
		lastPosition--;
	}
	ByteBuf content = undecodedChunk.copy(startReaderIndex, lastPosition - startReaderIndex);
	try {
		httpData.addContent(content, delimiterFound);
	} catch (IOException e) {
		throw new ErrorDataDecoderException(e);
	}
	undecodedChunk.readerIndex(lastPosition);
	return delimiterFound;
}
 
Example #23
Source File: Client.java    From java-dcp-client with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to return the failover logs for the given partitions (vbids).
 * <p>
 * If the list is empty, the failover logs for all partitions will be returned. Note that the returned
 * ByteBufs can be analyzed using the {@link DcpFailoverLogResponse} flyweight.
 *
 * @param vbids the partitions to return the failover logs from.
 * @return an {@link Observable} containing all failover logs.
 */
public Observable<ByteBuf> failoverLogs(Short... vbids) {
  List<Short> partitions = partitionsForVbids(numPartitions(), vbids);

  LOGGER.debug("Asking for failover logs on partitions {}", partitions);

  return Observable
      .from(partitions)
      .flatMapSingle(new Func1<Short, Single<ByteBuf>>() {
        @Override
        public Single<ByteBuf> call(Short p) {
          return conductor.getFailoverLog(p);
        }
      });
}
 
Example #24
Source File: AbstractVpnNextHopParserSerializer.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public CNextHop parseNextHop(final ByteBuf buffer) throws BGPParsingException {
    Preconditions.checkArgument(buffer.readableBytes() == (this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH),
            "Length of byte array for NEXT_HOP should be %s, but is %s",
            this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH, buffer.readableBytes());
    buffer.readBytes(RouteDistinguisherUtil.RD_LENGTH);
    return NextHopUtil.parseNextHop(buffer.readBytes(this.ipAddrLength));
}
 
Example #25
Source File: CustomReqRepBenchmark.java    From netty-zmtp with Apache License 2.0 5 votes vote down vote up
private static MessageId readId(final ByteBuf data, final int size) {
  if (size != 16) {
    throw new IllegalArgumentException();
  }
  final long seq = data.readLong();
  final long timestamp = data.readLong();
  return MessageId.from(seq, timestamp);
}
 
Example #26
Source File: AltsTsiFrameProtector.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("fallthrough")
private void decodeFrame(ByteBufAllocator alloc, List<Object> out)
    throws GeneralSecurityException {
  switch (state) {
    case READ_HEADER:
      if (unhandledBytes < HEADER_BYTES) {
        return;
      }
      handleHeader();
      // fall through
    case READ_PROTECTED_PAYLOAD:
      if (unhandledBytes < requiredProtectedBytes) {
        return;
      }
      ByteBuf unprotectedBuf;
      try {
        unprotectedBuf = handlePayload(alloc);
      } finally {
        clearState();
      }
      if (unprotectedBuf != null) {
        out.add(unprotectedBuf);
      }
      break;
    default:
      throw new AssertionError("impossible enum value");
  }
}
 
Example #27
Source File: ModbusResponseSerializationTest.java    From modbus with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadHoldingRegistersResponse() {
    ReadHoldingRegistersResponse response = new ReadHoldingRegistersResponse(Unpooled.buffer().writeByte(1).writeByte(2));
    response.retain().content().markReaderIndex();

    ByteBuf encoded = encoder.encode(response, Unpooled.buffer());
    ReadHoldingRegistersResponse decoded = (ReadHoldingRegistersResponse) decoder.decode(encoded);

    response.content().resetReaderIndex();

    assertEquals(response.getFunctionCode(), decoded.getFunctionCode());
    assertEquals(response.getRegisters(), decoded.getRegisters());
}
 
Example #28
Source File: BulkInputStream.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
default void readFully(byte[] dst, int dstOffset, int dstLen)  throws IOException {
  final ByteBuf buf1 = Unpooled.buffer(dstLen);
  try {
    readFully(buf1, dstLen);
    buf1.getBytes(0, dst, dstOffset, dstLen);
  } finally {
    buf1.release();
  }
}
 
Example #29
Source File: DcpLoggingHandler.java    From java-dcp-client with Apache License 2.0 5 votes vote down vote up
@Override
protected String format(ChannelHandlerContext ctx, String eventName, Object arg) {
  if (arg instanceof ByteBuf) {
    return formatDcpPacket(ctx, eventName, (ByteBuf) arg);
  } else if (arg instanceof ByteBufHolder) {
    return formatDcpPacket(ctx, eventName, ((ByteBufHolder) arg).content());
  } else {
    return super.format(ctx, eventName, arg);
  }
}
 
Example #30
Source File: ConsumerManageProcessor.java    From qmq with Apache License 2.0 5 votes vote down vote up
private ConsumeManageRequest deserialize(ByteBuf buf) {
    String subject = PayloadHolderUtils.readString(buf);
    String consumerGroup = PayloadHolderUtils.readString(buf);
    int code = buf.readInt();
    ConsumeManageRequest request = new ConsumeManageRequest();
    request.setSubject(subject);
    request.setGroup(consumerGroup);
    request.setConsumerFromWhere(code);
    return request;
}