Example usage for io.netty.buffer ByteBuf writableBytes

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

Introduction

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

Prototype

public abstract int writableBytes();

Source Link

Document

Returns the number of writable bytes which is equal to (this.capacity - this.writerIndex) .

Usage

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

License:Apache License

@Override
public synchronized int read(ByteBuf dest, long pos, int length) throws IOException {
    long prevPos = pos;
    while (length > 0) {
        // check if it is in the write buffer
        if (writeBuffer != null && writeBufferStartPosition.get() <= pos) {
            int positionInBuffer = (int) (pos - writeBufferStartPosition.get());
            int bytesToCopy = Math.min(writeBuffer.writerIndex() - positionInBuffer, dest.writableBytes());

            if (bytesToCopy == 0) {
                throw new IOException("Read past EOF");
            }/*from   w  w w .  ja v a  2  s .c  o  m*/

            dest.writeBytes(writeBuffer, positionInBuffer, bytesToCopy);
            pos += bytesToCopy;
            length -= bytesToCopy;
        } else if (writeBuffer == null && writeBufferStartPosition.get() <= pos) {
            // here we reach the end
            break;
            // first check if there is anything we can grab from the readBuffer
        } else if (readBufferStartPosition <= pos && pos < readBufferStartPosition + readBuffer.writerIndex()) {
            int positionInBuffer = (int) (pos - readBufferStartPosition);
            int bytesToCopy = Math.min(readBuffer.writerIndex() - positionInBuffer, dest.writableBytes());
            dest.writeBytes(readBuffer, positionInBuffer, bytesToCopy);
            pos += bytesToCopy;
            length -= bytesToCopy;
            // let's read it
        } else {
            readBufferStartPosition = pos;

            int readBytes = fileChannel.read(readBuffer.internalNioBuffer(0, readCapacity),
                    readBufferStartPosition);
            if (readBytes <= 0) {
                throw new IOException("Reading from filechannel returned a non-positive value. Short read.");
            }
            readBuffer.writerIndex(readBytes);
        }
    }
    return (int) (pos - prevPos);
}

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

License:Apache License

/**
 * Read as many bytes into dest as dest.capacity() starting at position pos in the
 * FileChannel. This function can read from the buffer or the file channel
 * depending on the implementation..//from www.j av  a  2 s. c  o  m
 * @param dest
 * @param pos
 * @return The total number of bytes read.
 *         -1 if the given position is greater than or equal to the file's current size.
 * @throws IOException if I/O error occurs
 */
public int read(ByteBuf dest, long pos) throws IOException {
    return read(dest, pos, dest.writableBytes());
}

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

License:Apache License

/**
 * If the log id of current writable channel is the same as entryLogId and the position
 * we want to read might end up reading from a position in the write buffer of the
 * buffered channel, route this read to the current logChannel. Else,
 * read from the BufferedReadChannel that is provided.
 * @param entryLogId//from  www  . j  a  v a  2 s .co m
 * @param channel
 * @param buff remaining() on this bytebuffer tells us the last position that we
 *             expect to read.
 * @param pos The starting position from where we want to read.
 * @return
 */
private int readFromLogChannel(long entryLogId, BufferedReadChannel channel, ByteBuf buff, long pos)
        throws IOException {
    BufferedLogChannel bc = entryLogManager.getCurrentLogIfPresent(entryLogId);
    if (null != bc) {
        synchronized (bc) {
            if (pos + buff.writableBytes() >= bc.getFileChannelPosition()) {
                return bc.read(buff, pos);
            }
        }
    }
    return channel.read(buff, pos);
}

From source file:org.apache.bookkeeper.tools.perf.table.IncrementTask.java

License:Apache License

void incKey(long i) {
    ByteBuf keyBuf = PooledByteBufAllocator.DEFAULT.heapBuffer(flags.keySize);
    getKey(keyBuf, i, keyRange);// w  ww.  jav  a  2 s  . c  om
    keyBuf.writerIndex(keyBuf.readerIndex() + keyBuf.writableBytes());

    final long startTime = System.nanoTime();
    table.increment(keyBuf, 100).whenComplete((result, cause) -> {
        if (null != semaphore) {
            semaphore.release();
        }
        if (null != cause) {
            log.error("Error at increment key/amount", cause);
        } else {
            long latencyMicros = TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startTime);
            writeOpStats.recordOp(latencyMicros);
        }
        keyBuf.release();
    });
}

From source file:org.apache.bookkeeper.tools.perf.table.KeyGenerator.java

License:Apache License

public void generateKeyFromLong(ByteBuf slice, long n) {
    int startPos = 0;
    if (keysPerPrefix > 0) {
        long numPrefix = (numKeys + keysPerPrefix - 1) / keysPerPrefix;
        long prefix = n % numPrefix;
        int bytesToFill = Math.min(prefixSize, 8);
        for (int i = 0; i < bytesToFill; i++) {
            slice.setByte(i, (byte) (prefix % 256));
            prefix /= 256;//  w w  w  .  j  av a  2  s. c  o  m
        }
        for (int i = 8; i < bytesToFill; ++i) {
            slice.setByte(i, '0');
        }
        startPos = bytesToFill;
    }
    for (int i = slice.writableBytes() - 1; i >= startPos; --i) {
        slice.setByte(i, (byte) ('0' + (n % 10)));
        n /= 10;
    }
}

