Example usage for java.nio ByteBuffer arrayOffset

List of usage examples for java.nio ByteBuffer arrayOffset

Introduction

In this page you can find the example usage for java.nio ByteBuffer arrayOffset.

Prototype

public final int arrayOffset() 

Source Link

Document

Returns the offset of the byte array which this buffer is based on, if there is one.

Usage

From source file:com.homeadvisor.kafdrop.service.MessageInspector.java

private byte[] readBytes(ByteBuffer buffer, int offset, int size) {
    byte[] dest = new byte[size];
    if (buffer.hasArray()) {
        System.arraycopy(buffer.array(), buffer.arrayOffset() + offset, dest, 0, size);
    } else {/* w ww.ja v  a 2 s  .c o  m*/
        buffer.mark();
        buffer.get(dest);
        buffer.reset();
    }
    return dest;
}

From source file:org.apache.avro.file.XZCodec.java

@Override
public ByteBuffer decompress(ByteBuffer data) throws IOException {
    ByteArrayOutputStream baos = getOutputBuffer(data.remaining());
    InputStream bytesIn = new ByteArrayInputStream(data.array(), data.arrayOffset() + data.position(),
            data.remaining());/*  ww w . j a v  a 2  s  .  c om*/
    InputStream ios = new XZCompressorInputStream(bytesIn);
    try {
        IOUtils.copy(ios, baos);
    } finally {
        ios.close();
    }
    return ByteBuffer.wrap(baos.toByteArray());
}

From source file:org.apache.avro.file.ZstandardCodec.java

@Override
public ByteBuffer decompress(ByteBuffer compressedData) throws IOException {
    ByteArrayOutputStream baos = getOutputBuffer(compressedData.remaining());
    InputStream bytesIn = new ByteArrayInputStream(compressedData.array(),
            compressedData.arrayOffset() + compressedData.position(), compressedData.remaining());
    InputStream ios = new ZstdCompressorInputStream(bytesIn);
    try {/*from  ww w.  ja va  2s . co m*/
        IOUtils.copy(ios, baos);
    } finally {
        ios.close();
    }
    return ByteBuffer.wrap(baos.toByteArray());
}

From source file:org.apache.hadoop.hbase.io.hfile.bucket.FileMmapEngine.java

/**
 * Transfers data from the given byte buffer to file
 * @param srcBuffer the given byte buffer from which bytes are to be read
 * @param offset The offset in the file where the first byte to be written
 * @throws IOException//from w  ww.ja  va2s . c  om
 */
@Override
public void write(ByteBuffer srcBuffer, long offset) throws IOException {
    assert srcBuffer.hasArray();
    bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(), srcBuffer.arrayOffset());
}

From source file:com.navercorp.pinpoint.common.server.bo.serializer.trace.v1.SpanBoTest.java

@Test
public void serialize2_V1() {
    SpanBo spanBo = new SpanBo();
    spanBo.setAgentId("agent");
    String service = createString(5);
    spanBo.setApplicationId(service);/*from  w ww.ja v  a 2 s  . c o m*/
    String endPoint = createString(127);
    spanBo.setEndPoint(endPoint);
    String rpc = createString(255);
    spanBo.setRpc(rpc);

    spanBo.setServiceType(ServiceType.STAND_ALONE.getCode());
    spanBo.setApplicationServiceType(ServiceType.UNKNOWN.getCode());

    final ByteBuffer bytes = spanSerializer.writeColumnValue(spanBo);

    SpanBo newSpanBo = new SpanBo();
    Buffer valueBuffer = new OffsetFixedBuffer(bytes.array(), bytes.arrayOffset(), bytes.remaining());
    int i = spanDecoder.readSpan(newSpanBo, valueBuffer);
    logger.debug("length:{}", i);
    Assert.assertEquals(bytes.limit(), i);

    Assert.assertEquals(spanBo.getServiceType(), spanBo.getServiceType());
    Assert.assertEquals(spanBo.getApplicationServiceType(), spanBo.getApplicationServiceType());
}

From source file:com.baynote.kafka.hadoop.KafkaRecordReader.java

/**
 * {@inheritDoc}//from w w  w .j a v  a  2s  . c  o m
 */
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (key == null) {
        key = new LongWritable();
    }
    if (value == null) {
        value = new BytesWritable();
    }
    if (continueItr()) {
        final MessageAndOffset msg = getCurrentMessageItr().next();
        final long msgOffset = msg.offset();
        final Message message = msg.message();
        final ByteBuffer buffer = message.payload();
        value.set(buffer.array(), buffer.arrayOffset(), message.payloadSize());
        key.set(msgOffset);
        pos = msgOffset;
        return true;
    }
    return false;
}

From source file:org.apache.accumulo.core.security.AuthenticationTokenIdentifier.java

@Override
public void write(DataOutput out) throws IOException {
    if (null != impl) {
        ThriftMessageUtil msgUtil = new ThriftMessageUtil();
        ByteBuffer serialized = msgUtil.serialize(impl);
        out.writeInt(serialized.limit());
        out.write(serialized.array(), serialized.arrayOffset(), serialized.limit());
    } else {// w  w w .j av a2s. co m
        out.writeInt(0);
    }
}

