List of usage examples for io.netty.buffer ByteBuf readByte
public abstract byte readByte();
From source file:com.quavo.util.buf.ByteBufUtils.java
License:Open Source License
/** * Converts data from a {@link ByteBuf} into a readable jag string. * * @param buffer The {@link ByteBuf}./*from w w w. jav a 2 s . co m*/ * @return The string. */ public static String readJagString(ByteBuf buffer) { StringBuilder bldr = new StringBuilder(); byte b; buffer.readByte(); while (buffer.isReadable() && (b = buffer.readByte()) != STRING_TERMINATOR) { bldr.append((char) b); } return bldr.toString(); }
From source file:com.rs3e.network.protocol.codec.handshake.HandshakeDecoder.java
License:Open Source License
@Override public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception { if (!in.readable()) return;/*w w w . ja va2 s .c om*/ ctx.pipeline().remove(HandshakeDecoder.class); int incomingOpcode = in.readByte() & 0xFF; HandshakeState handshakeState = HandshakeState.forId(incomingOpcode); if (handshakeState == null) { return; } switch (handshakeState) { case HANDSHAKE_UPDATE: ctx.pipeline().addFirst(new UpdateEncoder(), new UpdateStatusEncoder(), new XorEncoder(), new UpdateDecoder()); break; case HANDSHAKE_WORLD_LIST: ctx.pipeline().addFirst(new WorldListEncoder(), new WorldListDecoder()); break; case HANDSHAKE_LOGIN: ctx.pipeline().addFirst(new LoginEncoder(), new LoginDecoder()); ctx.write(new LoginResponse(0)); break; default: break; } ctx.nextInboundMessageBuffer().add(new HandshakeMessage(handshakeState)); ctx.fireInboundBufferUpdated(); if (in.readable()) { ChannelHandlerContext head = ctx.pipeline().firstContext(); head.nextInboundByteBuffer().writeBytes(in.readBytes(in.readableBytes())); head.fireInboundBufferUpdated(); } }
From source file:com.rs3e.network.protocol.codec.js5.UpdateEncoder.java
License:Open Source License
@Override public void encode(ChannelHandlerContext ctx, FileResponse response, ByteBuf buf) throws Exception { ByteBuf container = response.getContainer(); int type = response.getType(); int file = response.getFile(); int compression = container.readUnsignedByte(); int size = ((container.readByte() & 0xff) << 24) + ((container.readByte() & 0xff) << 16) + ((container.readByte() & 0xff) << 8) + (container.readByte() & 0xff); if (!response.isPriority()) { file |= 0x80000000;//www . j a v a 2 s .c o m } buf.writeByte(type); buf.writeInt(file); buf.writeByte(compression); buf.writeInt(size); int bytes = container.readableBytes(); if (bytes > 502) { bytes = 502; } buf.writeBytes(container.readBytes(bytes)); for (;;) { bytes = container.readableBytes(); if (bytes == 0) { break; } else if (bytes > 507) { bytes = 507; } buf.writeByte(type); buf.writeInt(file); buf.writeBytes(container.readBytes(bytes)); } }
From source file:com.sample.netty.socket.utils.MessageDecoder.java
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if (in.readableBytes() < 4) { return;/*from w w w.ja va 2 s .c o m*/ } StringBuilder sb = new StringBuilder(); while (in.isReadable()) { sb.append((char) in.readByte()); } out.add(sb.toString()); }
From source file:com.savageboy74.savagetech.handler.packets.OpenGuiPacket.java
License:Open Source License
@Override public void decodeInto(ChannelHandlerContext context, ByteBuf buffer) { id = buffer.readByte(); }
From source file:com.savageboy74.savagetech.handler.packets.PacketPipeline.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception { ByteBuf payload = msg.payload(); byte discriminator = payload.readByte(); Class<? extends AbstractPacket> apClass = this.packets.get(discriminator); if (apClass == null) { throw new NullPointerException(Strings.Packets.NOT_REGISTERED + apClass.getCanonicalName()); }// w w w. jav a2 s. c o m AbstractPacket abstractPacket = apClass.newInstance(); abstractPacket.decodeInto(ctx, payload.slice()); EntityPlayer player; switch (FMLCommonHandler.instance().getEffectiveSide()) { case CLIENT: player = Minecraft.getMinecraft().thePlayer; abstractPacket.handleClientSide(player); break; case SERVER: INetHandler handler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); player = ((NetHandlerPlayServer) handler).playerEntity; abstractPacket.handleServerSide(player); break; default: } out.add(abstractPacket); }
From source file:com.savageboy74.savagetech.network.message.MessageKeyPressed.java
License:Open Source License
@Override public void fromBytes(ByteBuf buf) { this.keyPressed = buf.readByte(); }
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.seventh_root.ld33.client.network.LD33ClientBoundPacketDecoder.java
License:Apache License
private String readString(ByteBuf buf) { String str = ""; byte b = -1;// www .j a v a 2 s . c o m while (buf.readableBytes() > 0 && b != 0) { b = buf.readByte(); if (b != 0) str += (char) b; } return str; }
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; }//www . j a v a 2s .c o 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(); } } }