Example usage for io.netty.buffer ByteBuf arrayOffset

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

Introduction

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

Prototype

public abstract int arrayOffset();

Source Link

Document

Returns the offset of the first byte within the backing byte array of this buffer.

Usage

From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java

License:Apache License

/**
 * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library
 * which is faster as it computes using hardware machine instruction else it computes using crc32c algo.
 *
 * @param payload/*  w  w w .  j  a  va2s. c om*/
 * @return
 */
public static int computeChecksum(ByteBuf payload) {
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        return CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes());
    } else if (payload.hasArray()) {
        return CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
                payload.readableBytes());
    } else {
        return CRC32C_HASH.calculate(payload.nioBuffer());
    }
}

From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java

License:Apache License

/**
 * Computes incremental checksum with input previousChecksum and input payload
 *
 * @param previousChecksum : previously computed checksum
 * @param payload/*  w  ww  . j ava  2s .com*/
 * @return
 */
public static int resumeChecksum(int previousChecksum, ByteBuf payload) {
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        return CRC32C_HASH.resume(previousChecksum, payload.memoryAddress() + payload.readerIndex(),
                payload.readableBytes());
    } else if (payload.hasArray()) {
        return CRC32C_HASH.resume(previousChecksum, payload.array(),
                payload.arrayOffset() + payload.readerIndex(), payload.readableBytes());
    } else {
        return CRC32C_HASH.resume(previousChecksum, payload.nioBuffer());
    }
}

From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java

License:Apache License

/**
 * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library
 * which is faster as it computes using hardware machine instruction else it computes using crc32c algo.
 *
 * @param payload// ww w.  j a v  a 2  s. c  om
 * @return
 */
public static long computeChecksum(ByteBuf payload) {
    int crc;
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        crc = CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes());
    } else if (payload.hasArray()) {
        crc = CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
                payload.readableBytes());
    } else {
        crc = CRC32C_HASH.calculate(payload.nioBuffer());
    }
    return crc & 0xffffffffL;
}

From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java

License:Apache License

/**
 * Computes incremental checksum with input previousChecksum and input payload
 *
 * @param previousChecksum : previously computed checksum
 * @param payload//from www .  ja v a  2  s  .  c om
 * @return
 */
public static long resumeChecksum(long previousChecksum, ByteBuf payload) {
    int crc = (int) previousChecksum;
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        crc = CRC32C_HASH.resume(crc, payload.memoryAddress() + payload.readerIndex(), payload.readableBytes());
    } else if (payload.hasArray()) {
        crc = CRC32C_HASH.resume(crc, payload.array(), payload.arrayOffset() + payload.readerIndex(),
                payload.readableBytes());
    } else {
        crc = CRC32C_HASH.resume(crc, payload.nioBuffer());
    }
    return crc & 0xffffffffL;
}

From source file:com.yahoo.pulsar.broker.admin.BrokerStats.java

License:Apache License

@GET
@Path("/destinations")
@ApiOperation(value = "Get all the destination stats by namesapce", response = OutputStream.class, responseContainer = "OutputStream") // https://github.com/swagger-api/swagger-ui/issues/558
                                                                                                                                       // map
                                                                                                                                       // support
                                                                                                                                       // missing
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission") })
public StreamingOutput getDestinations2() throws Exception {
    // Ensure super user access only
    validateSuperUserAccess();/*www .  j  av a 2 s.  c o m*/
    return new StreamingOutput() {
        public void write(OutputStream output) throws IOException, WebApplicationException {
            ByteBuf statsBuf = null;
            try {
                statsBuf = pulsar().getBrokerService().getDimensionMetrics();
                output.write(statsBuf.array(), statsBuf.arrayOffset(), statsBuf.readableBytes());
            } catch (Exception e) {
                throw new WebApplicationException(e);
            } finally {
                ReferenceCountUtil.release(statsBuf);
            }
        }
    };
}

From source file:com.yahoo.pulsar.broker.admin.PersistentTopics.java

License:Apache License

@GET
@Path("/{property}/{cluster}/{namespace}/{destination}/subscription/{subName}/position/{messagePosition}")
@ApiOperation(value = "Peek nth message on a topic subscription.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"),
        @ApiResponse(code = 404, message = "Topic, subscription or the message position does not exist") })
