List of usage examples for io.netty.buffer ByteBuf readByte
public abstract byte readByte();
From source file:cn.lambdalib.multiblock.MsgBlockMulti.java
License:MIT License
@Override public void fromBytes(ByteBuf buf) { x = buf.readInt();/*from ww w .j av a2 s . c om*/ y = buf.readInt(); z = buf.readInt(); dir = ForgeDirection.values()[buf.readByte()]; s = buf.readByte(); }
From source file:cn.lambdalib.s11n.network.NetworkS11n.java
License:MIT License
/** * Deserialize an object with given type hint. * @return The deserialized object of type T. * @throws ContextException if deserialization is interrupted. That is, the object can't be restored out of * contextual difference (e.g. Some entity exists on server, but not created in one client). * @throws RuntimeException if deserialization failed non-trivially. *//*from ww w. jav a 2 s. com*/ public static <T, U extends T> T deserializeWithHint(ByteBuf buf, Class<U> type) { NetS11nAdaptor<? super U> adaptor = (NetS11nAdaptor) _adaptor(type); // System.out.println("adaptor " + type + " is " + adaptor); if (adaptor != null) { // Note that if adaptor's real type doesn't extend T there will be a cast exception. // With type erasure we can't do much about it. return (T) adaptor.read(buf); } else if (type.isArray()) { // Deserialize array int size = buf.readShort(); Class componentType = type.getComponentType(); Object ret = Array.newInstance(componentType, size); for (int i = 0; i < size; ++i) { Array.set(ret, i, deserialize(buf)); } return (T) ret; } else if (type.isEnum()) { // Deserialize enum return type.getEnumConstants()[buf.readByte()]; } else { // Recursive deserialization return deserializeRecursively(buf, type); } }
From source file:cn.liutils.api.player.ControlData.java
License:Open Source License
public void fromBytes(ByteBuf buf) { clear();/* ww w. ja v a 2s .c o m*/ while (buf.isReadable()) { byte tp = buf.readByte(); switch (tp) { case 0: while (buf.isReadable()) { byte pre = buf.readByte(); if (pre == -1) break; if (pre == StateType.LOCK_POSITION.ordinal()) { state[pre] = new StateLockPosition(player, buf); } if (pre == StateType.LOCK_ROTATION.ordinal()) { state[pre] = new StateLockRotation(player, buf); } if (pre == StateType.LOCK_CONTROL_MOVE.ordinal()) { state[pre] = new StateLockControlMove(player, buf); } if (pre == StateType.LOCK_CONTROL_JUMP.ordinal()) { state[pre] = new StateLockControlJump(player, buf); } if (pre == StateType.LOCK_CONTROL_SPIN.ordinal()) { state[pre] = new StateLockControlSpin(player, buf); } } break; case -1: return; default: LIUtils.log.warn("Unknown type: " + tp); } } }
From source file:cn.weaponmod.core.network.MessageWMKey.java
License:Open Source License
@Override public void fromBytes(ByteBuf buf) { keyid = buf.readByte(); keyDown = buf.readBoolean(); }
From source file:com.addthis.basis.util.Varint.java
License:Apache License
/** * @param buf to read bytes from/*from www . jav a 2 s. co m*/ * @return decode value * @throws IOException if {@link DataInput} throws {@link IOException} * @throws IllegalArgumentException if variable-length value does not terminate * after 9 bytes have been read * @see #writeUnsignedVarLong(long, DataOutput) */ public static long readUnsignedVarLong(ByteBuf buf) { long value = 0L; int i = 0; long b; while (((b = buf.readByte()) & 0x80L) != 0) { value |= (b & 0x7F) << i; i += 7; Preconditions.checkArgument(i <= 63, "Variable length quantity is too long (must be <= 63)"); } return value | (b << i); }
From source file:com.addthis.basis.util.Varint.java
License:Apache License
/** * @throws IllegalArgumentException if variable-length value does not terminate * after 5 bytes have been read * @throws IOException if {@link DataInput} throws {@link IOException} * @see #readUnsignedVarLong(DataInput)//from w w w . ja va 2 s .c om */ public static int readUnsignedVarInt(ByteBuf buf) { int value = 0; int i = 0; int b; while (((b = buf.readByte()) & 0x80) != 0) { value |= (b & 0x7F) << i; i += 7; Preconditions.checkArgument(i <= 35, "Variable length quantity is too long (must be <= 35)"); } return value | (b << i); }
From source file:com.addthis.hydra.data.util.KeyTopper.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { map = new HashMap<>(); errors = null;/*from w ww . j av a2s. co m*/ if (b.length == 0) { return; } ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try { byte marker = byteBuf.getByte(byteBuf.readerIndex()); if (marker == 0) { errors = new HashMap<>(); // Consume the sentinel byte value byteBuf.readByte(); } int mapSize = Varint.readUnsignedVarInt(byteBuf); try { if (mapSize > 0) { for (int i = 0; i < mapSize; i++) { int keyLength = Varint.readUnsignedVarInt(byteBuf); byte[] keybytes = new byte[keyLength]; byteBuf.readBytes(keybytes); String k = new String(keybytes, "UTF-8"); long value = Varint.readUnsignedVarLong(byteBuf); map.put(k, value); if (hasErrors()) { long error = Varint.readUnsignedVarLong(byteBuf); if (error != 0) { errors.put(k, error); } } } } } catch (Exception e) { throw Throwables.propagate(e); } } finally { byteBuf.release(); } }
From source file:com.addthis.hydra.task.source.Mark.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { ByteBuf buffer = Unpooled.wrappedBuffer(b); try {/*from ww w.j a v a 2 s. c o m*/ int valLength = Varint.readUnsignedVarInt(buffer); byte[] valBytes = new byte[valLength]; buffer.readBytes(valBytes); setValue(new String(valBytes)); setIndex(Varint.readUnsignedVarLong(buffer)); setEnd(buffer.readByte() == 1); setError(Varint.readUnsignedVarInt(buffer)); } finally { buffer.release(); } }
From source file:com.addthis.hydra.task.source.SimpleMark.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { ByteBuf buffer = Unpooled.wrappedBuffer(b); try {//from www . j a v a2 s .c o m int valLength = Varint.readUnsignedVarInt(buffer); byte[] valBytes = new byte[valLength]; buffer.readBytes(valBytes); val = new String(valBytes); index = Varint.readUnsignedVarLong(buffer); end = buffer.readByte() == 1; } finally { buffer.release(); } }
From source file:com.antsdb.saltedfish.server.mysql.packet.AuthPacket.java
License:Open Source License
@Override public void read(MysqlServerHandler handler, ByteBuf in) { if (!checkResponseVer41(in)) { clientParam = BufferUtils.readInt(in); maxThreeBytes = BufferUtils.readLongInt(in); user = BufferUtils.readString(in); rawPwd = BufferUtils.readString(in); dbname = BufferUtils.readString(in); } else {/*ww w . j av a 2s. c om*/ clientParam = BufferUtils.readLong(in); maxThreeBytes = BufferUtils.readLong(in); in.readByte(); // charset in.skipBytes(23); // reserved for future user = BufferUtils.readString(in); if ((clientParam & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA) != 0) { int passwordLength = in.readByte(); byte[] bytes = new byte[passwordLength]; in.readBytes(bytes); rawPwd = new String(bytes, Charsets.ISO_8859_1); } else { if ((clientParam & CLIENT_SECURE_CONNECTION) != 0) { int passwordLength = in.readByte(); byte[] bytes = new byte[passwordLength]; in.readBytes(bytes); rawPwd = new String(bytes, Charsets.ISO_8859_1); } else { rawPwd = BufferUtils.readString(in); } } if ((clientParam & MysqlServerHandler.CLIENT_CONNECT_WITH_DB) != 0) { dbname = BufferUtils.readString(in); } if ((clientParam & MysqlServerHandler.CLIENT_PLUGIN_AUTH) != 0) { BufferUtils.readString(in); } } }