Example usage for io.netty.buffer ByteBuf writeLong

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

Introduction

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

Prototype

public abstract ByteBuf writeLong(long value);

Source Link

Document

Sets the specified 64-bit long integer at the current writerIndex and increases the writerIndex by 8 in this buffer.

Usage

From source file:org.dcache.xrootd.core.XrootdSigverDecoder.java

License:Open Source License

/**
 *  A signature consists of a SHA-256 hash of
 *    1. an unsigned 64-bit sequence number,
 *    2. the request header, and/*from   w  w  w .j a v a2  s.com*/
 *    3. the request payload,
 *  in that exact order.
 *
 *  In this case, 2 + 3 are given in order by the frame buffer, which
 *  contains the raw bytes of the request.
 */
private byte[] generateHash(long seqno, byte[] payload, ChannelHandlerContext ctx) throws XrootdException {
    ByteBuf buffer = ctx.alloc().buffer(8 + payload.length);
    try {
        buffer.writeLong(seqno);
        buffer.writeBytes(payload);
        byte[] contents = new byte[buffer.readableBytes()];
        buffer.getBytes(0, contents);

        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return digest.digest(contents);
    } catch (NoSuchAlgorithmException e) {
        throw new XrootdException(kXR_error, e.toString());
    } finally {
        buffer.release();
    }
}

From source file:org.dcache.xrootd.protocol.messages.ReadVResponse.java

License:Open Source License

@Override
public void writeTo(ChannelHandlerContext ctx, ChannelPromise promise) {
    checkState(refCnt() > 0);//w  ww . ja v a2s  .  c  o  m

    CompositeByteBuf buffer = ctx.alloc().compositeBuffer(2 * length + 1);

    ByteBuf header = ctx.alloc().buffer(8);
    header.writeShort(request.getStreamId());
    header.writeShort(stat);
    header.writeInt(getDataLength());
    buffer.addComponent(header);

    for (int i = 0; i < length; i++) {
        header = ctx.alloc().buffer(READ_LIST_HEADER_SIZE);
        header.writeInt(requests[index + i].getFileHandle());
        header.writeInt(data[index + i].readableBytes());
        header.writeLong(requests[index + i].getOffset());
        buffer.addComponent(header);
        buffer.addComponent(data[index + i].retain());
    }

    buffer.writerIndex(buffer.capacity());
    ctx.write(buffer, promise);

    release();
}

From source file:org.dcache.xrootd.tpc.protocol.messages.OutboundCloseRequest.java

License:Open Source License

@Override
protected void getParams(ByteBuf buffer) {
    buffer.writeInt(fhandle);/*from  ww  w  .ja  v  a 2s . co  m*/
    /*
     * The original open was read-only,
     * no need to enforce file size on the server end
     */
    buffer.writeLong(0);
    buffer.writeInt(0);
    buffer.writeInt(0);
}

From source file:org.dcache.xrootd.tpc.protocol.messages.OutboundReadRequest.java

License:Open Source License

@Override
protected void getParams(ByteBuf buffer) {
    buffer.writeInt(fhandle);
    buffer.writeLong(offset);
    buffer.writeInt(rlen);
    buffer.writeInt(0);
}

From source file:org.dcache.xrootd.tpc.protocol.messages.OutboundSigverRequest.java

License:Open Source License

@Override
protected void getParams(ByteBuf buffer) {
    buffer.writeShort(expectrid);//  w  w w .  ja  va  2 s .  com
    buffer.writeByte(0);
    buffer.writeByte(0); // we do not send write requests, so no data = 0x0
    buffer.writeLong(seqno);
    buffer.writeByte(kXR_SHA256);
    buffer.writeBytes(RESERVED);
    buffer.writeInt(signature.length);
    buffer.writeBytes(signature);
}

From source file:org.dcache.xrootd.tpc.protocol.messages.OutboundSigverRequest.java

License:Open Source License

/**
 *  A signature consists of a SHA-256 hash of
 *    1. an unsigned 64-bit sequence number,
 *    2. the request header, and/*  w  w  w  .  j  a  v a 2  s .c  o m*/
 *    3. the request payload,
 *  in that exact order.
 */
private byte[] getSignature(AbstractXrootdOutboundRequest request, ChannelHandlerContext ctx)
        throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    ByteBuf buffer = ctx.alloc().buffer(12 + request.getParamsLen());
    try {
        buffer.writeLong(seqno);
        request.writeToBuffer(buffer);
        byte[] contents = new byte[buffer.readableBytes()];
        buffer.getBytes(0, contents);
        return digest.digest(contents);
    } finally {
        buffer.release();
    }
}

