List of usage examples for io.netty.buffer ByteBuf writeShort
public abstract ByteBuf writeShort(int value);
From source file:io.atomix.cluster.messaging.impl.MessageEncoder.java
License:Apache License
private void encodeMessage(InternalMessage message, ByteBuf out) { // If the address hasn't been written to the channel, write it. if (!addressWritten) { out.writeShort(VERSION); final InetAddress senderIp = address.address(); final byte[] senderIpBytes = senderIp.getAddress(); out.writeByte(senderIpBytes.length); out.writeBytes(senderIpBytes);//from w w w. j a v a 2s . c om // write sender port out.writeInt(address.port()); addressWritten = true; } out.writeByte(message.type().id()); out.writeInt(this.preamble); // write message id out.writeLong(message.id()); final byte[] payload = message.payload(); // write payload length out.writeInt(payload.length); // write payload. out.writeBytes(payload); }
From source file:io.atomix.cluster.messaging.impl.MessageEncoder.java
License:Apache License
private void encodeRequest(InternalRequest request, ByteBuf out) { encodeMessage(request, out);//w ww.ja v a 2s .c o m final ByteBuf buf = out.alloc().buffer(ByteBufUtil.utf8MaxBytes(request.subject())); try { final int length = ByteBufUtil.writeUtf8(buf, request.subject()); // write length of message type out.writeShort(length); // write message type bytes out.writeBytes(buf); } finally { buf.release(); } }
From source file:io.crate.protocols.postgres.ClientMessages.java
License:Apache License
static void sendParseMessage(ByteBuf buffer, String stmtName, String query, int[] paramOids) { buffer.writeByte('P'); byte[] stmtNameBytes = stmtName.getBytes(StandardCharsets.UTF_8); byte[] queryBytes = query.getBytes(StandardCharsets.UTF_8); buffer.writeInt(4 + stmtNameBytes.length + 1 + queryBytes.length + 1 + 2 + paramOids.length * 4); writeCString(buffer, stmtNameBytes); writeCString(buffer, queryBytes);//w w w . j av a2 s .c om buffer.writeShort(paramOids.length); for (int paramOid : paramOids) { buffer.writeInt(paramOid); } }
From source file:io.crate.protocols.postgres.ClientMessages.java
License:Apache License
static void sendBindMessage(ByteBuf buffer, String portalName, String statementName, List<Object> params) { buffer.writeByte('B'); byte[] portalBytes = portalName.getBytes(StandardCharsets.UTF_8); byte[] statementBytes = statementName.getBytes(StandardCharsets.UTF_8); int beforeLengthWriterIndex = buffer.writerIndex(); buffer.writeInt(0);/*from w w w . java 2 s . co m*/ writeCString(buffer, portalBytes); writeCString(buffer, statementBytes); buffer.writeShort(0); // formatCode use 0 to default to text for all buffer.writeShort(params.size()); int paramsLength = 0; for (Object param : params) { BytesRef value = DataTypes.STRING.value(param); buffer.writeInt(value.length); // the strings here are _not_ zero-padded because we specify the length upfront buffer.writeBytes(value.bytes, value.offset, value.length); paramsLength += 4 + value.length; } buffer.writeShort(0); // numResultFormatCodes - 0 to default to text for all buffer.setInt(beforeLengthWriterIndex, 4 + portalBytes.length + 1 + statementBytes.length + 1 + 2 + // numFormatCodes 2 + // numParams paramsLength + 2); // numResultColumnFormatCodes }
From source file:io.crate.protocols.postgres.Messages.java
License:Apache License
/** * Byte1('D')//from ww w .ja v a 2 s. c o m * Identifies the message as a data row. * <p> * Int32 * Length of message contents in bytes, including self. * <p> * Int16 * The number of column values that follow (possibly zero). * <p> * Next, the following pair of fields appear for each column: * <p> * Int32 * The length of the column value, in bytes (this count does not include itself). * Can be zero. As a special case, -1 indicates a NULL column value. No value bytes follow in the NULL case. * <p> * ByteN * The value of the column, in the format indicated by the associated format code. n is the above length. */ static void sendDataRow(Channel channel, Row row, List<? extends DataType> columnTypes, @Nullable FormatCodes.FormatCode[] formatCodes) { int length = 4 + 2; assert columnTypes.size() == row .numColumns() : "Number of columns in the row must match number of columnTypes. Row: " + row + " types: " + columnTypes; ByteBuf buffer = channel.alloc().buffer(); buffer.writeByte('D'); buffer.writeInt(0); // will be set at the end buffer.writeShort(row.numColumns()); for (int i = 0; i < row.numColumns(); i++) { DataType dataType = columnTypes.get(i); PGType pgType = PGTypes.get(dataType); Object value = row.get(i); if (value == null) { buffer.writeInt(-1); length += 4; } else { FormatCodes.FormatCode formatCode = FormatCodes.getFormatCode(formatCodes, i); switch (formatCode) { case TEXT: length += pgType.writeAsText(buffer, value); break; case BINARY: length += pgType.writeAsBinary(buffer, value); break; default: throw new AssertionError("Unrecognized formatCode: " + formatCode); } } } buffer.setInt(1, length); channel.write(buffer); }
From source file:io.crate.protocols.postgres.Messages.java
License:Apache License
/** * ParameterDescription (B)/*from www . ja v a 2 s . c o m*/ * * Byte1('t') * * Identifies the message as a parameter description. * Int32 * * Length of message contents in bytes, including self. * Int16 * * The number of parameters used by the statement (can be zero). * * Then, for each parameter, there is the following: * * Int32 * * Specifies the object ID of the parameter data type. * * @param channel The channel to write the parameter description to. * @param parameters A {@link SortedSet} containing the parameters from index 1 upwards. */ static void sendParameterDescription(Channel channel, DataType[] parameters) { final int messageByteSize = 4 + 2 + parameters.length * 4; ByteBuf buffer = channel.alloc().buffer(messageByteSize); buffer.writeByte('t'); buffer.writeInt(messageByteSize); if (parameters.length > Short.MAX_VALUE) { throw new IllegalArgumentException("Too many parameters. Max supported: " + Short.MAX_VALUE); } buffer.writeShort(parameters.length); for (DataType dataType : parameters) { int pgTypeId = PGTypes.get(dataType).oid(); buffer.writeInt(pgTypeId); } channel.write(buffer); }
From source file:io.crate.protocols.postgres.Messages.java
License:Apache License
/** * RowDescription (B)//from ww w . j a va2s . c om * <p> * | 'T' | int32 len | int16 numCols * <p> * For each field: * <p> * | string name | int32 table_oid | int16 attr_num | int32 oid | int16 typlen | int32 type_modifier | int16 format_code * <p> * See https://www.postgresql.org/docs/current/static/protocol-message-formats.html */ static void sendRowDescription(Channel channel, Collection<Field> columns, @Nullable FormatCodes.FormatCode[] formatCodes) { int length = 4 + 2; int columnSize = 4 + 2 + 4 + 2 + 4 + 2; ByteBuf buffer = channel.alloc().buffer(length + (columns.size() * (10 + columnSize))); // use 10 as an estimate for columnName length buffer.writeByte('T'); buffer.writeInt(0); // will be set at the end buffer.writeShort(columns.size()); int idx = 0; for (Field column : columns) { byte[] nameBytes = column.path().outputName().getBytes(StandardCharsets.UTF_8); length += nameBytes.length + 1; length += columnSize; writeCString(buffer, nameBytes); buffer.writeInt(0); // table_oid buffer.writeShort(0); // attr_num PGType pgType = PGTypes.get(column.valueType()); buffer.writeInt(pgType.oid()); buffer.writeShort(pgType.typeLen()); buffer.writeInt(pgType.typeMod()); buffer.writeShort(FormatCodes.getFormatCode(formatCodes, idx).ordinal()); idx++; } buffer.setInt(1, length); ChannelFuture channelFuture = channel.writeAndFlush(buffer); if (LOGGER.isTraceEnabled()) { channelFuture.addListener((ChannelFutureListener) future -> LOGGER.trace("sentRowDescription")); } }
From source file:io.crate.protocols.postgres.types.SmallIntType.java
License:Apache License
@Override public int writeAsBinary(ByteBuf buffer, @Nonnull Object value) { buffer.writeInt(TYPE_LEN);//from w w w .ja va 2 s. com buffer.writeShort((short) value); return INT32_BYTE_SIZE + TYPE_LEN; }
From source file:io.github.stormcloud_dev.stormcloud.frame.clientbound.ActivateObjectClientBoundFrame.java
License:Apache License
@Override public void writeData(ByteBuf buf) { super.writeData(buf); buf.writeDouble(getPlayerX());//w w w .ja v a2 s .com buf.writeDouble(getPlayerY()); buf.writeShort(getObjectActivated()); buf.writeShort(getObjectMId()); buf.writeByte(getActiveState()); buf.writeByte(getSuccess()); }
From source file:io.github.stormcloud_dev.stormcloud.frame.clientbound.ActivateSwitchClientBoundFrame.java
License:Apache License
@Override public void writeData(ByteBuf buf) { super.writeData(buf); buf.writeShort(getX()); buf.writeShort(getY()); }