List of usage examples for io.netty.buffer ByteBuf readBytes
public abstract ByteBuf readBytes(ByteBuffer dst);
From source file:com.rs3e.utility.ByteBufUtils.java
License:Open Source License
public static String readString(ByteBuf buffer) { buffer.markReaderIndex();/*from ww w . j a v a 2 s . c o m*/ int len = 0; while (buffer.readUnsignedByte() != 0) len++; buffer.resetReaderIndex(); byte[] bytes = new byte[len]; buffer.readBytes(bytes); buffer.readerIndex(buffer.readerIndex() + 1); return new String(bytes, Charsets.ASCII_LATIN1_CHARSET); }
From source file:com.savageboy74.savagetech.network.message.MessageTileEntityBase.java
License:Open Source License
@Override public void fromBytes(ByteBuf buf) { this.x = buf.readInt(); this.y = buf.readInt(); this.z = buf.readInt(); this.orientation = buf.readByte(); this.state = buf.readByte(); int customNameLength = buf.readInt(); this.customName = new String(buf.readBytes(customNameLength).array()); int ownerLength = buf.readInt(); this.owner = new String(buf.readBytes(ownerLength).array()); }
From source file:com.seagate.kinetic.common.protocol.codec.KineticDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { in.markReaderIndex();/*from w ww.j a v a2 s . co m*/ // Wait until the length prefix is available // (magic ('F') + proto-msg-size + value-size) if (in.readableBytes() < 9) { in.resetReaderIndex(); return; } // 1. Read magic number. int magicNumber = in.readUnsignedByte(); if (magicNumber != 'F') { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid magic number: " + magicNumber); } // 2. protobuf message size int protoMessageLength = in.readInt(); // 3. attched value size int attachedValueLength = in.readInt(); // wait until whole message is available if (in.readableBytes() < (protoMessageLength + attachedValueLength)) { in.resetReaderIndex(); return; } // 4. read protobuf message byte[] decoded = new byte[protoMessageLength]; in.readBytes(decoded); // kinetic message KineticMessage km = new KineticMessage(); // construct protobuf message Message.Builder mbuilder = Message.newBuilder(); try { mbuilder.mergeFrom(decoded); } catch (Exception e) { in.resetReaderIndex(); logger.log(Level.WARNING, e.getMessage(), e); throw new RuntimeException(e); } // 5. read attched value if any if (attachedValueLength > 0) { // construct byte[] byte[] attachedValue = new byte[attachedValueLength]; // read from buffer in.readBytes(attachedValue); // set to message // mbuilder.setValue(ByteString.copyFrom(attachedValue)); km.setValue(attachedValue); } Message message = mbuilder.build(); km.setMessage(message); // get command bytes ByteString commandBytes = message.getCommandBytes(); // build command Command.Builder commandBuilder = Command.newBuilder(); try { commandBuilder.mergeFrom(commandBytes); km.setCommand(commandBuilder.build()); } catch (InvalidProtocolBufferException e) { logger.log(Level.WARNING, e.getMessage(), e); } // the whole message out.add(km); // print inbound message if (printMessage) { logger.info("Inbound protocol message: "); String printMsg = ProtocolMessageUtil.toString(km); logger.info(printMsg); } }
From source file:com.seventh_root.ld33.client.network.LD33ClientBoundPacketDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { byte[] output = new byte[in.readableBytes()]; in.readBytes(output); System.out.println(Arrays.toString(output)); in.resetReaderIndex();//from w w w. j av a 2 s . c o m while (in.isReadable()) { int id = in.readInt(); switch (id) { case 0: byte[] encodedPublicKey = new byte[in.readInt()]; in.readBytes(encodedPublicKey); out.add(new PublicKeyClientBoundPacket(encodedPublicKey)); break; case 1: out.add(new PlayerLoginClientBoundPacket()); break; case 2: String joiningPlayerUUID = readString(in); String joiningPlayerName = readString(in); int joiningPlayerResources = in.readInt(); out.add(new PlayerJoinClientBoundPacket(UUID.fromString(joiningPlayerUUID), joiningPlayerName, joiningPlayerResources)); break; case 3: String quittingPlayerUUID = readString(in); String quittingPlayerName = readString(in); out.add(new PlayerQuitClientBoundPacket(UUID.fromString(quittingPlayerUUID), quittingPlayerName)); break; case 4: String loginResponseMessage = readString(in); boolean loginSuccess = in.readBoolean(); out.add(new PlayerLoginResponseClientBoundPacket(loginResponseMessage, loginSuccess)); break; case 5: String spawningUnitUUID = readString(in); String spawningUnitPlayerUUID = readString(in); int spawningUnitX = in.readInt(); int spawningUnitY = in.readInt(); String type = readString(in); long spawningUnitCompletionTime = in.readLong(); Unit unit; switch (type) { case "wall": unit = new Wall(UUID.fromString(spawningUnitUUID), Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)), client.getWorldPanel().getWorld().getTileAt(spawningUnitX, spawningUnitY), spawningUnitCompletionTime); break; case "dragon": unit = new Dragon(UUID.fromString(spawningUnitUUID), Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)), client.getWorldPanel().getWorld().getTileAt(spawningUnitX, spawningUnitY), spawningUnitCompletionTime); break; case "flag": unit = new Flag(UUID.fromString(spawningUnitUUID), Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)), client.getWorldPanel().getWorld().getTileAt(spawningUnitX, spawningUnitY), spawningUnitCompletionTime); break; default: unit = null; break; } out.add(new UnitSpawnClientBoundPacket(unit)); break; case 6: String movingUnitUUID = readString(in); int movingUnitX = in.readInt(); int movingUnitY = in.readInt(); int movingUnitTargetX = in.readInt(); int movingUnitTargetY = in.readInt(); out.add(new UnitMoveClientBoundPacket( Unit.getByUUID(null, client.getWorldPanel().getWorld(), UUID.fromString(movingUnitUUID)), movingUnitX, movingUnitY, movingUnitTargetX, movingUnitTargetY)); break; case 7: String chatMessage = readString(in); out.add(new ChatMessageClientBoundPacket(chatMessage)); break; case 8: String purchasingPlayerUUID = readString(in); int purchasedUnitX = in.readInt(); int purchasedUnitY = in.readInt(); String purchasedUnitType = readString(in); out.add(new UnitPurchaseClientBoundPacket(UUID.fromString(purchasingPlayerUUID), purchasedUnitX, purchasedUnitY, purchasedUnitType)); break; } } }
From source file:com.seventh_root.ld33.server.network.LD33ServerBoundPacketDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { while (in.isReadable()) { int id = in.readInt(); switch (id) { case 0:/* w ww . java 2 s . c om*/ byte[] encodedPublicKey = new byte[in.readInt()]; in.readBytes(encodedPublicKey); out.add(new PublicKeyServerBoundPacket(encodedPublicKey)); break; case 1: String loggingInPlayerName = readString(in); byte[] encryptedPassword = new byte[in.readInt()]; in.readBytes(encryptedPassword); boolean signUp = in.readBoolean(); out.add(new PlayerLoginServerBoundPacket(loggingInPlayerName, encryptedPassword, signUp)); break; case 2: out.add(new PlayerJoinServerBoundPacket()); break; case 3: out.add(new PlayerQuitServerBoundPacket()); break; case 4: String loginResponseMessage = readString(in); boolean loginSuccess = in.readBoolean(); out.add(new PlayerLoginResponseServerBoundPacket(loginResponseMessage, loginSuccess)); break; case 5: String spawningUnitUUID = readString(in); String spawningUnitPlayerUUID = readString(in); int spawningUnitX = in.readInt(); int spawningUnitY = in.readInt(); String spawningUnitType = readString(in); long spawningUnitCompletionTime = in.readLong(); Unit spawningUnit; switch (spawningUnitType) { case "wall": spawningUnit = new Wall(UUID.fromString(spawningUnitUUID), Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)), server.getWorld().getTileAt(spawningUnitX, spawningUnitY), spawningUnitCompletionTime); break; case "dragon": spawningUnit = new Dragon(UUID.fromString(spawningUnitUUID), Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)), server.getWorld().getTileAt(spawningUnitX, spawningUnitY), spawningUnitCompletionTime); break; case "flag": spawningUnit = new Flag(UUID.fromString(spawningUnitUUID), Player.getByUUID(null, UUID.fromString(spawningUnitPlayerUUID)), server.getWorld().getTileAt(spawningUnitX, spawningUnitY), spawningUnitCompletionTime); break; default: spawningUnit = null; break; } out.add(new UnitSpawnServerBoundPacket(spawningUnit)); break; case 6: String movingUnitUUID = readString(in); int movingUnitTargetX = in.readInt(); int movingUnitTargetY = in.readInt(); out.add(new UnitMoveServerBoundPacket(Unit.getByUUID(server.getDatabaseConnection(), server.getWorld(), UUID.fromString(movingUnitUUID)), movingUnitTargetX, movingUnitTargetY)); break; case 7: String chatMessage = readString(in); out.add(new ChatMessageServerBoundPacket(chatMessage)); break; case 8: int purchasedUnitX = in.readInt(); int purchasedUnitY = in.readInt(); String purchasedUnitType = readString(in); out.add(new UnitPurchaseServerBoundPacket(purchasedUnitX, purchasedUnitY, purchasedUnitType)); break; } } }
From source file:com.sheldon.javaPrj.netty.FileServerHandler.java
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("read"); // System.out.println(msg); ByteBuf bb = (ByteBuf) msg; FileOutputStream out = new FileOutputStream(new File("/tmp/test.bk"), false); byte[] ba = new byte[bb.readableBytes()]; bb.readBytes(ba); out.write(ba);//from w w w . ja v a 2 s. co m out.flush(); out.close(); }
From source file:com.splicemachine.stream.KryoDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // LOG.warn("Decoding"); if (in.readableBytes() < 2) return;/*from ww w.j av a2s.c o m*/ in.markReaderIndex(); int len = in.readUnsignedShort(); // LOG.warn("Read lenght " + len); if (in.readableBytes() < len) { // LOG.warn("Not enough data "); in.resetReaderIndex(); return; } // LOG.warn("Decoding object "); byte[] buf = new byte[len]; in.readBytes(buf); Input input = new Input(buf); Kryo decoder = kp.get(); try { Object object = decoder.readClassAndObject(input); out.add(object); } finally { kp.returnInstance(decoder); } // LOG.warn("Decoded " + object); }
From source file:com.spotify.ffwd.riemann.RiemannFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;//from w ww. j a v a2 s.c om } final long length = in.getUnsignedInt(0); if (length > MAX_SIZE) { throw new CorruptedFrameException( String.format("frame size (%s) larger than max (%d)", length, MAX_SIZE)); } final int intLength = (int) length; if (in.readableBytes() < (4 + length)) { return; } in.skipBytes(4); final ByteBuf frame = in.readBytes(intLength); try { out.add(serializer.parse0(frame)); } finally { frame.release(); } }
From source file:com.spotify.folsom.client.ascii.RequestTestTemplate.java
License:Apache License
protected void assertRequest(AsciiRequest<?> request, String expectedCmd) { ByteBufAllocator bba = UnpooledByteBufAllocator.DEFAULT; ByteBuffer bb = ByteBuffer.allocate(1024); ByteBuf out = request.writeRequest(bba, bb); byte[] b = new byte[out.readableBytes()]; out.readBytes(b); assertEquals(expectedCmd, new String(b)); }
From source file:com.spotify.folsom.client.binary.BinaryMemcacheDecoder.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out) throws Exception { for (int i = 0; i < BATCH_SIZE; i++) { if (buf.readableBytes() < 24) { return; }/* w w w.j av a2s . co m*/ buf.markReaderIndex(); final int magicNumber = buf.readUnsignedByte(); // byte 0 if (magicNumber != 0x81) { throw fail(buf, String.format("Invalid magic number: 0x%2x", magicNumber)); } final int opcode = buf.readByte(); // byte 1 final int keyLength = buf.readUnsignedShort(); // byte 2-3 final int extrasLength = buf.readUnsignedByte(); // byte 4 buf.skipBytes(1); final int statusCode = buf.readUnsignedShort(); // byte 6-7 final MemcacheStatus status = MemcacheStatus.fromInt(statusCode); final int totalLength = buf.readInt(); // byte 8-11 final int opaque = buf.readInt(); final long cas = buf.readLong(); if (buf.readableBytes() < totalLength) { buf.resetReaderIndex(); return; } buf.skipBytes(extrasLength); buf.skipBytes(keyLength); final int valueLength = totalLength - keyLength - extrasLength; byte[] valueBytes; if (valueLength == 0) { valueBytes = NO_BYTES; } else { valueBytes = new byte[valueLength]; } buf.readBytes(valueBytes); replies.add(new ResponsePacket((byte) opcode, status, opaque, cas, valueBytes)); if ((opaque & 0xFF) == 0) { out.add(replies); replies = new BinaryResponse(); } } }