List of usage examples for io.netty.buffer ByteBuf readBytes
public abstract ByteBuf readBytes(ByteBuffer dst);
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);//from w w w . j a v a2 s . c o 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
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);//from w w w .j a v a2 s . c o 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:book.netty.n2defualtC4P2.TimeClientHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, "UTF-8"); System.out.println("Now is : " + body + " ; the counter is : " + ++counter); }
From source file:book.netty.n2defualtC4P2.TimeServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, "UTF-8").substring(0, req.length - System.getProperty("line.separator").length()); System.out.println("The time server receive order : " + body + " ; the counter is : " + ++counter); String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new java.util.Date(System.currentTimeMillis()).toString() : "BAD ORDER"; currentTime = currentTime + System.getProperty("line.separator"); ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes()); ctx.writeAndFlush(resp);//from w w w . java2 s. c om }
From source file:books.netty.protocol.netty.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 va2s . com 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.setBody(marshallingDecoder.decode(frame)); } message.setHeader(header); return message; }
From source file:books.netty.protocol.netty.codec.TestCodeC.java
License:Apache License
public NettyMessage decode(ByteBuf in) throws Exception { NettyMessage message = new NettyMessage(); Header header = new Header(); header.setCrcCode(in.readInt());/*from w ww . ja va2 s . c o m*/ header.setLength(in.readInt()); header.setSessionID(in.readLong()); header.setType(in.readByte()); header.setPriority(in.readByte()); int size = in.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 = in.readInt(); keyArray = new byte[keySize]; in.readBytes(keyArray); key = new String(keyArray, "UTF-8"); attch.put(key, marshallingDecoder.decode(in)); } keyArray = null; key = null; header.setAttachment(attch); } if (in.readableBytes() > 4) { message.setBody(marshallingDecoder.decode(in)); } message.setHeader(header); return message; }
From source file:buildcraft.core.lib.network.PacketNBT.java
License:Minecraft Mod Public
@Override public void readData(ByteBuf data) { super.readData(data); int length = data.readUnsignedShort(); byte[] compressed = new byte[length]; data.readBytes(compressed); try {//ww w . j a v a 2 s. com this.nbttagcompound = CompressedStreamTools.func_152457_a(compressed, NBTSizeTracker.field_152451_a); } catch (IOException e) { e.printStackTrace(); } }
From source file:buildcraft.core.lib.network.PacketTileState.java
License:Minecraft Mod Public
@Override public void writeData(ByteBuf data) { super.writeData(data); ByteBuf tmpState = Unpooled.buffer(); tmpState.writeByte(stateList.size()); for (StateWithId stateWithId : stateList) { tmpState.writeByte(stateWithId.stateId); stateWithId.state.writeData(tmpState); }/*from w w w .jav a 2s.c o m*/ data.writeShort((short) tmpState.readableBytes()); data.writeBytes(tmpState.readBytes(tmpState.readableBytes())); }
From source file:buildcraft.core.lib.network.PacketTileState.java
License:Minecraft Mod Public
@Override public void readData(ByteBuf data) { super.readData(data); state = Unpooled.buffer();/*from ww w.j av a 2 s.c om*/ int length = data.readUnsignedShort(); state.writeBytes(data.readBytes(length)); }
From source file:buildcraft.core.network.PacketNBT.java
License:Minecraft Mod Public
@Override public void readData(ByteBuf data) { super.readData(data); short length = data.readShort(); byte[] compressed = new byte[length]; data.readBytes(compressed); try {// ww w . j a v a 2s . com this.nbttagcompound = CompressedStreamTools.func_152457_a(compressed, NBTSizeTracker.field_152451_a); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }