Example usage for io.netty.buffer ByteBuf getBytes

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

Introduction

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

Prototype

public abstract int getBytes(int index, FileChannel out, long position, int length) throws IOException;

Source Link

Document

Transfers this buffer's data starting at the specified absolute index to the specified channel starting at the given file position.

Usage

From source file:se.sics.kompics.network.netty.serialization.ProtobufSerializer.java

License:Open Source License

@Override
public Object fromBinary(ByteBuf msg, Optional<Class> hint) {
    final byte[] array;
    final int offset;
    final int length = msg.readableBytes();
    if (msg.hasArray()) {
        array = msg.array();/*w w w  .j av  a  2s . c  o m*/
        offset = msg.arrayOffset() + msg.readerIndex();
    } else {
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        offset = 0;
    }

    Object o = null;
    try {
        if (extensionRegistry == null) {
            if (HAS_PARSER) {
                o = prototype.getParserForType().parseFrom(array, offset, length);
            } else {
                o = prototype.newBuilderForType().mergeFrom(array, offset, length).build();
            }
        } else {
            if (HAS_PARSER) {
                o = prototype.getParserForType().parseFrom(array, offset, length, extensionRegistry);
            } else {
                o = prototype.newBuilderForType().mergeFrom(array, offset, length, extensionRegistry).build();
            }
        }
    } catch (InvalidProtocolBufferException ex) {
        Serializers.LOG.error("ProtobufSerializer: Couldn't deserialize object.", ex);
    }

    return o;
}