List of usage examples for io.netty.buffer ByteBuf retain
@Override public abstract ByteBuf retain();
From source file:io.liveoak.common.codec.ResourceCodecManager.java
License:Open Source License
public ResourceState decode(MediaType mediaType, ByteBuf buf) throws Exception { if (buf == null) { buf = Unpooled.EMPTY_BUFFER;//from w ww .j a v a 2 s .c o m } if (MediaType.OCTET_STREAM.equals(mediaType)) { return new DefaultBinaryResourceState(buf.retain()); } ResourceCodec codec = getResourceCodec(mediaType); if (codec == null || !codec.hasDecoder()) { throw new UnsupportedMediaTypeException(MediaTypeMatcher.singleton(mediaType)); } return codec.decode(buf); }
From source file:io.liveoak.container.protocols.http.HttpRequestBodyHandler.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception { if (msg instanceof LastHttpContent) { ByteBuf content = ((HttpContent) msg).content(); if (fileUpload != null) { // if it's a PUT or a POST fileUpload.addContent(content.retain(), true); // TODO - not sure this is ever necessary - defensive coding if (request.state() != null) { ((LazyResourceState) request.state()).fileUpload(fileUpload); } else { fileUpload.delete();/*from w w w . j a v a2 s . c o m*/ } if (completion != null) { // complete the request as body is now fully available completion.run(); completion = null; } else { // mark that body is fully available complete = true; } } else if (content.readableBytes() > 0) { log.debug("on LastHttpContent: " + content.readableBytes() + " bytes discarded!"); } } else if (msg instanceof HttpContent) { ByteBuf content = ((HttpContent) msg).content(); if (fileUpload != null) { fileUpload.addContent(content.retain(), false); } else if (content.readableBytes() > 0) { log.debug("on HttpContent: " + content.readableBytes() + " bytes discarded!"); } // only continue reading body if resource has declared interest if (completion != null) { ctx.pipeline().firstContext().read(); } } else if (msg instanceof ResourceRequest) { // beginning of a new request complete = false; if (fileUpload != null) { fileUpload.delete(); } fileUpload = null; ResourceRequest request = (ResourceRequest) msg; if (request.requestType() != RequestType.CREATE && request.requestType() != RequestType.UPDATE) { // not POST or PUT out.add(request); ctx.pipeline().firstContext().read(); return; } // use original HttpRequest to get to Content-Length, and Content-Type HttpRequest original = (HttpRequest) request.requestContext().requestAttributes() .getAttribute("HTTP_REQUEST"); // use last component of target URI as posted resource filename List<ResourcePath.Segment> segments = request.resourcePath().segments(); String filename = segments.size() < 1 ? "unknown" : segments.get(segments.size() - 1).name(); factory.createAttribute(original, "filename", filename); String contentLength = original.headers().get(CONTENT_LENGTH); long clen = 0; if (contentLength != null) { factory.createAttribute(original, CONTENT_LENGTH, contentLength); try { clen = Long.parseLong(contentLength); } catch (Exception ignored) { log.debug("Invalid Content-Length received: " + contentLength); } } String contentType = original.headers().get(CONTENT_TYPE); if (contentType == null) { contentType = "application/octet-stream"; } factory.createAttribute(original, CONTENT_TYPE, contentType); fileUpload = factory.createFileUpload(original, request.resourcePath().toString(), filename, contentType, "binary", Charset.forName("utf-8"), clen); // save request for later, so we can update it with fileUpload once body is fully available this.request = request; out.add(request); } else if (msg instanceof Invocation) { Invocation invocation = (Invocation) msg; if (complete) { // body is fully available we should continue processing the request invocation.run(); } else { completion = invocation; } } else { // in any other case simply pass the message on as if we're not here out.add(ReferenceCountUtil.retain(msg)); } }
From source file:io.liveoak.container.protocols.websocket.WebSocketStompFrameEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { out.add(new TextWebSocketFrame(msg.retain())); }
From source file:io.nodyn.stream.StreamWrap.java
License:Apache License
public void write(ByteBuf buf) throws IOException { this.channelFuture.channel().writeAndFlush(buf.retain()); }
From source file:io.reactivex.netty.protocol.http.client.RequestProcessor.java
License:Apache License
public Observable<Void> handlePost(final HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) { return request.getContent().flatMap(new Func1<ByteBuf, Observable<Void>>() { @Override/*from w w w . j av a 2 s. c om*/ public Observable<Void> call(ByteBuf byteBuf) { return response.writeAndFlush(byteBuf.retain()); } }); }
From source file:io.reactivex.netty.protocol.http.server.Http10Test.java
License:Apache License
@Test public void testHttp1_0RequestWithContent() throws Exception { HttpClientRequest<ByteBuf> request = HttpClientRequest.create(HttpVersion.HTTP_1_0, HttpMethod.GET, "/"); final ByteBuf response = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", mockServerPort) .enableWireLogging(LogLevel.ERROR).build().submit(request) .flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() { @Override/*from w ww .jav a2 s. co m*/ public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> response) { return response.getContent(); } }).map(new Func1<ByteBuf, ByteBuf>() { @Override public ByteBuf call(ByteBuf byteBuf) { return byteBuf.retain(); } }).toBlocking().toFuture().get(1, TimeUnit.MINUTES); Assert.assertEquals("Unexpected Content.", WELCOME_SERVER_MSG, response.toString(Charset.defaultCharset())); response.release(); }
From source file:io.vertx.core.http.impl.FileStreamChannel.java
License:Open Source License
@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { ByteBuf chunk; while (!stream.isNotWritable() && (chunk = (ByteBuf) in.current()) != null) { bytesWritten += chunk.readableBytes(); stream.writeData(chunk.retain(), false); stream.handlerContext.flush();/* w w w.j a v a 2s .c o m*/ in.remove(); } }
From source file:io.vertx.core.http.impl.Http2ConnectionBase.java
License:Open Source License
/** * Return a buffer from HTTP/2 codec that Vert.x can use: * * - if it's a direct buffer (coming likely from OpenSSL) : we get a heap buffer version * - if it's a composite buffer we do the same * - otherwise we increase the ref count *//* w ww . j a v a 2 s . com*/ static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) { if (buf == Unpooled.EMPTY_BUFFER) { return buf; } if (buf.isDirect() || buf instanceof CompositeByteBuf) { if (buf.isReadable()) { ByteBuf buffer = allocator.heapBuffer(buf.readableBytes()); buffer.writeBytes(buf); return buffer; } else { return Unpooled.EMPTY_BUFFER; } } return buf.retain(); }
From source file:jlibs.wamp4j.netty.NettyOutputStream.java
License:Apache License
@Override public WAMPOutputStream duplicate() { ByteBuf duplicate = buffer.duplicate(); duplicate.retain(); return new NettyOutputStream(duplicate); }
From source file:me.ferrybig.p2pnetwork.codec.PacketPreProcessor.java
@Override protected void channelRead0(ChannelHandlerContext ctx, Packet msg) throws Exception { try {//from ww w. j av a 2s . c o m Address from; Function<Packet, ChannelFuture> reply; if (msg instanceof RelayPacket) { RelayPacket relayPacket = (RelayPacket) msg; int packetId = relayPacket.getData().readInt(); from = relayPacket.getFrom(); msg = PacketMap.readPacket(packetId, relayPacket.getData()); relayPacket.release(); // This is a safe release reply = p -> { ByteBuf wrapped = ctx.alloc().buffer(); try { wrapped.writeInt(PacketMap.getPacketId(p)); p.write(wrapped); return ctx.writeAndFlush( new RelayPacket(wrapped.retain(), relayPacket.getAddress(), from, 255)); } finally { p.release(); wrapped.release(); } }; } else { from = connection; reply = ctx::writeAndFlush; } ctx.fireChannelRead(new ProcessedPacket(from, ctx.channel(), (Packet) msg.retain(), reply)); } finally { msg.release(); } }