Example usage for io.netty.buffer ByteBuf nioBuffer

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

Introduction

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

Prototype

public abstract ByteBuffer nioBuffer();

Source Link

Document

Exposes this buffer's readable bytes as an NIO ByteBuffer .

Usage

From source file:org.aotorrent.common.storage.FileStorage.java

License:Apache License

public void store(int pieceIndex, ByteBuf byteBuffer) throws IOException {
    Collection<StorageFilesInfo> storageFiles = getAffectedFiles(pieceIndex);

    for (StorageFilesInfo storageFile : storageFiles) {

        ByteBuf buf = Unpooled.buffer((int) storageFile.getLength());
        byteBuffer.getBytes((int) storageFile.getPieceOffset(), buf, (int) storageFile.getLength());

        final File file = storageFile.getFile();

        if (file.getParentFile() != null) {
            //noinspection ResultOfMethodCallIgnored
            file.getParentFile().mkdirs();
        }/*from  www .ja  v a  2 s . c o m*/

        try (RandomAccessFile randomAccessFile = new RandomAccessFile(file.getCanonicalPath(), "rw")) {
            try (FileChannel fileChannel = randomAccessFile.getChannel()) {
                fileChannel.position(storageFile.getFileOffset());
                fileChannel.write(buf.nioBuffer());
            }
        }
    }
}

From source file:org.apache.activemq.transport.amqp.client.AmqpConnection.java

License:Apache License

@Override
public void onData(final ByteBuf incoming) {

    // We need to retain until the serializer gets around to processing it.
    ReferenceCountUtil.retain(incoming);

    serializer.execute(new Runnable() {

        @Override/*from ww w.j  a  v  a2s.c o m*/
        public void run() {
            ByteBuffer source = incoming.nioBuffer();
            LOG.trace("Client Received from Broker {} bytes:", source.remaining());

            if (protonTransport.isClosed()) {
                LOG.debug("Ignoring incoming data because transport is closed");
                return;
            }

            do {
                ByteBuffer buffer = protonTransport.getInputBuffer();
                int limit = Math.min(buffer.remaining(), source.remaining());
                ByteBuffer duplicate = source.duplicate();
                duplicate.limit(source.position() + limit);
                buffer.put(duplicate);
                protonTransport.processInput();
                source.position(source.position() + limit);
            } while (source.hasRemaining());

            ReferenceCountUtil.release(incoming);

            // Process the state changes from the latest data and then answer back
            // any pending updates to the Broker.
            processUpdates();
            pumpToProtonTransport();
        }
    });
}

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

License:Apache License

private void writePreV2Journal(File journalDir, int numEntries) throws Exception {
    long logId = System.currentTimeMillis();
    File fn = new File(journalDir, Long.toHexString(logId) + ".txn");

    FileChannel fc = new RandomAccessFile(fn, "rw").getChannel();

    ByteBuffer zeros = ByteBuffer.allocate(512);
    fc.write(zeros, 4 * 1024 * 1024);//from ww  w.ja v a2 s .  co m
    fc.position(0);

    byte[] data = "JournalTestData".getBytes();
    long lastConfirmed = LedgerHandle.INVALID_ENTRY_ID;
    for (int i = 1; i <= numEntries; i++) {
        ByteBuf packet = ClientUtil.generatePacket(1, i, lastConfirmed, i * data.length, data);
        lastConfirmed = i;
        ByteBuffer lenBuff = ByteBuffer.allocate(4);
        lenBuff.putInt(packet.readableBytes());
        lenBuff.flip();

        fc.write(lenBuff);
        fc.write(packet.nioBuffer());
        packet.release();
    }
}

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

License:Apache License

@Override
public long addEntry(ByteBuf entry) throws IOException {
    long ledgerId = entry.getLong(entry.readerIndex() + 0);
    long entryId = entry.getLong(entry.readerIndex() + 8);
    long lac = entry.getLong(entry.readerIndex() + 16);

    memTable.addEntry(ledgerId, entryId, entry.nioBuffer(), this);
    interleavedLedgerStorage.ledgerCache.updateLastAddConfirmed(ledgerId, lac);
    return entryId;
}

From source file:org.apache.bookkeeper.clients.impl.kv.KvUtils.java

License:Apache License

public static ByteString toProtoKey(ByteBuf key) {
    return UnsafeByteOperations.unsafeWrap(key.nioBuffer());
}

From source file:org.apache.bookkeeper.clients.impl.kv.KvUtils.java

License:Apache License

public static PutRequest.Builder newPutRequest(ByteBuf key, ByteBuf value, PutOption<ByteBuf> option) {
    return PutRequest.newBuilder().setKey(UnsafeByteOperations.unsafeWrap(key.nioBuffer()))
            .setValue(UnsafeByteOperations.unsafeWrap(value.nioBuffer())).setPrevKv(option.prevKv());
}

From source file:org.apache.bookkeeper.clients.impl.kv.KvUtils.java

License:Apache License

public static IncrementRequest.Builder newIncrementRequest(ByteBuf key, long amount,
        IncrementOption<ByteBuf> option) {
    return IncrementRequest.newBuilder().setKey(UnsafeByteOperations.unsafeWrap(key.nioBuffer()))
            .setAmount(amount).setGetTotal(option.getTotal());
}

From source file:org.apache.bookkeeper.clients.impl.kv.KvUtils.java

License:Apache License

public static DeleteRangeRequest.Builder newDeleteRequest(ByteBuf key, DeleteOption<ByteBuf> option) {
    DeleteRangeRequest.Builder builder = DeleteRangeRequest.newBuilder()
            .setKey(UnsafeByteOperations.unsafeWrap(key.nioBuffer())).setPrevKv(option.prevKv());
    if (null != option.endKey()) {
        builder = builder.setRangeEnd(UnsafeByteOperations.unsafeWrap(option.endKey().nioBuffer()));
    }/*  w ww.j a  va 2 s  .c o m*/
    return builder;
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufSimpleTableImpl.java

License:Apache License

private RoutingHeader.Builder newRoutingHeader(ByteBuf pKey) {
    return RoutingHeader.newBuilder().setStreamId(streamId)
            .setRKey(UnsafeByteOperations.unsafeWrap(pKey.nioBuffer()));
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufTableRangeImpl.java

License:Apache License

private RoutingHeader.Builder newRoutingHeader(ByteBuf pKey) {
    return RoutingHeader.newBuilder().setStreamId(streamId).setRangeId(rangeProps.getRangeId())
            .setRKey(UnsafeByteOperations.unsafeWrap(pKey.nioBuffer()));
}