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.apache.bookkeeper.proto.checksum.MacDigestManager.java

License:Apache License

@Override
void update(ByteBuf data) {
    mac.get().update(data.nioBuffer());
}

From source file:org.apache.bookkeeper.proto.checksum.StandardCRC32Digest.java

License:Apache License

@Override
public void update(ByteBuf buf) {
    crc.update(buf.nioBuffer());
}

From source file:org.apache.bookkeeper.proto.ReadEntryProcessorV3.java

License:Apache License

/**
 * Read a specific entry./*from w  w  w  .  j  ava2s . c  om*/
 *
 * @param readResponseBuilder
 *          read response builder.
 * @param entryId
 *          entry to read
 * @param startTimeSw
 *          stop watch to measure the read operation.
 * @return read response or null if it is a fence read operation.
 * @throws IOException
 */
protected ReadResponse readEntry(ReadResponse.Builder readResponseBuilder, long entryId,
        boolean readLACPiggyBack, Stopwatch startTimeSw) throws IOException {
    ByteBuf entryBody = requestProcessor.getBookie().readEntry(ledgerId, entryId);
    if (null != fenceResult) {
        handleReadResultForFenceRead(entryBody, readResponseBuilder, entryId, startTimeSw);
        return null;
    } else {
        try {
            readResponseBuilder.setBody(ByteString.copyFrom(entryBody.nioBuffer()));
            if (readLACPiggyBack) {
                readResponseBuilder.setEntryId(entryId);
            } else {
                long knownLAC = requestProcessor.getBookie().readLastAddConfirmed(ledgerId);
                readResponseBuilder.setMaxLAC(knownLAC);
            }
            registerSuccessfulEvent(readStats, startTimeSw);
            readResponseBuilder.setStatus(StatusCode.EOK);
            return readResponseBuilder.build();
        } finally {
            ReferenceCountUtil.release(entryBody);
        }
    }
}

From source file:org.apache.bookkeeper.proto.ReadEntryProcessorV3.java

License:Apache License

private void getFenceResponse(ReadResponse.Builder readResponse, ByteBuf entryBody, boolean fenceResult) {
    StatusCode status;/*www . j  a  v  a2 s. c  om*/
    if (!fenceResult) {
        status = StatusCode.EIO;
        registerFailedEvent(requestProcessor.getRequestStats().getFenceReadWaitStats(), lastPhaseStartTime);
    } else {
        status = StatusCode.EOK;
        readResponse.setBody(ByteString.copyFrom(entryBody.nioBuffer()));
        registerSuccessfulEvent(requestProcessor.getRequestStats().getFenceReadWaitStats(), lastPhaseStartTime);
    }

    if (null != entryBody) {
        ReferenceCountUtil.release(entryBody);
    }

    readResponse.setStatus(status);
}

From source file:org.apache.bookkeeper.proto.ReadLacProcessorV3.java

License:Apache License

private ReadLacResponse getReadLacResponse() {
    final long startTimeNanos = MathUtils.nowInNano();
    ReadLacRequest readLacRequest = request.getReadLacRequest();
    long ledgerId = readLacRequest.getLedgerId();

    final ReadLacResponse.Builder readLacResponse = ReadLacResponse.newBuilder().setLedgerId(ledgerId);

    if (!isVersionCompatible()) {
        readLacResponse.setStatus(StatusCode.EBADVERSION);
        return readLacResponse.build();
    }/* w w  w. j a  v a2  s .  com*/

    logger.debug("Received ReadLac request: {}", request);
    StatusCode status = StatusCode.EOK;
    ByteBuf lastEntry = null;
    ByteBuf lac = null;
    try {
        lac = requestProcessor.bookie.getExplicitLac(ledgerId);
        if (lac != null) {
            readLacResponse.setLacBody(ByteString.copyFrom(lac.nioBuffer()));
        }
    } catch (Bookie.NoLedgerException e) {
        status = StatusCode.ENOLEDGER;
        logger.error("No ledger found while performing readLac from ledger: {}", ledgerId, e);
    } catch (IOException e) {
        status = StatusCode.EIO;
        logger.error("IOException while performing readLac from ledger: {}", ledgerId);
    } finally {
        ReferenceCountUtil.release(lac);
    }

    try {
        lastEntry = requestProcessor.bookie.readEntry(ledgerId, BookieProtocol.LAST_ADD_CONFIRMED);
        if (lastEntry != null) {
            readLacResponse.setLastEntryBody(ByteString.copyFrom(lastEntry.nioBuffer()));
        }
    } catch (Bookie.NoLedgerException e) {
        status = StatusCode.ENOLEDGER;
        logger.error("No ledger found while trying to read last entry: {}", ledgerId, e);
    } catch (IOException e) {
        status = StatusCode.EIO;
        logger.error("IOException while trying to read last entry: {}", ledgerId, e);
    } finally {
        ReferenceCountUtil.release(lastEntry);
    }

    if ((lac == null) && (lastEntry == null)) {
        status = StatusCode.ENOENTRY;
    }

    if (status == StatusCode.EOK) {
        requestProcessor.getRequestStats().getReadLacStats()
                .registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
    } else {
        requestProcessor.getRequestStats().getReadLacStats()
                .registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
    }
    // Finally set the status and return
    readLacResponse.setStatus(status);
    return readLacResponse.build();
}

