@Override public void fromBytes(ByteBuf buf) { int elementNameLength = buf.readInt(); this.elementName = new String(buf.readBytes(elementNameLength).array()); int elementTextLength = buf.readInt(); this.elementText = new String(buf.readBytes(elementTextLength).array()); }
@Override public void fromBytes(ByteBuf buffer) { int ownerLength = buffer.readInt(); this.owner = new String(buffer.readBytes(ownerLength).array()); int notifierLength = buffer.readInt(); this.notifier = new String(buffer.readBytes(notifierLength).array()); }
@Override public void decodeFrom(ChannelHandlerContext ctx, ByteBuf buffer) throws IOException { data = new byte[buffer.readInt()]; signature = new byte[buffer.readInt()]; buffer.readBytes(data); buffer.readBytes(signature); }
/** * Deserialize an object from a given byte buffer. * * @param b The bytebuf to deserialize. * @return The deserialized object. */ @Override public Object deserialize(ByteBuf b, CorfuRuntime rt) { int classNameLength = b.readShort(); byte[] classNameBytes = new byte[classNameLength]; b.readBytes(classNameBytes, 0, classNameLength); String className = new String(classNameBytes); if (className.equals("null")) { return null; } else if (className.equals("CorfuObject")) { int SMRClassNameLength = b.readShort(); byte[] SMRClassNameBytes = new byte[SMRClassNameLength]; b.readBytes(SMRClassNameBytes, 0, SMRClassNameLength); String SMRClassName = new String(SMRClassNameBytes); try { return rt.getObjectsView() .build() .setStreamID(new UUID(b.readLong(), b.readLong())) .setType(Class.forName(SMRClassName)) .open(); } catch (ClassNotFoundException cnfe) { log.error("Exception during deserialization!", cnfe); throw new RuntimeException(cnfe); } } else { try (ByteBufInputStream bbis = new ByteBufInputStream(b)) { try (InputStreamReader r = new InputStreamReader(bbis)) { return gson.fromJson(r, Class.forName(className)); } } catch (IOException | ClassNotFoundException ie) { log.error("Exception during deserialization!", ie); throw new RuntimeException(ie); } } }
protected void internalReceive(HttpResponseStatus status, HttpHeaders headers, ByteBuf content) { try { if (status.code() > 399) { byte[] b = new byte[content.readableBytes()]; content.readBytes(b); onErrorResponse(status, headers, new String(b, CharsetUtil.UTF_8)); return; } if (type == ByteBuf.class) { _doReceive(status, headers, type.cast(content)); } else if (type == String.class || type == CharSequence.class) { byte[] b = new byte[content.readableBytes()]; content.readBytes(b); _doReceive(status, headers, type.cast(new String(b, CharsetUtil.UTF_8))); } else if (type == byte[].class) { byte[] b = new byte[content.readableBytes()]; content.readBytes(b); _doReceive(status, headers, type.cast(b)); } else { ObjectMapper mapper = new ObjectMapper(); byte[] b = new byte[content.readableBytes()]; content.readBytes(b); try { Object o = mapper.readValue(b, type); _doReceive(status, headers, type.cast(o)); } catch (Exception ex) { Exceptions.chuck(ex); } } } finally { latch.countDown(); } }
public EncryptionKeyResponseMessage decode(ByteBuf buffer) throws IOException { byte[] sharedSecret = new byte[ByteBufUtils.readVarInt(buffer)]; buffer.readBytes(sharedSecret); byte[] verifyToken = new byte[ByteBufUtils.readVarInt(buffer)]; buffer.readBytes(verifyToken); return new EncryptionKeyResponseMessage(sharedSecret, verifyToken); }
@Override public SignatureCodec read(ByteBuf buf) { byte[] me = new byte[Number160.BYTE_ARRAY_SIZE]; buf.readBytes(me); number1 = new Number160(me); buf.readBytes(me); number2 = new Number160(me); return this; }
@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()); }
@Override public void readFrom(ByteBuf fromBuffer) { short length = fromBuffer.readShort(); byte[] array = new byte[length]; fromBuffer.readBytes(array); this.command = new String(array, PROTOCOL_ENCODING); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // System.out.println("decode:" + in.readableBytes()); if (in.readableBytes() > 4) { in.markReaderIndex(); int needBytes = in.readInt(); // System.out.println("needBytes:" + needBytes); if (in.readableBytes() >= needBytes) { byte[] content = new byte[in.readableBytes()]; in.readBytes(content); // byte[] data= ZLipHelper.decompress(content); // System.out.println("data:" + new String(data)); Amf3Input amf3Input = new Amf3Input(SerializationContext.getSerializationContext()); // amf3Input = new Amf3Input(SerializationContext.getSerializationContext()); InputStream bais = new ByteArrayInputStream(content); amf3Input.setInputStream(bais); try { Object decoded = amf3Input.readObject(); if (decoded != null) { out.add(decoded); // System.out.println("decoded:" + decoded); } } catch (Exception e) { } // amf3Input.close(); } else { in.resetReaderIndex(); } } }
/** * This test makes sure that even when the decoder is confronted with various chunk sizes in the * middle of decoding, it can recover and decode all the time eventually. */ @Test public void shouldHandleNonUniformNetworkBatches() { ByteBuf incoming = Unpooled.copiedBuffer(SET_REQUEST_WITH_CONTENT); while (incoming.isReadable()) { channel.writeInbound(incoming.readBytes(5)); } BinaryMemcacheRequest request = (BinaryMemcacheRequest) channel.readInbound(); assertThat(request, notNullValue()); assertThat(request.getHeader(), notNullValue()); assertThat(request.getKey(), notNullValue()); assertThat(request.getExtras(), nullValue()); request.release(); MemcacheContent content1 = (MemcacheContent) channel.readInbound(); MemcacheContent content2 = (MemcacheContent) channel.readInbound(); assertThat(content1, instanceOf(MemcacheContent.class)); assertThat(content2, instanceOf(LastMemcacheContent.class)); assertThat(content1.content().readableBytes(), is(3)); assertThat(content2.content().readableBytes(), is(5)); content1.release(); content2.release(); }
@Override public byte[] allBytes() { ByteBuf dup = buf.duplicate().resetReaderIndex(); byte[] res = new byte[dup.readableBytes()]; dup.readBytes(res); return res; }
public NettyMessage decode(ByteBuf in) throws Exception { NettyMessage message = new NettyMessage(); Header header = new Header(); header.setCrcCode(in.readInt()); 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; }
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception { Integer streamId = msg.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text()); if (streamId == null) { logger.error("HttpResponseHandler unexpected message received: " + msg); return; } onResponseReceived(ctx, streamIdUrlMap.get(streamId)); Map.Entry<ChannelFuture, ChannelPromise> entry = streamIdPromiseMap.get(streamId); if (entry == null) { logger.error("Message received for unknown stream id " + streamId); } else { // Do stuff with the message (for now just print it) ByteBuf content = msg.content(); ContentType contentType = ContentType.parse(msg.headers().get(HttpHeaderNames.CONTENT_TYPE)); if (content.isReadable()) { int contentLength = content.readableBytes(); byte[] arr = new byte[contentLength]; content.readBytes(arr); handleContent(arr, ctx, contentType); } entry.getValue().setSuccess(); } }
/** * @param headerLength * @param middleLength * @param endLength * @param buf * @return the new ErrorPacket from buffer * @throws OpenR66ProtocolPacketException */ public static ErrorPacket createFromBuffer( int headerLength, int middleLength, int endLength, ByteBuf buf) throws OpenR66ProtocolPacketException { final byte[] bheader = new byte[headerLength - 1]; final byte[] bmiddle = new byte[middleLength]; if (headerLength - 1 > 0) { buf.readBytes(bheader); } if (middleLength > 0) { buf.readBytes(bmiddle); } if (endLength != 4) { throw new OpenR66ProtocolPacketException("Packet not correct"); } return new ErrorPacket(new String(bheader), new String(bmiddle), buf.readInt()); }
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { // TODO Auto-generated method stub ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null; } Message message = new Message(); Header header = new Header(); header.setSrcCode(in.readInt()); 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, nettyMarshallingDecoder.decode(ctx, in)); } keyArray = null; key = null; header.setAttachment(attch); } return message; }
private void setInput(ByteBuf decompressed) { byte[] in = new byte[decompressed.readableBytes()]; decompressed.readBytes(in); z.next_in = in; z.next_in_index = 0; z.avail_in = in.length; }
@Override public RequestMessage deserializeRequest(final ByteBuf msg) throws SerializationException { try { final Kryo kryo = kryoThreadLocal.get(); final byte[] payload = new byte[msg.readableBytes()]; msg.readBytes(payload); try (final Input input = new Input(payload)) { // by the time the message gets here, the mime length/type have been already read, so this // part just // needs to process the payload. final UUID id = kryo.readObject(input, UUID.class); final String processor = input.readString(); final String op = input.readString(); final RequestMessage.Builder builder = RequestMessage.build(op).overrideRequestId(id).processor(processor); final Map<String, Object> args = kryo.readObject(input, HashMap.class); args.forEach(builder::addArg); return builder.create(); } } catch (Exception ex) { logger.warn( "Request [{}] could not be deserialized by {}.", msg, GryoMessageSerializerV1d0.class.getName()); throw new SerializationException(ex); } }
@Override public void split(ChannelHandlerContext ctx, ByteBuf input, List<Object> list) throws Exception { input.markReaderIndex(); final byte[] array = new byte[3]; for (int i = 0; i < array.length; ++i) { if (!input.isReadable()) { input.resetReaderIndex(); return; } array[i] = input.readByte(); if (array[i] >= 0) { final PacketDataSerializer packetDataSerializer = new PacketDataSerializer( Unpooled.wrappedBuffer(array), ProtocolVersion.MINECRAFT_1_7_10); try { final int length = packetDataSerializer.readVarInt(); if (input.readableBytes() < length) { input.resetReaderIndex(); return; } list.add(input.readBytes(length)); return; } finally { packetDataSerializer.release(); } } } throw new CorruptedFrameException("length wider than 21-bit"); }
@Override public ResponseMessage deserializeResponse(final ByteBuf msg) throws SerializationException { try { final Kryo kryo = kryoThreadLocal.get(); final byte[] payload = new byte[msg.readableBytes()]; msg.readBytes(payload); try (final Input input = new Input(payload)) { final UUID requestId = kryo.readObjectOrNull(input, UUID.class); final int status = input.readShort(); final String statusMsg = input.readString(); final Map<String, Object> statusAttributes = (Map<String, Object>) kryo.readClassAndObject(input); final Object result = kryo.readClassAndObject(input); final Map<String, Object> metaAttributes = (Map<String, Object>) kryo.readClassAndObject(input); return ResponseMessage.build(requestId) .code(ResponseStatusCode.getFromValue(status)) .statusMessage(statusMsg.intern()) .statusAttributes(statusAttributes) .result(result) .responseMetaData(metaAttributes) .create(); } } catch (Exception ex) { logger.warn( "Response [{}] could not be deserialized by {}.", msg, GryoMessageSerializerV1d0.class.getName()); throw new SerializationException(ex); } }
private boolean handleCompressedFrame(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable(FRAME_COMPRESS_HEADER_LENGTH)) { return false; } int compressedPayloadLength = in.readInt(); if (!in.isReadable(compressedPayloadLength)) { return false; } // decompress payload Inflater inflater = new Inflater(); if (in.hasArray()) { inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), compressedPayloadLength); in.skipBytes(compressedPayloadLength); } else { byte[] array = new byte[compressedPayloadLength]; in.readBytes(array); inflater.setInput(array); } while (!inflater.finished()) { ByteBuf decompressed = ctx.alloc().heapBuffer(1024, 1024); byte[] outArray = decompressed.array(); int count = inflater.inflate(outArray, decompressed.arrayOffset(), decompressed.writableBytes()); decompressed.writerIndex(count); // put data in the pipeline out.add(decompressed); } return true; }
@Override public void appendMessages(Tpp tpp, Collection<MessageBatchWithRawData> batches) throws Exception { ByteBuf bodyBuf = Unpooled.buffer(); KafkaMessageBrokerSender sender = getSender(tpp.getTopic()); try { for (MessageBatchWithRawData batch : batches) { List<PartialDecodedMessage> pdmsgs = batch.getMessages(); for (PartialDecodedMessage pdmsg : pdmsgs) { m_messageCodec.encodePartial(pdmsg, bodyBuf); byte[] bytes = new byte[bodyBuf.readableBytes()]; bodyBuf.readBytes(bytes); bodyBuf.clear(); ByteBuf propertiesBuf = pdmsg.getDurableProperties(); HermesPrimitiveCodec codec = new HermesPrimitiveCodec(propertiesBuf); Map<String, String> propertiesMap = codec.readStringStringMap(); sender.send(tpp.getTopic(), propertiesMap.get("pK"), bytes); BrokerStatusMonitor.INSTANCE.kafkaSend(tpp.getTopic()); } } } finally { bodyBuf.release(); } }
@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); }
@Override public Object decode(ByteBuf buf, State state) throws IOException { byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); bytes = Snappy.uncompress(bytes); ByteBuf bf = Unpooled.wrappedBuffer(bytes); return innerCodec.getValueDecoder().decode(bf, state); }
@Override public void channelRead0(final ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; byte[] bytes = in.array(); String sInfo = Hex.encodeHexStr(bytes); logger.info(String.format("primary received:%s", sInfo)); // 接收到包的长度 int length = in.readableBytes(); // 终端设备上报到服务器位始 $$ String head = in.readBytes(2).toString(Charset.defaultCharset()); if ("$$".equals(head)) { // 从包的字段得到包的长度 short l = in.readShort(); System.out.println(l); if (length == l) { // 得到产品id 序列号 final String serialNumber = byteToArray(in.readBytes(7).array()); // 得到协议号 final String agreement = byteToArray(in.readBytes(2).array()); // 得到数据 final String content = operation(serialNumber, head, length, l, agreement, in, ctx); // 得到校验位 checksum String checksum = byteToArray(in.readBytes(2).array()); // 得到结束符\r\n final String end = byteToArray(in.readBytes(2).array()); executorService.execute( new Runnable() { public void run() { try { serviceHandler(ctx, serialNumber, agreement, content, end); } catch (Exception e) { e.printStackTrace(); } } }); } } }
public static byte[] serialiseAddress(Address addr) { ByteBuf buf = Unpooled.buffer(); AddressSerializer.INSTANCE.toBinary(addr, buf); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); buf.release(); return bytes; }
public static byte[] serialiseKey(Key key) { ByteBuf buf = Unpooled.buffer(); serialiseKey(key, buf); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); buf.release(); return bytes; }
public void reading(ByteBuf datas) { datas.readBytes(temp, datas.readableBytes()); if (this.temp.writableBytes() != 0) { end = false; } else { end = true; } }
@Override protected void handleData(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) { byte[] data = new byte[byteBuf.readableBytes() - 5]; byteBuf.skipBytes(5); byteBuf.readBytes(data); String content = new String(data); System.out.println(name + " get content: " + content); }
@Override public void readData(ByteBuf data) { super.readData(data); state = Unpooled.buffer(); int length = data.readInt(); state.writeBytes(data.readBytes(length)); }