Java Code Examples for io.netty.buffer.ByteBuf.setInt()

The following are Jave code examples for showing how to use setInt() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: message-broker   File: FieldTable.java   Source Code and License Vote up 6 votes
private void writeWithCalculatedSize(ByteBuf buf) {
    int sizeIndex = buf.writerIndex();
    buf.writerIndex(sizeIndex + 4);

    long tableEntrySize = 0L;
    for (Map.Entry<ShortString, FieldValue> fieldEntry : properties.entrySet()) {
        ShortString key = fieldEntry.getKey();
        FieldValue value = fieldEntry.getValue();

        tableEntrySize = tableEntrySize + key.getSize() + value.getSize();
        key.write(buf);
        value.write(buf);
    }

    buf.setInt(sizeIndex, (int) tableEntrySize);
}
 
Example 2
Project: proteus-java   File: ProteusMetadata.java   Source Code and License Vote up 6 votes
public static int encode(ByteBuf byteBuf, int namespaceId, int serviceId, int methodId, ByteBuf metadata) {
    int offset = 0;

    byteBuf.setShort(offset, 1);
    offset += VERSION_SIZE;

    byteBuf.setInt(offset, namespaceId);
    offset += NAMESPACE_ID_SIZE;

    byteBuf.setInt(offset, serviceId);
    offset += SERVICE_ID_SIZE;

    byteBuf.setInt(offset, methodId);
    offset += METHOD_ID_SIZE;

    int metadataLength = metadata.readableBytes();
    byteBuf.setInt(offset, metadataLength);
    offset += METADATA_LENGTH_SIZE;

    byteBuf.setBytes(offset, metadata);
    offset += metadataLength;

    byteBuf.writerIndex(offset);

    return offset;
}
 
