List of usage examples for io.netty.buffer ByteBuf setInt
public abstract ByteBuf setInt(int index, int value);
From source file:io.crate.protocols.postgres.Messages.java
License:Apache License
/** * Byte1('D')/* w w w .j a va 2 s .co 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
/** * RowDescription (B)// w ww . j a v a 2 s . co m * <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.PGArray.java
License:Apache License
@Override public int writeAsBinary(ByteBuf buffer, @Nonnull Object value) { int dimensions = getDimensions(value); List<Integer> dimensionsList = new ArrayList<>(); buildDimensions((Object[]) value, dimensionsList, dimensions, 1); int bytesWritten = 4 + 4 + 4; final int lenIndex = buffer.writerIndex(); buffer.writeInt(0);/*ww w . ja v a 2s .c om*/ buffer.writeInt(dimensions); buffer.writeInt(1); // flags bit 0: 0=no-nulls, 1=has-nulls buffer.writeInt(typElem()); for (Integer dim : dimensionsList) { buffer.writeInt(dim); // upper bound buffer.writeInt(dim); // lower bound bytesWritten += 8; } int len = bytesWritten + writeArrayAsBinary(buffer, (Object[]) value, dimensionsList, 1); buffer.setInt(lenIndex, len); return len; }
From source file:io.hekate.network.netty.NetworkProtocolCodec.java
License:Apache License
private static void doEncode(Object msg, ByteBufDataWriter out, Codec<Object> codec) throws CodecException { ByteBuf buf = out.buffer(); try {//from ww w . j a v a 2s .c o m // Header indexes. int headStartIdx = buf.writerIndex(); int headEndIdx = headStartIdx + HEADER_LENGTH; // Placeholder for the header. buf.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 = buf.writerIndex() - headStartIdx; // Magic length value: // negative - for protocol messages // positive - for user messages if (internalMsg) { len = -len; } // Update length header. buf.setInt(headStartIdx, len); } catch (CodecException e) { throw e; } catch (Throwable t) { throw new CodecException("Failed to encode message [message=" + msg + ']', t); } }
From source file:io.hydramq.disk.DiskSegment.java
License:Open Source License
public void write(Message message, MessageIOListener messageIOListener) throws HydraRuntimeException { try {/*w ww .j a v a 2 s . c om*/ ByteBuf buffer = allocator.directBuffer(); buffer.writeInt(0); conversionContext.write(message, buffer); int messageSize = buffer.readableBytes() - 4; buffer.setInt(0, messageSize); boolean shouldFlush = flushStrategy.requiresFlush(buffer.readableBytes()); indexWriteBuffer.clear(); indexWriteBuffer.putInt((int) data.size()); indexWriteBuffer.putLong(Clock.systemUTC().millis()); indexWriteBuffer.flip(); ByteBuffer nioBuffer = buffer.nioBuffer(); while (nioBuffer.hasRemaining()) { data.write(nioBuffer); } while (indexWriteBuffer.hasRemaining()) { index.write(indexWriteBuffer); } buffer.release(); if (shouldFlush) { data.force(true); index.force(true); } size += 1; if (messageIOListener != null) { messageIOListener.onMessage(1, messageSize); } } catch (IOException ex) { throw new HydraRuntimeException("Error writing message to segment " + segmentDirectory.toString()); } }
From source file:io.pravega.shared.protocol.netty.CommandEncoder.java
License:Open Source License
@SneakyThrows(IOException.class) private void writeMessage(AppendBlock block, ByteBuf out) { int startIdx = out.writerIndex(); ByteBufOutputStream bout = new ByteBufOutputStream(out); bout.writeInt(block.getType().getCode()); bout.write(LENGTH_PLACEHOLDER);//w ww. j a va 2 s. c o m block.writeFields(bout); bout.flush(); bout.close(); int endIdx = out.writerIndex(); int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE; out.setInt(startIdx + TYPE_SIZE, fieldsSize + currentBlockSize); }
From source file:io.pravega.shared.protocol.netty.CommandEncoder.java
License:Open Source License
@SneakyThrows(IOException.class) private int writeMessage(WireCommand msg, ByteBuf out) { int startIdx = out.writerIndex(); ByteBufOutputStream bout = new ByteBufOutputStream(out); bout.writeInt(msg.getType().getCode()); bout.write(LENGTH_PLACEHOLDER);/*from ww w . j a va 2 s . c o m*/ msg.writeFields(bout); bout.flush(); bout.close(); int endIdx = out.writerIndex(); int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE; out.setInt(startIdx + TYPE_SIZE, fieldsSize); return endIdx - startIdx; }
From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java
License:Apache License
public static void encodeText(DataType id, Object value, ByteBuf buff) { int index = buff.writerIndex(); buff.writeInt(0);/*from w w w. java 2 s . c o m*/ textEncode(id, value, buff); buff.setInt(index, buff.writerIndex() - index - 4); }
From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java
License:Apache License
private static <T> void binaryEncodeArray(T[] values, DataType type, ByteBuf buff) { int startIndex = buff.writerIndex(); buff.writeInt(1); // ndim buff.writeInt(0); // dataoffset buff.writeInt(type.id); // elemtype buff.writeInt(values.length); // dimension buff.writeInt(1); // lower bnds boolean hasNulls = false; for (T value : values) { if (value == null) { hasNulls = true;/*from w ww. j a v a2 s .com*/ buff.writeInt(-1); } else { int idx = buff.writerIndex(); buff.writeInt(0); encodeBinary(type, value, buff); buff.setInt(idx, buff.writerIndex() - idx - 4); } } if (hasNulls) { buff.setInt(startIndex + 4, 1); } }
From source file:io.reactiverse.pgclient.impl.codec.decoder.InitiateSslHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ByteBuf byteBuf = Unpooled.buffer(); byteBuf.writeInt(0);/*from www. j ava 2 s. com*/ byteBuf.writeInt(code); // out.writeInt(0x12345679); byteBuf.setInt(0, byteBuf.writerIndex()); ctx.writeAndFlush(byteBuf); super.channelActive(ctx); }