Java Code Examples for io.netty.buffer.ByteBuf#getBytes()

The following examples show how to use io.netty.buffer.ByteBuf#getBytes() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
public static String getValidatedPrefixedString(@NotNull final ByteBuf buf, final int utf8StringLength, final boolean validateShouldNotCharacters) {
    checkNotNull(buf);

    if (buf.readableBytes() < utf8StringLength) {
        return null;
    }

    final byte[] bytes = new byte[utf8StringLength];

    buf.getBytes(buf.readerIndex(), bytes);

    if (Utf8Utils.containsMustNotCharacters(bytes)) {
        return null;
    }

    if (validateShouldNotCharacters && Utf8Utils.hasControlOrNonCharacter(bytes)) {
        return null;
    }
    //The ByteBuf.getBytes method, doesn't move the read index, therefor we have to do this manually.
    buf.skipBytes(utf8StringLength);
    return new String(bytes, UTF_8);
}
 
Example 2
@Override
public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
    assertEquals(1024, msg.readableBytes());

    byte[] actual = new byte[msg.readableBytes()];
    msg.getBytes(0, actual);

    int lastIdx = counter;
    for (int i = 0; i < actual.length; i ++) {
        assertEquals(data[i + lastIdx], actual[i]);
    }

    if (channel.parent() != null) {
        channel.write(msg.retain());
    }

    counter += actual.length;
}
 
Example 3
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
        throws Exception {
    final byte[] array;
    final int offset;
    final int length = msg.readableBytes();
    if (msg.hasArray()) {
        array = msg.array();
        offset = msg.arrayOffset() + msg.readerIndex();
    } else {
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        offset = 0;
    }
    MessageNano prototype = clazz.getConstructor().newInstance();
    out.add(MessageNano.mergeFrom(prototype, array, offset, length));
}
 
Example 4
Source Project: ambry   File: CryptoServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert the given {@link ByteBuf} to a {@link CompositeByteBuf} if the {@code isCompositeByteBuf} is true.
 * @param buf The given {@link ByteBuf}.
 * @return The result {@link ByteBuf}.
 */
private ByteBuf maybeConvertToComposite(ByteBuf buf) {
  if (!isCompositeByteBuf) {
    return buf.retainedDuplicate();
  } else {
    byte[] data = new byte[buf.readableBytes()];
    buf.getBytes(buf.readerIndex(), data);
    return fromByteArrayToCompositeByteBuf(data);
  }
}
 
Example 5
Source Project: plog   File: ProtocolDecoder.java    License: Apache License 2.0 5 votes vote down vote up
private FourLetterCommand readCommand(DatagramPacket msg) {
    final ByteBuf content = msg.content();
    final int trailLength = content.readableBytes() - 6;
    if (trailLength < 0) {
        return null;
    }
    final byte[] trail = new byte[trailLength];
    final byte[] cmdBuff = new byte[4];
    content.getBytes(2, cmdBuff, 0, 4);
    content.getBytes(6, trail, 0, trail.length);
    return new FourLetterCommand(new String(cmdBuff), msg.sender(), trail);
}
 
Example 6
Source Project: pulsar   File: CompressionCodecZLib.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf encode(ByteBuf source) {
    byte[] array;
    int length = source.readableBytes();

    int sizeEstimate = (int) Math.ceil(source.readableBytes() * 1.001) + 14;
    ByteBuf compressed = PulsarByteBufAllocator.DEFAULT.heapBuffer(sizeEstimate);

    int offset = 0;
    if (source.hasArray()) {
        array = source.array();
        offset = source.arrayOffset() + source.readerIndex();
    } else {
        // If it's a direct buffer, we need to copy it
        array = new byte[length];
        source.getBytes(source.readerIndex(), array);
    }

    Deflater deflater = this.deflater.get();
    deflater.reset();
    deflater.setInput(array, offset, length);
    while (!deflater.needsInput()) {
        deflate(deflater, compressed);
    }

    return compressed;
}
 
