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

The following examples show how to use io.netty.buffer.ByteBuf#readInt() . 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
Source Project: crate   File: PGArrayTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Ignore // For multi-dimensions -1 is used both for "padding" until the max length of the dimension,
        // but also for null handling, therefore we cannot distinguish between the two
public void testBinaryEncodingDecodingRoundtrip_MultipleDimensionsWithNulls() {
    List<Object> sourceArray = List.of(
        Arrays.asList(1, 2, 3),
        Arrays.asList(4, 5, null),
        Arrays.asList(null, 6, 7, 8),
        Arrays.asList(null, null, 9, null)
    );

    ByteBuf buffer = Unpooled.buffer();
    pgArray.writeAsBinary(buffer, sourceArray);
    int length = buffer.readInt();
    Object targetArray = pgArray.readBinaryValue(buffer, length);
    buffer.release();
    assertThat(targetArray, is(sourceArray));
    // Because of the null handling problem it returns:
    // {
    //     {1, 2, 3, null},
    //     {4, 5, null, null},
    //     {null, 6, 7, 8},
    //     {null, null, 9, null}
    // }
}
 
Example 2
Source Project: herddb   File: PduCodec.java    License: Apache License 2.0 6 votes vote down vote up
public static List<byte[]> readIndexesDefinition(Pdu pdu) {
    ByteBuf buffer = pdu.buffer;
    buffer.readerIndex(VERSION_SIZE
            + FLAGS_SIZE
            + TYPE_SIZE
            + MSGID_SIZE
    );
    ByteBufUtils.skipArray(buffer); // tablespace
    ByteBufUtils.skipArray(buffer); // tableName
    int num = buffer.readInt();
    List<byte[]> res = new ArrayList<>();
    for (int i = 0; i < num; i++) {
        res.add(ByteBufUtils.readArray(buffer));
    }
    return res;
}
 
Example 3
Source Project: crate   File: PGArray.java    License: Apache License 2.0 6 votes vote down vote up
private void readArrayAsBinary(ByteBuf buffer,
                               final List<Object> array,
                               final int[] dims,
                               final int thisDimension) {
    if (thisDimension == dims.length - 1) {
        for (int i = 0; i < dims[thisDimension]; ++i) {
            int len = buffer.readInt();
            if (len == -1) {
                array.add(null);
            } else {
                array.add(innerType.readBinaryValue(buffer, len));
            }
        }
    } else {
        for (int i = 0; i < dims[thisDimension]; ++i) {
            ArrayList<Object> list = new ArrayList<>(dims[thisDimension + 1]);
            array.add(list);
            readArrayAsBinary(buffer, list, dims, thisDimension + 1);
        }
    }
}
 
Example 4
Source Project: crate   File: TimestampTypeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBinaryRoundtrip() {
    ByteBuf buffer = Unpooled.buffer();
    try {
        long value = 1467072000000L;
        int written = pgType.writeAsBinary(buffer, value);
        int length = buffer.readInt();
        assertThat(written - 4, is(length));
        long readValue = (long) pgType.readBinaryValue(buffer, length);
        assertThat(readValue, is(value));
    } finally {
        buffer.release();
    }
}
 
Example 5
Source Project: crate   File: TimestampZTypeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBinaryRoundtrip() {
    ByteBuf buffer = Unpooled.buffer();
    try {
        long value = 1467072000000L;
        int written = pgType.writeAsBinary(buffer, value);
        int length = buffer.readInt();
        assertThat(written - 4, is(length));
        long readValue = (long) pgType.readBinaryValue(buffer, length);
        assertThat(readValue, is(value));
    } finally {
        buffer.release();
    }
}
 
Example 6
Source Project: rsocket-java   File: ErrorFrameCodec.java    License: Apache License 2.0 5 votes vote down vote up
public static int errorCode(ByteBuf byteBuf) {
  byteBuf.markReaderIndex();
  byteBuf.skipBytes(FrameHeaderCodec.size());
  int i = byteBuf.readInt();
  byteBuf.resetReaderIndex();
  return i;
}
 
Example 7
@Override
public void fromBytes(ByteBuf buffer){
    super.fromBytes(buffer);
    targetEntityId = buffer.readInt();
    colors = new int[buffer.readInt()];
    for(int i = 0; i < colors.length; i++) {
        colors[i] = buffer.readInt();
    }
}
 
