List of usage examples for io.netty.buffer ByteBuf readByte
public abstract byte readByte();
From source file:eu.xworlds.util.raknet.RaknetDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception { final ByteBuf buf = msg.content(); buf.order(ByteOrder.BIG_ENDIAN); final byte id = buf.readByte(); final Constructor<? extends RaknetMessage> ctor = this.messages.get(Byte.valueOf(id)); if (ctor == null) { final RaknetMessage result = new InvalidRaknetMessage(id, buf, msg.sender(), msg.recipient()); out.add(result);/*from w w w . ja v a2s. c om*/ } else { try { final RaknetMessage result = ctor.newInstance(buf, msg.sender(), msg.recipient()); out.add(result); } catch (Exception ex) { final RaknetMessage result = new InvalidRaknetMessage(id, buf, msg.sender(), msg.recipient(), ex); out.add(result); } } }
From source file:fourteen.proprietaryprotocol.codec.NettyMessageDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null; }/*from w ww. j a va 2 s .c om*/ NettyMessage message = new NettyMessage(); Header header = new Header(); header.setCrcCode(frame.readInt()); header.setLength(frame.readInt()); header.setSessionId(frame.readLong()); header.setType(frame.readByte()); header.setPriority(frame.readByte()); int size = frame.readInt(); if (size > 0) { Map<String, Object> attch = new HashMap<String, Object>(size); int keySize = 0; byte[] keyArray = null; String key = null; for (int i = 0; i < size; i++) { keySize = frame.readInt(); keyArray = new byte[keySize]; frame.readBytes(keyArray); key = new String(keyArray, "UTF-8"); attch.put(key, marshallingDecoder.decode(frame)); } keyArray = null; key = null; header.setAttachment(attch); } if (frame.readableBytes() > 4) { message.setObject(marshallingDecoder.decode(frame)); } message.setHeader(header); return message; }
From source file:fr.letroll.ttorrentandroid.client.io.PeerMessageCodec.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { if (buf.readableBytes() == 0) { out.add(new PeerMessage.KeepAliveMessage()); return;/* w w w . ja v a 2s . co m*/ } byte type = buf.readByte(); PeerMessage message; switch (type) { case 0: message = new PeerMessage.ChokeMessage(); break; case 1: message = new PeerMessage.UnchokeMessage(); break; case 2: message = new PeerMessage.InterestedMessage(); break; case 3: message = new PeerMessage.NotInterestedMessage(); break; case 4: message = new PeerMessage.HaveMessage(); break; case 5: message = new PeerMessage.BitfieldMessage(); break; case 6: message = new PeerMessage.RequestMessage(); break; case 7: message = new PeerMessage.PieceMessage(); break; case 8: message = new PeerMessage.CancelMessage(); break; case 20: byte extendedType = buf.readByte(); switch (extendedType) { case 0: message = new PeerExtendedMessage.HandshakeMessage(); break; default: throw new IOException("Unknown extended message type " + extendedType); } default: throw new IOException("Unknown message type " + type); } message.fromWire(buf); out.add(message); if (buf.readableBytes() > 0) throw new IOException("Badly framed message " + message + "; remaining=" + buf.readableBytes()); }
From source file:gedi.remote.codec.DefaultDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < Integer.BYTES) return;/* ww w . j ava 2s . c om*/ in.markReaderIndex(); int size = in.readInt(); if (in.readableBytes() < size) { in.resetReaderIndex(); return; } // everything has arrived, decode char[] classname = new char[in.readInt()]; for (int i = 0; i < classname.length; i++) classname[i] = (char) (in.readByte() & 255); String clsName = String.valueOf(classname); if (clsName.length() == 1) { switch (clsName) { case "A": char[] re = new char[in.readInt()]; for (int i = 0; i < re.length; i++) re[i] = (char) (in.readByte() & 255); out.add(String.valueOf(re)); break; case "B": out.add(in.readByte()); break; case "S": out.add(in.readShort()); break; case "I": out.add(in.readInt()); break; case "L": out.add(in.readLong()); break; case "F": out.add(in.readFloat()); break; case "D": out.add(in.readDouble()); break; } } else { if (!ClassPathCache.getInstance().existsClass(clsName)) { in.resetReaderIndex(); return; } Class<?> cls = Class.forName(clsName); BinarySerializable re = (BinarySerializable) cls.newInstance(); BinaryBlob buff = new BinaryBlob(size - Integer.BYTES - classname.length); in.readBytes(buff.getBuffer()); buff.getBuffer().flip(); re.deserialize(buff); out.add(re); } }
From source file:hellfirepvp.astralsorcery.common.network.packet.server.PktPlayEffect.java
License:Open Source License
@Override public void fromBytes(ByteBuf buf) { this.typeOrdinal = buf.readByte(); this.pos = ByteBufUtils.readPos(buf); this.data = buf.readInt(); }
From source file:hellfirepvp.astralsorcery.common.network.packet.server.PktSyncConfig.java
License:Open Source License
@Override public void fromBytes(ByteBuf buf) { int count = buf.readByte(); fields = new ArrayList<>(count); for (int i = 0; i < count; i++) { fields.add(new SyncTuple(null, null)); //Empty init }// w w w. j a v a 2 s . c o m for (int i = 0; i < count; i++) { byte[] data = new byte[buf.readShort()]; buf.readBytes(data); ByteArrayInputStream in = new ByteArrayInputStream(data); SyncTuple tuple = null; String key = null; try { key = new DataInputStream(in).readUTF(); Object value = new ObjectInputStream(in).readObject(); tuple = new SyncTuple(key, value); } catch (Exception ignored) { } if (tuple == null) { fields = null; if (key != null) { AstralSorcery.log.info("Could not read config from server with key: " + key); } break; } fields.set(i, tuple); } }
From source file:hellfirepvp.astralsorcery.common.network.packet.server.PktSyncKnowledge.java
License:Open Source License
@Override public void fromBytes(ByteBuf buf) { this.state = buf.readByte(); int cLength = buf.readInt(); if (cLength != -1) { knownConstellations = new ArrayList<>(cLength); for (int i = 0; i < cLength; i++) { String val = ByteBufUtils.readString(buf); knownConstellations.add(val); }//from w ww. ja v a 2 s. co m } else { knownConstellations = new ArrayList<>(); } cLength = buf.readInt(); if (cLength != -1) { seenConstellations = new ArrayList<>(cLength); for (int i = 0; i < cLength; i++) { String val = ByteBufUtils.readString(buf); seenConstellations.add(val); } } else { seenConstellations = new ArrayList<>(); } int rLength = buf.readInt(); if (rLength != -1) { researchProgression = new ArrayList<>(rLength); for (int i = 0; i < rLength; i++) { researchProgression.add(ResearchProgression.getById(buf.readInt())); } } else { researchProgression = new ArrayList<>(); } int attunementPresent = buf.readByte(); if (attunementPresent != -1) { String attunement = ByteBufUtils.readString(buf); IConstellation c = ConstellationRegistry.getConstellationByName(attunement); if (c == null || !(c instanceof IMajorConstellation)) { AstralSorcery.log.warn( "[AstralSorcery] received constellation-attunement progress-packet with unknown constellation: " + attunement); } else { this.attunedConstellation = (IMajorConstellation) c; } } int perkLength = buf.readInt(); if (perkLength != -1) { this.appliedPerks = new HashMap<>(perkLength); for (int i = 0; i < perkLength; i++) { int id = buf.readInt(); int lvl = buf.readInt(); this.appliedPerks.put(ConstellationPerks.getById(id).getSingleInstance(), lvl); } } else { this.appliedPerks = new HashMap<>(); } this.wasOnceAttuned = buf.readBoolean(); this.progressTier = buf.readInt(); this.alignmentCharge = buf.readDouble(); }
From source file:herddb.proto.PduCodec.java
License:Apache License
public static Object readObject(ByteBuf dii) { int type = ByteBufUtils.readVInt(dii); switch (type) { case TYPE_BYTEARRAY: return ByteBufUtils.readArray(dii); case TYPE_LONG: return dii.readLong(); case TYPE_INTEGER: return dii.readInt(); case TYPE_SHORT: return dii.readShort(); case TYPE_BYTE: return dii.readByte(); case TYPE_STRING: return ByteBufUtils.readUnpooledRawString(dii); case TYPE_TIMESTAMP: return new java.sql.Timestamp(dii.readLong()); case TYPE_NULL: return null; case TYPE_BOOLEAN: return dii.readBoolean(); case TYPE_DOUBLE: return dii.readDouble(); default://from www.j a va2 s. c om throw new IllegalArgumentException("bad column type " + type); } }
From source file:herddb.utils.ByteBufUtils.java
License:Apache License
protected static final int readVIntUnfolded(ByteBuf buffer) { byte b = buffer.readByte(); int i = b & 0x7F; if ((b & 0x80) != 0) { b = buffer.readByte();/*w w w. j a v a 2s.com*/ i |= (b & 0x7F) << 7; if ((b & 0x80) != 0) { b = buffer.readByte(); i |= (b & 0x7F) << 14; if ((b & 0x80) != 0) { b = buffer.readByte(); i |= (b & 0x7F) << 21; if ((b & 0x80) != 0) { b = buffer.readByte(); i |= (b & 0x7F) << 28; } } } } return i; }
From source file:herddb.utils.ByteBufUtils.java
License:Apache License
protected static final int readVIntFolded(ByteBuf buffer) { byte b = buffer.readByte(); int i = b & 0x7F; for (int shift = 7; (b & 0x80) != 0; shift += 7) { b = buffer.readByte();// w w w.j a va2s. co m i |= (b & 0x7F) << shift; } return i; }