@Override public Object decode(ByteBuf buf, State state) throws IOException { Kryo kryo = null; try { kryo = kryoPool.get(); return kryo.readClassAndObject(new Input(new ByteBufInputStream(buf))); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RedissonKryoCodecException(e); } finally { if (kryo != null) { kryoPool.yield(kryo); } } }
/** * Reads a compressed NBTTagCompound from this buffer */ public NBTTagCompound readNBTTagCompoundFromBuffer() throws IOException { int i = this.readerIndex(); byte b0 = this.readByte(); if (b0 == 0) { return null; } else { this.readerIndex(i); return CompressedStreamTools.read(new ByteBufInputStream(this), new NBTSizeTracker(2097152L)); } }
private HttpResponse processRequest(FullHttpRequest request) throws JsonProcessingException { HttpResponse response; ByteBuf responseContent = Unpooled.buffer(); HttpResponseStatus responseStatus = HttpResponseStatus.OK; try (ByteBufOutputStream os = new ByteBufOutputStream(responseContent); ByteBufInputStream is = new ByteBufInputStream(request.content().retain())){ int result = jsonRpcServer.handleRequest(is, os); responseStatus = HttpResponseStatus.valueOf(DefaultHttpStatusCodeProvider.INSTANCE.getHttpStatusCode(result)); } catch (Exception e) { LOGGER.error("Unexpected error", e); responseContent = buildErrorContent(JSON_RPC_SERVER_ERROR_HIGH_CODE, HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase()); responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR; } finally { response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, responseStatus, responseContent ); } return response; }
@Override protected final Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { /* This is initialized to null because the decode function must return * null if the buffer does not contain a complete frame and cannot be * decoded. */ List<T> ms = null; ByteBuf frame = null; while (null != (frame = (ByteBuf) super.decode(ctx, buffer))) { if (ms == null) ms = new ArrayList<T>(); ByteBufInputStream is = new ByteBufInputStream(frame); TCompactProtocol thriftProtocol = new TCompactProtocol(new TIOStreamTransport(is)); T message = allocateMessage(); message.read(thriftProtocol); ms.add(message); } return ms; }
@SuppressWarnings("resource") @Override public void readFromClientData(Connection connection, ByteBuf clientData) { protocolVersion = clientData.readInt(); //protocol version ByteBuf logindata = Unpooled.wrappedBuffer(ArraySerializer.readByteArray(clientData, connection.getVersion())); // skip chain data logindata.skipBytes(logindata.readIntLE()); // decode skin data try { InputStream inputStream = new ByteBufInputStream(logindata, logindata.readIntLE()); ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } clientPayload = decodeToken(result.toString("UTF-8")); } catch (IOException e) { e.printStackTrace(); } }
/** * Reads a compressed NBTTagCompound from this buffer */ @Nullable public NBTTagCompound readNBTTagCompoundFromBuffer() throws IOException { int i = this.readerIndex(); byte b0 = this.readByte(); if (b0 == 0) { return null; } else { this.readerIndex(i); try { return CompressedStreamTools.read(new ByteBufInputStream(this), new NBTSizeTracker(2097152L)); } catch (IOException ioexception) { throw new EncoderException(ioexception); } } }
@Override public CompoundTag read(ByteBuf buffer) throws Exception { Preconditions.checkArgument(buffer.readableBytes() <= 2097152, "Cannot read NBT (got %s bytes)", buffer.readableBytes()); int readerIndex = buffer.readerIndex(); byte b = buffer.readByte(); if (b == 0) { return null; } else { buffer.readerIndex(readerIndex); ByteBufInputStream bytebufStream = new ByteBufInputStream(buffer); try (DataInputStream dataInputStream = new DataInputStream(bytebufStream)) { return (CompoundTag) NBTIO.readTag((DataInput) dataInputStream); } } }
@Override public void fromBytes(ByteBuf buf) { ByteBufInputStream input = new ByteBufInputStream(buf); List<Frame> frames = new ArrayList<Frame>(); try { this.filename = input.readUTF(); int count = input.readInt(); for (int i = 0; i < count; i++) { Frame frame = new Frame(); frame.fromBytes(input); frames.add(frame); } } catch (IOException e) { e.printStackTrace(); } this.frames = frames; }
public NbtTagCompound readNbtTagCompound() { final int currIndex = this.readerIndex(); final byte firstTag = this.readByte(); if (firstTag == NbtTagType.END.getTypeID()) { return null; } this.readerIndex(currIndex); try { return (NbtTagCompound) NbtInputStream.readTag(new ByteBufInputStream(this), NbtLimiter.getDefault()); } catch (final IOException e) { throw new RuntimeException("Can't decode nbt.", e); } }
/** * Parses the passed channel buffer into a JsonNode * @param buff The buffer to parse * @param nullIfNoContent If true, returns null if no content is available to parse * @return the parsed JsonNode */ public static JsonNode parseToNode(final ByteBuf buff, final boolean nullIfNoContent) { if (buff == null || buff.readableBytes() < 1) { if(nullIfNoContent) return null; throw new IllegalArgumentException("Incoming data was null"); } final InputStream is = new ByteBufInputStream(buff); try { return parseToNode(is); } catch (Exception e) { if(nullIfNoContent) return null; throw new JSONException(e); } finally { try { is.close(); } catch (Exception x) {/* No Op */} } }
/** * Serializes the passed object to an off-heap buffer and returns an InputStream to read it back * @param obj The object to serialize * @return an InputStream to read back the JSON serialized object */ public static InputStream serializeOffHeapLoopBack(final Object obj) { if(obj==null) throw new IllegalArgumentException("The passed object was null"); final ByteBuf cb = byteBufAllocator.buffer(); final OutputStream os = new ByteBufOutputStream(cb); try { serialize(obj, os); os.flush(); os.close(); } catch (Exception ex) { throw new RuntimeException("Failed to write object to buffer", ex); } return new ByteBufInputStream(cb) { @Override public void close() throws IOException { super.close(); try { cb.release(); } catch (Exception x) {/* No Op */} } }; }
private Object decodeFrame0(ByteBuf buffer) throws Exception { final Protocol0.Message message; final InputStream inputStream = new ByteBufInputStream(buffer); try { message = Protocol0.Message.parseFrom(inputStream); } catch (final InvalidProtocolBufferException e) { throw new Exception("Invalid protobuf message", e); } if (message.hasEvent()) { return decodeEvent0(message.getEvent()); } if (message.hasMetric()) { return decodeMetric0(message.getMetric()); } return null; }
private Object decode0(ByteBuf in, List<Object> out) throws IOException, JsonProcessingException { final JsonNode tree; try (final InputStream input = new ByteBufInputStream(in)) { tree = mapper.readTree(input); } final JsonNode typeNode = tree.get("type"); if (typeNode == null) { throw new IllegalArgumentException("Missing field 'type'"); } final String type = typeNode.asText(); if ("event".equals(type)) { return decodeEvent(tree, out); } if ("metric".equals(type)) { return decodeMetric(tree, out); } throw new IllegalArgumentException("Invalid metric type '" + type + "'"); }
private void postBatch( final ChannelHandlerContext ctx, final FullHttpRequest in, final List<Object> out ) { final Batch batch; try (final InputStream inputStream = new ByteBufInputStream(in.content())) { batch = mapper.readValue(inputStream, Batch.class); } catch (final IOException e) { throw new HttpException(HttpResponseStatus.BAD_REQUEST); } out.add(batch); ctx .channel() .writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)) .addListener((ChannelFutureListener) future -> future.channel().close()); }
@Override public Decoder<Object> getValueDecoder() { return new Decoder<Object>() { @Override public Object decode(ByteBuf buf, State state) throws IOException { Kryo kryo = null; try { kryo = kryoPool.get(); return kryo.readClassAndObject(new Input(new ByteBufInputStream(buf))); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RedissonKryoCodecException(e); } finally { if (kryo != null) { kryoPool.yield(kryo); } } } }; }
/** * Decodes the buffered image from the encoded favicon string. * * @param encoded the encoded string * @return the buffered image */ private static BufferedImage decode(String encoded) throws IOException { checkArgument(encoded.startsWith(FAVICON_PREFIX), "unknown favicon format"); ByteBuf base64 = Unpooled.copiedBuffer(encoded.substring(FAVICON_PREFIX.length()), StandardCharsets.UTF_8); try { ByteBuf buf = Base64.decode(base64); try { BufferedImage result = ImageIO.read(new ByteBufInputStream(buf)); checkState(result.getWidth() == 64, "favicon must be 64 pixels wide"); checkState(result.getHeight() == 64, "favicon must be 64 pixels high"); return result; } finally { buf.release(); } } finally { base64.release(); } }
@Nullable @Override public DataView readLimitedDataView(int maximumDepth, int maxBytes) { final int index = this.buf.readerIndex(); if (this.buf.readByte() == 0) { return null; } this.buf.readerIndex(index); try { try (NbtDataContainerInputStream input = new NbtDataContainerInputStream( new LimitInputStream(new ByteBufInputStream(this.buf), maxBytes), false, maximumDepth)) { return input.read(); } } catch (IOException e) { throw new CodecException(e); } }
@Override protected void channelRead0(ChannelHandlerContext context, Object message) throws Exception { final Channel channel = context.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(channel, (FullHttpResponse) message); channel.pipeline().addBefore(HANDLER_NAME, "websocket-frame-aggregator", new WebSocketFrameAggregator(64 * 1024)); subscriber.onStart(); return; } if (message instanceof FullHttpResponse) { final FullHttpResponse response = (FullHttpResponse) message; throw new IllegalStateException( "Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } final WebSocketFrame frame = (WebSocketFrame) message; if (frame instanceof PingWebSocketFrame) { context.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame)frame).retain().content())); } else if (frame instanceof BinaryWebSocketFrame) { final ByteBufInputStream input = new ByteBufInputStream(((BinaryWebSocketFrame)message).content()); final Envelope envelope = Envelope.ADAPTER.decode(input); subscriber.onNext(envelope); } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { DatagramPacket packet = (DatagramPacket) msg; try { InputStream stream = new ByteBufInputStream(packet.content()); Object data = new CompactObjectInputStream(stream, ClassResolvers.cacheDisabled(null)).readObject(); MDC.put("id", LogSourceId.getInstance().getId()); logger.callAppenders(((LoggingEventWrapper) data).event); } catch (Throwable e){ System.out.println(e); } ReferenceCountUtil.release(msg); }
private ByteBufOrStream getCompressedBody() { if (decompressor == Codec.Identity.NONE) { throw Status.INTERNAL.withDescription( DEBUG_STRING + ": Can't decode compressed frame as compression not configured.") .asRuntimeException(); } try { // Enforce the maxMessageSizeBytes limit on the returned stream. InputStream unlimitedStream = decompressor.decompress(new ByteBufInputStream(nextFrame, true)); return new ByteBufOrStream( new SizeEnforcingInputStream(unlimitedStream, maxMessageSizeBytes, DEBUG_STRING)); } catch (IOException e) { throw new RuntimeException(e); } }
@SuppressWarnings("resource") @Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null; } int readerIndex = frame.readerIndex(); byte version = frame.readByte(); frame.readerIndex(readerIndex); if (version == KRYO_STREAM_VERSION) { return decodeAsKryo(frame); } else { return new CompactObjectInputStream( new ByteBufInputStream(frame), classResolver).readObject(); } }
private byte[] toByteArray(ByteBuf buf) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ByteBufInputStream in = new ByteBufInputStream(buf)) { if (in.available() > 1) { in.mark(Short.BYTES); int magicbyte = in.readUnsignedShort(); in.reset(); InputStream wrap; if (magicbyte == 0x1f8b) { wrap = new GZIPInputStream(in); } else { wrap = in; } byte[] buffer = new byte[BUFFER_SIZE]; int n; while (-1 != (n = wrap.read(buffer))) { out.write(buffer, 0, n); } } } return out.toByteArray(); }
private RequestContext createRequestContext(FullHttpRequest request) throws URISyntaxException { RequestContext context = new RequestContext(); context.RequestBody = new ByteBufInputStream(request.content()); if (request.headers() != null) { context.RequestHeaders = new HashMap<String, String>(); for (Map.Entry<String, String> headerEntry : request.headers()) { context.RequestHeaders.put(headerEntry.getKey(), headerEntry.getValue()); } } context.RequestMethod = request.getMethod().name(); URI uri = new URI(request.getUri()); context.RequestPath = uri.getPath(); context.RequestProtocol = request.getProtocolVersion().toString(); context.RequestQueryString = uri.getQuery(); context.RequestScheme = uri.getScheme(); return context; }
@Test public void testEncode() throws Exception { final EmbeddedChannel channel = new EmbeddedChannel(new GelfCompressionGzipEncoder()); final String message = "Test string"; assertTrue(channel.writeOutbound(Unpooled.wrappedBuffer(message.getBytes(StandardCharsets.UTF_8)))); assertTrue(channel.finish()); final ByteBufInputStream byteBufInputStream = new ByteBufInputStream((ByteBuf) channel.readOutbound()); final GZIPInputStream gzipInputStream = new GZIPInputStream(byteBufInputStream); byte[] bytes = new byte[message.length()]; assertEquals(message.length(), gzipInputStream.read(bytes, 0, message.length())); assertEquals(message, new String(bytes, StandardCharsets.UTF_8)); }
@Test public void testEncode() throws Exception { final EmbeddedChannel channel = new EmbeddedChannel(new GelfCompressionZlibEncoder()); final String message = "Test string"; assertTrue(channel.writeOutbound(Unpooled.wrappedBuffer(message.getBytes(StandardCharsets.UTF_8)))); assertTrue(channel.finish()); final ByteBufInputStream byteBufInputStream = new ByteBufInputStream((ByteBuf) channel.readOutbound()); final InflaterInputStream zlibInputStream = new InflaterInputStream(byteBufInputStream); byte[] bytes = new byte[message.length()]; assertEquals(message.length(), zlibInputStream.read(bytes, 0, message.length())); assertEquals(message, new String(bytes, StandardCharsets.UTF_8)); }
@Override public T decode(HttpHeaders headers, ByteBuf content) throws EtcdException, IOException { try { final DataInput di = new ByteBufInputStream(content); final T value = MAPPER.readValue(di, this.type); if (headers != null && EtcdHeaderAwareResponse.class.isAssignableFrom(this.type)) { ((EtcdHeaderAwareResponse) value).loadHeaders(headers); } return value; } catch (NoSuchMethodError e) { LOGGER.warn("Jackson failed to deserialize JSON, please check you have Jackson > 2.8.0 in your classpath", e); throw new RuntimeException(e); } }
/** * Extract the information part of Netty Request and fill OData Request * @param odRequest * @param httpRequest * @param split * @param contextPath * @return * @throws ODataLibraryException */ private ODataRequest fillODataRequest(final ODataRequest odRequest, final HttpRequest httpRequest, final int split, final String contextPath) throws ODataLibraryException { final int requestHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillODataRequest"); try { ByteBuf byteBuf = ((HttpContent)httpRequest).content(); ByteBufInputStream inputStream = new ByteBufInputStream(byteBuf); odRequest.setBody(inputStream); odRequest.setProtocol(httpRequest.protocolVersion().text()); odRequest.setMethod(extractMethod(httpRequest)); int innerHandle = debugger.startRuntimeMeasurement("ODataNettyHandlerImpl", "copyHeaders"); copyHeaders(odRequest, httpRequest); debugger.stopRuntimeMeasurement(innerHandle); innerHandle = debugger.startRuntimeMeasurement("ODataNettyHandlerImpl", "fillUriInformation"); fillUriInformationFromHttpRequest(odRequest, httpRequest, split, contextPath); debugger.stopRuntimeMeasurement(innerHandle); return odRequest; } finally { debugger.stopRuntimeMeasurement(requestHandle); } }
public static NBTTagCompoundWrapper readTag(ByteBuf from, ProtocolVersion version) { try { if (isUsingShortLengthNBT(version)) { final short length = from.readShort(); if (length < 0) { return NBTTagCompoundWrapper.NULL; } try (InputStream inputstream = new GZIPInputStream(new ByteBufInputStream(from.readSlice(length)))) { return NBTTagCompoundSerializer.readTag(new DataInputStream(inputstream)); } } else if (isUsingDirectNBT(version)) { return NBTTagCompoundSerializer.readTag(new ByteBufInputStream(from)); } else { throw new IllegalArgumentException(MessageFormat.format("Dont know how to read nbt of version {0}", version)); } } catch (IOException e) { throw new DecoderException(e); } }
@Override protected void readData(ByteBufInputStream buffer) throws IOException { tileLocationX = buffer.readInt(); tileLocationY = buffer.readInt(); tileLocationZ = buffer.readInt(); int numTexts = buffer.readInt(); text = new String[numTexts]; for (int i = 0; i < numTexts; i++) { int stringLength = buffer.readInt(); char[] stringChars = new char[stringLength]; for (int j = 0; j < stringLength; j++) { stringChars[j] = buffer.readChar(); } text[i] = new String(stringChars); } }