Example 8
public static SyncGroupAssignment readAssignment(ByteBuf buffer) throws Exception {
    int length = buffer.readInt();
    short headerVersion = buffer.readShort();
    Map<String, List<Integer>> topicPartitions = Maps.newHashMap();

    int topicSize = buffer.readInt();
    for (int i = 0; i < topicSize; i++) {
        String topic = Serializer.readString(buffer, Serializer.SHORT_SIZE);
        int partitionSize = buffer.readInt();
        List<Integer> partitions = (partitionSize == -1 ? Collections.emptyList() : Lists.newArrayListWithCapacity(partitionSize));
        for (int j = 0; j < partitionSize; j++) {
            partitions.add(buffer.readInt());
        }
        topicPartitions.put(topic, partitions);
    }

    int userDataLength = buffer.readInt();
    byte[] userData = null;
    if (userDataLength > 0) {
        userData = new byte[userDataLength];
        buffer.readBytes(userData);
    }

    SyncGroupAssignment syncGroupAssignment = new SyncGroupAssignment();
    syncGroupAssignment.setUserData(userData);
    syncGroupAssignment.setTopicPartitions(topicPartitions);
    return syncGroupAssignment;
}
 
Example 9
public static MusPropList readPropList(ByteBuf in) {
    // Length of list
    int length = in.readInt();

    // Allocate props
    MusPropList props = new MusPropList(length);

    // Parse them
    for (int i = 0; i < length; i++) {
        // Symbol type (always string)
        in.readShort();

        // Symbol (key)
        String symbol = MusUtil.readEvenPaddedString(in);

        // Data type
        short dataType = in.readShort();

        // Data (value)
        int dataLength;
        if (dataType == MusTypes.Integer) {
            dataLength = 4;
        } else {
            dataLength = in.readInt();
        }
        byte[] data = new byte[dataLength];
        in.readBytes(data);
        if ((dataLength % 2) != 0) {
            in.readByte();
        }

        // Set prop
        props.setPropAsBytes(symbol, dataType, data);
    }

    return props;
}
 
Example 10
@Override
protected void read0(ByteBuf from) {
	from.readByte();
	username = StringSerializer.readShortUTF16BEString(from);
	host = StringSerializer.readShortUTF16BEString(from);
	port = from.readInt();
}
 
Example 11
public static LargeForwardOpenResponse decode(ByteBuf buffer) {
    int o2tConnectionId = buffer.readInt();
    int t2oConnectionId = buffer.readInt();
    int connectionSerialNumber = buffer.readUnsignedShort();
    int originatorVendorId = buffer.readUnsignedShort();
    long originatorSerialNumber = buffer.readUnsignedInt();
    long o2tActualPacketInterval = TimeUnit.MICROSECONDS
        .convert(buffer.readUnsignedInt(), TimeUnit.MILLISECONDS);
    long t2oActualPacketInterval = TimeUnit.MICROSECONDS
        .convert(buffer.readUnsignedInt(), TimeUnit.MILLISECONDS);
    int applicationReplySize = buffer.readUnsignedByte();
    buffer.skipBytes(1); // reserved

    ByteBuf applicationReply = applicationReplySize > 0 ?
        buffer.readSlice(applicationReplySize).copy() :
        Unpooled.EMPTY_BUFFER;

    return new LargeForwardOpenResponse(
        o2tConnectionId,
        t2oConnectionId,
        connectionSerialNumber,
        originatorVendorId,
        originatorSerialNumber,
        Duration.ofMillis(o2tActualPacketInterval),
        Duration.ofMillis(t2oActualPacketInterval),
        applicationReplySize,
        applicationReply
    );
}
 
Example 12
@Override
public void read(ByteBuf buffer) throws IOException {
	int size = buffer.readInt();
	friendRequests = new ArrayList<User>(size);
	for (int i = 0; i < size; i++) {
		User friendRequest = PacketBuffer.readUser(buffer);
		friendRequests.add(friendRequest);
	}
}
 
