List of usage examples for io.netty.buffer ByteBuf writeBytes
public abstract ByteBuf writeBytes(ByteBuffer src);
From source file:com.example.spring.boot.netty.TcpClientHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) { new Thread(new Runnable() { @Override/*from ww w. j a v a 2s.c o m*/ public void run() { while (true) { ByteBuf buf = Unpooled.buffer(256); buf.writeBytes(MSG.getBytes()); ctx.writeAndFlush(buf); try { Thread.sleep(tcpClient.getInterval()); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); }
From source file:com.farsunset.cim.sdk.android.filter.ClientMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Protobufable message, ByteBuf out) throws Exception { Protobufable data = (Protobufable) message; byte[] byteArray = data.getByteArray(); out.writeBytes(createHeader(data.getType(), byteArray.length)); out.writeBytes(byteArray);// ww w .j av a2 s . c o m }
From source file:com.farsunset.cim.sdk.client.filter.ClientMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Object message, ByteBuf out) throws Exception { if (message instanceof Protobufable) { Protobufable data = (Protobufable) message; byte[] byteArray = data.getByteArray(); out.writeBytes(createHeader(data.getType(), byteArray.length)); out.writeBytes(byteArray);/*from w w w . j ava2s .c o m*/ } logger.info(message.toString()); }
From source file:com.farsunset.cim.sdk.server.filter.ServerMessageEncoder.java
License:Apache License
@Override protected void encode(final ChannelHandlerContext ctx, final Object object, ByteBuf out) throws Exception { Object protocol = ctx.channel().attr(AttributeKey.valueOf(CIMSession.PROTOCOL)).get(); /**/*from w w w .j a v a 2 s. c o m*/ * websocket?? */ if (Objects.equals(CIMSession.WEBSOCKET, protocol) && object instanceof HandshakerResponse) { out.writeBytes(object.toString().getBytes()); return; } /** * websocket? */ if (Objects.equals(CIMSession.WEBSOCKET, protocol) && object instanceof EncodeFormatable) { EncodeFormatable data = (EncodeFormatable) object; byte[] body = data.getProtobufBody(); byte[] header = createHeader(data.getDataType(), body.length); byte[] protobuf = new byte[body.length + CIMConstant.DATA_HEADER_LENGTH]; System.arraycopy(header, 0, protobuf, 0, header.length); System.arraycopy(body, 0, protobuf, header.length, body.length); byte[] binaryFrame = encodeDataFrame(protobuf); out.writeBytes(binaryFrame); return; } /** * ?websocket?Protobuf??? */ if (Objects.equals(CIMSession.NATIVEAPP, protocol) && object instanceof EncodeFormatable) { EncodeFormatable data = (EncodeFormatable) object; byte[] body = data.getProtobufBody(); byte[] header = createHeader(data.getDataType(), body.length); out.writeBytes(header); out.writeBytes(body); } }
From source file:com.feihong.newzxclient.tcp.NettyClientHandler.java
License:Apache License
protected void sendMsg(Msg.CommonMessage msg) { if (mContext != null) { byte[] result = msg.toByteArray(); ByteBuf buf = Unpooled.buffer(4 + result.length); buf.writeInt(result.length);/*w w w .ja v a2 s . co m*/ buf.writeBytes(result); //System.out.println("send Msg success!"); mContext.writeAndFlush(buf); } }
From source file:com.flowpowered.network.util.ByteBufUtils.java
License:MIT License
/** * Writes an UTF8 string to a byte buffer. * * @param buf The byte buffer to write too * @param value The string to write// w w w . ja va2 s.c o m * @throws java.io.IOException If the writing fails */ public static void writeUTF8(ByteBuf buf, String value) throws IOException { final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); if (bytes.length >= Short.MAX_VALUE) { throw new IOException( "Attempt to write a string with a length greater than Short.MAX_VALUE to ByteBuf!"); } // Write the string's length writeVarInt(buf, bytes.length); buf.writeBytes(bytes); }
From source file:com.flysoloing.learning.network.netty.http2.helloworld.multiplex.server.HelloWorldHttp2Handler.java
License:Apache License
/** * If receive a frame with end-of-stream set, send a pre-canned response. *///ww w . ja v a2s . c om public void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) throws Exception { if (headers.isEndStream()) { ByteBuf content = ctx.alloc().buffer(); content.writeBytes(RESPONSE_BYTES.duplicate()); ByteBufUtil.writeAscii(content, " - via HTTP/2"); sendResponse(ctx, content); } }
From source file:com.flysoloing.learning.network.netty.http2.helloworld.server.HelloWorldHttp1Handler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { if (HttpUtil.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }/*from w ww. j av a 2 s . c o m*/ boolean keepAlive = HttpUtil.isKeepAlive(req); ByteBuf content = ctx.alloc().buffer(); content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate()); ByteBufUtil.writeAscii(content, " - via " + req.protocolVersion() + " (" + establishApproach + ")"); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.writeAndFlush(response); } }
From source file:com.flysoloing.learning.network.netty.http2.helloworld.server.HelloWorldHttp2Handler.java
License:Apache License
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) { if (endOfStream) { ByteBuf content = ctx.alloc().buffer(); content.writeBytes(RESPONSE_BYTES.duplicate()); ByteBufUtil.writeAscii(content, " - via HTTP/2"); sendResponse(ctx, streamId, content); }//from w ww .j av a 2s .c om }
From source file:com.flysoloing.learning.network.netty.http2.tiles.FallbackRequestHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, HttpRequest req) throws Exception { if (HttpUtil.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }//w w w . j a v a 2 s . c o m ByteBuf content = ctx.alloc().buffer(); content.writeBytes(response.duplicate()); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); ctx.write(response).addListener(ChannelFutureListener.CLOSE); }