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

The following examples show how to use io.netty.buffer.ByteBuf#readableBytes() . 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: TrafficReader.java    From LagMonitor with MIT License 7 votes vote down vote up
private void onChannel(Object object, boolean incoming) {
    ByteBuf bytes = null;
    if (object instanceof ByteBuf) {
        bytes = ((ByteBuf) object);
    } else if (object instanceof ByteBufHolder) {
        bytes = ((ByteBufHolder) object).content();
    }

    if (bytes != null) {
        int readableBytes = bytes.readableBytes();
        if (incoming) {
            incomingBytes.add(readableBytes);
        } else {
            outgoingBytes.add(readableBytes);
        }
    }
}
 
Example 2
Source File: WelderDecoder.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    if (msg.isReadable()) {
        // fill byte array with incoming message
        byte[] bytes = new byte[msg.readableBytes()];
        int readerIndex = msg.readerIndex();
        msg.getBytes(readerIndex, bytes);
        
        // first 7 bytes are the sensor ID, last is the status
        // and the result message will look something like
        // MachineID=2371748;Status=Good
        StringBuilder sb = new StringBuilder();
        sb.append("MachineID=")
        .append(new String(bytes, 0, PAYLOAD_SIZE - 1)).append(";")
        .append("Status=");
        if (bytes[PAYLOAD_SIZE - 1] == '1') {
            sb.append("Good");
        } else {
            sb.append("Failure");
        }
        out.add(sb.toString());
    } else {
        out.add(null);
    }
}
 
Example 3
Source File: KcpTestServer.java    From dfactor with MIT License 6 votes vote down vote up
@Override
		protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket pack) throws Exception {
			final ByteBuf buf = pack.content();
			final int size = buf.readableBytes();
			if(size > 0){
				int connId = 0;
				if(buf.readByte()==Kcp.FLAG && size > 1 + 4){ //valid kcp head
					connId = buf.getInt(buf.readerIndex());
				}
				if(connId > 0){ //valid kcp pack
					pack.retain();
					kcpSys.onReceive(pack, connId);
//					log.I("Recv kcp pack, sender="+pack.sender().toString());
				}else{  //normal udp pack
					log.I("Recv udp pack, sender="+pack.sender().toString());
				}
			}else{
				log.E("Invalid pack, len=0, sender="+pack.sender().toString());
			}
		}
 
Example 4
Source File: RROLabelSubobjectParser.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(),
            "Array of bytes is mandatory. Can't be null or empty.");
    if (buffer.readableBytes() < HEADER_LENGTH) {
        throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
                + "; Expected: >" + HEADER_LENGTH + ".");
    }
    final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);

    final short cType = buffer.readUnsignedByte();

    final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
    if (labelType == null) {
        LOG.warn("Ignoring RRO label subobject with unknown C-TYPE: {}", cType);
        return null;
    }
    final LabelBuilder builder = new LabelBuilder()
            .setUniDirectional(reserved.get(U_FLAG_OFFSET))
            .setGlobal(reserved.get(G_FLAG_OFFSET))
            .setLabelType(labelType);
    return new SubobjectBuilder()
            .setSubobjectType(new LabelCaseBuilder()
            .setLabel(builder.build()).build())
            .build();
}
 
Example 5
Source File: KeyValueHandler.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes a {@link TouchRequest} into its lower level representation.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 */
private static BinaryMemcacheRequest handleTouchRequest(final ChannelHandlerContext ctx, final TouchRequest msg) {
    ByteBuf extras = ctx.alloc().buffer();
    extras.writeInt(msg.expiry());

    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;
    byte extrasLength = (byte) extras.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key);
    request.setExtras(extras);
    request.setOpcode(OP_TOUCH);
    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + extrasLength);
    request.setExtrasLength(extrasLength);
    return request;
}
 