Example 7
@Override
public void writeTo(ByteBuf byteBuf, LispEncapsulatedControl message)
        throws LispWriterException {

    // specify LISP message type
    byte msgType = (byte) (ECM_MSG_CODE << TYPE_SHIFT_BIT);

    byte security = DISABLE_BIT;
    if (message.isSecurity()) {
        security = (byte) (ENABLE_BIT << SECURITY_SHIFT_BIT);
    }

    byteBuf.writeByte(msgType + security);

    // fill zero into reserved field
    byteBuf.writeByte((byte) UNUSED_ZERO);
    byteBuf.writeByte((byte) UNUSED_ZERO);
    byteBuf.writeByte((byte) UNUSED_ZERO);

    ByteBuf buffer = Unpooled.buffer();
    message.getControlMessage().writeTo(buffer);
    byte[] dataBytes = new byte[buffer.writerIndex()];
    buffer.getBytes(0, dataBytes, 0, buffer.writerIndex());

    message.innerUdp().setPayload(new Data(dataBytes));
    message.innerIpHeader().setPayload(message.innerUdp());

    byteBuf.writeBytes(message.innerIpHeader().serialize());
}
 
Example 8
Source Project: distributedlog   File: ByteBufUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static byte[] getArray(ByteBuf buffer) {
    if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.writableBytes() == 0) {
        return buffer.array();
    }
    byte[] data = new byte[buffer.readableBytes()];
    buffer.getBytes(buffer.readerIndex(), data);
    return data;
}
 
Example 9
Source Project: push   File: MsgPackDecode.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    final int length = msg.readableBytes();
    final byte[] array = new byte[length];
    msg.getBytes(msg.readerIndex(), array, 0, length);
    out.add(new MessagePack().read(array, Packet.class));
}
 
Example 10
Source Project: zuul   File: Gzipper.java    License: Apache License 2.0 5 votes vote down vote up
private void write(ByteBuf bb) throws IOException {
    byte[] bytes;
    int offset;
    final int length = bb.readableBytes();
    if (bb.hasArray()) {
        /* avoid memory copy if possible */
        bytes = bb.array();
        offset = bb.arrayOffset();
    } else {
        bytes = new byte[length];
        bb.getBytes(bb.readerIndex(), bytes);
        offset = 0;
    }
    gzos.write(bytes, offset, length);
}
 
Example 11
public void decode(ByteBuf byteBuf)
{
	byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);

	ByteBuf lengthBuf = Unpooled.buffer(4);
	lengthBuf = lengthBuf.order(ByteOrder.LITTLE_ENDIAN);

	byteBuf.getBytes(byteBuf.readerIndex(), lengthBuf, 3);
	bodyLength = lengthBuf.getInt(0);
	serialNum = byteBuf.getByte(byteBuf.readerIndex() + 3);
	byteBuf.writerIndex(byteBuf.capacity());
	byteBuf.readerIndex(byteBuf.readerIndex() + 3);
}
 
Example 12
Source Project: qpid-jms   File: NettySimpleAmqpServer.java    License: Apache License 2.0 5 votes vote down vote up
public void setBuffer(ByteBuf value, boolean validate) {
    if (validate && !hasValidPrefix(value) || value.array().length != 8) {
        throw new IllegalArgumentException("Not an AMQP header buffer");
    }

    value.getBytes(0, buffer, 0, 8);
}
 
Example 13
Source Project: redisson   File: LocalCachedMapUpdate.java    License: Apache License 2.0 5 votes vote down vote up
public Entry(ByteBuf keyBuf, ByteBuf valueBuf) {
    key = new byte[keyBuf.readableBytes()];
    keyBuf.getBytes(keyBuf.readerIndex(), key);
    
    value = new byte[valueBuf.readableBytes()];
    valueBuf.getBytes(valueBuf.readerIndex(), value);
}
 
Example 14
Source Project: netty-4.1.22   File: ProtobufDecoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
        throws Exception {
    final byte[] array;
    final int offset;
    final int length = msg.readableBytes();
    if (msg.hasArray()) {
        array = msg.array();
        offset = msg.arrayOffset() + msg.readerIndex();
    } else {
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        offset = 0;
    }

    if (extensionRegistry == null) {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(array, offset, length));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(array, offset, length).build());
        }
    } else {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(
                    array, offset, length, extensionRegistry));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(
                    array, offset, length, extensionRegistry).build());
        }
    }
}
 
Example 15
Source Project: datacollector   File: CollectdParser.java    License: Apache License 2.0 4 votes vote down vote up
private String parseUser(int offset, ByteBuf buf) {
  int userLength = buf.getUnsignedShort(offset);
  byte[] userBytes = new byte[userLength];
  buf.getBytes(offset + 2, userBytes, 0, userLength);
  return new String(userBytes, StandardCharsets.UTF_8);
}
 
Example 16
Source Project: jt-808-protocol   File: Decoder4LoggingOnly.java    License: MIT License 4 votes vote down vote up
private String buf2Str(ByteBuf in) {
	byte[] dst = new byte[in.readableBytes()];
	in.getBytes(0, dst);
	return HexStringUtils.toHexString(dst);
}
 