From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.PacketDecodingTest.java

License:Apache License

static ByteBuf createNumericPartBuffer(Long value, PartType partType) {
    ByteBuf buffer = Unpooled.buffer();
    buffer.writeShort(partType.getId());
    buffer.writeShort(12);/*ww w  .  j a v a  2  s.co  m*/
    buffer.writeLong(value);
    return buffer;
}

From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.PacketDecodingTest.java

License:Apache License

static ByteBuf createValuesPartBuffer(Values values) {
    List<Number> data = values.getData();
    ListIterator<Number> dataIterator = data.listIterator();
    List<DataType> dataTypes = values.getDataTypes();
    ListIterator<DataType> dataTypeIterator = dataTypes.listIterator();

    ByteBuf payloadBuffer = Unpooled.buffer();

    while (dataTypeIterator.hasNext()) {
        payloadBuffer.writeByte(dataTypeIterator.next().getId());
    }/*from w  w  w. ja  v a  2  s .c  om*/

    dataTypeIterator = dataTypes.listIterator();
    while (dataIterator.hasNext()) {
        DataType dataType = dataTypeIterator.next();
        Number number = dataIterator.next();
        switch (dataType) {
        case COUNTER:
        case ABSOLUTE:
            BigInteger bigInteger = (BigInteger) number;
            payloadBuffer.writeBytes(bigInteger.toByteArray());
            break;
        case DERIVE:
            payloadBuffer.writeLong((Long) number);
            break;
        case GAUGE:
            payloadBuffer.writeLong(ByteBufUtil.swapLong(Double.doubleToLongBits((Double) number)));
            break;
        default:
            fail("Unknown data type: " + dataType);
        }
    }

    ByteBuf headerBuffer = Unpooled.buffer();
    headerBuffer.writeShort(VALUES.getId());
    headerBuffer.writeShort(6 + payloadBuffer.writerIndex());
    headerBuffer.writeShort(data.size());

    ByteBuf buffer = Unpooled.buffer();
    buffer.writeBytes(headerBuffer.duplicate()).writeBytes(payloadBuffer.duplicate());
    return buffer;
}

From source file:org.helios.octo.server.streams.StreamOutputAdapter.java

License:Open Source License

@Override
public void write(ChannelHandlerContext ctx, MessageList<Object> msgs, ChannelPromise promise) {
    MessageList<ByteBuf> buffers = msgs.cast();
    ByteBuf acc = ctx.attr(ACC).get();/*w w  w .  j a  v a 2 s . c om*/
    if (acc == null) {
        acc = Unpooled.buffer();
        ctx.attr(ACC).set(acc);
    }
    for (ByteBuf b : buffers) {
        acc.writeBytes(b);
    }
    msgs.releaseAllAndRecycle();
    while (true) {
        int loc = acc.bytesBefore(ByteBufIndexFinder.LF);
        if (loc == -1)
            break;
        ByteBuf buf = Unpooled.buffer(10);
        buf.writeLong(System.nanoTime()).writeByte(0).writeByte(streamType);
        //.writeBytes(acc.readBytes(loc+1));
        ChannelHandler se = ctx.pipeline().remove("stringEncoder");
        ctx.write(buf);
        ctx.pipeline().addAfter("out", "stringEncoder", se);
        byte[] bytes = new byte[loc];
        acc.readBytes(bytes);
        String s = new String(bytes);
        log.info("Writing out [" + s + "]");
        ctx.write(s + "\n");
    }
}

From source file:org.onlab.netty.MessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext context, InternalMessage message, ByteBuf out) throws Exception {

    // write message id
    out.writeLong(message.id());

    Endpoint sender = message.sender();

    IpAddress senderIp = sender.host();/*w w w . j  a v  a2 s  .  co m*/
    if (senderIp.version() == Version.INET) {
        out.writeByte(0);
    } else {
        out.writeByte(1);
    }
    out.writeBytes(senderIp.toOctets());

    // write sender port
    out.writeInt(sender.port());

    byte[] messageTypeBytes = message.type().getBytes(Charsets.UTF_8);

    // write length of message type
    out.writeInt(messageTypeBytes.length);

    // write message type bytes
    out.writeBytes(messageTypeBytes);

    byte[] payload = message.payload();

    // write payload length
    out.writeInt(payload.length);

    // write payload.
    out.writeBytes(payload);
}