Example 6
Source File: AbstractBmpMessageWithTlvParser.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
protected final void parseTlvs(final T builder, final ByteBuf bytes) throws BmpDeserializationException {
    Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
    if (!bytes.isReadable()) {
        return;
    }
    while (bytes.isReadable()) {
        final int type = bytes.readUnsignedShort();
        final int length = bytes.readUnsignedShort();
        if (length > bytes.readableBytes()) {
            throw new BmpDeserializationException("Wrong length specified. Passed: " + length
                    + "; Expected: <= " + bytes.readableBytes() + ".");
        }
        final ByteBuf tlvBytes = bytes.readSlice(length);
        LOG.trace("Parsing BMP TLV : {}", ByteBufUtil.hexDump(tlvBytes));

        final Tlv tlv = this.tlvRegistry.parseTlv(type, tlvBytes);
        if (tlv != null) {
            LOG.trace("Parsed BMP TLV {}.", tlv);
            addTlv(builder, tlv);
        }
    }
}
 
Example 7
Source File: RpcRequestCodec.java    From buddha with MIT License 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext context, ByteBuf byteBuf, List<Object> list)
        throws Exception {
    if (byteBuf.readableBytes() < 4) {
        return;
    }
    int len = byteBuf.readInt();
    if (byteBuf.readableBytes() < len) {
        throw new RuntimeException("Insufficient bytes to be read, expected: " + len);
    }
    byte[] bytes = new byte[len];
    byteBuf.readBytes(bytes);
    Serializer serializer = SerializerFactory.load();
    Object object = serializer.deserialize(bytes, RpcRequest.class);
    list.add(object);
}
 
Example 8
Source File: DrillBackwardsCompatibilityHandler.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
static NettyArrowBuf padValues(BufferAllocator allocator, SerializedField.Builder fieldBuilder, ByteBuf oldBuf,
                          int originalTypeByteWidth, int targetTypeByteWidth) {
  if (targetTypeByteWidth <= originalTypeByteWidth) {
    throw new IllegalArgumentException("the target width must be larger than the original one. "
      + targetTypeByteWidth + " is not larger than " + originalTypeByteWidth);
  }
  if (oldBuf.readableBytes() % originalTypeByteWidth != 0) {
    throw new IllegalArgumentException("the buffer size must be a multiple of the type width. "
      + oldBuf.readableBytes() + " is not a multiple of " + originalTypeByteWidth);
  }
  int valueCount = oldBuf.readableBytes() / originalTypeByteWidth;
  int newBufferLength = targetTypeByteWidth * valueCount;
  NettyArrowBuf newBuf = allocator.buffer(newBufferLength).asNettyBuffer();
  for (int byteIndex = 0; byteIndex < originalTypeByteWidth; byteIndex++) {
    for (int i = 0; i < valueCount; i++) {
      int oldIndex = i * originalTypeByteWidth + byteIndex;
      int newIndex = i * targetTypeByteWidth + byteIndex;
      newBuf.setByte(newIndex, oldBuf.getByte(oldIndex));
    }
  }
  newBuf.writerIndex(newBufferLength);
  fieldBuilder.setBufferLength(newBufferLength);
  oldBuf.release();
  return newBuf;
}
 
Example 9
Source File: CompactorTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private List<String> compactAndVerify(String topic, Map<String, byte[]> expected) throws Exception {
    BookKeeper bk = pulsar.getBookKeeperClientFactory().create(
            this.conf, null, Optional.empty(), null);
    Compactor compactor = new TwoPhaseCompactor(conf, pulsarClient, bk, compactionScheduler);
    long compactedLedgerId = compactor.compact(topic).get();

    LedgerHandle ledger = bk.openLedger(compactedLedgerId,
                                        Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE,
                                        Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD);
    Assert.assertEquals(ledger.getLastAddConfirmed() + 1, // 0..lac
                        expected.size(),
                        "Should have as many entries as there is keys");

    List<String> keys = new ArrayList<>();
    Enumeration<LedgerEntry> entries = ledger.readEntries(0, ledger.getLastAddConfirmed());
    while (entries.hasMoreElements()) {
        ByteBuf buf = entries.nextElement().getEntryBuffer();
        RawMessage m = RawMessageImpl.deserializeFrom(buf);
        String key = extractKey(m);
        keys.add(key);

        ByteBuf payload = extractPayload(m);
        byte[] bytes = new byte[payload.readableBytes()];
        payload.readBytes(bytes);
        Assert.assertEquals(bytes, expected.remove(key),
                            "Compacted version should match expected version");
        m.close();
    }
    Assert.assertTrue(expected.isEmpty(), "All expected keys should have been found");
    return keys;
}
 