Example 17
Source Project: ambry   File: GCMCryptoServiceTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Test basic encryption and decryption for random data in {@link ByteBuf}.
 * @throws Exception Any unexpected error
 */
@Test
public void testEncryptDecryptNettyByteBuf() throws Exception {
  // testEncryptDecryptBytes already tests the correctness of the encrypt decrypt methods with ByteBuffer, in this
  // test case, we can make the assumption that these two functions always provide correct answers.

  String key = TestUtils.getRandomKey(DEFAULT_KEY_SIZE_IN_CHARS);
  Properties props = getKMSProperties(key, DEFAULT_KEY_SIZE_IN_CHARS);
  VerifiableProperties verifiableProperties = new VerifiableProperties((props));
  SecretKeySpec secretKeySpec = new SecretKeySpec(Hex.decode(key), "AES");
  GCMCryptoService cryptoService =
      (GCMCryptoService) (new GCMCryptoServiceFactory(verifiableProperties, REGISTRY).getCryptoService());
  byte[] fixedIv = new byte[12];
  for (int i = 0; i < 5; i++) {
    int size = TestUtils.RANDOM.nextInt(MAX_DATA_SIZE);
    byte[] randomData = new byte[size];
    TestUtils.RANDOM.nextBytes(randomData);
    ByteBuffer toEncrypt = ByteBuffer.wrap(randomData);
    ByteBuf toEncryptByteBufHeap = ByteBufAllocator.DEFAULT.heapBuffer(size);
    ByteBuf toEncryptByteBufDirect = ByteBufAllocator.DEFAULT.ioBuffer(size);
    toEncryptByteBufHeap.writeBytes(randomData);
    toEncryptByteBufDirect.writeBytes(randomData);

    ByteBuffer encryptedBytes = cryptoService.encrypt(toEncrypt, secretKeySpec, fixedIv);
    ByteBuf encryptedBytesByteBufHeap = cryptoService.encrypt(toEncryptByteBufHeap, secretKeySpec, fixedIv);
    ByteBuf encryptedBytesByteBufDirect = cryptoService.encrypt(toEncryptByteBufDirect, secretKeySpec, fixedIv);

    // EncryptedByteBuf should be a head buffer always.
    Assert.assertTrue(encryptedBytesByteBufHeap.hasArray());
    Assert.assertTrue(encryptedBytesByteBufDirect.hasArray());
    Assert.assertEquals(encryptedBytes.remaining(), encryptedBytesByteBufHeap.readableBytes());
    Assert.assertEquals(encryptedBytes.remaining(), encryptedBytesByteBufDirect.readableBytes());
    Assert.assertEquals(toEncrypt.remaining(), 0);
    Assert.assertEquals(toEncryptByteBufDirect.readableBytes(), 0);
    Assert.assertEquals(toEncryptByteBufHeap.readableBytes(), 0);

    byte[] arrayFromByteBuf = new byte[encryptedBytesByteBufHeap.readableBytes()];
    encryptedBytesByteBufHeap.getBytes(encryptedBytesByteBufHeap.readerIndex(), arrayFromByteBuf);
    Assert.assertArrayEquals(encryptedBytes.array(), arrayFromByteBuf);
    encryptedBytesByteBufDirect.getBytes(encryptedBytesByteBufDirect.readerIndex(), arrayFromByteBuf);
    Assert.assertArrayEquals(encryptedBytes.array(), arrayFromByteBuf);

    ByteBuf toDecryptByteBufHeap = encryptedBytesByteBufHeap;
    ByteBuf toDecryptByteBufDirect = ByteBufAllocator.DEFAULT.ioBuffer(encryptedBytesByteBufHeap.readableBytes());
    toDecryptByteBufDirect.writeBytes(toDecryptByteBufHeap, 0, toDecryptByteBufHeap.readableBytes());

    ByteBuffer decryptedBytes = cryptoService.decrypt(encryptedBytes, secretKeySpec);
    ByteBuf decryptedBytesByteBufHeap = cryptoService.decrypt(toDecryptByteBufHeap, secretKeySpec);
    ByteBuf decryptedBytesByteBufDirect = cryptoService.decrypt(toDecryptByteBufDirect, secretKeySpec);

    Assert.assertTrue(decryptedBytesByteBufHeap.hasArray());
    Assert.assertTrue(decryptedBytesByteBufDirect.hasArray());
    Assert.assertEquals(decryptedBytes.remaining(), decryptedBytesByteBufHeap.readableBytes());
    Assert.assertEquals(decryptedBytes.remaining(), decryptedBytesByteBufDirect.readableBytes());
    Assert.assertEquals(encryptedBytes.remaining(), 0);
    Assert.assertEquals(toDecryptByteBufDirect.readableBytes(), 0);
    Assert.assertEquals(toDecryptByteBufHeap.readableBytes(), 0);

    arrayFromByteBuf = new byte[decryptedBytesByteBufHeap.readableBytes()];
    decryptedBytesByteBufHeap.getBytes(decryptedBytesByteBufHeap.readerIndex(), arrayFromByteBuf);
    Assert.assertArrayEquals(decryptedBytes.array(), arrayFromByteBuf);
    decryptedBytesByteBufDirect.getBytes(decryptedBytesByteBufDirect.readerIndex(), arrayFromByteBuf);
    Assert.assertArrayEquals(decryptedBytes.array(), arrayFromByteBuf);

    toEncryptByteBufHeap.release();
    toEncryptByteBufDirect.release();
    encryptedBytesByteBufHeap.release();
    encryptedBytesByteBufDirect.release();
    toDecryptByteBufDirect.release();
    decryptedBytesByteBufHeap.release();
    decryptedBytesByteBufDirect.release();
  }
}
 
