Example usage for io.netty.buffer ByteBuf array

List of usage examples for io.netty.buffer ByteBuf array

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf array.

Prototype

public abstract byte[] array();

Source Link

Document

Returns the backing byte array of this buffer.

Usage

From source file:appeng.tile.AEBaseTile.java

License:Open Source License

/**
 * This builds a tag with the actual data that should be sent to the client for update syncs.
 * If the tile entity doesn't need update syncs, it returns null.
 *//*from w  ww  . ja v a  2s .  c  o m*/
private NBTTagCompound writeUpdateData() {
    final NBTTagCompound data = new NBTTagCompound();

    final ByteBuf stream = Unpooled.buffer();

    try {
        this.writeToStream(stream);
        if (stream.readableBytes() == 0) {
            return null;
        }
    } catch (final Throwable t) {
        AELog.debug(t);
    }

    stream.capacity(stream.readableBytes());
    data.setByteArray("X", stream.array());
    return data;
}

From source file:appeng.tile.misc.TilePaint.java

License:Open Source License

@TileEvent(TileEventType.WORLD_NBT_WRITE)
public void writeToNBT_TilePaint(final NBTTagCompound data) {
    final ByteBuf myDat = Unpooled.buffer();
    this.writeBuffer(myDat);
    if (myDat.hasArray()) {
        data.setByteArray("dots", myDat.array());
    }/* w w  w .  java 2s. co  m*/
}

From source file:at.yawk.accordion.codec.unsafe.TestMessenger.java

License:Mozilla Public License

@Override
public Channel<ByteBuf> getChannel(String name) {
    return new Channel<ByteBuf>() {
        @Override/*  w  ww.  j ava  2  s  .  co m*/
        public void publish(ByteBuf message) {
            byte[] data = Arrays.copyOf(message.array(), message.readableBytes());
            System.out.println(Arrays.toString(data));
            System.out.println(new String(data, StandardCharsets.UTF_8));
        }

        @Override
        public void subscribe(Consumer<ByteBuf> listener) {
        }
    };
}

From source file:at.yawk.accordion.compression.SnappyCompressor.java

License:Mozilla Public License

private static byte[] toArray(ByteBuf raw) {
    return Arrays.copyOfRange(raw.array(), raw.arrayOffset() + raw.readerIndex(),
            raw.arrayOffset() + raw.readerIndex() + raw.readableBytes());
}

From source file:at.yawk.accordion.distributed.ConnectionManager.java

License:Mozilla Public License

/**
 * Send a raw packet (with header fields already included) to the given connection. The given ByteBuf will not be
 * modified.//w  w  w .  j ava2s.com
 */
private void copyAndSend(Connection connection, ByteBuf full) {
    ByteBuf copy = full.copy();
    // 8 for packet ID, at least 1 for channel name or we're doing something wrong
    assert copy.readableBytes() > 9 : Arrays.toString(copy.array());
    connection.send(copy);
}

From source file:cl.uandes.so.client.LoginClientHandler.java

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    if (msg.content().readableBytes() < 3) {
        return;//from w  w w .  j a  v  a2 s  .co  m
    }
    byte[] address = null;
    String ip = "";
    byte[] test = { msg.content().readByte() };
    System.out.println(byteArray2Hex(test));
    int readbytes = 1;
    if (msg.content().readByte() == (byte) 0x52) {
        System.out.println("Byte 0x52 found, readablebytes: " + msg.content().readableBytes());
        ByteBuf data = msg.content().readBytes(msg.content().readableBytes());
        ip += new String(data.array());
        //System.out.println(byteArray2Hex(data.array()));

        this.address.append(ip);
        ctx.close();
    }

}

From source file:cl.uandes.so.server.LoginServerHandler.java

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    //if(packet.content().readableBytes() < 24) {
    //    return;
    //}//  w  ww.  ja  v a 2s .c om
    // TEST Cliente: echo -e "\xff\xff\xff\xff\xff\xff\xff\xff\x41\x67\x65\x74\x41\x64\x64\x72\x65\x73\x73\x26\x50\x6f\x72\x74" | nc -4 -w 1 -u 127.0.0.1 9990
    String expected_payload = "ffffffffffffffff416765744164647265737326506f7274";
    //System.err.println(packet);
    //System.out.println(packet.toString());

    byte[] payload = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0x41, (byte) 0x67, (byte) 0x65, (byte) 0x74, (byte) 0x41,
            (byte) 0x64, (byte) 0x64, (byte) 0x72, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x26,
            (byte) 0x50, (byte) 0x6f, (byte) 0x72, (byte) 0x74 };

    ByteBuf data = packet.content().readBytes(packet.content().readableBytes());
    System.out.println(data.array().length);

    byte[] header = { (byte) 0xff, (byte) 0x52 };

    System.out.println(Utils.byteArray2Hex(data.array()));
    if (Utils.byteArray2Hex(data.array()).equals(Utils.byteArray2Hex(payload))) {
        System.out.println("Got login of client, sending multicast group address (header+multicastgroup)");
        ctx.write(new DatagramPacket(
                Unpooled.copiedBuffer(ArrayUtils.addAll(header, multicastgroup.getBytes())), packet.sender()));
    }

}

From source file:cloudeventbus.codec.Decoder.java

License:Open Source License