From source file:org.apache.bookkeeper.statelib.impl.journal.AbstractStateStoreWithJournal.java

License:Apache License

protected synchronized CompletableFuture<DLSN> writeCommandBuf(ByteBuf cmdBuf) {
    long txId = ++nextRevision;
    return FutureUtils.ensure(writer.write(new LogRecord(txId, cmdBuf.nioBuffer())), () -> cmdBuf.release());
}

From source file:org.apache.bookkeeper.statelib.impl.journal.AbstractStateStoreWithJournal.java

License:Apache License

protected synchronized CompletableFuture<Long> writeCommandBufReturnTxId(ByteBuf cmdBuf) {
    long txId = ++nextRevision;
    return FutureUtils.ensure(writer.write(new LogRecord(txId, cmdBuf.nioBuffer())).thenApply(dlsn -> txId),
            () -> cmdBuf.release());/*ww w .  j  a  v  a  2  s  .c o  m*/
}

From source file:org.apache.bookkeeper.statelib.impl.kv.KVUtils.java

License:Apache License

static Command newCommand(ByteBuf cmdBuf) throws InvalidProtocolBufferException {
    return Command.parseFrom(cmdBuf.nioBuffer());
}

From source file:org.apache.bookkeeper.statelib.impl.mvcc.MVCCUtils.java

License:Apache License

static Command newCommand(ByteBuf recordBuf) {
    try {//from   www.j a va 2s. c  o  m
        return Command.parseFrom(recordBuf.nioBuffer());
    } catch (InvalidProtocolBufferException e) {
        log.error("Found a corrupted record on replaying log stream", e);
        throw new StateStoreRuntimeException("Found a corrupted record on replaying log stream", e);
    }
}

From source file:org.apache.carbondata.spark.dictionary.client.SecureDictionaryClientHandler.java

License:Apache License

/**
 * client send request to server/*w w w . ja  v a 2  s  .  c  o m*/
 *
 * @param key DictionaryMessage
 * @return DictionaryMessage
 */
public DictionaryMessage getDictionary(DictionaryMessage key, TransportClient client) {
    DictionaryMessage dictionaryMessage;
    ByteBuffer resp = null;
    try {

        ByteBuf buffer = ByteBufAllocator.DEFAULT.heapBuffer();
        key.writeData(buffer);
        resp = client.sendRpcSync(buffer.nioBuffer(), 100000);
    } catch (Exception e) {
        LOGGER.error("Error while send request to server ", e);
    }
    try {
        if (resp == null) {
            StringBuilder message = new StringBuilder();
            message.append("DictionaryMessage { ColumnName: ").append(key.getColumnName())
                    .append(", DictionaryValue: ").append(key.getDictionaryValue()).append(", type: ")
                    .append(key.getType()).append(" }");
            throw new RuntimeException("Request timed out for key : " + message);
        }
        DictionaryMessage newKey = new DictionaryMessage();
        ByteBuf data = Unpooled.wrappedBuffer(resp);
        newKey.readFullLength(data);
        data.release();
        return newKey;
    } catch (Exception e) {
        LOGGER.error(e);
        throw new RuntimeException(e);
    }
}