Example 18
public static InetAddress getInetAddress(final ByteBuf buf, final int offset, final int length) {
    final byte[] data = new byte[length];
    buf.getBytes(offset, data, 0, length);

    return getInetAddress(data);
}
 
Example 19
Source Project: joyqueue   File: JoinGroupCodec.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public JoinGroupRequest decode(KafkaHeader header, ByteBuf buffer) throws Exception {
    JoinGroupRequest request = new JoinGroupRequest();
    List<JoinGroupRequest.ProtocolMetadata> groupProtocols = null;

    request.setGroupId(Serializer.readString(buffer, Serializer.SHORT_SIZE));
    request.setSessionTimeout(buffer.readInt());

    if (header.getVersion() >= 1) {
        request.setRebalanceTimeout(buffer.readInt());
    }

    String memberId = StringUtils.defaultString(Serializer.readString(buffer, Serializer.SHORT_SIZE), StringUtils.EMPTY);
    request.setMemberId(memberId);
    request.setProtocolType(Serializer.readString(buffer, Serializer.SHORT_SIZE));

    int size = buffer.readInt();

    if (size > 0) {
        groupProtocols = Lists.newLinkedList();
    }
    for (int i = 0; i < size; i++) {
        String groupName = Serializer.readString(buffer, Serializer.SHORT_SIZE);
        int length = buffer.readInt();
        ByteBuf byteBuf = buffer.readBytes(length);
        byte[] bytes;
        if (byteBuf.hasArray()) {
            bytes = byteBuf.array();
        } else {
            bytes = new byte[length];
            byteBuf.getBytes(byteBuf.readerIndex(), bytes);
        }
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        byteBuffer.rewind();
        JoinGroupRequest.ProtocolMetadata protocolMetadata = new JoinGroupRequest.ProtocolMetadata(groupName, byteBuffer);
        groupProtocols.add(protocolMetadata);
    }

    request.setGroupProtocols(groupProtocols);
    return request;
}
 
Example 20
@Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> list) throws Exception {
        ByteBuf b = null;
        try {
//        log.info(in.capacity()+";"+in.getClass());
            int readAble = in.readableBytes();
//        log.info("readAble:"+readAble);
            if (readAble < size) {
                return;
            }
            int opcode = 0, id = 0;
            if (!isReadHead) {
                size = in.readInt();
                opcode = in.readInt();
                id = in.readInt();

                isReadHead = true;
                if (size > readAble - headSize) {
                    return;
                }
            }
            b = in.readBytes(size); // 这里有data
            byte[] bbb = new byte[size];
            b.getBytes(0, bbb);
            // add之后好像in就被重置了
            in.discardReadBytes();
            NettyPBPacket nettyPBPacket = new NettyPBPacket();
            nettyPBPacket.setData(bbb);
            nettyPBPacket.setOpcode(opcode);
            nettyPBPacket.setId(id);
            list.add(nettyPBPacket);
            // 清理临时变量
            size = headSize;
            isReadHead = false;
        }catch (Throwable e){
            throw e;
        }finally {
            if(b != null){
                b.release();
            }
        }
    }