Example 3
Project: reactive-pg-client   File: Parse.java   Source Code and License Vote up 6 votes
private static void encode(long statement, String query, int[] paramDataTypes, ByteBuf out) {
  int pos = out.writerIndex();
  out.writeByte(PARSE);
  out.writeInt(0);
  if(statement == 0) {
    out.writeByte(0);
  } else {
    out.writeLong(statement);
  }
  Util.writeCStringUTF8(out, query);
  // no parameter data types (OIDs)
  if(paramDataTypes == null) {
    out.writeShort(0);
  } else {
    // Parameter data types (OIDs)
    out.writeShort(paramDataTypes.length);
    for (int paramDataType : paramDataTypes) {
      out.writeInt(paramDataType);
    }
  }
  out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
 
Example 4
Project: reactive-pg-client   File: Describe.java   Source Code and License Vote up 6 votes
private static void encode(long statement, String portal, ByteBuf out) {
  int pos = out.writerIndex();
  out.writeByte(DESCRIBE);
  out.writeInt(0);
  if (statement != 0) {
    out.writeByte('S');
    out.writeLong(statement);
  } else if (portal != null) {
    out.writeByte('P');
    Util.writeCStringUTF8(out, portal);
  } else {
    out.writeByte('S');
    Util.writeCStringUTF8(out, "");
  }
  out.setInt(pos + 1, out.writerIndex() - pos- 1);
}
 
Example 5
Project: reactive-pg-client   File: StartupMessage.java   Source Code and License Vote up 6 votes
@Override
public void encode(ByteBuf out) {

  int pos = out.writerIndex();

  out.writeInt(0);
  // protocol version
  out.writeShort(3);
  out.writeShort(0);

  writeCString(out, BUFF_USER);
  Util.writeCStringUTF8(out, username);
  writeCString(out, BUFF_DATABASE);
  Util.writeCStringUTF8(out, database);
  writeCString(out, BUFF_APPLICATION_NAME);
  writeCString(out, BUFF_VERTX_PG_CLIENT);
  writeCString(out, BUFF_CLIENT_ENCODING);
  writeCString(out, BUFF_UTF8);
  writeCString(out, BUFF_DATE_STYLE);
  writeCString(out, BUFF_ISO);
  writeCString(out, BUFF_EXTRA_FLOAT_DIGITS);
  writeCString(out, BUFF_2);

  out.writeByte(0);
  out.setInt(pos, out.writerIndex() - pos);
}
 
Example 6
Project: Quavo   File: ByteBufUtils.java   Source Code and License Vote up 6 votes
/**
 * Deciphers the specified {@link ByteBuf} with the given key.
 * 
 * @param buffer The {@link ByteBuf}.
 * @param key The key.
 * @throws IllegalArgumentException if the key is not exactly 4 elements long.
 */
private static void decipherXTEA(ByteBuf buffer, int start, int end, int[] key) {
	if (key.length != 4) {
		throw new IllegalArgumentException();
	}
	int numQuads = (end - start) / 8;
	for (int i = 0; i < numQuads; i++) {
		int sum = 0x9E3779B9 * 32;
		int v0 = buffer.getInt(start + i * 8);
		int v1 = buffer.getInt(start + i * 8 + 4);
		for (int j = 0; j < 32; j++) {
			v1 -= (((v0 << 4) ^ (v0 >>> 5)) + v0) ^ (sum + key[(sum >>> 11) & 3]);
			sum -= 0x9E3779B9;
			v0 -= (((v1 << 4) ^ (v1 >>> 5)) + v1) ^ (sum + key[sum & 3]);
		}
		buffer.setInt(start + i * 8, v0);
		buffer.setInt(start + i * 8 + 4, v1);
	}
}
 
Example 7
Project: fresco_floodlight   File: ThriftFrameEncoder.java   Source Code and License Vote up 6 votes
@Override
protected void encode(ChannelHandlerContext ctx, T msg, ByteBuf out)
        throws Exception {

    int lengthIndex = out.writerIndex();
    // length field, will be filled in later.
    out.writeInt(0);

    int startIndex = out.writerIndex();
    ByteBufOutputStream os = new ByteBufOutputStream(out);
    TCompactProtocol thriftProtocol =
            new TCompactProtocol(new TIOStreamTransport(os));
    msg.write(thriftProtocol);
    os.close();
    int endIndex = out.writerIndex();

    // update the length field
    int length = endIndex - startIndex;
    out.setInt(lengthIndex, length);
}
 
Example 8
Project: QDrill   File: TestEndianess.java   Source Code and License Vote up 5 votes
@Test
public void testLittleEndian() {
  final DrillConfig drillConfig = DrillConfig.create();
  final BufferAllocator a = RootAllocatorFactory.newRoot(drillConfig);
  final ByteBuf b = a.buffer(4);
  b.setInt(0, 35);
  assertEquals(b.getByte(0), 35);
  assertEquals(b.getByte(1), 0);
  assertEquals(b.getByte(2), 0);
  assertEquals(b.getByte(3), 0);
  b.release();
  a.close();
}
 
Example 9
Project: reactive-pg-client   File: Execute.java   Source Code and License Vote up 5 votes
private static void encode(String portal, int rowCount, ByteBuf out) {
  int pos = out.writerIndex();
  out.writeByte(EXECUTE);
  out.writeInt(0);
  if (portal != null) {
    out.writeCharSequence(portal, StandardCharsets.UTF_8);
  }
  out.writeByte(0);
  out.writeInt(rowCount); // Zero denotes "no limit" maybe for ReadStream<Row>
  out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
 
Example 10
Project: reactive-pg-client   File: Query.java   Source Code and License Vote up 5 votes
@Override
public void encode(ByteBuf out) {
  int pos = out.writerIndex();
  out.writeByte(QUERY);
  out.writeInt(0);
  Util.writeCStringUTF8(out, getQuery());
  out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
 
Example 11
Project: reactive-pg-client   File: Close.java   Source Code and License Vote up 5 votes
@Override
public void encode(ByteBuf out) {
  int pos = out.writerIndex();
  out.writeByte(CLOSE);
  out.writeInt(0);
  out.writeByte('S'); // 'S' to close a prepared statement or 'P' to close a portal
  Util.writeCStringUTF8(out, statement != null ? statement : "");
  out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
 
Example 12
Project: reactive-pg-client   File: CancelRequest.java   Source Code and License Vote up 5 votes
@Override
public void encode(ByteBuf out) {
  int pos = out.writerIndex();
  out.writeInt(0);
  out.writeInt(code);
  out.writeInt(processId);
  out.writeInt(secretKey);
  out.setInt(pos, out.writerIndex() - pos);
}
 
Example 13
Project: reactive-pg-client   File: PasswordMessage.java   Source Code and License Vote up 5 votes
@Override
public void encode(ByteBuf out) {
  int pos = out.writerIndex();
  out.writeByte(PASSWORD_MESSAGE);
  out.writeInt(0);
  Util.writeCStringUTF8(out, hash);
  out.setInt(pos + 1, out.writerIndex() - pos- 1);
}
 
Example 14
Project: reactive-pg-client   File: InitiateSslHandler.java   Source Code and License Vote up 5 votes
@Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ByteBuf byteBuf = Unpooled.buffer();
    byteBuf.writeInt(0);
    byteBuf.writeInt(code);
//    out.writeInt(0x12345679);
    byteBuf.setInt(0, byteBuf.writerIndex());
    ctx.writeAndFlush(byteBuf);
    super.channelActive(ctx);
  }
 
Example 15
Project: miracle-remote   File: KryoEncoder.java   Source Code and License Vote up 5 votes
@Override
protected void encode(final ChannelHandlerContext ctx, final Object msg, final ByteBuf out) throws Exception {
    int startIdx = out.writerIndex();
    kryoPool.encode(out, msg);
    int endIdx = out.writerIndex();
    out.setInt(startIdx, endIdx - startIdx - 4);
}
 
Example 16
Project: angel   File: PSClient.java   Source Code and License Vote up 4 votes
private void changeSeqId(int seqId, ByteBuf update) {
  update.setInt(0, seqId);
}
 
Example 17
Project: hekate   File: NetworkProtocolCodec.java   Source Code and License Vote up 4 votes
private static void doEncode(Object msg, ByteBufDataWriter out, Codec<Object> codec) throws CodecException {
    try {
        ByteBuf outBuf = out.buffer();

        // Header indexes.
        int headStartIdx = outBuf.writerIndex();

        // Header end index.
        int headEndIdx = headStartIdx + HEADER_LENGTH;

        // Placeholder for the header.
        outBuf.ensureWritable(HEADER_LENGTH).writerIndex(headEndIdx);

        boolean internalMsg;

        if (msg instanceof NetworkProtocol) {
            // Encode internal message.
            internalMsg = true;

            NetworkProtocol netMsg = (NetworkProtocol)msg;

            encodeInternal(out, codec, netMsg);
        } else {
            // Encode user-defined message.
            internalMsg = false;

            codec.encode(msg, out);
        }

        // Calculate real message length.
        int len = outBuf.writerIndex() - headStartIdx;

        // Magic length value:
        //   negative - for protocol messages
        //   positive - for user messages
        if (internalMsg) {
            len = -len;
        }

        // Update length header.
        outBuf.setInt(headStartIdx, len);
    } catch (CodecException e) {
        throw e;
    } catch (Throwable t) {
        throw new CodecException("Failed to encode message [message=" + msg + ", codec=" + codec + ']', t);
    }
}
 
Example 18
Project: reactive-pg-client   File: Bind.java   Source Code and License Vote up 4 votes
private static void encode(String portal, long statement, List<Object> paramValues, DataType<?>[] dataTypes, ByteBuf out) {
  int pos = out.writerIndex();
  out.writeByte(BIND);
  out.writeInt(0);
  if (portal != null) {
    out.writeCharSequence(portal, StandardCharsets.UTF_8);
  }
  out.writeByte(0);
  if (statement == 0) {
    out.writeByte(0);
  } else {
    out.writeLong(statement);
  }
  if(paramValues == null) {
    // No parameter formats
    out.writeShort(0);
    // No parameter values
    out.writeShort(0);
  } else {

    // byte[][] foobar = Util.paramValues(paramValues);
    int len = paramValues.size();
    out.writeShort(len);
    // Parameter formats
    for (int c = 0;c < len;c++) {
      // for now each format is Binary
      out.writeShort(1);
    }
    out.writeShort(len);
    for (int c = 0;c < len;c++) {
      Object param = paramValues.get(c);
      if (param == null) {
        // NULL value
        out.writeInt(-1);
      } else {
        DataType dataType = dataTypes[c];
        dataType.binaryEncoder.encode(param, out);
      }
    }
  }

  // Result columns are all in Binary format
  out.writeShort(1);
  out.writeShort(1);
  out.setInt(pos + 1, out.writerIndex() - pos - 1);
}