Example usage for java.nio ByteBuffer slice

List of usage examples for java.nio ByteBuffer slice

Introduction

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

Prototype

public abstract ByteBuffer slice();

Source Link

Document

Returns a sliced buffer that shares its content with this buffer.

Usage

From source file:org.apache.hadoop.hive.llap.cache.LlapAllocatorBuffer.java

public void initialize(ByteBuffer byteBuffer, int offset, int length) {
    this.byteBuffer = byteBuffer.slice();
    this.byteBuffer.position(offset);
    this.byteBuffer.limit(offset + length);
    this.allocSize = length;
}

From source file:org.commoncrawl.util.shared.S3InputStream.java

@Override
public boolean contentAvailable(NIOHttpConnection theConnection, int itemId, String itemKey,
        NIOBufferList contentBuffer) {/*from   www  .j a va 2 s.c o m*/

    ByteBuffer buffer = null;
    IOException exception = null;
    //int receivedBytes = 0;
    try {
        while ((buffer = contentBuffer.read()) != null) {
            if (buffer.position() != 0) {
                buffer = buffer.slice();
            }
            //receivedBytes += buffer.remaining();
            buffer.position(buffer.limit());
            _bufferQueue.write(buffer);
        }
        _bufferQueue.flush();
    } catch (IOException e) {
        LOG.error(CCStringUtils.stringifyException(e));
        exception = e;
    }
    if (_bufferQueue.available() >= MAX_BUFFER_SIZE) {
        theConnection.disableReads();
        pausedConnection.set(theConnection);
    }
    //long nanoTimeStart = System.nanoTime();
    _writeLock.lock();
    //long nanoTimeEnd = System.nanoTime();
    //System.out.println("Received: " + receivedBytes + "for URI:" + uri + " Lock took:" + (nanoTimeEnd-nanoTimeStart));
    try {
        Condition writeCondition = _writeEvent.getAndSet(null);
        if (exception != null) {
            _eofCondition.set(true);
            _exception.set(exception);
        }
        if (writeCondition != null) {
            writeCondition.signal();
        }
    } finally {
        _writeLock.unlock();
    }
    return true;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * from to //w ww  .  ja v  a 2 s  . c  o m
 * 
 * @param fromBuffer
 *            Buffer ? flush
 * @param toBuffer
 *            Buffer ? fill
 * @return number of bytes moved
 */
public static int put(ByteBuffer fromBuffer, ByteBuffer toBuffer) {
    int put;
    int remaining = fromBuffer.remaining();
    if (remaining > 0) { // 
        if (remaining <= toBuffer.remaining()) {
            toBuffer.put(fromBuffer);
            put = remaining;
            // from 
            fromBuffer.position(fromBuffer.limit());
        }
        // heap buffer
        else if (fromBuffer.hasArray()) {
            put = toBuffer.remaining();
            // ??
            toBuffer.put(fromBuffer.array(), fromBuffer.arrayOffset() + fromBuffer.position(), put);
            fromBuffer.position(fromBuffer.position() + put);
        }
        // direct buffer
        else {
            // ??
            put = toBuffer.remaining();
            ByteBuffer slice = fromBuffer.slice();
            slice.limit(put);
            toBuffer.put(slice);
            fromBuffer.position(fromBuffer.position() + put);
        }
    } else {
        put = 0;
    }
    return put;
}

From source file:org.commoncrawl.util.S3InputStream.java

@Override
public boolean contentAvailable(NIOHttpConnection theConnection, int itemId, String itemKey,
        NIOBufferList contentBuffer) {// w  w w.j av  a 2  s  .  c om

    ByteBuffer buffer = null;
    IOException exception = null;
    //int receivedBytes = 0;
    try {
        while ((buffer = contentBuffer.read()) != null) {
            if (buffer.position() != 0) {
                buffer = buffer.slice();
            }
            //receivedBytes += buffer.remaining();
            buffer.position(buffer.limit());
            _bufferQueue.write(buffer);
        }
        _bufferQueue.flush();
    } catch (IOException e) {
        LOG.error(CCStringUtils.stringifyException(e));
        exception = e;
    }
    if (_bufferQueue.available() >= MAX_BUFFER_SIZE) {
        LOG.info("*** PAUSING DOWNLOADS FOR:" + theConnection.getURL());
        theConnection.disableReads();
        pausedConnection.set(theConnection);
    }
    //long nanoTimeStart = System.nanoTime();
    _writeLock.lock();
    //long nanoTimeEnd = System.nanoTime();
    //System.out.println("Received: " + receivedBytes + "for URI:" + uri + " Lock took:" + (nanoTimeEnd-nanoTimeStart));
    try {
        Condition writeCondition = _writeEvent.getAndSet(null);
        if (exception != null) {
            _eofCondition.set(true);
            _exception.set(exception);
        }
        if (writeCondition != null) {
            writeCondition.signal();
        }
    } finally {
        _writeLock.unlock();
    }
    return true;
}

From source file:org.wso2.andes.kernel.AndesMessageMetadata.java

/**
 * Create a copy of metadata/*w w  w .  ja v a  2  s.  c  om*/
 *
 * @param originalMetadata          source metadata that needs to be copied
 * @param isCompressed Value to indicate if the message is compressed or not
 * @return copy of the metadata as a byte array
 */
private byte[] createNewMetadata(byte[] originalMetadata, boolean isCompressed) {
    ByteBuffer buf = ByteBuffer.wrap(originalMetadata);
    buf.position(1);
    buf = buf.slice();
    MessageMetaDataType type = MessageMetaDataType.values()[originalMetadata[0]];
    metaDataType = type;
    StorableMessageMetaData originalMessageMetadata = type.getFactory().createMetaData(buf);

    byte[] underlying;
    //TODO need to implement factory pattern here
    if ((MessageMetaDataType.META_DATA_MQTT).equals(type)) {
        underlying = MQTTMetaDataHandler.constructMetadata(buf, originalMessageMetadata, isCompressed);
    } else {
        underlying = AMQPMetaDataHandler.constructMetadata(buf, originalMessageMetadata, isCompressed);
    }

    return underlying;
}

From source file:org.wso2.andes.kernel.AndesMessageMetadata.java

/**
 * Create a copy of updated metadata, for durable topic subscriptions
 *
 * @param originalMetadata source metadata that needs to be copied
 * @param routingKey       routing key of the message
 * @param exchangeName     exchange of the message
 * @return copy of the metadata as a byte array
 *///w  w w. j a  v  a  2 s.  c  om
private byte[] createNewMetadata(byte[] originalMetadata, String routingKey, String exchangeName,
        long arrivalTime) {
    ByteBuffer buf = ByteBuffer.wrap(originalMetadata);
    buf.position(1);
    buf = buf.slice();
    MessageMetaDataType type = MessageMetaDataType.values()[originalMetadata[0]];
    metaDataType = type;
    StorableMessageMetaData originalMessageMetadata = type.getFactory().createMetaData(buf);

    byte[] underlying;
    //TODO need to implement factory pattern here
    if ((MessageMetaDataType.META_DATA_MQTT).equals(type)) {
        underlying = MQTTMetaDataHandler.constructMetadata(routingKey, buf, originalMessageMetadata,
                exchangeName);
    } else {
        underlying = AMQPMetaDataHandler.constructMetadata(routingKey, buf, originalMessageMetadata,
                exchangeName, arrivalTime);
    }

    return underlying;
}

From source file:org.wso2.andes.kernel.AndesMessageMetadata.java

private void parseMetaData() {
    ByteBuffer buf = ByteBuffer.wrap(metadata);
    buf.position(1);/*ww w.ja va  2  s.c o m*/
    buf = buf.slice();
    MessageMetaDataType type = MessageMetaDataType.values()[metadata[0]];
    metaDataType = type;
    StorableMessageMetaData mdt = type.getFactory().createMetaData(buf);
    //todo need to discuss on making the flow more generic
    if (type.equals(MessageMetaDataType.META_DATA_0_10) || type.equals(MessageMetaDataType.META_DATA_0_8)) {
        this.isPersistent = ((MessageMetaData) mdt).isPersistent();
        this.expirationTime = ((MessageMetaData) mdt).getMessageHeader().getExpiration();
        this.arrivalTime = ((MessageMetaData) mdt).getArrivalTime();
        this.destination = ((MessageMetaData) mdt).getMessagePublishInfo().getRoutingKey().toString();
        this.messageContentLength = ((MessageMetaData) mdt).getContentSize();
        this.isTopic = ((MessageMetaData) mdt).getMessagePublishInfo().getExchange()
                .equals(AMQPUtils.TOPIC_EXCHANGE_NAME);
        this.messageRouterName = ((MessageMetaData) mdt).getMessagePublishInfo().getExchange().toString();
        this.isCompressed = ((MessageMetaData) mdt).isCompressed();
    }
    //For MQTT Specific Types
    if (type.equals(MessageMetaDataType.META_DATA_MQTT)) {
        this.arrivalTime = ((MQTTMessageMetaData) mdt).getMessageArrivalTime();
        this.isTopic = ((MQTTMessageMetaData) mdt).isTopic();
        this.messageRouterName = MQTTUtils.MQTT_EXCHANGE_NAME;
        this.destination = ((MQTTMessageMetaData) mdt).getDestination();
        this.isPersistent = ((MQTTMessageMetaData) mdt).isPersistent();
        this.messageContentLength = ((MQTTMessageMetaData) mdt).getContentSize();
        this.qosLevel = ((MQTTMessageMetaData) mdt).getQosLevel();
        this.isCompressed = ((MQTTMessageMetaData) mdt).isCompressed();
    }

}

From source file:com.googlecode.mp4parser.boxes.microsoft.XtraBox.java

@Override
public void _parseDetails(ByteBuffer content) {
    int boxSize = content.remaining();
    data = content.slice(); //Keep this in case we fail to parse
    successfulParse = false;//from  ww w.  jav  a2s. c  o m
    try {
        tags.clear();
        while (content.remaining() > 0) {
            XtraTag tag = new XtraTag();
            tag.parse(content);
            tags.addElement(tag);
        }
        int calcSize = detailSize();
        if (boxSize != calcSize) {
            throw new RuntimeException("Improperly handled Xtra tag: Calculated sizes don't match ( " + boxSize
                    + "/" + calcSize + ")");
        }
        successfulParse = true;
    } catch (Exception e) {
        successfulParse = false;
        System.err.println("Malformed Xtra Tag detected: " + e.toString());
        e.printStackTrace();
        content.position(content.position() + content.remaining());
    } finally {
        content.order(ByteOrder.BIG_ENDIAN); //Just in case we bailed out mid-parse we don't want to leave the byte order in MS land
    }
}

From source file:io.Text.java

/**
 * Returns the Unicode Scalar Value (32-bit integer value)
 * for the character at <code>position</code>. Note that this
 * method avoids using the converter or doing String instatiation
 * @return the Unicode scalar value at position or -1
 *          if the position is invalid or points to a
 *          trailing byte/* w ww  .  ja  v a  2  s.co m*/
 */
public int charAt(int position) {
    if (position > this.length)
        return -1; // too long
    if (position < 0)
        return -1; // duh.

    ByteBuffer bb = (ByteBuffer) ByteBuffer.wrap(bytes).position(position);
    return bytesToCodePoint(bb.slice());
}