List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:io.reactivex.netty.protocol.http.sse.ServerSentEventDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (null == sseEncodingCharset) { throw new IllegalArgumentException("Can not read SSE data as UTF-8 charset is not available."); }/*from w ww . j ava 2 s . c o m*/ while (in.isReadable()) { final int readerIndexAtStart = in.readerIndex(); switch (state) { case SkipColonAndWhiteSpaces: if (skipColonAndWhiteSpaces(in)) { state = State.ReadFieldValue; } break; case SkipLineDelimitersAndSpaces: if (skipLineDelimiters(in)) { state = State.ReadFieldName; } break; case DiscardTillEOL: if (skipTillEOL(in)) { state = State.SkipLineDelimitersAndSpaces; } break; case ReadFieldName: final int indexOfColon = scanAndFindColon(in); if (-1 == indexOfColon) { // No colon found // Accumulate data into the field name buffer. if (null == incompleteData) { incompleteData = ctx.alloc().buffer(); } // accumulate into incomplete data buffer to be used when the full data arrives. incompleteData.writeBytes(in); } else { int fieldNameLengthInTheCurrentBuffer = indexOfColon - readerIndexAtStart; ByteBuf fieldNameBuffer; if (null != incompleteData) { // Read the remaining data into the temporary buffer in.readBytes(incompleteData, fieldNameLengthInTheCurrentBuffer); fieldNameBuffer = incompleteData; incompleteData = null; } else { // Consume the data from the input buffer. fieldNameBuffer = ctx.alloc().buffer(fieldNameLengthInTheCurrentBuffer, fieldNameLengthInTheCurrentBuffer); in.readBytes(fieldNameBuffer, fieldNameLengthInTheCurrentBuffer); } state = State.SkipColonAndWhiteSpaces; // We have read the field name, next we should skip colon & WS. try { currentFieldType = readCurrentFieldTypeFromBuffer(fieldNameBuffer); } finally { if (null == currentFieldType) { state = State.DiscardTillEOL; // Ignore this event completely. } fieldNameBuffer.release(); } } break; case ReadFieldValue: final int endOfLineStartIndex = scanAndFindEndOfLine(in); if (-1 == endOfLineStartIndex) { // End of line not found, accumulate data into a temporary buffer. if (null == incompleteData) { incompleteData = ctx.alloc().buffer(in.readableBytes()); } // accumulate into incomplete data buffer to be used when the full data arrives. incompleteData.writeBytes(in); } else { // Read the data till end of line into the value buffer. final int bytesAvailableInThisIteration = endOfLineStartIndex - readerIndexAtStart; if (null == incompleteData) { incompleteData = ctx.alloc().buffer(bytesAvailableInThisIteration, bytesAvailableInThisIteration); } incompleteData.writeBytes(in, bytesAvailableInThisIteration); switch (currentFieldType) { case Data: if (incompleteData.isReadable()) { out.add(ServerSentEvent.withEventIdAndType(lastEventId, lastEventType, incompleteData)); } else { incompleteData.release(); } break; case Id: if (incompleteData.isReadable()) { lastEventId = incompleteData; } else { incompleteData.release(); lastEventId = null; } break; case EventType: if (incompleteData.isReadable()) { lastEventType = incompleteData; } else { incompleteData.release(); lastEventType = null; } break; } /** * Since all data is read, reset the incomplete data to null. Release of this buffer happens in * the following ways * 1) If this was a data buffer, it is released when ServerSentEvent is released. * 2) If this was an eventId buffer, it is released when next Id arrives or when the connection * is closed. * 3) If this was an eventType buffer, it is released when next type arrives or when the connection * is closed. */ incompleteData = null; state = State.SkipLineDelimitersAndSpaces; // Skip line delimiters after reading a field value completely. } break; } } }
From source file:io.reactivex.netty.protocol.http.UnicastContentSubjectTest.java
License:Apache License
@Test public void testByteBufRelease() throws Exception { UnicastContentSubject<ByteBuf> subject = UnicastContentSubject.createWithoutNoSubscriptionTimeout(); ByteBuf buffer = Unpooled.buffer();/* w w w . java 2 s . co m*/ Assert.assertEquals("Created byte buffer not retained.", 1, buffer.refCnt()); subject.onNext(buffer); subject.onCompleted(); final AtomicInteger byteBufRefCnt = new AtomicInteger(-1); ByteBuf last = subject.doOnNext(new Action1<ByteBuf>() { @Override public void call(ByteBuf byteBuf) { byteBufRefCnt.set(byteBuf.refCnt()); byteBuf.release();// Simulate consumption as ByteBuf refcount is 1 when created. } }).toBlocking().last(); Assert.assertEquals("Unexpected ByteBuf ref count when received.", 2, byteBufRefCnt.get()); Assert.assertSame("Unexpected byte buffer received.", buffer, last); Assert.assertEquals("Byte buffer not released.", 0, last.refCnt()); }
From source file:io.reactivex.netty.RxEventPipelineConfigurator.java
License:Apache License
@Override public void configureNewPipeline(ChannelPipeline pipeline) { pipeline.addLast(new ChannelDuplexHandler() { @Override//from w ww .j a v a 2 s . com public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { boolean handled = false; if (ByteBuf.class.isAssignableFrom(msg.getClass())) { ByteBuf byteBuf = (ByteBuf) msg; if (byteBuf.isReadable()) { int protocolVersion = byteBuf.readByte(); if (protocolVersion != PROTOCOL_VERSION) { throw new RuntimeException("Unsupported protocol version: " + protocolVersion); } int observableNameLength = byteBuf.readByte(); String observableName = null; if (observableNameLength > 0) { // read name observableName = new String(byteBuf.readBytes(observableNameLength).array()); } int operation = byteBuf.readByte(); RemoteRxEvent.Type type = null; Map<String, String> subscribeParams = null; byte[] valueData = null; if (operation == 1) { logger.debug("READ request for RemoteRxEvent: next"); type = RemoteRxEvent.Type.next; valueData = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(valueData); } else if (operation == 2) { logger.debug("READ request for RemoteRxEvent: error"); type = RemoteRxEvent.Type.error; valueData = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(valueData); } else if (operation == 3) { logger.debug("READ request for RemoteRxEvent: completed"); type = RemoteRxEvent.Type.completed; } else if (operation == 4) { logger.debug("READ request for RemoteRxEvent: subscribed"); type = RemoteRxEvent.Type.subscribed; // read subscribe parameters int subscribeParamsLength = byteBuf.readInt(); if (subscribeParamsLength > 0) { // read byte into map byte[] subscribeParamsBytes = new byte[subscribeParamsLength]; byteBuf.readBytes(subscribeParamsBytes); subscribeParams = fromBytesToMap(subscribeParamsBytes); } } else if (operation == 5) { logger.debug("READ request for RemoteRxEvent: unsubscribed"); type = RemoteRxEvent.Type.unsubscribed; } else { throw new RuntimeException("operation: " + operation + " not support."); } handled = true; byteBuf.release(); ctx.fireChannelRead(new RemoteRxEvent(observableName, type, valueData, subscribeParams)); } } if (!handled) { super.channelRead(ctx, msg); } } @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof RemoteRxEvent) { ByteBuf buf = ctx.alloc().buffer(); buf.writeByte(PROTOCOL_VERSION); RemoteRxEvent event = (RemoteRxEvent) msg; String observableName = event.getName(); if (observableName != null && !observableName.isEmpty()) { // write length int nameLength = observableName.length(); if (nameLength < 127) { buf.writeByte(nameLength); buf.writeBytes(observableName.getBytes()); } else { throw new RuntimeException( "observableName " + observableName + " exceeds max limit of 127 characters"); } } else { // no name provided, write 0 bytes for name length buf.writeByte(0); } if (event.getType() == RemoteRxEvent.Type.next) { logger.debug("WRITE request for RemoteRxEvent: next"); buf.writeByte(1); buf.writeBytes(event.getData()); super.write(ctx, buf, promise); } else if (event.getType() == RemoteRxEvent.Type.error) { logger.debug("WRITE request for RemoteRxEvent: error"); buf.writeByte(2); buf.writeBytes(event.getData()); super.write(ctx, buf, promise); } else if (event.getType() == RemoteRxEvent.Type.completed) { logger.debug("WRITE request for RemoteRxEvent: completed"); buf.writeByte(3); super.write(ctx, buf, promise); super.flush(ctx); // TODO why do I need to explicitly call flush for this to work??? empty data?? } else if (event.getType() == RemoteRxEvent.Type.subscribed) { logger.debug("WRITE request for RemoteRxEvent: subscribed"); buf.writeByte(4); Map<String, String> subscribeParameters = event.getSubscribeParameters(); if (subscribeParameters != null && !subscribeParameters.isEmpty()) { byte[] subscribeBytes = fromMapToBytes(subscribeParameters); buf.writeInt(subscribeBytes.length); // write int for length buf.writeBytes(subscribeBytes); // write data } else { buf.writeInt(0); // no data } super.write(ctx, buf, promise); super.flush(ctx); } else if (event.getType() == RemoteRxEvent.Type.unsubscribed) { logger.debug("WRITE request for RemoteRxEvent: unsubscribed"); buf.writeByte(5); super.write(ctx, buf, promise); super.flush(ctx); // TODO why do I need to explicitly call flush for this to work??? empty data?? } } else { super.write(ctx, msg, promise); } } }); }
From source file:io.reactivex.netty.samples.SimplePostClient.java
License:Apache License
public Observable<String> postMessage() { HttpClient<String, ByteBuf> client = RxNetty.<String, ByteBuf>newHttpClientBuilder(host, port) .pipelineConfigurator(PipelineConfigurators.httpClientConfigurator()) .enableWireLogging(LogLevel.ERROR).build(); HttpClientRequest<String> request = HttpClientRequest.create(HttpMethod.POST, path); request.withHeader(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded"); String authString = USERNAME + ":" + PASSWORD; ByteBuf authByteBuf = Unpooled.copiedBuffer(authString.toCharArray(), CharsetUtil.UTF_8); ByteBuf encodedAuthByteBuf = Base64.encode(authByteBuf); request.withHeader(HttpHeaders.Names.AUTHORIZATION, "Basic " + encodedAuthByteBuf.toString(CharsetUtil.UTF_8)); request.withRawContentSource(Observable.just(MESSAGE), StringTransformer.DEFAULT_INSTANCE); return client.submit(request).flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<String>>() { @Override/* www . j a v a 2 s. c o m*/ public Observable<String> call(HttpClientResponse<ByteBuf> response) { if (!response.getStatus().equals(HttpResponseStatus.OK)) { return Observable.<String>error(new HttpStatusNotOKException()); } return response.getContent() // .defaultIfEmpty(Unpooled.EMPTY_BUFFER) .map(new Func1<ByteBuf, ByteBuf>() { @Override public ByteBuf call(ByteBuf buf) { return Unpooled.copiedBuffer(buf); } }).reduce(new Func2<ByteBuf, ByteBuf, ByteBuf>() { @Override public ByteBuf call(ByteBuf buf1, ByteBuf buf2) { ByteBuf buf3 = Unpooled.copiedBuffer(buf1, buf2); buf1.release(); buf2.release(); return buf3; } }).map(new Func1<ByteBuf, String>() { @Override public String call(ByteBuf buf4) { String str = buf4.toString(Charset.defaultCharset()); buf4.release(); return str; } }); } }).retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() { @Override public Observable<?> call(Observable<? extends Throwable> notificationHandler) { return notificationHandler.flatMap(new Func1<Throwable, Observable<Throwable>>() { @Override public Observable<Throwable> call(Throwable e) { if ((e instanceof ConnectException && e.getMessage().subSequence(0, 18).equals("Connection refused")) || (e instanceof HttpStatusNotOKException) || (e instanceof NoSuchElementException && e.getMessage().equals("Sequence contains no elements"))) { // logger.error(e.getMessage(), e); return Observable.<Throwable>just(e); } return Observable.<Throwable>error(e); } }).zipWith(Observable.range(1, 4), (e, i) -> { // TODO create tuple class to contain both e, i if (i < 4) { return new Throwable(String.valueOf(i)); } else { return e; } }).flatMap((e) -> { try { int i = Integer.valueOf(e.getMessage()); logger.info("retry({}{}) after {}sec", i, (i == 1) ? "st" : (i == 2) ? "nd" : (i == 3) ? "rd" : "th", 1); return Observable.timer(3, TimeUnit.SECONDS); } catch (NumberFormatException nfe) { return Observable.<Throwable>error(e); } }); } }); // .toBlocking().singleOrDefault("No Data"); }
From source file:io.reactivex.netty.samples.SimplePostServer.java
License:Apache License
public HttpServer<ByteBuf, ByteBuf> createServer() { HttpServer<ByteBuf, ByteBuf> server = RxNetty .newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() { @Override/*from w ww . j a v a 2 s. c o m*/ public Observable<Void> handle(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) { return request.getContent().map(new Func1<ByteBuf, ByteBuf>() { @Override public ByteBuf call(ByteBuf buf) { ByteBuf buf0 = Unpooled.copiedBuffer(buf); logger.info("buf0 {} refCnt() : {}", buf0.toString(), buf0.refCnt()); return buf0; } }).reduce(new Func2<ByteBuf, ByteBuf, ByteBuf>() { @Override public ByteBuf call(ByteBuf buf1, ByteBuf buf2) { logger.info("reduce"); logger.info("buf1 {} refCnt() : {}", buf1.toString(), buf1.refCnt()); logger.info("buf2 {} refCnt() : {}", buf2.toString(), buf2.refCnt()); ByteBuf buf3 = Unpooled.copiedBuffer(buf1, buf2); buf1.release(); logger.info("buf1 release"); logger.info("buf1 {} refCnt() : {}", buf1.toString(), buf1.refCnt()); buf2.release(); logger.info("buf2 release"); logger.info("buf2 {} refCnt() : {}", buf2.toString(), buf2.refCnt()); logger.info("buf3 {} refCnt() : {}", buf3.toString(), buf3.refCnt()); return buf3; } }).map(new Func1<ByteBuf, Void>() { @Override public Void call(ByteBuf buf4) { String str = buf4.toString(Charset.defaultCharset()); QueryStringDecoder decoder = new QueryStringDecoder(str, false); Map<String, List<String>> map = decoder.parameters(); for (String key : map.keySet()) { System.out.println(key + " : " + map.get(key).get(0)); } // response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); response.setStatus(HttpResponseStatus.OK); response.writeStringAndFlush("1"); logger.info("buf4 {} refCnt() : {}", buf4.toString(), buf4.refCnt()); buf4.release(); logger.info("buf4 release"); logger.info("buf4 {} refCnt() : {}", buf4.toString(), buf4.refCnt()); return null; } }) // .collect(new Func0<List<String>>() { // // @Override public List<String> call() { // return new ArrayList<>(); // } // }, new Action2<List<String>, ByteBuf>() { // // @Override public void call(List<String> list, ByteBuf buf) { // // System.out.println(list.size() + " , " + buf.toString(Charset.defaultCharset())); // list.add(buf.toString(Charset.defaultCharset())); // } // }) // .map(new Func1<List<String>, Void>() { // // @Override public Void call(List<String> list) { // String str = ""; // for (String s : list) { // str += s; // } // // System.out.println(str); // QueryStringDecoder decoder = new QueryStringDecoder(str, false); // Map<String, List<String>> map = decoder.parameters(); // for (String key : map.keySet()) { // System.out.println(key + " : " + map.get(key).get(0)); // } // response.writeStringAndFlush("1"); // return null; // } // }) .ignoreElements(); } }).pipelineConfigurator(new HttpServerPipelineConfigurator<ByteBuf, ByteBuf>()) // .enableWireLogging(LogLevel.ERROR) .build(); logger.info("Simple POST server started..."); return server; }
From source file:io.riox.springxd.sinks.websocket.NettyWebSocketServerHandler.java
License:Apache License
void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);/*www. j a v a2 s.co m*/ buf.release(); HttpHeaders.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:io.scalecube.socketio.pipeline.FlashPolicyHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // ?/*w w w . j ava 2 s .c o m*/ if (msg instanceof ByteBuf) { ByteBuf message = (ByteBuf) msg; if (message.readableBytes() >= policyRequestBuffer.readableBytes()) { ByteBuf data = message.slice(0, policyRequestBuffer.readableBytes()); if (data.equals(policyRequestBuffer)) { // Remove SSL handler from pipeline otherwise on channel close SSL handler // will fail all pending writes instead of flushing them and as a result // client won't get flash policy file. if (ctx.pipeline().get(SocketIOChannelInitializer.SSL_HANDLER) != null) { ctx.pipeline().remove(SocketIOChannelInitializer.SSL_HANDLER); } // Send flash policy file and close connection ByteBuf response = PipelineUtils.copiedBuffer(ctx.alloc(), policyResponse); ChannelFuture f = ctx.writeAndFlush(response); f.addListener(ChannelFutureListener.CLOSE); if (log.isDebugEnabled()) log.debug("Sent flash policy file to channel: {}", ctx.channel()); message.release(); return; } } ctx.pipeline().remove(this); } ctx.fireChannelRead(msg); }
From source file:io.scalecube.socketio.pipeline.JsonpPollingHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // ?/* w w w . j ava2 s .co m*/ if (msg instanceof FullHttpRequest) { final FullHttpRequest req = (FullHttpRequest) msg; final HttpMethod requestMethod = req.getMethod(); final QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri()); final String requestPath = queryDecoder.path(); if (requestPath.startsWith(connectPath)) { if (log.isDebugEnabled()) log.debug("Received HTTP JSONP-Polling request: {} {} from channel: {}", requestMethod, requestPath, ctx.channel()); final String sessionId = PipelineUtils.getSessionId(requestPath); final String origin = PipelineUtils.getOrigin(req); if (HttpMethod.GET.equals(requestMethod)) { // Process polling request from client SocketAddress clientIp = PipelineUtils.getHeaderClientIPParamValue(req, remoteAddressHeader); String jsonpIndexParam = PipelineUtils.extractParameter(queryDecoder, "i"); final ConnectPacket packet = new ConnectPacket(sessionId, origin); packet.setTransportType(TransportType.JSONP_POLLING); packet.setJsonpIndexParam(jsonpIndexParam); packet.setRemoteAddress(clientIp); ctx.fireChannelRead(packet); } else if (HttpMethod.POST.equals(requestMethod)) { // Process message request from client ByteBuf buffer = req.content(); String content = buffer.toString(CharsetUtil.UTF_8); if (content.startsWith("d=")) { QueryStringDecoder queryStringDecoder = new QueryStringDecoder(content, CharsetUtil.UTF_8, false); content = PipelineUtils.extractParameter(queryStringDecoder, "d"); content = preprocessJsonpContent(content); ByteBuf buf = PipelineUtils.copiedBuffer(ctx.alloc(), content); List<Packet> packets = PacketFramer.decodePacketsFrame(buf); buf.release(); for (Packet packet : packets) { packet.setSessionId(sessionId); packet.setOrigin(origin); ctx.fireChannelRead(packet); } } else { log.warn( "Can't process HTTP JSONP-Polling message. Incorrect content format: {} from channel: {}", content, ctx.channel()); } } else { log.warn( "Can't process HTTP JSONP-Polling request. Unknown request method: {} from channel: {}", requestMethod, ctx.channel()); } ReferenceCountUtil.release(msg); return; } } super.channelRead(ctx, msg); }
From source file:io.scalecube.socketio.pipeline.PacketEncoderHandler.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception { if (msg instanceof IPacket) { IPacket packet = (IPacket) msg;// w w w.ja v a 2 s.com if (log.isDebugEnabled()) log.debug("Sending packet: {} to channel: {}", msg, ctx.channel()); ByteBuf encodedPacket = encodePacket(packet); if (log.isDebugEnabled()) log.debug("Encoded packet: {}", encodedPacket); TransportType transportType = packet.getTransportType(); if (transportType == TransportType.WEBSOCKET || transportType == TransportType.FLASHSOCKET) { out.add(new TextWebSocketFrame(encodedPacket)); } else if (transportType == TransportType.XHR_POLLING) { out.add(PipelineUtils.createHttpResponse(packet.getOrigin(), encodedPacket, false)); } else if (transportType == TransportType.JSONP_POLLING) { String jsonpIndexParam = (packet.getJsonpIndexParam() != null) ? packet.getJsonpIndexParam() : "0"; String encodedStringPacket = encodedPacket.toString(CharsetUtil.UTF_8); encodedPacket.release(); String encodedJsonpPacket = String.format(JSONP_TEMPLATE, jsonpIndexParam, encodedStringPacket); HttpResponse httpResponse = PipelineUtils.createHttpResponse(packet.getOrigin(), PipelineUtils.copiedBuffer(ctx.alloc(), encodedJsonpPacket), true); httpResponse.headers().add("X-XSS-Protection", "0"); out.add(httpResponse); } else { throw new UnsupportedTransportTypeException(transportType); } } else { if (msg instanceof ReferenceCounted) { ((ReferenceCounted) msg).retain(); } out.add(msg); } }
From source file:io.servicecomb.foundation.vertx.TestVertxUtils.java
License:Apache License
@Test public void testgetBytesFastByteBufCopy() { ByteBuf byteBuf = Unpooled.directBuffer(); byteBuf.writeByte(1);/*from ww w .jav a2s. c o m*/ Assert.assertFalse(byteBuf.hasArray()); byte[] result = VertxUtils.getBytesFast(byteBuf); Assert.assertEquals(1, result[0]); byteBuf.release(); }