Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

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

Prototype

public final Buffer position(int newPosition) 

Source Link

Document

Sets the position of this buffer.

Usage

From source file:com.samsung.px.pig.storage.DynamoDBStorage.java

private WriteRequestWithCapacity getWriteRequestWithCapacity(Tuple tuple) throws IOException {
    ResourceFieldSchema[] fields = this.schema.getFields();
    Map<String, AttributeValue> dynamoItem = new HashMap<String, AttributeValue>();
    int dataSize = 0;
    int dynamoItemSize = 0;
    int tupleSize = tuple.size();
    for (int i = 0; i < tupleSize; i++) {
        Object field = tuple.get(i);
        AttributeValue dynamoValue = null;

        switch (DataType.findType(field)) {

        case DataType.NULL:
            // dynamodb does not support null values
            // simply don't write field
            reportCounter(DYNAMO_COUNTER_NULL_FIELDS_DISCARDED, 1);
            break;
        case DataType.BOOLEAN:
            if (((Boolean) field).booleanValue()) {
                dynamoValue = new AttributeValue().withN("1");
            } else {
                dynamoValue = new AttributeValue().withN("0");
            }//from   ww w .ja  v  a2s .c  o  m
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.INTEGER:
        case DataType.LONG:
        case DataType.FLOAT:
        case DataType.DOUBLE:
            String numAsString = field.toString();
            dynamoValue = new AttributeValue().withN(numAsString);
            dataSize += numAsString.length();
            dynamoItemSize += numAsString.length();
            break;
        case DataType.BYTEARRAY:
            byte[] b = ((DataByteArray) field).get();
            ByteBuffer buffer = ByteBuffer.allocate(b.length);
            buffer.put(b, 0, b.length);
            buffer.position(0);
            dynamoValue = new AttributeValue().withB(buffer);
            dataSize += b.length;
            dynamoItemSize += b.length;
            break;
        case DataType.CHARARRAY:
            String fieldStr = (String) field;
            int fieldLen = fieldStr.length();
            if (fieldLen > 0) {
                dynamoValue = new AttributeValue().withS(fieldStr);
                dataSize += fieldLen;
                dynamoItemSize += fieldLen;
            } else {
                // DynamoDB cannot handle empty strings
                reportCounter(DYNAMO_COUNTER_EMPTY_STRING_FIELDS_DISCARDED, 1);
            }
            break;
        case DataType.BYTE:
            ByteBuffer buf = ByteBuffer.allocate(1);
            buf.put((Byte) field);
            buf.position(0);
            dynamoValue = new AttributeValue().withB(buf);
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.MAP:
        case DataType.TUPLE:
            Tuple listTuple = (Tuple) field;

            if (listTuple.size() > 0) {

                Collection<String> listOfValues = new ArrayList<String>();
                for (int k = 0; k < listTuple.size(); k++) {
                    String strItem = (String) listTuple.get(k);
                    int itemLen = strItem.length();
                    dataSize += itemLen;
                    dynamoItemSize += itemLen;
                    listOfValues.add(strItem);
                }
                dynamoValue = new AttributeValue().withSS(listOfValues);

            } else {
                // DynamoDB cannot handle empty strings
                reportCounter(DYNAMO_COUNTER_EMPTY_STRING_FIELDS_DISCARDED, 1);
            }
            break;
        case DataType.BAG:
            throw new RuntimeException("DynamoDBStorage does not support Maps, Tuples or Bags");
        }

        if (dynamoValue != null) {
            ResourceFieldSchema fieldSchema = fields[i];
            String fieldName = fieldSchema.getName();
            if (fieldName == null) {
                throw new IllegalArgumentException(
                        "Cannot write a field with no name (element " + i + " )  FieldSchema: " + fields);
            }
            dynamoItemSize += fieldName.length();
            dynamoItem.put(fieldName, dynamoValue);
        }
    }

    // check for max item size
    if (dynamoItemSize > DYNAMO_MAX_ITEM_SIZE_IN_BYTES) {
        throw new RuntimeException("Item size " + dynamoItemSize + " bytes is larger than max dynamo item size "
                + DYNAMO_MAX_ITEM_SIZE_IN_BYTES + ". Aborting. Item: " + dynamoItem);
    }

    WriteRequest writeRequest = new WriteRequest().withPutRequest(new PutRequest().withItem(dynamoItem));

    return new WriteRequestWithCapacity(writeRequest, dynamoItemSize, dataSize);

}

