List of usage examples for io.netty.buffer ByteBuf writeBytes
public abstract ByteBuf writeBytes(ByteBuffer src);
From source file:com.irh.material.basics.netty.chapter14_1.codec.NettyMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception { if (msg == null || msg.getHeader() == null) { throw new Exception("The encode message is null"); }/*www . java2 s .c om*/ sendBuf.writeInt((msg.getHeader().getCrcCode())); sendBuf.writeInt((msg.getHeader().getLength())); sendBuf.writeLong((msg.getHeader().getSessionID())); sendBuf.writeByte((msg.getHeader().getType())); sendBuf.writeByte((msg.getHeader().getPriority())); sendBuf.writeInt((msg.getHeader().getAttachment().size())); String key = null; byte[] keyArray = null; Object value = null; for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) { key = param.getKey(); keyArray = key.getBytes("UTF-8"); sendBuf.writeInt(keyArray.length); sendBuf.writeBytes(keyArray); value = param.getValue(); marshallingEncoder.encode(value, sendBuf); } key = null; keyArray = null; value = null; if (msg.getBody() != null) { marshallingEncoder.encode(msg.getBody(), sendBuf); } else sendBuf.writeInt(0); sendBuf.setInt(4, sendBuf.readableBytes() - 8); }
From source file:com.jadarstudios.rankcapes.forge.network.packet.PacketBase.java
License:MIT License
/** * Write a string to a {@link ByteBuf}//from w w w. ja v a 2s . co m * * @param string the string to write * @param data the buffer to write to * * @throws IndexOutOfBoundsException thrown if the buffer is too small for the data */ public void writeString(String string, ByteBuf data) throws IndexOutOfBoundsException { byte[] stringBytes = string.getBytes(); data.writeInt(stringBytes.length); data.writeBytes(stringBytes); }
From source file:com.jayqqaa12.jbase.tcp.netty.code.DreamJSONEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception { // System.out.println("begin encode"); if (StringUtils.isEmpty(msg)) { return;// w ww .ja va2 s .c o m } byte[] message = msg.getBytes("UTF-8"); out.writeInt(message.length); out.writeBytes(message); // System.out.println("end encode"); }
From source file:com.juaby.labs.rpc.client.Rpc2ClientEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, RpcMessage msg, ByteBuf out) throws Exception { // GIOP header out.writeBytes(msg.getGIOPHeader()); // Major version out.writeByte(msg.getMajor());/*w w w. ja v a2 s .c o m*/ // Minor version out.writeByte(msg.getMinor()); // Flags out.writeByte(msg.getFlags()); // Value out.writeByte(msg.getValue()); // ID out.writeInt(msg.getId()); // Total length out.writeInt(msg.getTotalLength()); // Body length out.writeInt(msg.getBodyLength()); // Body out.writeBytes(msg.getBody()); }
From source file:com.kazzla.asterisk.netty.MessageEncoder.java
License:Apache License
public void encode(ChannelHandlerContext ctx, Message msg, ByteBuf b) throws CodecException { b.writeBytes(codec.encode(msg)); }
From source file:com.kixeye.kixmpp.KixmppCodec.java
License:Apache License
/** * @see io.netty.handler.codec.ByteToMessageCodec#encode(io.netty.channel.ChannelHandlerContext, java.lang.Object, io.netty.buffer.ByteBuf) *///from w ww .ja v a2 s .co m @Override protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { if (msg instanceof Element) { new XMLOutputter().output((Element) msg, new ByteBufOutputStream(out)); } else if (msg instanceof KixmppStreamStart) { KixmppStreamStart streamStart = (KixmppStreamStart) msg; if (streamStart.doesIncludeXmlHeader()) { out.writeBytes("<?xml version='1.0' encoding='UTF-8'?>".getBytes(StandardCharsets.UTF_8)); } out.writeBytes("<stream:stream ".getBytes(StandardCharsets.UTF_8)); if (streamStart.getId() != null) { out.writeBytes(String.format("id=\"%s\" ", streamStart.getId()).getBytes(StandardCharsets.UTF_8)); } if (streamStart.getFrom() != null) { out.writeBytes(String.format("from=\"%s\" ", streamStart.getFrom().getFullJid()) .getBytes(StandardCharsets.UTF_8)); } if (streamStart.getTo() != null) { out.writeBytes(String.format("to=\"%s\" ", streamStart.getTo().getFullJid()) .getBytes(StandardCharsets.UTF_8)); } out.writeBytes( "version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">" .getBytes(StandardCharsets.UTF_8)); } else if (msg instanceof KixmppStreamEnd) { out.writeBytes("</stream:stream>".getBytes(StandardCharsets.UTF_8)); } else if (msg instanceof String) { out.writeBytes(((String) msg).getBytes(StandardCharsets.UTF_8)); } else if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; out.writeBytes(buf, 0, buf.readableBytes()); } if (logger.isDebugEnabled()) { logger.debug("Sending: [{}]", out.toString(StandardCharsets.UTF_8)); } }
From source file:com.kradac.karview.netty.MyEncoder.java
@Override protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf buf) throws Exception { String msj = String.valueOf(msg); String[] data = msj.split("%%"); msj = data[0];/*from w w w. j a v a 2 s .co m*/ System.out.println(">>> " + Integer.parseInt(data[1])); if (Integer.parseInt(data[1]) == 1) { System.out.println("pas1"); buf.writeByte(0x00); buf.writeByte(msj.length() + 4 + 2 + 1); //Aumenta un 1 por el 0 que se aumenta buf.writeByte(0x00); buf.writeByte(0x01); buf.writeByte(0x04); buf.writeByte(0x00); buf.writeByte(0x00); buf.writeBytes(msj.getBytes()); } else { System.out.println("pas2"); buf.writeByte(0x00); buf.writeByte(msj.length() + 4 + 2); buf.writeByte(0x00); buf.writeByte(0x01); buf.writeByte(0x04); buf.writeByte(0x00); buf.writeBytes(msj.getBytes()); } }
From source file:com.l2jmobius.commons.network.codecs.CryptCodec.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) { // Check if there are any data to encrypt. if (!msg.isReadable()) { return;/*www.j a va 2 s.com*/ } msg.resetReaderIndex(); _crypt.encrypt(msg); msg.resetReaderIndex(); out.writeBytes(msg); }
From source file:com.lambdaworks.redis.ProtectedModeTests.java
License:Apache License
@BeforeClass public static void beforeClass() throws Exception { server = new MockTcpServer(); server.addHandler(() -> {/* w w w .ja v a 2 s . co m*/ return new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { String message = getMessage(); ByteBuf buffer = ctx.alloc().buffer(message.length() + 3); buffer.writeBytes("-".getBytes()); buffer.writeBytes(message.getBytes()); buffer.writeByte('\r').writeByte('\n'); ctx.writeAndFlush(buffer).addListener(future -> { ctx.close(); }); } }; }); server.initialize(TestSettings.nonexistentPort()); client = RedisClient.create(TestClientResources.get(), RedisURI.create(TestSettings.host(), TestSettings.nonexistentPort())); }
From source file:com.lambdaworks.redis.protocol.CommandArgsTest.java
License:Apache License
@Test public void addValues() throws Exception { CommandArgs<String, String> args = new CommandArgs<>(codec).addValues(Arrays.asList("1", "2")); ByteBuf buffer = Unpooled.buffer();//from w ww . j ava 2 s.c om args.encode(buffer); ByteBuf expected = Unpooled.buffer(); expected.writeBytes(("$1\r\n" + "1\r\n" + "$1\r\n" + "2\r\n").getBytes()); assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII)); }