From source file:com.joyent.manta.http.entity.DigestedEntity.java

@Override
public void writeTo(final OutputStream out) throws IOException {
    digest.reset(); // reset the digest state in case we're in a retry

    // If our wrapped entity is backed by a buffer of some form
    // we can read easily read the whole buffer into our message digest.
    if (wrapped instanceof MemoryBackedEntity) {
        final MemoryBackedEntity entity = (MemoryBackedEntity) wrapped;
        final ByteBuffer backingBuffer = entity.getBackingBuffer();

        if (backingBuffer.hasArray()) {
            final byte[] bytes = backingBuffer.array();
            final int offset = backingBuffer.arrayOffset();
            final int position = backingBuffer.position();
            final int limit = backingBuffer.limit();

            digest.update(bytes, offset + position, limit - position);
            backingBuffer.position(limit);

            wrapped.writeTo(out);/*w ww .j  a  va  2 s. c  o m*/
        }
    } else {
        try (DigestOutputStream dout = new DigestOutputStream(digest);
                TeeOutputStream teeOut = new TeeOutputStream(out, dout)) {
            wrapped.writeTo(teeOut);
            teeOut.flush();
        }
    }
}

From source file:com.navercorp.pinpoint.common.server.bo.serializer.trace.v1.SpanBoTest.java

@Test
public void serialize_V1() {
    final SpanBo spanBo = new SpanBo();
    spanBo.setAgentId("agentId");
    spanBo.setApplicationId("applicationId");
    spanBo.setEndPoint("end");
    spanBo.setRpc("rpc");

    spanBo.setParentSpanId(5);/*from  ww  w.  j a  v a 2s. c om*/
    spanBo.setAgentStartTime(1);

    TransactionId transactionId = new TransactionId("agentId", 2, 3);
    spanBo.setTransactionId(transactionId);
    spanBo.setElapsed(4);
    spanBo.setStartTime(5);

    spanBo.setServiceType(ServiceType.STAND_ALONE.getCode());

    spanBo.setLoggingTransactionInfo(LoggingInfo.INFO.getCode());

    spanBo.setExceptionInfo(1000, "Exception");

    ByteBuffer bytes = spanSerializer.writeColumnValue(spanBo);

    SpanBo newSpanBo = new SpanBo();
    Buffer valueBuffer = new OffsetFixedBuffer(bytes.array(), bytes.arrayOffset(), bytes.remaining());
    int i = spanDecoder.readSpan(newSpanBo, valueBuffer);
    logger.debug("length:{}", i);
    Assert.assertEquals(bytes.limit(), i);
    Assert.assertEquals(newSpanBo.getAgentId(), spanBo.getAgentId());
    Assert.assertEquals(newSpanBo.getApplicationId(), spanBo.getApplicationId());
    Assert.assertEquals(newSpanBo.getAgentStartTime(), spanBo.getAgentStartTime());
    Assert.assertEquals(newSpanBo.getElapsed(), spanBo.getElapsed());
    Assert.assertEquals(newSpanBo.getEndPoint(), spanBo.getEndPoint());
    Assert.assertEquals(newSpanBo.getErrCode(), spanBo.getErrCode());
    Assert.assertEquals(newSpanBo.getFlag(), spanBo.getFlag());

    //        not included for serialization
    //        Assert.assertEquals(newSpanBo.getTraceAgentStartTime(), spanBo.getTraceAgentStartTime());
    //        Assert.assertEquals(newSpanBo.getTraceTransactionSequence(), spanBo.getTraceTransactionSequence());
    Assert.assertEquals(newSpanBo.getParentSpanId(), spanBo.getParentSpanId());

    Assert.assertEquals(newSpanBo.getServiceType(), spanBo.getServiceType());
    Assert.assertEquals(newSpanBo.getApplicationServiceType(), spanBo.getServiceType());

    Assert.assertEquals(newSpanBo.getVersion(), spanBo.getVersion());

    Assert.assertEquals(newSpanBo.getLoggingTransactionInfo(), spanBo.getLoggingTransactionInfo());

    Assert.assertEquals(newSpanBo.getExceptionId(), spanBo.getExceptionId());
    Assert.assertEquals(newSpanBo.getExceptionMessage(), spanBo.getExceptionMessage());

}

From source file:org.apache.nutch.indexer.html.HtmlIndexingFilter.java

private NutchDocument addRawContent(NutchDocument doc, WebPage page, String url) {
    ByteBuffer raw = page.getContent();
    if (raw != null) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Html indexing for: " + url.toString());
        }//from  ww  w .  j a  v a  2 s .c  o m
        ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(raw.array(),
                raw.arrayOffset() + raw.position(), raw.remaining());
        Scanner scanner = new Scanner(arrayInputStream);
        scanner.useDelimiter("\\Z");//To read all scanner content in one String
        String data = "";
        if (scanner.hasNext()) {
            data = scanner.next();
        }
        doc.add("rawcontent", StringUtil.cleanField(data));
    }
    return doc;
}