List of usage examples for io.netty.buffer ByteBuf array
public abstract byte[] array();
From source file:org.restcomm.media.rtp.netty.RtpDemultiplexer.java
License:Open Source License
private RtpPacket buildRtpPacket(ByteBuf msg) { // Retrieve data from network final byte[] data = msg.array(); final int length = msg.readableBytes(); final int offset = msg.arrayOffset(); // Wrap data into an RTP packet final RtpPacket rtpPacket = new RtpPacket(length - offset, false); rtpPacket.getBuffer().put(data, offset, length).flip(); return rtpPacket; }
From source file:org.restcomm.media.rtp.netty.RtpDemultiplexer.java
License:Open Source License
private RtcpPacket buildRtcpPacket(ByteBuf msg) { // Retrieve data from network final byte[] data = msg.array(); final int offset = msg.arrayOffset(); // Wrap data into an RTP packet final RtcpPacket rtcpPacket = new RtcpPacket(); rtcpPacket.decode(data, offset);//from w w w. j a va 2s . c o m return rtcpPacket; }
From source file:org.restexpress.Request.java
License:Apache License
/** * Returns the byte array underlying the Netty ByteBuf for this request. * However, if the ByteBuf returns false to hasArray(), this * method returns null.// w ww. j a v a 2 s . c om * @author actolap All ByteBufs may not have backing array (i.e. direct memory) * * @return an array of byte, or null, if the ByteBuf is not backed by a byte array. */ public byte[] getBodyAsBytes() { ByteBuf buf = getBody(); int length = buf.readableBytes(); byte[] bytes; if (buf.hasArray()) { bytes = buf.array(); } else { bytes = new byte[length]; buf.getBytes(buf.readerIndex(), bytes); } return bytes; }
From source file:org.rzo.netty.ahessian.crypto.Util.java
License:Apache License
static ByteBuf code(StreamCipher cipher, ByteBuf e, boolean decode) throws Exception { try {//from ww w .ja va 2 s .c om ByteBuf b = e; byte[] encodedData = cipher.crypt(b.array(), b.readerIndex(), b.readableBytes()); return Unpooled.wrappedBuffer(encodedData); } catch (Exception ex) { ex.printStackTrace(); throw ex; } }
From source file:org.rzo.netty.mcast.MulticastEndpoint.java
License:Apache License
public void send(ByteBuf msg) throws Exception { byte[] arr = msg.array(); byte[] buf = new byte[arr.length + id.length]; System.arraycopy(id, 0, buf, 0, id.length); System.arraycopy(arr, 0, buf, id.length, arr.length); ByteBuf bbuf = Unpooled.wrappedBuffer(buf); if (debug && logger != null) logger.info("discovery send " + new String(bbuf.array())); datagramChannel.writeAndFlush(new DatagramPacket(bbuf, multicastAddress)).sync(); // datagramChannel.writeAndFlush(buf, multicastAddress); }
From source file:org.spongepowered.clean.network.netty.PacketDecryptor.java
License:MIT License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { int length = msg.readableBytes(); if (this.dataBuffer.length < length) { this.dataBuffer = new byte[length]; }/*from w w w . ja va2 s. c o m*/ msg.readBytes(this.dataBuffer, 0, length); ByteBuf plain = ctx.alloc().heapBuffer(this.cipher.getOutputSize(length)); int actual = this.cipher.update(this.dataBuffer, 0, length, plain.array(), plain.arrayOffset()); plain.writerIndex(actual); out.add(plain); }
From source file:org.spout.vanilla.protocol.rcon.codec.RconCodec.java
License:Open Source License
@Override public T decode(ByteBuf buffer) { buffer.order(ByteOrder.LITTLE_ENDIAN); ByteBuf expandingBytes = Unpooled.buffer(buffer.writerIndex()); byte b;/*from w ww . ja v a 2s . co m*/ while ((b = buffer.readByte()) != 0) { expandingBytes.writeByte(b); } assert buffer.readByte() == 0; // Second null byte String value = new String(expandingBytes.array(), CharsetUtil.US_ASCII); return createMessage(value); }
From source file:org.spout.vanilla.protocol.VanillaProtocol.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public <T extends Message> Message getWrappedMessage(T dynamicMessage) throws IOException { MessageCodec<T> codec = (MessageCodec<T>) getCodecLookupService().find(dynamicMessage.getClass()); ByteBuf buffer = codec.encode(Spout.getPlatform() == Platform.CLIENT, dynamicMessage); return new ServerPluginMessage(getName(codec), buffer.array()); }
From source file:org.spoutcraft.mod.protocol.codec.AddonListCodec.java
License:MIT License
@Override public AddonListMessage decode(Spoutcraft game, ByteBuf buffer) throws IOException { final AddonListMessage message; try {//from w w w . j av a2 s .c o m message = new AddonListMessage(new SerializableHashMap<String, String>(buffer.array())); } catch (ClassNotFoundException e) { throw new IOException(e); } return message; }
From source file:org.springframework.cloud.stream.app.netty.tcp.source.NettyTcpReceivingChannelAdapter.java
License:Apache License
@Override protected void onInit() { super.onInit(); serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override// w w w. j a v a 2 s . c o m protected void initChannel(final SocketChannel socketChannel) throws Exception { final ChannelPipeline channelPipeline = socketChannel.pipeline(); boolean first = true; ChannelInboundHandler[] channelInboundHandlers = channelInboundHandlerFactory .createChannelInboundHandlers(); for (ChannelInboundHandler channelInboundHandler : channelInboundHandlers) { if (first) { channelPipeline.addFirst(channelInboundHandler); first = false; } else { channelPipeline.addLast(channelInboundHandler); } } channelPipeline.addLast("sourceOutputHandler", new SimpleChannelInboundHandler<ByteBuf>() { @Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { byte[] data; if (msg.hasArray()) { data = msg.array(); } else { data = new byte[msg.readableBytes()]; msg.readBytes(data); } AbstractIntegrationMessageBuilder<byte[]> builder = getMessageBuilderFactory() .withPayload(data); sendMessage(builder.build()); } }); socketChannel.closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { socketChannel.close(); } }); } }); serverBootstrap.validate(); }