List of usage examples for io.netty.buffer ByteBuf writeByte
public abstract ByteBuf writeByte(int value);
From source file:cn.weaponmod.core.network.MessageWMKey.java
License:Open Source License
@Override public void toBytes(ByteBuf buf) { buf.writeByte(keyid); buf.writeBoolean(keyDown); }
From source file:com.addthis.basis.util.Varint.java
License:Apache License
/** * Encodes a value using the variable-length encoding from * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> * Google Protocol Buffers</a>. Zig-zag is not used, so input must not be negative. * If values can be negative, use {@link #writeSignedVarLong(long, DataOutput)} * instead. This method treats negative input as like a large unsigned value. * * @param value value to encode//w ww . j av a 2 s . c o m * @param buf to write bytes to * @throws IOException if {@link DataOutput} throws {@link IOException} */ public static void writeUnsignedVarLong(long value, ByteBuf buf) { while ((value & 0xFFFFFFFFFFFFFF80L) != 0L) { buf.writeByte(((int) value & 0x7F) | 0x80); value >>>= 7; } buf.writeByte((int) value & 0x7F); }
From source file:com.addthis.basis.util.Varint.java
License:Apache License
/** * @see #writeUnsignedVarLong(long, DataOutput) *//*w w w . ja v a2 s . c o m*/ public static void writeUnsignedVarInt(int value, ByteBuf buf) { while ((value & 0xFFFFFF80) != 0L) { buf.writeByte((value & 0x7F) | 0x80); value >>>= 7; } buf.writeByte(value & 0x7F); }
From source file:com.addthis.hydra.data.util.KeyTopper.java
License:Apache License
/** * Encode the data structure into a serialized representation. * Encode the number of elements followed by each (key, value) * pair. If the error estimation is used then encode the special * byte value 0 (since we will never encode 0 as the size * of a non-empty map) at the head of the byte array. * @param version/*from w ww . j a v a 2s.c o m*/ * @return */ @Override public byte[] bytesEncode(long version) { if (map.size() == 0) { return EMPTY; } byte[] retBytes = null; ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer(); try { if (hasErrors()) { byteBuf.writeByte(0); } Varint.writeUnsignedVarInt(map.size(), byteBuf); for (Map.Entry<String, Long> mapEntry : map.entrySet()) { String key = mapEntry.getKey(); if (key == null) { throw new NullPointerException("KeyTopper decoded null key"); } byte[] keyBytes = key.getBytes("UTF-8"); Varint.writeUnsignedVarInt(keyBytes.length, byteBuf); byteBuf.writeBytes(keyBytes); Varint.writeUnsignedVarLong(mapEntry.getValue(), byteBuf); if (hasErrors()) { Long error = errors.get(key); if (error != null) { Varint.writeUnsignedVarLong(error, byteBuf); } else { Varint.writeUnsignedVarLong(0, byteBuf); } } } retBytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(retBytes); } catch (UnsupportedEncodingException e) { throw Throwables.propagate(e); } finally { byteBuf.release(); } return retBytes; }
From source file:com.addthis.hydra.task.source.Mark.java
License:Apache License
@Override public byte[] bytesEncode(long version) { byte[] retBytes = null; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(); try {/* w w w . ja va 2s . com*/ byte[] valBytes = getValue().getBytes(); Varint.writeUnsignedVarInt(valBytes.length, buffer); buffer.writeBytes(valBytes); Varint.writeUnsignedVarLong(getIndex(), buffer); buffer.writeByte(isEnd() ? 1 : 0); Varint.writeUnsignedVarInt(error, buffer); retBytes = new byte[buffer.readableBytes()]; buffer.readBytes(retBytes); } finally { buffer.release(); } return retBytes; }
From source file:com.addthis.hydra.task.source.SimpleMark.java
License:Apache License
@Override public byte[] bytesEncode(long version) { byte[] retBytes = null; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(); try {/* ww w.j a va2 s . co m*/ byte[] valBytes = val.getBytes(); Varint.writeUnsignedVarInt(valBytes.length, buffer); buffer.writeBytes(valBytes); Varint.writeUnsignedVarLong(index, buffer); buffer.writeByte(end ? 1 : 0); retBytes = new byte[buffer.readableBytes()]; buffer.readBytes(retBytes); } finally { buffer.release(); } return retBytes; }
From source file:com.addthis.meshy.service.stream.StreamTarget.java
License:Apache License
private int doSendMore(SendWatcher sender) { if (log.isTraceEnabled()) { log.trace("{} sendMore for {} rem={} closed={} streamComplete={} fileIn={}", this, sender, sendRemain, closed, streamComplete, fileIn); }/*from w ww. ja va2 s.c o m*/ try { if (maybeDropSend()) { if (StreamService.LOG_DROP_MORE || log.isDebugEnabled()) { log.debug("{} sendMore drop request sr={}, cl={}, co={}, fi={}", this, sendRemain, closed, streamComplete, fileIn); } return -1; } byte[] next = fileIn.nextBytes(StreamService.READ_WAIT); if (next != null) { if (log.isTraceEnabled()) { log.trace("{} send add read={}", this, next.length); } StreamService.readBytes.addAndGet(next.length); ByteBuf buf = getSendBuffer(next.length + StreamService.STREAM_BYTE_OVERHEAD); buf.writeByte(StreamService.MODE_MORE); buf.writeBytes(next); int bytesSent = send(buf, sender); sendRemain.addAndGet(-bytesSent); sentBytes += bytesSent; if (log.isTraceEnabled()) { log.trace("{} read read={} sendRemain={}", this, next.length, sendRemain); } return bytesSent; } else if (fileIn.isEOF()) { if (log.isTraceEnabled()) { log.trace("{} read close on null. sendRemain={}", this, sendRemain); } modeClose(); return -1; } else { if (log.isDebugEnabled()) { log.debug("{} read timeout. sendRemain={}", this, sendRemain); } return 0; } } catch (Exception ex) { ex.printStackTrace(); return -1; } }
From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java
License:Open Source License
/** * Add header to finish the full packet// www . ja v a 2 s . c om * @param out * @param packetSeq * @param writeBodyFunc write packet body function */ public static void writePacket(ByteBuf out, byte packetSeq, Callback writeBodyFunc) { int start = out.writerIndex(); out.writeZero(4); writeBodyFunc.callback(); int end = out.writerIndex(); out.writeByte(0); out.writerIndex(start); int length = end - start - MySQLPacket.packetHeaderSize; BufferUtils.writeLongInt(out, length); out.writeByte(packetSeq); out.writerIndex(end); if (_log.isTraceEnabled()) { int readerIndex = out.readerIndex(); out.readerIndex(start); byte[] bytes = new byte[end - start]; out.readBytes(bytes); out.readerIndex(readerIndex); String dump = '\n' + UberUtil.hexDump(bytes); _log.trace(dump); } }
From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java
License:Open Source License
/** * Writer com_stmt_prepare_response packet body * <pre>//from www .j ava 2 s . com * From server to client, in response to prepared statement initialization packet. * It is made up of: * 1.a PREPARE_OK packet * 2.if "number of parameters" > 0 * (field packets) as in a Result Set Header Packet * (EOF packet) * 3.if "number of columns" > 0 * (field packets) as in a Result Set Header Packet * (EOF packet) * * ----------------------------------------------------------------------------------------- * * Bytes Name * ----- ---- * 1 0 - marker for OK packet * 4 statement_handler_id * 2 number of columns in result set * 2 number of parameters in query * 1 filler (always 0) * 2 warning count * * @see http://dev.mysql.com/doc/internals/en/prepared-statement-initialization-packet.html * </pre> * @param buffer * @param statementId * @param columnsNumber * @param parametersNumber */ public void writePreparedOKBody(ByteBuf buffer, long statementId, int columnsNumber, int parametersNumber) { // flag = 0 buffer.writeByte(0); BufferUtils.writeUB4(buffer, statementId); BufferUtils.writeUB2(buffer, columnsNumber); BufferUtils.writeUB2(buffer, parametersNumber); // filler = 0 buffer.writeByte(0); // warningCount = 0; BufferUtils.writeUB2(buffer, 0); }
From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java
License:Open Source License
/** * /* ww w .ja v a 2 s. co m*/ * From server to client after command, if no error and result set -- that is, * if the command was a query which returned a result set. The Result Set Header * Packet is the first of several, possibly many, packets that the server sends * for result sets. The order of packets for a result set is: * * <pre> * (Result Set Header Packet) the number of columns * (Field Packets) column descriptors * (EOF Packet) marker: end of Field Packets * (Row Data Packets) row contents * (EOF Packet) marker: end of Data Packets * * Bytes Name * ----- ---- * 1-9 (Length-Coded-Binary) field_count * 1-9 (Length-Coded-Binary) extra * * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Result_Set_Header_Packet * </pre> * * @param buffer * @param meta */ public void writeColumnDefBody(ByteBuf buffer, FieldMeta meta) { //catalog BufferUtils.writeLenString(buffer, "def", this.cs); //db, schema if (meta.getSourceTable() != null) { BufferUtils.writeLenString(buffer, meta.getSourceTable().getNamespace(), this.cs); } else { BufferUtils.writeLenString(buffer, "", this.cs); } // table BufferUtils.writeLenString(buffer, meta.getTableAlias(), this.cs); // orgTable if (meta.getSourceTable() != null) { BufferUtils.writeLenString(buffer, meta.getSourceTable().getTableName(), this.cs); } else { BufferUtils.writeLenString(buffer, "", this.cs); } // col name BufferUtils.writeLenString(buffer, meta.getName(), this.cs); // col original name BufferUtils.writeLenString(buffer, meta.getSourceName(), this.cs); // next length buffer.writeByte((byte) 0x0C); if (meta.getType() == null) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, 0); buffer.writeByte((byte) (FIELD_TYPE_NULL & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte(0); } else if (meta.getType().getJavaType() == Boolean.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, meta.getType().getLength()); buffer.writeByte((byte) (FIELD_TYPE_TINY & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte(0); } else if (meta.getType().getSqlType() == Types.TINYINT) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, meta.getType().getLength()); buffer.writeByte((byte) (FIELD_TYPE_TINY & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte((byte) meta.getType().getScale()); } else if (meta.getType().getJavaType() == String.class) { // char set utf8_general_ci : 0x21 BufferUtils.writeInt(buffer, this.csidx); // length BufferUtils.writeUB4(buffer, meta.getType().getLength() * 3); // type code buffer.writeByte((byte) (FIELD_TYPE_VAR_STRING & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte(0); } else if (meta.getType().getJavaType() == Integer.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, 21); buffer.writeByte((byte) (FIELD_TYPE_LONG & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte(0); } else if (meta.getType().getJavaType() == Long.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, 21); buffer.writeByte((byte) (FIELD_TYPE_LONGLONG & 0xff)); buffer.writeByte(0x00); // signed. buffer.writeByte(0x00); buffer.writeByte(0); } else if (meta.getType().getJavaType() == BigInteger.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, meta.getType().getLength()); buffer.writeByte((byte) (FIELD_TYPE_LONGLONG & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte(0); } else if (meta.getType().getJavaType() == BigDecimal.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, meta.getType().getLength()); buffer.writeByte((byte) (FIELD_TYPE_DECIMAL & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte((byte) meta.getType().getScale()); } else if (meta.getType().getJavaType() == Float.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, meta.getType().getLength()); buffer.writeByte((byte) (FIELD_TYPE_FLOAT & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte((byte) meta.getType().getScale()); } else if (meta.getType().getJavaType() == Double.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, meta.getType().getLength()); buffer.writeByte((byte) (FIELD_TYPE_DOUBLE & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte((byte) meta.getType().getScale()); } else if (meta.getType().getJavaType() == Timestamp.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, meta.getType().getLength()); buffer.writeByte((byte) (FIELD_TYPE_TIMESTAMP & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte((byte) meta.getType().getScale()); } else if (meta.getType().getJavaType() == Date.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, meta.getType().getLength()); buffer.writeByte((byte) (FIELD_TYPE_DATE & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte((byte) meta.getType().getScale()); } else if (meta.getType().getJavaType() == Time.class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, meta.getType().getLength()); buffer.writeByte((byte) (FIELD_TYPE_TIME & 0xff)); buffer.writeByte(0x00); buffer.writeByte(0x00); buffer.writeByte((byte) meta.getType().getScale()); } // BLOB return byte[] as its java type else if (meta.getType().getJavaType() == byte[].class) { BufferUtils.writeInt(buffer, 0x3f); BufferUtils.writeUB4(buffer, 2147483647); buffer.writeByte((byte) (FIELD_TYPE_BLOB & 0xff)); // flag for Blob is x90 x00 buffer.writeByte(0x90); buffer.writeByte(0x00); buffer.writeByte((byte) meta.getType().getScale()); } else { throw new NotImplementedException("Unsupported data type:" + meta.getType().getJavaType()); } /** * need add meta info to specify flag if (meta.isNullable() == 1) { flags |= 0001; } if (meta.isSigned()) { flags |= 0020; } if (meta.isAutoIncrement()) { flags |= 0200; } */ // filler buffer.writeZero(2); }