From source file:com.linkedin.databus.core.DbusEventV1.java

@Override
public ByteBuffer value() {
    ByteBuffer value = _buf.asReadOnlyBuffer().order(_buf.order());
    value.position(_position + LongKeyOffset + keyLength());
    value = value.slice().order(_buf.order());
    int valueSize = valueLength();
    value.limit(valueSize);/*from w ww. ja  v  a  2 s  .co  m*/
    value.rewind();
    return value;
}

From source file:dk.statsbiblioteket.util.LineReaderTest.java

public void testNIO() throws Exception {
    byte[] INITIAL = new byte[] { 1, 2, 3, 4 };
    byte[] EXTRA = new byte[] { 5, 6, 7, 8 };
    byte[] FULL = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    byte[] FIFTH = new byte[] { 87 };
    byte[] FULL_WITH_FIFTH = new byte[] { 1, 2, 3, 4, 87, 6, 7, 8 };

    // Create temp-file with content
    File temp = createTempFile();
    FileOutputStream fileOut = new FileOutputStream(temp, true);
    fileOut.write(INITIAL);/*  w  w  w.  ja v a2s.c  o m*/
    fileOut.close();

    checkContent("The plain test-file should be correct", temp, INITIAL);
    {
        // Read the 4 bytes
        RandomAccessFile input = new RandomAccessFile(temp, "r");
        FileChannel channelIn = input.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(4096);
        channelIn.position(0);
        assertEquals("Buffer read should read full length", INITIAL.length, channelIn.read(buffer));
        buffer.position(0);

        checkContent("Using buffer should produce the right bytes", INITIAL, buffer);
        channelIn.close();
        input.close();
    }
    {
        // Fill new buffer
        ByteBuffer outBuffer = ByteBuffer.allocate(4096);
        outBuffer.put(EXTRA);
        outBuffer.flip();
        assertEquals("The limit of the outBuffer should be correct", EXTRA.length, outBuffer.limit());

        // Append new buffer to end
        RandomAccessFile output = new RandomAccessFile(temp, "rw");
        FileChannel channelOut = output.getChannel();
        channelOut.position(INITIAL.length);
        assertEquals("All bytes should be written", EXTRA.length, channelOut.write(outBuffer));
        channelOut.close();
        output.close();
        checkContent("The resulting file should have the full output", temp, FULL);
    }

    {
        // Fill single byte buffer
        ByteBuffer outBuffer2 = ByteBuffer.allocate(4096);
        outBuffer2.put(FIFTH);
        outBuffer2.flip();
        assertEquals("The limit of the second outBuffer should be correct", FIFTH.length, outBuffer2.limit());

        // Insert byte in the middle
        RandomAccessFile output2 = new RandomAccessFile(temp, "rw");
        FileChannel channelOut2 = output2.getChannel();
        channelOut2.position(4);
        assertEquals("The FIFTH should be written", FIFTH.length, channelOut2.write(outBuffer2));
        channelOut2.close();
        output2.close();
        checkContent("The resulting file with fifth should be complete", temp, FULL_WITH_FIFTH);
    }
}

From source file:de.rwhq.btree.InnerNode.java