From source file:org.apache.bookkeeper.tools.perf.table.WriteTask.java

License:Apache License

void writeKey(long i, byte[] valueBytes) {
    final ByteBuf keyBuf = PooledByteBufAllocator.DEFAULT.heapBuffer(flags.keySize);
    getKey(keyBuf, i, keyRange);/*from  w w w  . j  a  v  a 2 s.c o m*/
    keyBuf.writerIndex(keyBuf.readerIndex() + keyBuf.writableBytes());
    final ByteBuf valBuf = Unpooled.wrappedBuffer(valueBytes);

    final long startTime = System.nanoTime();
    table.put(keyBuf, valBuf).whenComplete((result, cause) -> {
        if (null != semaphore) {
            semaphore.release();
        }
        if (null != cause) {
            log.error("Error at put key/value", cause);
        } else {
            long latencyMicros = TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startTime);
            writeOpStats.recordOp(latencyMicros);
        }
        keyBuf.release();
        valBuf.release();
    });
}

From source file:org.apache.directory.server.dhcp.netty.Dhcp6Handler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Incomming DHCP : {}, from: {}", ByteBufUtil.hexDump(msg.content()), msg.sender());
    }/*from  w  w w .j  a va 2s .  co  m*/

    final Dhcp6Message incommingMsg;
    try {
        incommingMsg = dhcp6MessageDecoder.decode(msg.content().nioBuffer());
    } catch (final Dhcp6Exception.UnknownMsgException e) {
        LOG.warn("Unknown DHCP message type: {}. Ignoring", ByteBufUtil.hexDump(msg.content()), e);
        return;
    }

    final Optional<Dhcp6Message> reply = dhcpService
            .getReplyFor(new Dhcp6RequestContext(msg.sender().getAddress()), incommingMsg);

    if (reply.isPresent()) {
        LOG.debug("Responding with message: {}", reply.get());

        // TODO what size to allocate the buffer to ?
        ByteBuf buf = ctx.alloc().buffer(1024);
        ByteBuffer buffer = buf.nioBuffer(buf.writerIndex(), buf.writableBytes());
        dhcp6MessageEncoder.encode(buffer, reply.get());
        buffer.flip();
        buf.writerIndex(buf.writerIndex() + buffer.remaining());
        DatagramPacket packet = new DatagramPacket(buf, msg.sender());
        ctx.write(packet);
    } else {
        LOG.warn("No response from DHCP service received for: {}. Ignoring.", incommingMsg);
    }
}

From source file:org.apache.directory.server.dhcp.netty.DhcpHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    DhcpMessage request = decoder.decode(msg.content().nioBuffer());

    DhcpRequestContext context = interfaceManager.newRequestContext(
            (InetSocketAddress) ctx.channel().localAddress(), msg.sender(), msg.recipient(), request);
    if (context == null) {
        debug("IGNQUERY", msg.sender(), msg.recipient(), request);
        return;//from   w w  w  .  jav  a2 s .c  om
    }
    // debug("READ", msg.sender(), msg.recipient(), request);

    MDCUtils.init(context, request);
    try {
        DhcpMessage reply = dhcpService.getReplyFor(context, request);
        if (reply == null) {
            debug("NOREPLY", msg.sender(), msg.recipient(), request);
            return;
        }

        InterfaceAddress localAddress = interfaceManager.getResponseInterface(request.getRelayAgentAddress(),
                request.getCurrentClientAddress(), msg.sender().getAddress(), reply);
        if (localAddress == null) {
            debug("NOIFACE", msg.recipient(), msg.sender(), reply);
            return;
        }

        debug("READ", msg.sender(), msg.recipient(), request);

        InetSocketAddress isa = DhcpInterfaceUtils.determineMessageDestination(request, reply, localAddress,
                msg.sender().getPort());

        ByteBuf buf = ctx.alloc().buffer(1024);
        ByteBuffer buffer = buf.nioBuffer(buf.writerIndex(), buf.writableBytes());
        encoder.encode(buffer, reply);
        buffer.flip();
        buf.writerIndex(buf.writerIndex() + buffer.remaining());
        DatagramPacket packet = new DatagramPacket(buf, isa);
        debug("WRITE", packet.sender(), packet.recipient(), reply);
        ctx.write(packet, ctx.voidPromise());
    } finally {
        MDCUtils.fini();
    }
}

From source file:org.apache.distributedlog.common.util.ByteBufUtils.java

License:Apache License

public static byte[] getArray(ByteBuf buffer) {
    if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.writableBytes() == 0) {
        return buffer.array();
    }//w ww.  ja  v  a  2 s. c  o m
    byte[] data = new byte[buffer.readableBytes()];
    buffer.getBytes(buffer.readerIndex(), data);
    return data;
}

From source file:org.apache.spark.network.shuffle.protocol.BlockTransferMessage.java

License:Apache License

/** Serializes the 'type' byte followed by the message itself. */
public ByteBuffer toByteBuffer() {
    // Allow room for encoded message, plus the type byte
    ByteBuf buf = Unpooled.buffer(encodedLength() + 1);
    buf.writeByte(type().id);/*  w  ww . j a  v  a  2s  . c  om*/
    encode(buf);
    assert buf.writableBytes() == 0 : "Writable bytes remain: " + buf.writableBytes();
    return buf.nioBuffer();
}