Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

From source file:cubicchunks.network.PacketColumn.java

License:MIT License

@Override
public void fromBytes(ByteBuf buf) {
    this.chunkPos = new ChunkPos(buf.readInt(), buf.readInt());
    this.data = new byte[buf.readInt()];
    buf.readBytes(this.data);
}

From source file:cubicchunks.network.PacketCube.java

License:MIT License

@Override
public void fromBytes(ByteBuf buf) {
    this.cubePos = new CubePos(buf.readInt(), buf.readInt(), buf.readInt());
    this.data = new byte[buf.readInt()];
    buf.readBytes(this.data);
    int numTiles = buf.readInt();
    this.tileEntityTags = new ArrayList<>(numTiles);
    for (int i = 0; i < numTiles; i++) {
        this.tileEntityTags.add(ByteBufUtils.readTag(buf));
    }// w  ww  .j a  va2s .com
}

From source file:dan200.qcraft.shared.QCraftPacket.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buffer) {
    packetType = buffer.readByte();/*from   w  w  w. j  av  a  2 s  .  com*/
    byte nString = buffer.readByte();
    byte nInt = buffer.readByte();
    int nByte = buffer.readInt();
    if (nString == 0) {
        dataString = null;
    } else {
        dataString = new String[nString];
        for (int k = 0; k < nString; k++) {
            if (buffer.readBoolean()) {
                int len = buffer.readInt();
                byte[] b = new byte[len];
                buffer.readBytes(b);
                try {
                    dataString[k] = new String(b, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    dataString[k] = null;
                }
            }
        }
    }
    if (nInt == 0) {
        dataInt = null;
    } else {
        dataInt = new int[nInt];
        for (int k = 0; k < nInt; k++) {
            dataInt[k] = buffer.readInt();
        }
    }
    if (nByte == 0) {
        dataByte = null;
    } else {
        dataByte = new byte[nByte][];
        for (int k = 0; k < nByte; k++) {
            int length = buffer.readInt();
            if (length > 0) {
                dataByte[k] = new byte[length];
                buffer.getBytes(buffer.readerIndex(), dataByte[k]);
            }
        }
    }
}

From source file:dbseer.middleware.packet.MiddlewarePacketDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    if (buf.readableBytes() < 8) {
        Log.debug(this.getClass().getCanonicalName(), "buf less than 8 bytes");
        return;//from   w w w . j a  v a  2  s.c o m
    }

    buf.markReaderIndex();
    int header = buf.readInt();
    int length = buf.readInt();

    if (buf.readableBytes() < length) {
        buf.resetReaderIndex();
        Log.debug(this.getClass().getCanonicalName(),
                "readable bytes less than length = " + length + " and header = " + header);
        return;
    }
    String log = "";
    Log.debug(String.format("len = %d, readable = %d", length, buf.readableBytes()));

    if (length > 0) {
        byte[] readBuf = new byte[length];
        buf.readBytes(readBuf);
        log = new String(readBuf, "UTF-8");
    }

    out.add(new MiddlewarePacket(header, length, log));
}

From source file:de.cubeisland.engine.core.webapi.HttpRequestHandler.java

License:Open Source License

private void handleHttpRequest(ChannelHandlerContext context, FullHttpRequest message, String path,
        Parameters params, User authUser) {
    ApiHandler handler = this.server.getApiHandler(path);
    if (handler == null) {
        this.error(context, ROUTE_NOT_FOUND);
        return;//from ww  w  . ja  va  2s . co m
    }

    JsonNode data = null;
    ByteBuf requestContent = message.content();
    if (!requestContent.equals(EMPTY_BUFFER)) {
        try {
            byte[] bytes = new byte[requestContent.readableBytes()];
            requestContent.readBytes(bytes);
            data = this.objectMapper.readTree(bytes);
        } catch (Exception ex) {
            this.log.debug(ex, "Failed to parse the request body!");
            this.error(context, MALFORMED_DATA);
            return;
        }
    }
    final RequestMethod method = RequestMethod.getByName(message.getMethod().name());

    ApiRequest apiRequest = new ApiRequest((InetSocketAddress) context.channel().remoteAddress(), method,
            params, message.headers(), data, authUser);
    try {
        this.success(context, handler.execute(apiRequest));
    } catch (ApiRequestException e) {
        this.error(context, REQUEST_EXCEPTION, e);
    } catch (Throwable t) {
        this.error(context, UNKNOWN_ERROR);
    }
}

From source file:de.gandev.modjn.entity.func.request.WriteMultipleCoilsRequest.java

License:Apache License

@Override
public void decode(ByteBuf data) {
    super.decode(data);

    byteCount = data.readUnsignedByte();

    byte[] coils = new byte[byteCount];
    data.readBytes(coils);

    outputsValue = BitSet.valueOf(coils);
}

From source file:de.gandev.modjn.entity.func.response.ReadCoilsResponse.java

License:Apache License

@Override
public void decode(ByteBuf data) {
    byteCount = data.readUnsignedByte();

    byte[] coils = new byte[byteCount];
    data.readBytes(coils);

    coilStatus = BitSet.valueOf(coils);
}

From source file:de.gandev.modjn.entity.func.response.ReadDiscreteInputsResponse.java

License:Apache License

@Override
public void decode(ByteBuf data) {
    byteCount = data.readUnsignedByte();

    byte[] inputs = new byte[byteCount];
    data.readBytes(inputs);

    inputStatus = BitSet.valueOf(inputs);
}

From source file:de.jackwhite20.cascade.shared.protocol.packet.Packet.java

License:Open Source License

public String readString(ByteBuf byteBuf) throws UnsupportedEncodingException {

    byte[] bytes = new byte[byteBuf.readShort()];
    byteBuf.readBytes(bytes);

    return new String(bytes, "utf-8");
}

From source file:de.jackwhite20.comix.network.Protocol.java

License:Open Source License

public static String readString(ByteBuf buf) {
    int len = readVarInt(buf);
    byte[] b = new byte[len];
    buf.readBytes(b);
    return new String(b, UTF_8);
}