List of usage examples for io.netty.buffer ByteBuf writeByte
public abstract ByteBuf writeByte(int value);
From source file:com.cloudhopper.smpp.pdu.DataSm.java
License:Apache License
@Override public void writeBody(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException { ChannelBufferUtil.writeNullTerminatedString(buffer, this.serviceType); ChannelBufferUtil.writeAddress(buffer, this.sourceAddress); ChannelBufferUtil.writeAddress(buffer, this.destAddress); buffer.writeByte(this.esmClass); buffer.writeByte(this.registeredDelivery); buffer.writeByte(this.dataCoding); }
From source file:com.cloudhopper.smpp.pdu.QuerySmResp.java
License:Apache License
@Override public void writeBody(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException { ChannelBufferUtil.writeNullTerminatedString(buffer, this.messageId); ChannelBufferUtil.writeNullTerminatedString(buffer, this.finalDate); buffer.writeByte(this.messageState); buffer.writeByte(this.errorCode); }
From source file:com.cloudhopper.smpp.type.Address.java
License:Apache License
public void write(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException { buffer.writeByte(this.ton); buffer.writeByte(this.npi); ChannelBufferUtil.writeNullTerminatedString(buffer, this.address); }
From source file:com.cloudhopper.smpp.util.ChannelBufferUtil.java
License:Apache License
/** * Writes a C-String (null terminated) to a buffer. If the String is null * this method will only write out the NULL byte (0x00) to the buffer. * @param buffer//from w w w. j a va2 s. co m * @param value * @throws UnsupportedEncodingException */ static public void writeNullTerminatedString(ByteBuf buffer, String value) throws UnrecoverablePduException { if (value != null) { try { byte[] bytes = value.getBytes("ISO-8859-1"); buffer.writeBytes(bytes); } catch (UnsupportedEncodingException e) { throw new UnrecoverablePduException(e.getMessage(), e); } } // always write null byte buffer.writeByte((byte) 0x00); }
From source file:com.codnos.dbgp.internal.handlers.DBGpCommandEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { Command command = (Command) msg;//from w w w . j av a2 s. c o m String commandMessage = command.getMessage(); LOGGER.fine("sending command: " + commandMessage); out.writeBytes(commandMessage.getBytes(UTF_8)); out.writeByte(NULL_BYTE); }
From source file:com.comphenix.protocol.injector.netty.WirePacket.java
License:Open Source License
public static void writeVarInt(ByteBuf output, int i) { checkNotNull(output, "output cannot be null!"); while ((i & -128) != 0) { output.writeByte(i & 127 | 128); i >>>= 7;/*from w w w .j a v a 2s . c o m*/ } output.writeByte(i); }
From source file:com.corundumstudio.socketio.handler.EncoderHandler.java
License:Apache License
private void handleWebsocket(final OutPacketMessage msg, ChannelHandlerContext ctx) throws IOException { while (true) { Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport()); Packet packet = queue.poll();//from w w w . j a v a 2 s .c o m if (packet == null) { break; } final ByteBuf out = encoder.allocateBuffer(ctx.alloc()); encoder.encodePacket(packet, out, ctx.alloc(), true); WebSocketFrame res = new TextWebSocketFrame(out); if (log.isTraceEnabled()) { log.trace("Out message: {} sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId()); } if (out.isReadable()) { ctx.channel().writeAndFlush(res); } else { out.release(); } for (ByteBuf buf : packet.getAttachments()) { ByteBuf outBuf = encoder.allocateBuffer(ctx.alloc()); outBuf.writeByte(4); outBuf.writeBytes(buf); if (log.isTraceEnabled()) { log.trace("Out attachment: {} sessionId: {}", ByteBufUtil.hexDump(outBuf), msg.getSessionId()); } ctx.channel().writeAndFlush(new BinaryWebSocketFrame(outBuf)); } } }
From source file:com.corundumstudio.socketio.parser.Encoder.java
License:Apache License
public int encodePacket(Packet packet, ByteBuf buffer) throws IOException { ByteBufOutputStream out = new ByteBufOutputStream(buffer); int start = buffer.writerIndex(); int type = packet.getType().getValue(); buffer.writeByte(toChar(type)); buffer.writeByte(Packet.SEPARATOR);/*from w w w . j a v a 2 s . c o m*/ Long id = packet.getId(); String endpoint = packet.getEndpoint(); Object ack = packet.getAck(); if (Packet.ACK_DATA.equals(ack)) { buffer.writeBytes(toChars(id)); buffer.writeByte('+'); } else { if (id != null) { buffer.writeBytes(toChars(id)); } } buffer.writeByte(Packet.SEPARATOR); if (endpoint != null) { buffer.writeBytes(endpoint.getBytes()); } switch (packet.getType()) { case MESSAGE: if (packet.getData() != null) { buffer.writeByte(Packet.SEPARATOR); byte[] data = packet.getData().toString().getBytes(); buffer.writeBytes(data); } break; case EVENT: List<Object> args = packet.getArgs(); if (args.isEmpty()) { args = null; } buffer.writeByte(Packet.SEPARATOR); Event event = new Event(packet.getName(), args); jsonSupport.writeValue(out, event); break; case JSON: buffer.writeByte(Packet.SEPARATOR); jsonSupport.writeValue(out, packet.getData()); break; case CONNECT: if (packet.getQs() != null) { buffer.writeByte(Packet.SEPARATOR); byte[] qsData = packet.getQs().toString().getBytes(); buffer.writeBytes(qsData); } break; case ACK: if (packet.getAckId() != null || !packet.getArgs().isEmpty()) { buffer.writeByte(Packet.SEPARATOR); } if (packet.getAckId() != null) { byte[] ackIdData = toChars(packet.getAckId()); buffer.writeBytes(ackIdData); } if (!packet.getArgs().isEmpty()) { buffer.writeByte('+'); jsonSupport.writeValue(out, packet.getArgs()); } break; case ERROR: if (packet.getReason() != null || packet.getAdvice() != null) { buffer.writeByte(Packet.SEPARATOR); } if (packet.getReason() != null) { int reasonCode = packet.getReason().getValue(); buffer.writeByte(toChar(reasonCode)); } if (packet.getAdvice() != null) { int adviceCode = packet.getAdvice().getValue(); buffer.writeByte('+'); buffer.writeByte(toChar(adviceCode)); } break; } return charsScanner.getLength(buffer, start); }
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
private void processUtf8(ByteBuf in, ByteBuf out, boolean jsonpMode) { while (in.isReadable()) { short value = (short) (in.readByte() & 0xFF); if (value >>> 7 == 0) { if (jsonpMode && (value == '\\' || value == '\'')) { out.writeByte('\\'); }/*w w w. j a v a 2 s . co m*/ out.writeByte(value); } else { out.writeByte(((value >>> 6) | 0xC0)); out.writeByte(((value & 0x3F) | 0x80)); } } }
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
public void encodePackets(Queue<Packet> packets, ByteBuf buffer, ByteBufAllocator allocator, int limit) throws IOException { int i = 0;//from w ww.ja va 2 s. c om while (true) { Packet packet = packets.poll(); if (packet == null || i == limit) { break; } encodePacket(packet, buffer, allocator, false); i++; for (ByteBuf attachment : packet.getAttachments()) { buffer.writeByte(1); buffer.writeBytes(longToBytes(attachment.readableBytes() + 1)); buffer.writeByte(0xff); buffer.writeByte(4); buffer.writeBytes(attachment); } } }