Example 10
Source File: NettyIoSession.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  ByteBuf buf = (ByteBuf) msg;
  byte[] bytes = new byte[buf.readableBytes()];
  buf.getBytes(0, bytes);
  acceptor.factory.handlerBridge.messageReceived(handler, NettyIoSession.this, new ByteArrayBuffer(bytes));
}
 
Example 11
Source File: ByteUtil.java    From proxyee with MIT License 5 votes vote down vote up
public static ByteBuf insertText(ByteBuf byteBuf, int index, String str, Charset charset) {
    byte[] begin = new byte[index + 1];
    byte[] end = new byte[byteBuf.readableBytes() - begin.length];
    byteBuf.readBytes(begin);
    byteBuf.readBytes(end);
    byteBuf.writeBytes(begin);
    byteBuf.writeBytes(str.getBytes(charset));
    byteBuf.writeBytes(end);
    return byteBuf;
}
 
Example 12
Source File: SegmentHelper.java    From pravega with Apache License 2.0 5 votes vote down vote up
private byte[] getArray(ByteBuf buf) {
    final byte[] bytes = new byte[buf.readableBytes()];
    final int readerIndex = buf.readerIndex();
    buf.getBytes(readerIndex, bytes);
    release(Collections.singleton(buf));
    return bytes;
}
 
Example 13
Source File: IntegerDecoder.java    From netty-learning with MIT License 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (in.readableBytes() >= 4) {
        out.add(in.readInt());
    }
}
 
Example 14
Source File: SocketUtils.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
public static String getJson(Object msg) {
    String json;
    try {
        ByteBuf buf = (ByteBuf) msg;
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
        json = new String(bytes);
    } finally {
        ReferenceCountUtil.release(msg);
    }
    return json;

}
 
Example 15
Source File: DynamicCompositeByteBufTest.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadBytes() {
    DynamicCompositeByteBuf compositeByteBuf = new DynamicCompositeByteBuf();
    ByteBuf byteBuf1 = Unpooled.wrappedBuffer("hello".getBytes());
    compositeByteBuf.addBuffer(byteBuf1);
    byte[] bytes = new byte[byteBuf1.readableBytes()];
    compositeByteBuf.readBytes(bytes);
    String msg = new String(bytes);
    Assert.assertTrue(msg.equals("hello"));
}
 
Example 16
Source File: RpcxHttpHandler.java    From rpcx-java with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) {
    String servicePath = msg.headers().get(Constants.X_RPCX_SERVICEPATH);
    String serviceMethod = msg.headers().get(Constants.X_RPCX_SERVICEMETHOD);
    String traceId = msg.headers().get(Constants.X_RPCX_TRACEID);

    logger.info("service:{} method:{} traceId:{}", servicePath, serviceMethod, traceId);

    ByteBuf buf = msg.content();
    int len = buf.readableBytes();

    byte[] payload = new byte[len];
    buf.readBytes(payload);

    Message message = new Message();
    message.metadata.put("language", LanguageCode.HTTP.name());
    message.metadata.put("_host", getClientIp(ctx, msg));
    message.metadata.put("_port", "0");
    message.metadata.put(Constants.X_RPCX_TRACEID, traceId == null ? "" : traceId);
    message.servicePath = servicePath;
    message.serviceMethod = serviceMethod;
    message.payload = payload;
    message.setMessageType(MessageType.Request);
    message.setOneway(true);
    RemotingCommand command = RemotingCommand.createRequestCommand(message);
    command.setCode(1984);
    //这里会异步处理
    nettyServer.processRequestCommand(ctx, command);
}
 
Example 17
Source File: AbstractRedisClientProtocol.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
private ByteArrayOutputStream _readTilCRLF(ByteBuf byteBuf) {
	
	int readable = byteBuf.readableBytes();
	for(int i=0; i < readable ;i++){
		
		byte data = byteBuf.readByte();
		baous.write(data);
		switch(data){
			case '\r':
				crlfState = CRLF_STATE.CR;
				break;
			case '\n':
				if(crlfState == CRLF_STATE.CR){
					crlfState = CRLF_STATE.CRLF;
					break;
				}
			default:
				crlfState = CRLF_STATE.CONTENT;
				break;
		}
		
		if(crlfState == CRLF_STATE.CRLF){
			return baous;
		}
	}
	return null;
	
}
 