@Override
public void load() throws IOException {
    final ByteBuffer buf = rawPage.bufferForReading(0);
    if (NodeType.deserialize(buf.getChar()) != NODE_TYPE)
        throw new IOException(
                "You are trying to load a InnerNode from a byte array, that does not contain an InnerNode");

    buf.position(Header.NUMBER_OF_KEYS.getOffset());
    numberOfKeys = buf.getInt();//from   w ww  .java 2s .co  m
    valid = true;
}

From source file:io.pcp.parfait.dxm.PcpMmvWriter.java

private void populateDataBuffer(ByteBuffer dataFileBuffer, Collection<PcpValueInfo> valueInfos)
        throws IOException {

    // Automatically cleanup the file if this is a mapping where we
    // mandate PID checking from the MMV PMDA (MMV_FLAG_PROCESS) and
    // we were able to stash a path name earlier
    if (file != null && flags.contains(MmvFlag.MMV_FLAG_PROCESS)) {
        file.deleteOnExit();/*w  ww .j a  v a 2s .  c  om*/
    }

    dataFileBuffer.position(0);
    dataFileBuffer.put(TAG);
    dataFileBuffer.putInt(mmvVersion.getVersion());
    long generation = System.currentTimeMillis() / 1000;
    dataFileBuffer.putLong(generation);
    int gen2Offset = dataFileBuffer.position();
    // Generation 2 will be filled in later, once the file's ready
    dataFileBuffer.putLong(0);
    // 2 TOC blocks, 3 if there are instances
    dataFileBuffer.putInt(tocCount());
    dataFileBuffer.putInt(getFlagMask());
    dataFileBuffer.putInt(getProcessIdentifier());
    dataFileBuffer.putInt(clusterIdentifier);

    Collection<? extends MmvWritable> instanceDomains = getInstanceDomains();
    Collection<? extends MmvWritable> instances = getInstances();
    Collection<? extends MmvWritable> metrics = getMetricInfos();
    Collection<? extends MmvWritable> strings = getStrings();

    int tocBlockIndex = 0;

    if (!instanceDomains.isEmpty()) {
        dataFileBuffer.position(getTocOffset(tocBlockIndex++));
        writeToc(dataFileBuffer, TocType.INSTANCE_DOMAINS, instanceDomains.size(),
                instanceDomains.iterator().next().getOffset());
    }

    if (!instances.isEmpty()) {
        dataFileBuffer.position(getTocOffset(tocBlockIndex++));
        writeToc(dataFileBuffer, TocType.INSTANCES, instances.size(), instances.iterator().next().getOffset());
    }

    dataFileBuffer.position(getTocOffset(tocBlockIndex++));

    int metricsFirstEntryOffset = metrics.isEmpty() ? 0 : metrics.iterator().next().getOffset();
    int valuesFirstEntryOffset = valueInfos.isEmpty() ? 0 : valueInfos.iterator().next().getOffset();

    writeToc(dataFileBuffer, TocType.METRICS, metrics.size(), metricsFirstEntryOffset);
    dataFileBuffer.position(getTocOffset(tocBlockIndex++));
    writeToc(dataFileBuffer, TocType.VALUES, valueInfos.size(), valuesFirstEntryOffset);

    if (!getStrings().isEmpty()) {
        dataFileBuffer.position(getTocOffset(tocBlockIndex++));
        writeToc(dataFileBuffer, TocType.STRINGS, strings.size(), strings.iterator().next().getOffset());
    }

    for (MmvWritable instanceDomain : instanceDomains) {
        instanceDomain.writeToMmv(dataFileBuffer);
    }

    for (MmvWritable info : metrics) {
        info.writeToMmv(dataFileBuffer);
    }

    for (MmvWritable info : valueInfos) {
        info.writeToMmv(dataFileBuffer);
    }

    for (MmvWritable string : strings) {
        string.writeToMmv(dataFileBuffer);
    }

    // Once it's set up, let the agent know
    dataFileBuffer.position(gen2Offset);
    dataFileBuffer.putLong(generation);
}

From source file:automenta.knowtention.channel.LineFileChannel.java

