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:com.github.mrstampy.pprspray.core.streamer.MediaStreamType.java

License:Open Source License

private void setEomBytes() {
    int eomOrdinal = Integer.MAX_VALUE - ordinal();

    ByteBuf buf = Unpooled.buffer(4);
    buf.writeInt(eomOrdinal);/*  www  .j av a2 s.  c  om*/
    eomBytes = buf.array();
}

From source file:com.github.mrstampy.pprspray.core.streamer.MediaStreamType.java

License:Open Source License

private void setOrdinalBytes() {
    ByteBuf buf = Unpooled.buffer(4);
    buf.writeInt(ordinal());
    ordinalBytes = buf.array();
}

From source file:com.github.mrstampy.pprspray.core.streamer.negotiation.AcceptingNegotationSubscriber.java

License:Open Source License

@Override
protected void negotiationRequestedImpl(NegotiationChunk event) {
    KiSyChannel channel = MediaStreamerUtils.getChannel(event.getLocal());

    createReceiver(event);//from  w  w  w.  j  a  v  a 2s .  c  o  m

    registerMediaProcessor(event);

    ByteBuf ack = NegotiationMessageUtils.getNegotiationAckMessage(event.getMediaHash(), true);

    channel.send(ack.array(), event.getRemote());

    postNegotiationEvent(true, event);
}

From source file:com.kaijin.AdvPowerMan.CommonProxy.java

License:Open Source License

public void onPacketDataClient(ByteBuf source, EntityPlayer entityPlayer) {
    DataInputStream stream = new DataInputStream(
            new ByteArrayInputStream(Arrays.copyOfRange(source.array(), 1, source.array().length)));

    // Determine packet type and coordinates of affected tile entity
    int packetType = -1;
    int x;//ww w. j  av a2s.  c om
    int y;
    int z;
    try {
        packetType = stream.readInt();
        x = stream.readInt();
        y = stream.readInt();
        z = stream.readInt();
    } catch (IOException e) {
        FMLLog.getLogger().info("[AdvancedPowerManagement] " + "Failed to read packet from client. (Details: "
                + e.toString() + ")");
        return;
    }

    if (packetType == 0) {
        Exception e;
        try {
            World world = entityPlayer.worldObj;
            TileEntity tile = world.getTileEntity(x, y, z);

            int buttonID = stream.readInt();

            ((TECommon) tile).receiveGuiButton(buttonID);
            return;
        } catch (ClassCastException ex) {
            e = ex;
        } catch (NullPointerException ex) {
            e = ex;
        } catch (IOException ex) {
            e = ex;
        }

        FMLLog.getLogger()
                .info("[AdvancedPowerManagement] " + "Server received GUI button packet for " + x + ", " + y
                        + ", " + z + " but couldn't deliver to tile entity. (Details: " + e.toString() + ")");
        return;
    }
}

From source file:com.linecorp.armeria.server.thrift.TByteBufTransport.java

License:Apache License

@Nullable
@Override/*  w  w w .  j a va2  s  . com*/
public byte[] getBuffer() {
    final ByteBuf buf = this.buf;
    if (!buf.hasArray()) {
        return null;
    } else {
        return buf.array();
    }
}

From source file:com.minetats.mw.NamedPipe.java

License:Apache License

@Override
public ByteBuf readChunk(ByteBufAllocator bba) throws Exception {
    chunks++;//from   w  ww.j  a  v  a  2s.co m
    ByteBuf buf = bba.heapBuffer(chunkSize);
    boolean release = false;
    int read = 0;
    try {
        do {
            buf.writerIndex(buf.writerIndex() + read);
            read = file.read(buf.array(), buf.arrayOffset() + read, chunkSize - read);
        } while (read > 0);
        int index = buf.writerIndex() - 1;
        if (buf.getByte(index) == '\n' && buf.getByte(index - 1) == '\n') {
            endOfInput = true;
            System.out.println("endOfInput=" + endOfInput + ", read " + chunks + " chunks");
        }
        return buf;
    } finally {
        if (release) {
            buf.release();
        }
    }
}

From source file:com.necla.simba.server.gateway.server.frontend.FrontendFrameDecoder.java

License:Apache License