@Override
public Frame decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    in.markReaderIndex();//w w  w .  ja va  2s .  co  m
    final int frameLength = indexOf(in, Codec.DELIMITER);
    // Frame hasn't been fully read yet.
    if (frameLength < 0) {
        if (in.readableBytes() > maxMessageSize) {
            throw new TooLongFrameException("Frame exceeds maximum size");
        }
        in.resetReaderIndex();
        return null;
    }
    // Empty frame, discard and continue decoding
    if (frameLength == 0) {
        in.skipBytes(Codec.DELIMITER.length);
        return decode(ctx, in);
    }
    if (frameLength > maxMessageSize) {
        throw new TooLongFrameException("Frame exceeds maximum size");
    }
    final String command = in.readBytes(frameLength).toString(CharsetUtil.UTF_8);
    in.skipBytes(Codec.DELIMITER.length);
    final String[] parts = command.split("\\s+");
    final char frameTypeChar = parts[0].charAt(0);
    final FrameType frameType = FrameType.getFrameType(frameTypeChar);
    if (frameType == null) {
        throw new DecodingException("Invalid frame type " + frameTypeChar);
    }
    LOGGER.debug("Decoding frame of type {}", frameType);
    final int argumentsLength = parts.length - 1;
    switch (frameType) {
    case AUTH_RESPONSE:
        assertArgumentsLength(3, argumentsLength, "authentication response");
        final CertificateChain certificates = new CertificateChain();
        final byte[] rawCertificates = Base64.decodeBase64(parts[1].getBytes());
        CertificateStoreLoader.load(new ByteArrayInputStream(rawCertificates), certificates);
        final byte[] salt = Base64.decodeBase64(parts[2]);
        final byte[] digitalSignature = Base64.decodeBase64(parts[3]);
        return new AuthenticationResponseFrame(certificates, salt, digitalSignature);
    case AUTHENTICATE:
        assertArgumentsLength(1, argumentsLength, "authentication request");
        final byte[] challenge = Base64.decodeBase64(parts[1]);
        return new AuthenticationRequestFrame(challenge);
    case ERROR:
        if (parts.length == 0) {
            throw new DecodingException("Error is missing error code");
        }
        final Integer errorNumber = Integer.valueOf(parts[1]);
        final ErrorFrame.Code errorCode = ErrorFrame.Code.lookupCode(errorNumber);
        int messageIndex = 1;
        messageIndex = skipWhiteSpace(messageIndex, command);
        while (messageIndex < command.length() && Character.isDigit(command.charAt(messageIndex))) {
            messageIndex++;
        }
        messageIndex = skipWhiteSpace(messageIndex, command);
        final String errorMessage = command.substring(messageIndex).trim();
        if (errorMessage.length() > 0) {
            return new ErrorFrame(errorCode, errorMessage);
        } else {
            return new ErrorFrame(errorCode);
        }
    case GREETING:
        assertArgumentsLength(3, argumentsLength, "greeting");
        final int version = Integer.valueOf(parts[1]);
        final String agent = parts[2];
        final long id = Long.valueOf(parts[3]);
        return new GreetingFrame(version, agent, id);
    case PING:
        return PingFrame.PING;
    case PONG:
        return PongFrame.PONG;
    case PUBLISH:
        if (argumentsLength < 2 || argumentsLength > 3) {
            throw new DecodingException(
                    "Expected message frame to have 2 or 3 arguments. It has " + argumentsLength + ".");
        }
        final String messageSubject = parts[1];
        final String replySubject;
        final Integer messageLength;
        if (parts.length == 3) {
            replySubject = null;
            messageLength = Integer.valueOf(parts[2]);
        } else {
            replySubject = parts[2];
            messageLength = Integer.valueOf(parts[3]);
        }
        if (in.readableBytes() < messageLength + Codec.DELIMITER.length) {
            // If we haven't received the entire message body (plus the CRLF), wait until it arrives.
            in.resetReaderIndex();
            return null;
        }
        final ByteBuf messageBytes = in.readBytes(messageLength);
        final String messageBody = new String(messageBytes.array(), CharsetUtil.UTF_8);
        in.skipBytes(Codec.DELIMITER.length); // Ignore the CRLF after the message body.
        return new PublishFrame(new Subject(messageSubject),
                replySubject == null ? null : new Subject(replySubject), messageBody);
    case SERVER_READY:
        return ServerReadyFrame.SERVER_READY;
    case SUBSCRIBE:
        assertArgumentsLength(1, argumentsLength, "subscribe");
        return new SubscribeFrame(new Subject(parts[1]));
    case UNSUBSCRIBE:
        assertArgumentsLength(1, argumentsLength, "unsubscribe");
        return new UnsubscribeFrame(new Subject(parts[1]));
    default:
        throw new DecodingException("Unknown frame type " + frameType);
    }
}

From source file:cn.liutils.api.player.ControlData.java

License:Open Source License

@Override
public void saveNBTData(NBTTagCompound tag) {
    ByteBuf buf = Unpooled.buffer();
    toBytes(buf);
    tag.setByteArray(IDENTIFIER, buf.array());
}

From source file:cn.wantedonline.puppy.httpserver.component.HttpRequest.java

License:Apache License

public String getContentString(Charset charset) {
    ByteBuf content = content();
    return new String(content.array(), charset);
}