public Response peekNthMessage(@PathParam("property") String property, @PathParam("cluster") String cluster,
        @PathParam("namespace") String namespace, @PathParam("destination") @Encoded String destination,
        @PathParam("subName") String subName, @PathParam("messagePosition") int messagePosition,
        @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
    destination = decode(destination);//from  w  w w. j  ava 2  s.  com
    DestinationName dn = DestinationName.get(domain(), property, cluster, namespace, destination);
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(property, cluster, namespace,
            destination, authoritative);
    if (partitionMetadata.partitions > 0) {
        throw new RestException(Status.METHOD_NOT_ALLOWED,
                "Peek messages on a partitioned topic is not allowed");
    }
    validateAdminOperationOnDestination(dn, authoritative);
    PersistentTopic topic = getTopicReference(dn);
    PersistentReplicator repl = null;
    PersistentSubscription sub = null;
    Entry entry = null;
    if (subName.startsWith(topic.replicatorPrefix)) {
        repl = getReplicatorReference(subName, topic);
    } else {
        sub = getSubscriptionReference(subName, topic);
    }
    try {
        if (subName.startsWith(topic.replicatorPrefix)) {
            entry = repl.peekNthMessage(messagePosition).get();
        } else {
            entry = sub.peekNthMessage(messagePosition).get();
        }
        checkNotNull(entry);
        PositionImpl pos = (PositionImpl) entry.getPosition();
        ByteBuf metadataAndPayload = entry.getDataBuffer();

        // moves the readerIndex to the payload
        MessageMetadata metadata = Commands.parseMessageMetadata(metadataAndPayload);

        ResponseBuilder responseBuilder = Response.ok();
        responseBuilder.header("X-Pulsar-Message-ID", pos.toString());
        for (KeyValue keyValue : metadata.getPropertiesList()) {
            responseBuilder.header("X-Pulsar-PROPERTY-" + keyValue.getKey(), keyValue.getValue());
        }
        if (metadata.hasPublishTime()) {
            responseBuilder.header("X-Pulsar-publish-time",
                    DATE_FORMAT.format(Instant.ofEpochMilli(metadata.getPublishTime())));
        }

        // Decode if needed
        CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(metadata.getCompression());
        ByteBuf uncompressedPayload = codec.decode(metadataAndPayload, metadata.getUncompressedSize());

        // Copy into a heap buffer for output stream compatibility
        ByteBuf data = PooledByteBufAllocator.DEFAULT.heapBuffer(uncompressedPayload.readableBytes(),
                uncompressedPayload.readableBytes());
        data.writeBytes(uncompressedPayload);
        uncompressedPayload.release();

        StreamingOutput stream = new StreamingOutput() {

            @Override
            public void write(OutputStream output) throws IOException, WebApplicationException {
                output.write(data.array(), data.arrayOffset(), data.readableBytes());
                data.release();
            }
        };

        return responseBuilder.entity(stream).build();
    } catch (NullPointerException npe) {
        throw new RestException(Status.NOT_FOUND, "Message not found");
    } catch (Exception exception) {
        log.error("[{}] Failed to get message at position {} from {} {}", clientAppId(), messagePosition, dn,
                subName, exception);
        throw new RestException(exception);
    } finally {
        if (entry != null) {
            entry.release();
        }
    }
}

From source file:com.yahoo.pulsar.common.compression.CompressionCodecZLib.java

License:Apache License

@Override
public ByteBuf encode(ByteBuf source) {
    byte[] array;
    int length = source.readableBytes();

    int sizeEstimate = (int) Math.ceil(source.readableBytes() * 1.001) + 14;
    ByteBuf compressed = PooledByteBufAllocator.DEFAULT.heapBuffer(sizeEstimate);

    int offset = 0;
    if (source.hasArray()) {
        array = source.array();//from   w w w.  j  a v  a 2s  .co  m
        offset = source.arrayOffset() + source.readerIndex();
    } else {
        // If it's a direct buffer, we need to copy it
        array = new byte[length];
        source.getBytes(source.readerIndex(), array);
    }

    synchronized (deflater) {
        deflater.setInput(array, offset, length);
        while (!deflater.needsInput()) {
            deflate(compressed);
        }

        deflater.reset();
    }

    return compressed;
}

From source file:com.yahoo.pulsar.common.compression.CompressionCodecZLib.java

License:Apache License

private void deflate(ByteBuf out) {
    int numBytes;
    do {//from   w  w  w.j a va  2s. c o  m
        int writerIndex = out.writerIndex();
        numBytes = deflater.deflate(out.array(), out.arrayOffset() + writerIndex, out.writableBytes(),
                Deflater.SYNC_FLUSH);
        out.writerIndex(writerIndex + numBytes);
    } while (numBytes > 0);
}

From source file:com.yahoo.pulsar.common.compression.CompressionCodecZLib.java

License:Apache License

@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PooledByteBufAllocator.DEFAULT.heapBuffer(uncompressedLength, uncompressedLength);

    int len = encoded.readableBytes();

    byte[] array;
    int offset;/*from   w  ww .jav  a2s  . com*/
    if (encoded.hasArray()) {
        array = encoded.array();
        offset = encoded.arrayOffset() + encoded.readerIndex();
    } else {
        array = new byte[len];
        encoded.getBytes(encoded.readerIndex(), array);
        offset = 0;
    }

    int resultLength;
    synchronized (inflater) {
        inflater.setInput(array, offset, len);
        try {
            resultLength = inflater.inflate(uncompressed.array(), uncompressed.arrayOffset(),
                    uncompressedLength);
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
        inflater.reset();
    }

    checkArgument(resultLength == uncompressedLength);

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cHardware() {
    if (HARDWARE_CRC32C_HASH == null) {
        return;//  w w  w.ja v a 2s  . co m
    }

    ByteBuf payload = Unpooled.wrappedBuffer(inputBytes);

    // compute checksum using sse4.2 hw instruction
    int hw = HARDWARE_CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
            payload.readableBytes());
    assertEquals(hw, expectedChecksum);
}