Example 18
Source File: MqttMessageSerializer.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public static BrokerMessage convertToBrokerMsg(Channel client, MqttPublishMessage mqttMessage) {
    BrokerMessage brokerMessage = new BrokerMessage();

    long now = SystemClock.now();
    final String clientId = NettyAttrManager.getAttrClientId(client);
    brokerMessage.setApp(clientId);
    brokerMessage.setTopic(mqttMessage.variableHeader().topicName());
    brokerMessage.setCompressed(false);
    brokerMessage.setClientIp(IpUtil.toAddress(client.remoteAddress()).getBytes());
    brokerMessage.setStartTime(now);
    brokerMessage.setSource(SourceType.MQTT.getValue());
    brokerMessage.setBusinessId(Integer.toString(mqttMessage.variableHeader().packetId()));

    ByteBuf payload = mqttMessage.payload();
    byte[] body = new byte[payload.readableBytes()];
    int index = payload.readerIndex();
    payload.readBytes(body);
    payload.readerIndex(index);
    brokerMessage.setBody(body);
    writeExtension(mqttMessage.fixedHeader().qosLevel(), brokerMessage);

    CRC32 crc32 = new CRC32();
    crc32.update(brokerMessage.getBody().slice());
    brokerMessage.setBodyCRC(crc32.getValue());

    return brokerMessage;
}
 
Example 19
Source File: JsonRpcDecoder.java    From ovsdb with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out)
        throws IOException {
    LOG.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(),
        recordsRead, lastRecordBytes);

    if (lastRecordBytes == 0) {
        if (buf.readableBytes() < 4) {
            return; //wait for more data
        }

        skipSpaces(buf);

        byte[] buff = new byte[4];
        buf.getBytes(buf.readerIndex(), buff);
        ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4);
        JsonEncoding jsonEncoding = strapper.detectEncoding();
        if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
            throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported");
        }
    }

    int index = lastRecordBytes + buf.readerIndex();

    for (; index < buf.writerIndex(); index++) {
        switch (buf.getByte(index)) {
            case '{':
                if (!inS) {
                    leftCurlies++;
                }
                break;
            case '}':
                if (!inS) {
                    rightCurlies++;
                }
                break;
            case '"':
                if (buf.getByte(index - 1) != '\\') {
                    inS = !inS;
                }
                break;
            default:
                break;
        }

        if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) {
            ByteBuf slice = buf.readSlice(1 + index - buf.readerIndex());
            JsonParser jp = JSON_FACTORY.createParser((InputStream) new ByteBufInputStream(slice));
            JsonNode root = jp.readValueAsTree();
            out.add(root);
            leftCurlies = 0;
            rightCurlies = 0;
            lastRecordBytes = 0;
            recordsRead++;
            break;
        }

        /*
         * Changing this limit to being a warning, we do not wish to "break" in scale environment
         * and currently this limits the ovs of having only around 50 ports defined...
         * I do acknowledge the fast that this might be risky in case of huge amount of strings
         * in which the controller can crash with an OOM, however seems that we need a really huge
         * ovs to reach that limit.
         */

        //We do not want to issue a log message on every extent of the buffer
        //hence logging only once
        if (index - buf.readerIndex() >= maxFrameLength && !maxFrameLimitWasReached) {
            maxFrameLimitWasReached = true;
            LOG.warn("***** OVSDB Frame limit of {} bytes has been reached! *****", this.maxFrameLength);
        }
    }

    // end of stream, save the incomplete record index to avoid reexamining the whole on next run
    if (index >= buf.writerIndex()) {
        lastRecordBytes = buf.readableBytes();
    }
}
 
Example 20
Source File: MessageUtil.java    From jmqtt with Apache License 2.0 4 votes vote down vote up
public static byte[] readBytesFromByteBuf(ByteBuf byteBuf){
    byte[] bytes = new byte[byteBuf.readableBytes()];
    byteBuf.readBytes(bytes);
    return bytes;
}