Example 13
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {

    //这个HEAD_LENGTH是我们用于表示头长度的字节数
    if (byteBuf.readableBytes() < CommonConstants.HEAD_LENGTH) {
        return;
    }

    //我们标记一下当前的readIndex的位置
    byteBuf.markReaderIndex();

    // 读取传送过来的消息的长度。ByteBuf 的readInt()方法会让他的readIndex增加4
    int dataLength = byteBuf.readInt();

    // 我们读到的消息体长度为0,这是不应该出现的情况,这里出现这情况,关闭连接。
    if (dataLength < 0) {
        channelHandlerContext.close();
    }

    //读到的消息体长度如果小于我们传送过来的消息长度,则resetReaderIndex. 这个配合markReaderIndex使用的。把readIndex重置到mark的地方
    if (byteBuf.readableBytes() < dataLength) {
        byteBuf.resetReaderIndex();
        return;
    }

    byte[] body = new byte[dataLength];
    byteBuf.readBytes(body);

    //将byte数据转化为我们需要的对象
    BaseMsg baseMsg = JSON.parseObject(body,BaseMsg.class);
    list.add(baseMsg);
}
 
Example 14
@Override
public void fromBytes(ByteBuf buf) {
	super.fromBytes(buf);
    newCircut = buf.readInt();
    newLevel = buf.readInt();
    newGt = buf.readBoolean();
    pulse = buf.readBoolean();
}
 
Example 15
Source Project: joyqueue   File: RegisterCodec.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Register decode(Header header, ByteBuf buffer) throws Exception {
    int brokerId = buffer.readInt();
    return new Register().brokerId(brokerId>0?brokerId:null).brokerIp(Serializer.readString(buffer)).port(buffer.readInt());
}
 
Example 16
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
	slotId = buffer.readInt();
	data = buffer.slice();
}
 
Example 17
Source Project: SimplyJetpacks   File: MessageJetpackSync.java    License: MIT License 4 votes vote down vote up
@Override
public void fromBytes(ByteBuf buf) {
    this.entityId = buf.readInt();
    this.particleId = buf.readInt();
}
 
Example 18
@Override
protected void readClientData(ByteBuf clientdata) {
	entityId = clientdata.readInt();
	action = MiscSerializer.readByteEnum(clientdata, Action.CONSTANT_LOOKUP);
}
 
Example 19
@Override
public void fromBytes(ByteBuf buf){
    varName = ByteBufUtils.readUTF8String(buf);
    pos = new ChunkPosition(buf.readInt(), buf.readInt(), buf.readInt());
    stack = ByteBufUtils.readItemStack(buf);
}
 
Example 20
@Override
public LispGeoCoordinateLcafAddress readFrom(ByteBuf byteBuf)
                            throws LispParseError, LispReaderException {

    LispLcafAddress.deserializeCommon(byteBuf);

    // north flag -> 1 bit
    byte flagWithLatitude = byteBuf.readByte();

    boolean north = ByteOperator.getBit(flagWithLatitude, NORTH_INDEX);

    // latitude degree -> 15 bits
    short latitudeFirst = flagWithLatitude;
    if (north) {
        latitudeFirst = (short) (flagWithLatitude & 0x7F);
    }
    short latitude = (short) ((latitudeFirst << FLAG_SHIFT) + byteBuf.readByte());

    // latitude minute -> 8 bits
    byte latitudeMinute = byteBuf.readByte();

    // latitude second -> 8 bits
    byte latitudeSecond = byteBuf.readByte();

    // east flag -> 1 bit
    byte flagWithLongitude = byteBuf.readByte();

    boolean east = ByteOperator.getBit(flagWithLongitude, EAST_INDEX);

    // longitude degree -> 15 bits
    short longitudeFirst = flagWithLongitude;
    if (east) {
        longitudeFirst = (short) (flagWithLongitude & 0x7F);
    }
    short longitude = (short) ((longitudeFirst << FLAG_SHIFT) + byteBuf.readByte());

    // longitude minute -> 8 bits
    byte longitudeMinute = byteBuf.readByte();

    // longitude second -> 8 bits
    byte longitudeSecond = byteBuf.readByte();

    // altitude -> 32 bits
    int altitude = byteBuf.readInt();

    LispAfiAddress address = new AfiAddressReader().readFrom(byteBuf);

    return new GeoCoordinateAddressBuilder()
                    .withIsNorth(north)
                    .withLatitudeDegree(latitude)
                    .withLatitudeMinute(latitudeMinute)
                    .withLatitudeSecond(latitudeSecond)
                    .withIsEast(east)
                    .withLongitudeDegree(longitude)
                    .withLongitudeMinute(longitudeMinute)
                    .withLongitudeSecond(longitudeSecond)
                    .withAltitude(altitude)
                    .withAddress(address)
                    .build();
}