Example usage for io.netty.buffer ByteBuf isWritable

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

Introduction

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

Prototype

public abstract boolean isWritable();

Source Link

Document

Returns true if and only if (this.capacity - this.writerIndex) is greater than 0 .

Usage

From source file:com.friz.owari.network.codec.ExchangeEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ExchangeTransferEvent msg, ByteBuf out) throws Exception {
    if (!out.isWritable())
        return;/*from  www .j av  a  2 s. co m*/

    final PublicKey key = msg.getPublicKey();

    byte[] enc = key.getEncoded();

    out.writeInt(enc.length);
    out.writeBytes(enc);
}

From source file:com.friz.owari.network.codec.HandshakeEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, HandshakeEvent msg, ByteBuf out) throws Exception {
    if (!out.isWritable())
        return;//from   w  w w.  j  av  a  2 s  .co  m

    out.writeShort(msg.getStatus());
}

From source file:com.friz.owari.network.codec.PatchInitEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, PatchInitEvent msg, ByteBuf out) throws Exception {
    if (!out.isWritable())
        return;//ww w  . j a va2  s.c  om

    out.writeInt(msg.getCount());
    out.writeLong(msg.getSize());
}

From source file:com.tesora.dve.sql.LargeMaxPktTest.java

License:Open Source License

@Test
public void testComQueryMessageContinuationOverlap() throws Exception {
    int defaultMaxPacket = 0xFFFFFF;
    int payloadSize = (defaultMaxPacket * 4) + (4 * 40); //four full packets and a little change, divisible by 4.
    ByteBuf source = Unpooled.buffer(payloadSize, payloadSize);
    Random rand = new Random(239873L);
    while (source.isWritable())
        source.writeInt(rand.nextInt());
    Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer");

    ByteBuf dest = Unpooled.buffer(payloadSize);

    MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array());
    Packet.encodeFullMessage((byte) 0, outboundMessage, dest);

    int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4;
    Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata,
            "Number of bytes in destination buffer is wrong");

    Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum");
    Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero");
    Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER,
            "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER");

    ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1));
    Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1));

}

From source file:com.tesora.dve.sql.LargeMaxPktTest.java

License:Open Source License

@Test
public void testComQueryMessageContinuationExact() throws Exception {
    int defaultMaxPacket = 0xFFFFFF;
    int payloadSize = (defaultMaxPacket * 4); //four full packets exactly, requires an empty trailing packet.
    ByteBuf source = Unpooled.buffer(payloadSize, payloadSize);
    Random rand = new Random(239873L);
    while (source.isWritable())
        source.writeInt(rand.nextInt());
    Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer");

    ByteBuf dest = Unpooled.buffer(payloadSize);

    MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array());
    Packet.encodeFullMessage((byte) 0, outboundMessage, dest);

    int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4;//last packet has zero length payload
    Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata,
            "Number of bytes in destination buffer is wrong");

    Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum");
    Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero");
    Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER,
            "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER");

    ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1));
    Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1));

}

From source file:io.gatling.http.client.body.multipart.impl.MultipartChunkedInput.java

License:Apache License

private ChunkedInputState copyInto(ByteBuf target) throws IOException {

    if (done) {/*  w ww  .  j  a va2 s  .c o m*/
        return ChunkedInputState.STOP;
    }

    while (target.isWritable() && !done) {
        PartImpl currentPart = parts.get(currentPartIndex);
        currentPart.copyInto(target);

        if (currentPart.getState() == PartImplState.DONE) {
            currentPartIndex++;
            if (currentPartIndex == parts.size()) {
                done = true;
            }
        }
    }

    return ChunkedInputState.CONTINUE;
}

From source file:io.grpc.alts.internal.AltsChannelCrypter.java

License:Apache License

@Override
public void encrypt(ByteBuf outBuf, List<ByteBuf> plainBufs) throws GeneralSecurityException {
    checkArgument(outBuf.nioBufferCount() == 1);
    // Copy plaintext buffers into outBuf for in-place encryption on single direct buffer.
    ByteBuf plainBuf = outBuf.slice(outBuf.writerIndex(), outBuf.writableBytes());
    plainBuf.writerIndex(0);/*from   w  w  w .  ja  va2  s. c om*/
    for (ByteBuf inBuf : plainBufs) {
        plainBuf.writeBytes(inBuf);
    }

    verify(outBuf.writableBytes() == plainBuf.readableBytes() + TAG_LENGTH);
    ByteBuffer out = outBuf.internalNioBuffer(outBuf.writerIndex(), outBuf.writableBytes());
    ByteBuffer plain = out.duplicate();
    plain.limit(out.limit() - TAG_LENGTH);

    byte[] counter = incrementOutCounter();
    int outPosition = out.position();
    aeadCrypter.encrypt(out, plain, counter);
    int bytesWritten = out.position() - outPosition;
    outBuf.writerIndex(outBuf.writerIndex() + bytesWritten);
    verify(!outBuf.isWritable());
}

From source file:io.grpc.netty.NettyHandlerTestBase.java

License:Apache License

@Test
public void windowUpdateMatchesTarget() throws Exception {
    manualSetUp();/* ww w . j ava 2s.  c  o m*/
    Http2Stream connectionStream = connection().connectionStream();
    Http2LocalFlowController localFlowController = connection().local().flowController();
    makeStream();
    AbstractNettyHandler handler = (AbstractNettyHandler) handler();
    handler.setAutoTuneFlowControl(true);

    ByteBuf data = ctx().alloc().buffer(1024);
    while (data.isWritable()) {
        data.writeLong(1111);
    }
    int length = data.readableBytes();
    ByteBuf frame = dataFrame(3, false, data.copy());
    channelRead(frame);
    int accumulator = length;
    // 40 is arbitrary, any number large enough to trigger a window update would work
    for (int i = 0; i < 40; i++) {
        channelRead(dataFrame(3, false, data.copy()));
        accumulator += length;
    }
    long pingData = handler.flowControlPing().payload();
    channelRead(pingFrame(true, pingData));

    assertEquals(accumulator, handler.flowControlPing().getDataSincePing());
    assertEquals(2 * accumulator, localFlowController.initialWindowSize(connectionStream));
}

From source file:org.apache.bookkeeper.bookie.CompactionTest.java

License:Apache License

private ByteBuf genEntry(long ledger, long entry, int size) {
    ByteBuf bb = Unpooled.buffer(size);
    bb.writeLong(ledger);/* w  w  w .j a  v a 2s  .  c  o m*/
    bb.writeLong(entry);
    while (bb.isWritable()) {
        bb.writeByte((byte) 0xFF);
    }
    return bb;
}

From source file:org.asynchttpclient.request.body.generator.PushBody.java

License:Open Source License

private BodyState readNextChunk(ByteBuf target) throws IOException {
    BodyState res = BodyState.SUSPEND;/*from   ww w.  j a  v a2 s.c o m*/
    while (target.isWritable() && state != BodyState.STOP) {
        BodyChunk nextChunk = queue.peek();
        if (nextChunk == null) {
            // Nothing in the queue. suspend stream if nothing was read. (reads == 0)
            return res;
        } else if (!nextChunk.buffer.hasRemaining() && !nextChunk.last) {
            // skip empty buffers
            queue.remove();
        } else {
            res = BodyState.CONTINUE;
            readChunk(target, nextChunk);
        }
    }
    return res;
}