List of usage examples for io.netty.buffer ByteBuf readInt
public abstract int readInt();
From source file:com.zaradai.distributor.messaging.netty.InetSocketAddressSerializer.java
License:Apache License
public static InetSocketAddress deserialize(ByteBuf in) throws UnknownHostException { int length = in.readInt(); byte[] bytes = new byte[length]; in.readBytes(bytes);// w w w . j ava 2 s. c om InetAddress inetAddress = InetAddress.getByAddress(bytes); // read port and return return new InetSocketAddress(inetAddress, in.readInt()); }
From source file:com.zxcc.socket.protobuf.ProtobufVarint32FrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex();//ww w.j a va 2 s . c o m int bufLenght = 0; if (!in.isReadable()) { in.resetReaderIndex(); return; } if (in.readableBytes() < 4) { in.resetReaderIndex(); return; } bufLenght = in.readInt(); if (bufLenght < 0) { throw new CorruptedFrameException("negative length: " + bufLenght); } if (in.readableBytes() < bufLenght) { in.resetReaderIndex(); return; } else { out.add(in.readBytes(bufLenght)); return; } }
From source file:com.zz.learning.netty5.chap12.codec.MarshallingDecoder.java
License:Apache License
/** * ??//w ww. j a v a 2 s.c om * @param in * @return * @throws Exception */ protected Object decode(ByteBuf in) throws Exception { int objectSize = in.readInt(); /* * ?ByteBuf?? */ ByteBuf buf = in.slice(in.readerIndex(), objectSize); ByteInput input = new ChannelBufferByteInput(buf); try { unmarshaller.start(input); Object obj = unmarshaller.readObject(); unmarshaller.finish(); in.readerIndex(in.readerIndex() + objectSize); // return obj; } finally { unmarshaller.close(); } }
From source file:com.zz.learning.netty5.chap12.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 av a 2 s . c o m*/ 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(); // attachment 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(); // key 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) { //?4??0 message.setBody(marshallingDecoder.decode(frame)); } message.setHeader(header); return message; }
From source file:cubicchunks.network.PacketColumn.java
License:MIT License
@Override public void fromBytes(ByteBuf buf) { this.chunkPos = new ChunkPos(buf.readInt(), buf.readInt()); this.data = new byte[buf.readInt()]; buf.readBytes(this.data); }
From source file:cubicchunks.network.PacketCube.java
License:MIT License
@Override public void fromBytes(ByteBuf buf) { this.cubePos = new CubePos(buf.readInt(), buf.readInt(), buf.readInt()); this.data = new byte[buf.readInt()]; buf.readBytes(this.data); int numTiles = buf.readInt(); this.tileEntityTags = new ArrayList<>(numTiles); for (int i = 0; i < numTiles; i++) { this.tileEntityTags.add(ByteBufUtils.readTag(buf)); }// w w w. j av a2 s. co m }
From source file:cubicchunks.network.PacketCubeBlockChange.java
License:MIT License
@SuppressWarnings("deprecation") // Forge thinks we are trying to register a block or something :P @Override/* w ww. j a v a 2s . c om*/ public void fromBytes(ByteBuf in) { this.cubePos = new CubePos(in.readInt(), in.readInt(), in.readInt()); short numBlocks = in.readShort(); localAddresses = new short[numBlocks]; blockStates = new IBlockState[numBlocks]; for (int i = 0; i < numBlocks; i++) { localAddresses[i] = in.readShort(); blockStates[i] = Block.BLOCK_STATE_IDS.getByValue(readVarInt(in, 4)); } int numHmapChanges = in.readUnsignedByte(); heightValues = new int[numHmapChanges]; for (int i = 0; i < numHmapChanges; i++) { heightValues[i] = in.readInt(); } }
From source file:cubicchunks.network.PacketUnloadColumn.java
License:MIT License
@Override public void fromBytes(ByteBuf buf) { this.chunkPos = new ChunkPos(buf.readInt(), buf.readInt()); }
From source file:cubicchunks.network.PacketUnloadCube.java
License:MIT License
@Override public void fromBytes(ByteBuf in) { this.cubePos = new CubePos(in.readInt(), in.readInt(), in.readInt()); }
From source file:dan200.qcraft.shared.QCraftPacket.java
License:Open Source License
@Override public void fromBytes(ByteBuf buffer) { packetType = buffer.readByte();/*from w w w. j a v a2s .c om*/ byte nString = buffer.readByte(); byte nInt = buffer.readByte(); int nByte = buffer.readInt(); if (nString == 0) { dataString = null; } else { dataString = new String[nString]; for (int k = 0; k < nString; k++) { if (buffer.readBoolean()) { int len = buffer.readInt(); byte[] b = new byte[len]; buffer.readBytes(b); try { dataString[k] = new String(b, "UTF-8"); } catch (UnsupportedEncodingException e) { dataString[k] = null; } } } } if (nInt == 0) { dataInt = null; } else { dataInt = new int[nInt]; for (int k = 0; k < nInt; k++) { dataInt[k] = buffer.readInt(); } } if (nByte == 0) { dataByte = null; } else { dataByte = new byte[nByte][]; for (int k = 0; k < nByte; k++) { int length = buffer.readInt(); if (length > 0) { dataByte[k] = new byte[length]; buffer.getBytes(buffer.readerIndex(), dataByte[k]); } } } }