private ByteBuf decompress(ChannelHandlerContext ctx, ByteBuf frame) throws Exception {
    int readableBytes = frame.readableBytes();
    if (frame.hasArray()) {
        inflater.setInput(frame.array(), 0, readableBytes);
    } else {// w  w  w . j  a  v  a  2 s. c o m
        byte[] array = new byte[frame.readableBytes()];
        frame.getBytes(0, array);
        inflater.setInput(array);
    }
    int totalLength = 0;
    List<ByteBuf> all = new LinkedList<ByteBuf>();
    int multiplier = 2;
    alldone: while (true) {

        int maxOutputLength = inflater.getRemaining() * multiplier;
        // multiplier keeps increasing, so we will keep picking
        // larger and larger buffers the more times we have to loop
        // around, i.e., the more we realize that the data was very
        // heavily compressed, the larger our buffers are going to be.
        multiplier += 1;
        ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength);
        while (!inflater.needsInput()) {
            byte[] outArray = decompressed.array();
            int outIndex = decompressed.arrayOffset() + decompressed.writerIndex();
            int length = outArray.length - outIndex;
            if (length == 0)
                break;
            try {
                //LOG.debug("here1");
                int outputLength = inflater.inflate(outArray, outIndex, length);
                totalLength += outputLength;
                //LOG.debug("here2");

                if (outputLength > 0)
                    decompressed.writerIndex(decompressed.writerIndex() + outputLength);
            } catch (DataFormatException e) {
                throw new Exception("Could not inflate" + e.getMessage());
            }
            if (inflater.finished()) {
                all.add(decompressed);
                break alldone;
            }

        }
        all.add(decompressed);
    }
    inflater.reset();
    if (all.size() == 1)
        return all.get(0);
    else {
        ByteBuf allData = ctx.alloc().heapBuffer(totalLength);
        for (ByteBuf b : all) {
            //LOG.debug("capacity=" + allData.capacity());
            allData.writeBytes(b);
            b.release();
        }
        return allData;
    }

}

From source file:com.neoba.DsyncserverHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpContent con) throws Exception {

    ByteBuf in = (ByteBuf) con.copy().content();
    if (in.array().length == 0) {
        String time = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())).toString();
        respondAndFlush(Unpooled.wrappedBuffer(
                ("<h1>Neoba Experimental Server 1</h1>" + time + "<br>Status:OK<br>Please use the client")
                        .getBytes()),/*from  ww w  . j a va  2s  .  com*/
                ctx, true);
        logger.info("Browser request - responded");
        return;
    }
    //System.out.println(new String(in.array()));
    msgcount += 1;
    logger.info("Message Received from ip " + ctx.channel().remoteAddress() + ":" + msgcount);
    ConsoleUtils.printhex("request", in.array(), in.array().length);
    MessageInterpreter mi = new MessageInterpreter(ctx, in);
    ByteBuf reply = mi.generateReply();
    ConsoleUtils.printhex("response", reply.array(), reply.array().length);
    respondAndFlush(reply, ctx, false);
    logger.info("Message Responded :" + msgcount);

}

From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java

License:Apache License

/**
 * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library
 * which is faster as it computes using hardware machine instruction else it computes using crc32c algo.
 *
 * @param payload//from   w  w w  . ja  v a2s.c o  m
 * @return
 */
public static int computeChecksum(ByteBuf payload) {
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        return CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes());
    } else if (payload.hasArray()) {
        return CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
                payload.readableBytes());
    } else {
        return CRC32C_HASH.calculate(payload.nioBuffer());
    }
}

From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java

License:Apache License

/**
 * Computes incremental checksum with input previousChecksum and input payload
 *
 * @param previousChecksum : previously computed checksum
 * @param payload/*from w  w  w  .j  av a2s  .  c  o m*/
 * @return
 */
public static int resumeChecksum(int previousChecksum, ByteBuf payload) {
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        return CRC32C_HASH.resume(previousChecksum, payload.memoryAddress() + payload.readerIndex(),
                payload.readableBytes());
    } else if (payload.hasArray()) {
        return CRC32C_HASH.resume(previousChecksum, payload.array(),
                payload.arrayOffset() + payload.readerIndex(), payload.readableBytes());
    } else {
        return CRC32C_HASH.resume(previousChecksum, payload.nioBuffer());
    }
}