List of usage examples for io.netty.buffer ByteBuf readByte
public abstract byte readByte();
From source file:appeng.util.item.AEFluidStack.java
License:Open Source License
public static IAEFluidStack loadFluidStackFromPacket(final ByteBuf data) throws IOException { final byte mask = data.readByte(); // byte PriorityType = (byte) (mask & 0x03); final byte stackType = (byte) ((mask & 0x0C) >> 2); final byte countReqType = (byte) ((mask & 0x30) >> 4); final boolean isCraftable = (mask & 0x40) > 0; final boolean hasTagCompound = (mask & 0x80) > 0; // don't send this... final NBTTagCompound d = new NBTTagCompound(); final byte len2 = data.readByte(); final byte[] name = new byte[len2]; data.readBytes(name, 0, len2);/* w w w.ja v a 2s .c om*/ d.setString("FluidName", new String(name, "UTF-8")); d.setByte("Count", (byte) 0); if (hasTagCompound) { final int len = data.readInt(); final byte[] bd = new byte[len]; data.readBytes(bd); final DataInputStream di = new DataInputStream(new ByteArrayInputStream(bd)); d.setTag("tag", CompressedStreamTools.read(di)); } // long priority = getPacketValue( PriorityType, data ); final long stackSize = getPacketValue(stackType, data); final long countRequestable = getPacketValue(countReqType, data); final FluidStack fluidStack = FluidStack.loadFluidStackFromNBT(d); if (fluidStack == null) { return null; } final AEFluidStack fluid = AEFluidStack.create(fluidStack); // fluid.priority = (int) priority; fluid.setStackSize(stackSize); fluid.setCountRequestable(countRequestable); fluid.setCraftable(isCraftable); return fluid; }
From source file:appeng.util.item.AEItemStack.java
License:Open Source License
public static IAEItemStack loadItemStackFromPacket(final ByteBuf data) throws IOException { final byte mask = data.readByte(); // byte PriorityType = (byte) (mask & 0x03); final byte stackType = (byte) ((mask & 0x0C) >> 2); final byte countReqType = (byte) ((mask & 0x30) >> 4); final boolean isCraftable = (mask & 0x40) > 0; final boolean hasTagCompound = (mask & 0x80) > 0; // don't send this... final NBTTagCompound d = new NBTTagCompound(); // For some insane reason, Vanilla can only parse numeric item ids if they are strings short itemNumericId = data.readShort(); d.setString("id", String.valueOf(itemNumericId)); d.setShort("Damage", data.readShort()); d.setByte("Count", (byte) 0); if (hasTagCompound) { final int len = data.readInt(); final byte[] bd = new byte[len]; data.readBytes(bd);//from www . j a v a 2s. c om final ByteArrayInputStream di = new ByteArrayInputStream(bd); d.setTag("tag", CompressedStreamTools.read(new DataInputStream(di))); } // long priority = getPacketValue( PriorityType, data ); final long stackSize = getPacketValue(stackType, data); final long countRequestable = getPacketValue(countReqType, data); final ItemStack itemstack = ItemStack.loadItemStackFromNBT(d); if (itemstack == null) { return null; } final AEItemStack item = AEItemStack.create(itemstack); // item.priority = (int) priority; item.setStackSize(stackSize); item.setCountRequestable(countRequestable); item.setCraftable(isCraftable); return item; }
From source file:appeng.util.item.AEStack.java
License:Open Source License
static long getPacketValue(final byte type, final ByteBuf tag) { if (type == 0) { long l = tag.readByte(); l -= Byte.MIN_VALUE;/* w w w .j av a 2 s . c om*/ return l; } else if (type == 1) { long l = tag.readShort(); l -= Short.MIN_VALUE; return l; } else if (type == 2) { long l = tag.readInt(); l -= Integer.MIN_VALUE; return l; } return tag.readLong(); }
From source file:at.yawk.dbus.protocol.codec.MessageHeaderCodec.java
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf rawBuf, List<Object> out) throws Exception { if (toRead != 0) { if (rawBuf.readableBytes() < toRead) { return; }//from w ww .j a v a 2 s . c o m ByteBuf slice = rawBuf.slice().order(byteOrder); slice.writerIndex(slice.readerIndex() + toRead); slice.retain(); AlignableByteBuf decoding = AlignableByteBuf.decoding(slice); log.trace("INBOUND {}", decoding); out.add(decoding); rawBuf.readerIndex(rawBuf.readerIndex() + toRead); toRead = 0; } if (rawBuf.readableBytes() < MIN_HEADER_LENGTH) { return; } rawBuf.markReaderIndex(); byte endianness = rawBuf.readByte(); ByteOrder order; switch (endianness) { case 'l': order = ByteOrder.LITTLE_ENDIAN; break; case 'B': order = ByteOrder.BIG_ENDIAN; break; default: throw new DecoderException("Unknown byte order byte " + endianness); } AlignableByteBuf buf = AlignableByteBuf.decoding(rawBuf.resetReaderIndex().order(order)); buf.getBuffer().markReaderIndex(); buf.readByte(); // skip endianness byte we read above @Nullable MessageType type = MessageType.byId(buf.readByte()); byte flags = buf.readByte(); byte majorProtocolVersion = buf.readByte(); if (majorProtocolVersion != PROTOCOL_VERSION) { throw new DecoderException("Unsupported major protocol version " + majorProtocolVersion); } long bodyLength = buf.readUnsignedInt(); int serial = buf.readInt(); MessageHeader header = new MessageHeader(); header.setByteOrder(order); header.setMessageType(type); header.setNoReplyExpected((flags & NO_REPLY_EXPECTED) != 0); header.setNoAutoStart((flags & NO_AUTO_START) != 0); header.setAllowInteractiveAuthorization((flags & ALLOW_INTERACTIVE_AUTHORIZATION) != 0); header.setMajorProtocolVersion(majorProtocolVersion); header.setMessageBodyLength(bodyLength); header.setSerial(serial); header.setHeaderFields(new EnumMap<>(HeaderField.class)); ArrayObject headers = (ArrayObject) tryDecode(HEADER_FIELD_LIST_TYPE, buf); if (headers == null) { // not enough data buf.getBuffer().resetReaderIndex(); return; } for (DbusObject struct : headers.getValues()) { HeaderField field = HeaderField.byId(struct.get(0).byteValue()); if (field != null) { DbusObject value = struct.get(1).getValue(); if (!value.getType().equals(field.getType())) { throw new DecoderException("Invalid header type on " + field + ": got " + value.getType() + " but expected " + field.getType()); } header.getHeaderFields().put(field, value); } } if (type != null) { checkRequiredHeaderFieldsPresent(header); } if (!buf.canAlignRead(8)) { buf.getBuffer().resetReaderIndex(); return; } buf.alignRead(8); toRead = Math.toIntExact(header.getMessageBodyLength()); byteOrder = order; out.add(header); }
From source file:at.yawk.votifier.LineSplitter.java
License:Mozilla Public License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex();/* w ww . j a v a 2s. c om*/ int lineLength = 0; boolean found = false; while (in.isReadable()) { if (in.readByte() == '\n') { found = true; break; } lineLength++; } in.resetReaderIndex(); if (found) { byte[] line = new byte[lineLength]; in.readBytes(line); in.readByte(); // newline out.add(new String(line, StandardCharsets.UTF_8)); } }
From source file:blazingcache.network.netty.DodoMessageUtils.java
License:Apache License
private static Object readEncodedSimpleValue(ByteBuf encoded) { byte _opcode = encoded.readByte(); switch (_opcode) { case OPCODE_NULL_VALUE: return null; case OPCODE_STRING_VALUE: return readUTF8String(encoded); case OPCODE_INT_VALUE: return encoded.readInt(); case OPCODE_MAP_VALUE: { int len = encoded.readInt(); Map<Object, Object> ret = new HashMap<>(); for (int i = 0; i < len; i++) { Object mapkey = readEncodedSimpleValue(encoded); Object value = readEncodedSimpleValue(encoded); ret.put(mapkey, value);// ww w.j a v a 2 s. com } return ret; } case OPCODE_SET_VALUE: { int len = encoded.readInt(); Set<Object> ret = new HashSet<>(); for (int i = 0; i < len; i++) { Object o = readEncodedSimpleValue(encoded); ret.add(o); } return ret; } case OPCODE_LIST_VALUE: { int len = encoded.readInt(); List<Object> ret = new ArrayList<>(len); for (int i = 0; i < len; i++) { Object o = readEncodedSimpleValue(encoded); ret.add(o); } return ret; } case OPCODE_BYTEARRAY_VALUE: { int len = encoded.readInt(); byte[] ret = new byte[len]; encoded.readBytes(ret); return ret; } case OPCODE_LONG_VALUE: return encoded.readLong(); default: throw new RuntimeException("invalid opcode: " + _opcode); } }
From source file:blazingcache.network.netty.DodoMessageUtils.java
License:Apache License
public static Message decodeMessage(ByteBuf encoded) { byte version = encoded.readByte(); if (version != VERSION) { throw new RuntimeException("bad protocol version " + version); }/* ww w. j ava2 s .co m*/ int type = encoded.readInt(); String messageId = readUTF8String(encoded); String replyMessageId = null; String workerProcessId = null; Map<String, Object> params = new HashMap<>(); while (encoded.isReadable()) { byte opcode = encoded.readByte(); switch (opcode) { case OPCODE_REPLYMESSAGEID: replyMessageId = readUTF8String(encoded); break; case OPCODE_WORKERPROCESSID: workerProcessId = readUTF8String(encoded); break; case OPCODE_PARAMETERS: int size = encoded.readInt(); for (int i = 0; i < size; i++) { Object key = readEncodedSimpleValue(encoded); Object value = readEncodedSimpleValue(encoded); params.put((String) key, value); } break; default: throw new RuntimeException("invalid opcode: " + opcode); } } Message m = new Message(workerProcessId, type, params); if (replyMessageId != null) { m.replyMessageId = replyMessageId; } m.messageId = messageId; return m; }
From source file:blazingcache.network.netty.MessageUtils.java
License:Apache License
private static Object readEncodedSimpleValue(ByteBuf encoded) { byte _opcode = encoded.readByte(); switch (_opcode) { case OPCODE_NULL_VALUE: return null; case OPCODE_STRING_VALUE: return RawString.of(readUTF8String(encoded)); case OPCODE_INT_VALUE: return encoded.readInt(); case OPCODE_MAP_VALUE: { int len = encoded.readInt(); Map<Object, Object> ret = new HashMap<>(); for (int i = 0; i < len; i++) { Object mapkey = readEncodedSimpleValue(encoded); Object value = readEncodedSimpleValue(encoded); ret.put(mapkey, value);/* w w w .j av a 2 s.co m*/ } return ret; } case OPCODE_SET_VALUE: { int len = encoded.readInt(); Set<Object> ret = new HashSet<>(); for (int i = 0; i < len; i++) { Object o = readEncodedSimpleValue(encoded); ret.add(o); } return ret; } case OPCODE_LIST_VALUE: { int len = encoded.readInt(); List<Object> ret = new ArrayList<>(len); for (int i = 0; i < len; i++) { Object o = readEncodedSimpleValue(encoded); ret.add(o); } return ret; } case OPCODE_BYTEARRAY_VALUE: { int len = encoded.readInt(); byte[] ret = new byte[len]; encoded.readBytes(ret); return ret; } case OPCODE_LONG_VALUE: return encoded.readLong(); default: throw new RuntimeException("invalid opcode: " + _opcode); } }
From source file:blazingcache.network.netty.MessageUtils.java
License:Apache License
public static Message decodeMessage(ByteBuf encoded) { byte version = encoded.readByte(); if (version != VERSION) { throw new RuntimeException("bad protocol version " + version); }/*w w w .j a v a 2 s .c o m*/ int type = encoded.readInt(); String messageId = readUTF8String(encoded); String replyMessageId = null; String workerProcessId = null; Map<String, Object> params = new HashMap<>(); while (encoded.isReadable()) { byte opcode = encoded.readByte(); switch (opcode) { case OPCODE_REPLYMESSAGEID: replyMessageId = readUTF8String(encoded); break; case OPCODE_WORKERPROCESSID: workerProcessId = readUTF8String(encoded); break; case OPCODE_PARAMETERS: int size = encoded.readInt(); for (int i = 0; i < size; i++) { Object key = readEncodedSimpleValue(encoded); Object value = readEncodedSimpleValue(encoded); params.put(((RawString) key).toString(), value); } break; default: throw new RuntimeException("invalid opcode: " + opcode); } } Message m = new Message(workerProcessId, type, params); if (replyMessageId != null) { m.replyMessageId = replyMessageId; } m.messageId = messageId; return m; }
From source file:blusunrize.immersiveengineering.common.util.network.MessageSpeedloaderSync.java
@Override public void fromBytes(ByteBuf buf) { slot = buf.readByte(); hand = EnumHand.values()[buf.readByte()]; }