@Override
public void run() {

    FileInputStream fileInputStream = null;
    FileChannel channel = null;//from  w  ww . j av a 2s. c  om
    ByteBuffer buffer = null;
    LinkedList<String> lines = new LinkedList();
    StringBuilder builder = new StringBuilder();
    long lastSize = -1, lastLastModified = -1;

    while (running) {
        try {
            Thread.sleep(delayPeriodMS);
        } catch (InterruptedException ex) {
        }

        lines.clear();
        try {
            fileInputStream = new FileInputStream(file);

            channel = fileInputStream.getChannel();

            long lastModified = file.lastModified();
            long csize = channel.size();
            if ((lastModified == lastLastModified) && (csize == lastSize)) { //also check file update time?
                fileInputStream.close();
                continue;
            }

            int currentPos = (int) csize;

            buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, csize);
            buffer.position(currentPos);
            lastSize = csize;
            lastLastModified = lastModified;

            int count = 0;

            for (long i = csize - 1; i >= 0; i--) {

                char c = (char) buffer.get((int) i);

                if (c == '\n') {
                    count++;
                    builder.reverse();
                    lines.addFirst(builder.toString());
                    if (count == numLines) {
                        break;
                    }
                    builder.setLength(0);
                } else
                    builder.append(c);
            }

            update(lines);

            lines.clear();
            buffer.clear();
            channel.close();
            fileInputStream.close();
            fileInputStream = null;

        } catch (Exception ex) {
            Logger.getLogger(LineFileChannel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    try {
        channel.close();
    } catch (IOException ex) {
        Logger.getLogger(LineFileChannel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.koda.integ.hbase.blockcache.OffHeapBlockCacheOld.java

/**
 * Store external with codec.//w ww .  ja  va  2 s  . co  m
 * Format:
 * 0..3  - total record size (-4)
 * 4..7  - size of a key in bytes (16 if use hash128)
 * 8 .. x - key data
 * x+1 ..x+1- IN_MEMORY flag ( 1- in memory, 0 - not)
 * x+2 ... block, serialized and compressed 
 *
 * @param blockName the block name
 * @param buf the buf
 * @param inMemory the in memory
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void storeExternalWithCodec(String blockName, Cacheable buf, boolean inMemory) throws IOException {
    // If external storage is disable - bail out
    if (overflowExtEnabled == false) {
        return;
    }
    byte[] hashed = Utils.hash128(blockName);
    // Check if we have already this block in external storage cache
    if (extStorageCache.contains(hashed)) {
        return;
    }

    ByteBuffer buffer = extStorageCache.getLocalBufferWithAddress().getBuffer();
    deserializer.set(buf.getDeserializer());

    SerDe serde = extStorageCache.getSerDe();
    Codec codec = extStorageCache.getCompressionCodec();
    buffer.clear();

    buffer.position(4);

    // Save key
    buffer.putInt(hashed.length);
    buffer.put(hashed);
    buffer.put(inMemory ? (byte) 1 : (byte) 0);

    if (buf != null) {
        serde.writeCompressed(buffer, buf, codec);
        int pos = buffer.position();
        buffer.putInt(0, pos - 4);
    } else {
        buffer.putInt(0, 0);
    }

    StorageHandle handle = storage.storeData(buffer);

    try {
        // WE USE byte array as a key
        extStorageCache.put(hashed, handle);
    } catch (Exception e) {
        throw new IOException(e);
    }

}

From source file:net.sf.jinsim.response.ResponseFactory.java

public InSimResponse getPacketData(ByteBuffer buffer) throws UnhandledPacketTypeException,
        BufferUnderflowException, InstantiationException, IllegalAccessException {
    InSimResponse insimResponse = null;//from  w  w  w.j a  va  2 s  .  c o m
    /*
    if (buffer.limit() == 91) {
       // is OutGauge message
       insimResponse = new OutGaugeResponse();
    } else if (buffer.limit() == 63) {
       // is OutSim message
               
       log.debug("OutSim is not processed");
    } else 
     */
    if (buffer.limit() >= 3) {
        int packetId = buffer.get() & 0xFF;
        PacketType packetType = PacketType.getPacket(packetId);
        /*
        if (log.isDebugEnabled()) {
           log.debug("Packet is of type " + packetType);
        }
        */
        Class<? extends InSimResponse> insimResponseClass = (Class<? extends InSimResponse>) registeredTypes
                .get(packetType);
        if (insimResponseClass == null) {
            buffer.position(buffer.limit());
            throw new UnhandledPacketTypeException(packetId + ": is unkown");
        }
        insimResponse = (InSimResponse) insimResponseClass.newInstance();
    } else {
        if (log.isDebugEnabled()) {
            String bufferBytes = "";
            for (int i = 0; i < buffer.limit(); i++) {
                bufferBytes += buffer.get() + ", ";
            }
            log.debug("unknown packet: " + bufferBytes);
        } else {
            buffer.position(buffer.limit());
        }
    }

    if (insimResponse == null) {
        throw new UnhandledPacketTypeException("Can not identify response packet");
    }
    try {
        insimResponse.construct(buffer);
    } catch (BufferUnderflowException ex) {
        log.error(ex);
    }
    /*
     if (log.isDebugEnabled()) {
        log.debug("InSimResponse {packet size= " + (buffer.limit()+1) + "}: " + insimResponse);
     }
     */
    return insimResponse;
}

From source file:com.healthmarketscience.jackcess.impl.DatabaseImpl.java

/**
 * Returns the password mask retrieved from the given header page and
 * format, or {@code null} if this format does not use a password mask.
 */// w  ww.jav  a  2 s  . c o m
static byte[] getPasswordMask(ByteBuffer buffer, JetFormat format) {
    // get extra password mask if necessary (the extra password mask is
    // generated from the database creation date stored in the header)
    int pwdMaskPos = format.OFFSET_HEADER_DATE;
    if (pwdMaskPos < 0) {
        return null;
    }

    buffer.position(pwdMaskPos);
    double dateVal = Double.longBitsToDouble(buffer.getLong());

    byte[] pwdMask = new byte[4];
    PageChannel.wrap(pwdMask).putInt((int) dateVal);

    return pwdMask;
}

From source file:com.koda.integ.hbase.blockcache.OffHeapBlockCacheOld.java

/**
 * Read external.//from   w w  w.  ja  v a 2 s  .co  m
 *
 * @param blockName the block name
 * @return the cacheable
 * @throws IOException Signals that an I/O exception has occurred.
 */
@SuppressWarnings("unused")
private Cacheable readExternal(String blockName) throws IOException {
    if (overflowExtEnabled == false)
        return null;
    // Check if we have  already this block in external storage cache
    try {
        StorageHandle handle = (StorageHandle) extStorageCache.get(blockName);
        if (handle == null)
            return null;
        ByteBuffer buffer = extStorageCache.getLocalBufferWithAddress().getBuffer();

        buffer.clear();

        StorageHandle newHandle = storage.getData(handle, buffer);
        int size = buffer.getInt(0);
        if (size == 0)
            return null;
        boolean inMemory = buffer.get(4) == (byte) 1;
        buffer.position(5);
        buffer.limit(size + 4);
        if (deserializer.get() == null)
            return null;
        CacheableDeserializer<Cacheable> deserializer = this.deserializer.get();
        Cacheable obj = deserializer.deserialize(buffer);
        if (inMemory) {
            permGenCache.put(blockName, obj);
        } else {
            tenGenCache.put(blockName, obj);
        }

        if (newHandle.equals(handle) == false) {
            extStorageCache.put(blockName, newHandle);
        }

        return obj;

    } catch (NativeMemoryException e) {
        throw new IOException(e);
    }

}