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 int position() 

Source Link

Document

Returns the position of this buffer.

Usage

From source file:com.slytechs.capture.file.editor.AbstractRawIterator.java

public boolean verifyAdditionalRecords(final ByteBuffer buffer, final int count)
        throws EOFException, IOException {

    buffer.reset();/*from w  w w  .j  av  a2  s.  c o  m*/

    final int MAX_HEADER_LENGTH = 24;
    final ByteBuffer view = BufferUtils.duplicate(buffer);
    final int capacity = view.capacity();
    boolean status = true;

    for (int i = 0; i < count && view.position() + MAX_HEADER_LENGTH < capacity; i++) {
        view.mark();
        long length = headerReader.readLength(view);
        int p = view.position() + (int) length;

        if (pattern.match(view) == false) {
            status = false;
            break;
        }
        view.reset();

        if (p + MAX_HEADER_LENGTH > view.capacity()) {
            break;
        }

        view.limit(p + MAX_HEADER_LENGTH);
        view.position(p);
    }

    return status;
}

From source file:edu.hawaii.soest.kilonalu.tchain.TChainSource.java

/**
 * A method that executes the streaming of data from the source to the RBNB
 * server after all configuration of settings, connections to hosts, and
 * thread initiatizing occurs.  This method contains the detailed code for 
 * streaming the data and interpreting the stream.
 *///from  ww w .  ja v  a2  s.  co  m
protected boolean execute() {
    logger.debug("TChainSource.execute() called.");
    // do not execute the stream if there is no connection
    if (!isConnected())
        return false;

    boolean failed = false;

    SocketChannel socket = getSocketConnection();

    // while data are being sent, read them into the buffer
    try {
        // create four byte placeholders used to evaluate up to a four-byte 
        // window.  The FIFO layout looks like:
        //           -------------------------
        //   in ---> | One | Two |Three|Four |  ---> out
        //           -------------------------
        byte byteOne = 0x00, // set initial placeholder values
                byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00;

        // Create a buffer that will store the sample bytes as they are read
        ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize());

        // create a byte buffer to store bytes from the TCP stream
        ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize());

        // add a channel of data that will be pushed to the server.  
        // Each sample will be sent to the Data Turbine as an rbnb frame.
        ChannelMap rbnbChannelMap = new ChannelMap();

        // while there are bytes to read from the socket ...
        while (socket.read(buffer) != -1 || buffer.position() > 0) {

            // prepare the buffer for reading
            buffer.flip();

            // while there are unread bytes in the ByteBuffer
            while (buffer.hasRemaining()) {
                byteOne = buffer.get();
                logger.debug("char: " + (char) byteOne + "\t" + "b1: "
                        + new String(Hex.encodeHex((new byte[] { byteOne }))) + "\t" + "b2: "
                        + new String(Hex.encodeHex((new byte[] { byteTwo }))) + "\t" + "b3: "
                        + new String(Hex.encodeHex((new byte[] { byteThree }))) + "\t" + "b4: "
                        + new String(Hex.encodeHex((new byte[] { byteFour }))) + "\t" + "sample pos: "
                        + sampleBuffer.position() + "\t" + "sample rem: " + sampleBuffer.remaining() + "\t"
                        + "sample cnt: " + sampleByteCount + "\t" + "buffer pos: " + buffer.position() + "\t"
                        + "buffer rem: " + buffer.remaining() + "\t" + "state: " + state);

                // Use a State Machine to process the byte stream.
                // Start building an rbnb frame for the entire sample, first by 
                // inserting a timestamp into the channelMap.  This time is merely
                // the time of insert into the data turbine, not the time of
                // observations of the measurements.  That time should be parsed out
                // of the sample in the Sink client code

                switch (state) {

                case 0:

                    // sample line ending is '\r\n' (carraige return, newline)
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == this.firstDelimiterByte && byteTwo == this.secondDelimiterByte) {
                        // we've found the end of a sample, move on
                        state = 1;
                        break;

                    } else {
                        break;
                    }

                case 1: // read the rest of the bytes to the next EOL characters

                    // sample line is terminated by record delimiter bytes (usually \r\n or \n)
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == this.firstDelimiterByte && byteTwo == this.secondDelimiterByte) {

                        // rewind the sample to overwrite the line ending so we can add
                        // in the timestamp (then add the line ending)
                        sampleBuffer.position(sampleBuffer.position() - 1);
                        --sampleByteCount;

                        // add the delimiter to the end of the sample.
                        byte[] delimiterAsBytes = getFieldDelimiter().getBytes("US-ASCII");

                        for (byte delim : delimiterAsBytes) {
                            sampleBuffer.put(delim);
                            sampleByteCount++;
                        }

                        // then add a timestamp to the end of the sample
                        DATE_FORMAT.setTimeZone(TZ);
                        byte[] sampleDateAsBytes = DATE_FORMAT.format(new Date()).getBytes("US-ASCII");
                        for (byte b : sampleDateAsBytes) {
                            sampleBuffer.put(b);
                            sampleByteCount++;
                        }

                        // add the last two bytes found (usually \r\n) to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);
                            sampleByteCount++;
                            sampleBuffer.put(byteTwo);
                            sampleByteCount++;

                        } else {
                            sampleBuffer.compact();
                            sampleBuffer.put(byteOne);
                            sampleByteCount++;
                            sampleBuffer.put(byteTwo);
                            sampleByteCount++;

                        }

                        // extract just the length of the sample bytes out of the
                        // sample buffer, and place it in the channel map as a 
                        // byte array.  Then, send it to the data turbine.
                        byte[] sampleArray = new byte[sampleByteCount];
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);

                        // send the sample to the data turbine
                        rbnbChannelMap.PutTimeAuto("server");
                        String sampleString = new String(sampleArray, "US-ASCII");
                        int channelIndex = rbnbChannelMap.Add(getRBNBChannelName());
                        rbnbChannelMap.PutMime(channelIndex, "text/plain");
                        rbnbChannelMap.PutDataAsString(channelIndex, sampleString);
                        getSource().Flush(rbnbChannelMap);
                        logger.info("Sample: " + sampleString.substring(0, sampleString.length() - 2)
                                + " sent data to the DataTurbine. ");

                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;
                        sampleBuffer.clear();
                        sampleByteCount = 0;
                        rbnbChannelMap.Clear();
                        logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap.");
                        //state = 0;

                    } else { // not 0x0D20

                        // still in the middle of the sample, keep adding bytes
                        sampleByteCount++; // add each byte found

                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);
                        } else {
                            sampleBuffer.compact();
                            logger.debug("Compacting sampleBuffer ...");
                            sampleBuffer.put(byteOne);

                        }

                        break;
                    } // end if for 0x0D20 EOL

                } // end switch statement

                // shift the bytes in the FIFO window
                byteFour = byteThree;
                byteThree = byteTwo;
                byteTwo = byteOne;

            } //end while (more unread bytes)

            // prepare the buffer to read in more bytes from the stream
            buffer.compact();

        } // end while (more socket bytes to read)
        socket.close();

    } catch (IOException e) {
        // handle exceptions
        // In the event of an i/o exception, log the exception, and allow execute()
        // to return false, which will prompt a retry.
        failed = true;
        e.printStackTrace();
        return !failed;
    } catch (SAPIException sapie) {
        // In the event of an RBNB communication  exception, log the exception, 
        // and allow execute() to return false, which will prompt a retry.
        failed = true;
        sapie.printStackTrace();
        return !failed;
    }

    return !failed;
}

From source file:jext2.DataInode.java

/**
 * Read Inode data//w  w  w  .  j av a  2s  .c  om
 * @param  size    size of the data to be read
 * @param  offset  start address in data area
 * @return buffer of size size containing data.
 * @throws FileTooLarge
 * @throws IoError
 */
public ByteBuffer readData(int size, long fileOffset) throws JExt2Exception, FileTooLarge {
    /* Returning null may break things somewhere..
     * Zero length buffer breaks something in jlowfuse's c code */
    if (getSize() == 0)
        return ByteBuffer.allocateDirect(1);

    /*
     * size may be larger than the inode.size, it doesn't make sense to return
     * 4k of zeros
     */
    if (size > getSize())
        size = (int) getSize();

    ByteBuffer buf = ByteBuffer.allocateDirect(size);

    int blocksize = superblock.getBlocksize();

    long i = 0;
    long firstBlock = fileOffset / blocksize;
    long offset = fileOffset % blocksize;

    /*
     * just as size may be larger than the inode's data, the number of blocks
     * may also be.
     */
    long approxBlocks = (size / blocksize) + 1;
    long maxBlocks = this.getBlocks() / (superblock.getBlocksize() / 512);
    if (approxBlocks > maxBlocks)
        approxBlocks = maxBlocks;

    while (i < approxBlocks) {
        long start = firstBlock + i;
        long stop = firstBlock + approxBlocks;

        LinkedList<Long> b = accessData().getBlocks(start, stop);
        int blocksRead;

        /*
         * Note on the sparse file support:
         * getBlocks will return null if there is no data block for this
         * logical address. So just move the position count blocks forward.
         */

        if (b == null) { /* hole */
            blocksRead = 1;

            int unboundedLimit = buf.position() + blocksize;
            int limit = Math.min(unboundedLimit, buf.capacity());

            assert limit <= buf.capacity() : "New position, limit " + limit + " is beyond buffer's capacity, "
                    + buf;

            buf.limit(limit);
            buf.position(limit);

            assert buf.limit() == buf.position();

        } else { /* blocks */
            blocksRead = b.size();

            long pos = b.getFirst() * blocksize + offset;
            int unboundedLimit = buf.position() + blocksRead * blocksize;
            int limit = Math.min(unboundedLimit, buf.capacity());

            assert limit <= buf.capacity() : "New limit " + limit + " is beyond buffer's capacity, " + buf;

            buf.limit(limit);
            blockAccess.readToBufferUnsynchronized(pos, buf);
        }

        i += blocksRead;
        offset = 0;

        /* This should be removed soon. IllegalMonitorStateException happen
         * occasionally for unknown reasons.
         */
        try {
            accessData().getHierarchyLock().readLock().unlock();
        } catch (IllegalMonitorStateException e) {
            Logger log = Filesystem.getLogger();
            log.warning("IllegalMonitorStateException encountered in readData, inode=" + this);
            log.warning(String.format(
                    "context for exception: blocks=%s i=%d approxBlocks=%d off=%d buf=%s readlock=%s lock.readlock.holds=%s",
                    b, i, approxBlocks, fileOffset, buf, accessData().getHierarchyLock(),
                    accessData().getHierarchyLock().getReadHoldCount()));
        }

        if (buf.capacity() == buf.limit())
            break;
    }

    assert buf.position() == buf.limit() : "Buffer wasn't filled completely";
    assert buf.limit() == size : "Read buffer size does not match request size";

    if (buf.limit() > getSize())
        buf.limit((int) getSize());

    buf.rewind();
    return buf;
}

From source file:edu.uci.ics.crawler4j.crawler.fetcher.PageFetcher.java

private boolean loadPage(final Page p, final InputStream in, final int totalsize, final boolean isBinary,
        String encoding) {/* ww w .j  a  v a 2 s .co m*/
    ByteBuffer bBuf;

    if (totalsize > 0) {
        bBuf = ByteBuffer.allocate(totalsize + 1024);
    } else {
        bBuf = ByteBuffer.allocate(maxDownloadSize);
    }
    final byte[] b = new byte[1024];
    int len;
    double finished = 0;
    try {
        while ((len = in.read(b)) != -1) {
            if (finished + b.length > bBuf.capacity()) {
                break;
            }
            bBuf.put(b, 0, len);
            finished += len;
        }
    } catch (final BufferOverflowException boe) {
        System.out.println("Page size exceeds maximum allowed.");
        return false;
    } catch (final Exception e) {
        System.err.println(e.getMessage());
        return false;
    }

    bBuf.flip();
    if (isBinary) {
        byte[] tmp = new byte[bBuf.limit()];
        bBuf.get(tmp);
        p.setBinaryData(tmp);
    } else {
        String html = "";
        if (encoding == null) {
            int pos = bBuf.position();
            html = Charset.forName("US-ASCII").decode(bBuf).toString();
            bBuf.position(pos);
            pos = html.toLowerCase().indexOf("<meta http-equiv=\"content-type\" content=\"");
            if (pos >= 0) {
                int end = html.indexOf("\"", pos + 41);
                if (end >= 0) {
                    String content = html.substring(pos, end);
                    if (content.contains("charset=")) {
                        encoding = content.substring(content.indexOf("charset=") + 8);
                    }
                }
            }
        }
        if (encoding == null || !Charset.isSupported(encoding))
            encoding = "UTF-8";

        if (!encoding.equals("UTF-8")) {
            html = Charset.forName(encoding).decode(bBuf).toString();
        }

        if (html.length() == 0) {
            return false;
        }
        p.setHTML(html);
    }
    return true;
}

From source file:com.turbospaces.spaces.SpaceReceiveAdapter.java

@Override
public void receive(final Message msg) {
    final byte[] data = msg.getBuffer();
    final ObjectBuffer objectBuffer = new ObjectBuffer(jSpace.getSpaceConfiguration().getKryo());
    final Address nodeRaised = msg.getSrc();

    final MethodCall methodCall = (MethodCall) objectBuffer.readClassAndObject(data);
    final short id = methodCall.getMethodId();

    logger.debug("received {} from {}", methodCall, nodeRaised);

    if (id == SpaceMethodsMapping.BEGIN_TRANSACTION.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override/*  w w  w . j  av  a 2s  .com*/
            public void run() {
                /**
                 * 1. read transaction timeout
                 * 2. create local durable transaction for remote client and assign id
                 * 3. propagate remote transaction timeout and apply to local transaction
                 * 4. send transaction id back to client
                 */
                BeginTransactionMethodCall beginTransactionMethodCall = (BeginTransactionMethodCall) methodCall;
                long transactionTimeout = beginTransactionMethodCall.getTransactionTimeout();
                SpaceTransactionHolder spaceTransactionHolder = new SpaceTransactionHolder();
                spaceTransactionHolder.setSynchronizedWithTransaction(true);
                spaceTransactionHolder.setTimeoutInMillis(transactionTimeout);
                TransactionModificationContext mc = new TransactionModificationContext();
                mc.setProxyMode(true);
                spaceTransactionHolder.setModificationContext(mc);
                modificationContextFor(nodeRaised).put(mc.getTransactionId(), spaceTransactionHolder);
                methodCall.setResponseBody(objectBuffer.writeObjectData(mc.getTransactionId()));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.COMMIT_TRANSACTION.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                CommitRollbackMethodCall commitMethodCall = (CommitRollbackMethodCall) methodCall;
                try {
                    SpaceTransactionHolder th = modificationContextFor(nodeRaised)
                            .getIfPresent(commitMethodCall.getTransactionId());
                    Preconditions.checkState(th != null, "unable to find transaction with id = %s for commit",
                            commitMethodCall.getTransactionId());
                    jSpace.syncTx(th.getModificationContext(), true);
                } finally {
                    durableTransactions.remove(commitMethodCall.getTransactionId());
                }
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.ROLLBACK_TRANSACTION.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                CommitRollbackMethodCall commitMethodCall = (CommitRollbackMethodCall) methodCall;
                try {
                    SpaceTransactionHolder th = modificationContextFor(nodeRaised)
                            .getIfPresent(commitMethodCall.getTransactionId());
                    Preconditions.checkState(th != null, "unable to find transaction with id = %s for rollback",
                            commitMethodCall.getTransactionId());
                    jSpace.syncTx(th.getModificationContext(), false);
                } finally {
                    durableTransactions.remove(commitMethodCall.getTransactionId());
                }
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.WRITE.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                WriteMethodCall writeMethodCall = (WriteMethodCall) methodCall;
                byte[] entityAndClassData = writeMethodCall.getEntity();
                ByteBuffer byteBuffer = ByteBuffer.wrap(entityAndClassData);

                int modifiers = writeMethodCall.getModifiers();
                int timeout = writeMethodCall.getTimeout();
                int timeToLive = writeMethodCall.getTimeToLive();

                /**
                 * 1. read registered class (without actual data)
                 * 2. get the actual type of the remote entry
                 * 3. copy serialized entry state from the byte buffer, omit redundant serialization later
                 * 4. find appropriate transaction modification context if any
                 * 5. call write method itself
                 */
                RegisteredClass entryClass = jSpace.getSpaceConfiguration().getKryo().readClass(byteBuffer);
                Class<?> entryType = entryClass.getType();
                byte[] entityData = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(),
                        byteBuffer.capacity());
                Object entry = jSpace.getSpaceConfiguration().getKryo().readObjectData(byteBuffer, entryType);

                SpaceTransactionHolder holder = null;
                if (writeMethodCall.getTransactionId() != 0)
                    holder = modificationContextFor(nodeRaised)
                            .getIfPresent(writeMethodCall.getTransactionId());

                jSpace.write(holder, entry, entityData, timeToLive, timeout, modifiers);
                writeMethodCall.reset();
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.FETCH.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                FetchMethodCall fetchMethodCall = (FetchMethodCall) methodCall;
                byte[] entityData = fetchMethodCall.getEntity();

                int originalModifiers = fetchMethodCall.getModifiers();
                int timeout = fetchMethodCall.getTimeout();
                int maxResults = fetchMethodCall.getMaxResults();
                Object template = objectBuffer.readClassAndObject(entityData);
                int modifiers = originalModifiers | JSpace.RETURN_AS_BYTES;

                SpaceTransactionHolder holder = null;
                if (fetchMethodCall.getTransactionId() != 0)
                    holder = modificationContextFor(nodeRaised)
                            .getIfPresent(fetchMethodCall.getTransactionId());

                ByteBuffer[] buffers = (ByteBuffer[]) jSpace.fetch(holder, template, timeout, maxResults,
                        modifiers);
                if (buffers != null) {
                    byte[][] response = new byte[buffers.length][];
                    for (int i = 0; i < buffers.length; i++) {
                        ByteBuffer buffer = buffers[i];
                        response[i] = buffer.array();
                    }
                    fetchMethodCall.setResponseBody(objectBuffer.writeObjectData(response));
                }
                fetchMethodCall.reset();
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.NOTIFY.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                NotifyListenerMethodCall registerMethodCall = (NotifyListenerMethodCall) methodCall;
                byte[] entityData = registerMethodCall.getEntity();

                int originalModifiers = registerMethodCall.getModifiers();
                Object template = objectBuffer.readClassAndObject(entityData);
                int modifiers = originalModifiers | JSpace.RETURN_AS_BYTES;
                jSpace.notify(template, new SpaceNotificationListener() {

                    @Override
                    public void handleNotification(final Object entity, final SpaceOperation operation) {
                        ObjectBuffer innerObjectBuffer = new ObjectBuffer(
                                jSpace.getSpaceConfiguration().getKryo());
                        sendResponseBackAfterExecution(methodCall, new Runnable() {
                            @Override
                            public void run() {
                                NotifyListenerMethodCall methodCall = new NotifyListenerMethodCall();
                                methodCall.setEntity(((ByteBuffer) entity).array());
                                methodCall.setOperation(operation);
                            }
                        }, nodeRaised, innerObjectBuffer);
                    }
                }, modifiers);
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.SIZE.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                methodCall.setResponseBody(objectBuffer.writeObjectData(jSpace.size()));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.MB_USED.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                methodCall.setResponseBody(objectBuffer.writeObjectData(jSpace.mbUsed()));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.EVICT_ELEMENTS.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                EvictElementsMethodCall evictElementsMethodCall = (EvictElementsMethodCall) methodCall;
                methodCall.setResponseBody(objectBuffer
                        .writeObjectData(jSpace.evictElements(evictElementsMethodCall.getElements())));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.EVICT_PERCENTAGE.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                EvictPercentageMethodCall evictPercentageMethodCall = (EvictPercentageMethodCall) methodCall;
                methodCall.setResponseBody(objectBuffer
                        .writeObjectData(jSpace.evictPercentage(evictPercentageMethodCall.getPercentage())));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.EVICT_ALL.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                methodCall.setResponseBody(objectBuffer.writeObjectData(jSpace.evictAll()));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.SPACE_TOPOLOGY.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                methodCall.setResponseBody(
                        objectBuffer.writeObjectData(jSpace.getSpaceConfiguration().getTopology()));
            }
        }, nodeRaised, objectBuffer);
}

From source file:com.koda.integ.hbase.storage.FileExtStorage.java

@Override
public void flush() throws IOException {
    //TODO this method flashes only internal buffer 
    //and does not touch internal flusher queue
    LOG.info("Flushing internal buffer to the storage");
    long start = System.currentTimeMillis();
    writeLock.writeLock().lock();//w w  w .  j a va2  s . co  m
    try {
        ByteBuffer buf = activeBuffer.get();
        if (bufferOffset.get() == 0) {
            // skip flush
            LOG.info("Skipping flush");
            return;
        }
        if (buf != null) {
            if (buf.position() != 0)
                buf.flip();
            while (buf.hasRemaining()) {
                currentForWrite.getChannel().write(buf);
            }
            buf.clear();
            bufferOffset.set(0);
            // we advance to next file;
        } else {
            LOG.warn("Active buffer is NULL");
        }
    } catch (Exception e) {
        LOG.error(e);
    } finally {
        writeLock.writeLock().unlock();
        // Close file
        currentForWrite.close();
    }
    LOG.info("Flushing completed in " + (System.currentTimeMillis() - start) + "ms");
}

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

/**
 * Read external with codec.//  w ww. j a  v  a2  s.  c  o  m
 *
 * @param blockName the block name
 * @return the cacheable
 * @throws IOException Signals that an I/O exception has occurred.
 */
@SuppressWarnings("unused")
private Cacheable readExternalWithCodec(String blockName, boolean repeat, boolean caching) throws IOException {
    if (overflowExtEnabled == false)
        return null;
    // Check if we have  already this block in external storage cache
    try {
        // We use 16 - byte hash for external storage cache  
        byte[] hashed = Utils.hash128(blockName);

        StorageHandle handle = storage.newStorageHandle();
        byte[] data = (byte[]) extStorageCache.get(hashed);
        if (data == null) {
            if (repeat == false)
                extRefStats.miss(caching);
            return null;
        } else {
            extRefStats.hit(caching);
        }
        // Initialize handle 
        handle.fromBytes(data);

        ByteBuffer buffer = extStorageCache.getLocalBufferWithAddress().getBuffer();
        SerDe serde = extStorageCache.getSerDe();
        Codec codec = extStorageCache.getCompressionCodec();

        buffer.clear();

        StorageHandle newHandle = storage.getData(handle, buffer);
        if (buffer.position() > 0)
            buffer.flip();
        int size = buffer.getInt();
        if (size == 0) {
            // BIGBASE-45
            // Remove reference from reference cache
            // reference is in L3-RAM cache but no object in L3-DISK cache was found
            // remove only if handle is invalid
            if (storage.isValid(handle) == false) {
                extStorageCache.remove(hashed);
            }
            return null;
        }
        // Skip key
        int keySize = buffer.getInt();
        buffer.position(8 + keySize);

        boolean inMemory = buffer.get() == (byte) 1;

        buffer.limit(size + 4);
        Cacheable obj = (Cacheable) serde.readCompressed(buffer/*, codec*/);
        offHeapCache.put(blockName, obj);

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

        return obj;

    } catch (Throwable e) {
        fatalExternalReads.incrementAndGet();
        throw new IOException(e);
    }

}

From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java

/**
 * The weird constant above is to try to avoid mistakes in the painful (but
 * totally worth it) byte'ification method below. Using bytes as opposed to
 * json strings makes a non-trivial difference (~2x over json-smart and >4x
 * over org.json. So we just chuck json libraries and use our own byte[]
 * serializer for select packets./*  w  ww .  ja v  a2s .c  o  m*/
 * 
 * The serialization overhead really matters most for RequestPacket and
 * AcceptPacket. Every request, even with batching, must be deserialized by
 * the coordinator and must be serialized back while sending out the
 * AcceptPacket. The critical path is the following at a coordinator and is
 * incurred at least in part even with batching for every request: (1)
 * receive request, (2) send accept, (3) receive accept_replies, (4) send
 * commit Accordingly, we use byteification for {@link RequestPacket},
 * {@link AcceptPacket}, {@link BatchedAcceptReply} and
 * {@link BatchedCommit}.
 * 
 * */

protected byte[] toBytes(boolean instrument) {
    // return cached value if already present
    if ((this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT)
            && this.byteifiedSelf != null && !instrument)
        return this.byteifiedSelf;
    // check if we can use byteification at all; if not, use toString()
    if (!((BYTEIFICATION && IntegerMap.allInt()) || instrument)) {
        try {
            if (this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT)
                return this.byteifiedSelf = this.toString().getBytes(CHARSET); // cache
            return this.toString().getBytes(CHARSET);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
            return null;
        }
    }

    // else byteify
    try {
        int exactLength = 0;
        byte[] array = new byte[this.lengthEstimate()];
        ByteBuffer bbuf = ByteBuffer.wrap(array);
        assert (bbuf.position() == 0);

        // paxospacket stuff
        super.toBytes(bbuf);
        int ppPos = bbuf.position(); // for assertion
        assert (bbuf.position() == ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get()
                + SIZEOF_PAXOSPACKET_FIXED) : bbuf.position() + " != "
                        + ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get()
                        + SIZEOF_PAXOSPACKET_FIXED;
        exactLength += (bbuf.position());

        bbuf.putLong(this.requestID);
        bbuf.put(this.stop ? (byte) 1 : (byte) 0);
        exactLength += (Long.BYTES + 1);

        // addresses
        /* Note: 0 is ambiguous with wildcard address, but that's okay
         * because an incoming packet will never come with a wildcard
         * address. */
        bbuf.put(this.clientAddress != null ? this.clientAddress.getAddress().getAddress() : new byte[4]);
        // 0 (not -1) means invalid port
        bbuf.putShort(this.clientAddress != null ? (short) this.clientAddress.getPort() : 0);
        /* Note: 0 is an ambiguous wildcard address that could also be a
         * legitimate value of the listening socket address. If the request
         * happens to have no listening address, we will end up assuming it
         * was received on the wildcard address. At worst, the matching for
         * the corresponding response back to the client can fail. */
        bbuf.put(this.listenAddress != null ? this.listenAddress.getAddress().getAddress() : new byte[4]);
        // 0 (not -1) means invalid port
        bbuf.putShort(this.listenAddress != null ? (short) this.listenAddress.getPort() : 0);
        exactLength += 2 * (Integer.BYTES + Short.BYTES);

        // other non-final fields
        bbuf.putInt(this.entryReplica);
        bbuf.putLong(this.entryTime);
        bbuf.put(this.shouldReturnRequestValue ? (byte) 1 : (byte) 0);
        bbuf.putInt(this.forwardCount);
        exactLength += (Integer.BYTES + Long.BYTES + 1 + Integer.BYTES);

        // digest related fields: broadcasted, digest
        // whether this request was already broadcasted
        bbuf.put(this.broadcasted ? (byte) 1 : (byte) 0);
        exactLength += 1;
        assert (exactLength ==
        // where parent left us off
        ppPos + SIZEOF_REQUEST_FIXED
        // for the three int fields not yet filled
                - 4 * Integer.BYTES) : exactLength + " != [" + ppPos + " + " + SIZEOF_REQUEST_FIXED + " - "
                        + 4 * Integer.BYTES + "]";
        // digest length and digest iteself
        bbuf.putInt(this.digest != null ? this.digest.length : 0);
        exactLength += Integer.BYTES;
        if (this.digest != null)
            bbuf.put(this.digest);
        exactLength += (this.digest != null ? this.digest.length : 0);
        // /////////// end of digest related fields //////////

        // highly variable length fields
        // requestValue
        byte[] reqValBytes = this.requestValue != null ? this.requestValue.getBytes(CHARSET) : new byte[0];
        bbuf.putInt(reqValBytes != null ? reqValBytes.length : 0);
        bbuf.put(reqValBytes);
        exactLength += (4 + reqValBytes.length);

        // responseValue
        byte[] respValBytes = this.responseValue != null ? this.responseValue.getBytes(CHARSET) : new byte[0];
        bbuf.putInt(respValBytes != null ? respValBytes.length : 0);
        bbuf.put(respValBytes);
        exactLength += (4 + respValBytes.length);

        // batched requests batchSize|(length:batchedReqBytes)+
        bbuf.putInt(this.batchSize());
        exactLength += (4);
        if (this.batchSize() > 0)
            for (RequestPacket req : this.batched) {
                byte[] element = req.toBytes();
                bbuf.putInt(element.length);
                bbuf.put(element);
                exactLength += (4 + element.length);
            }

        // bbuf.array() was a generous allocation
        byte[] exactBytes = new byte[exactLength];
        bbuf.flip();
        assert (bbuf.remaining() == exactLength) : bbuf.remaining() + " != " + exactLength;
        bbuf.get(exactBytes);

        if (this.getType() == PaxosPacketType.REQUEST)
            this.byteifiedSelf = exactBytes;

        return exactBytes;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

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

private static void padRowBuffer(ByteBuffer buffer, int minRowSize, int trailerSize) {
    int pos = buffer.position();
    if ((pos + trailerSize) < minRowSize) {
        // pad the row to get to the min byte size
        int padSize = minRowSize - (pos + trailerSize);
        ByteUtil.clearRange(buffer, pos, pos + padSize);
        ByteUtil.forward(buffer, padSize);
    }//from   ww w  . j a  v a  2 s .c  om
}

From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java

public RequestPacket(ByteBuffer bbuf) throws UnsupportedEncodingException, UnknownHostException {
    super(bbuf);/* w  ww  .  j  a va2  s  . c  om*/
    int exactLength = bbuf.position();

    this.requestID = bbuf.getLong();
    this.stop = bbuf.get() == (byte) 1;
    exactLength += (8 + 1);

    // addresses
    byte[] ca = new byte[4];
    bbuf.get(ca);
    int cport = (int) bbuf.getShort();
    cport = cport >= 0 ? cport : cport + 2 * (Short.MAX_VALUE + 1);
    this.clientAddress = cport != 0 ? new InetSocketAddress(InetAddress.getByAddress(ca), cport) : null;
    byte[] la = new byte[4];
    bbuf.get(la);
    int lport = (int) bbuf.getShort();
    lport = lport >= 0 ? lport : lport + 2 * (Short.MAX_VALUE + 1);
    this.listenAddress = lport != 0 ? new InetSocketAddress(InetAddress.getByAddress(la), lport) : null;
    exactLength += (4 + 2 + 4 + 2);

    // other non-final fields
    this.entryReplica = bbuf.getInt();
    this.entryTime = bbuf.getLong();
    this.shouldReturnRequestValue = bbuf.get() == (byte) 1;
    this.forwardCount = bbuf.getInt();
    exactLength += (4 + 8 + 1 + 4);

    // digest related fields
    this.broadcasted = bbuf.get() == (byte) 1;
    int digestLength = bbuf.getInt();
    if (digestLength > 0)
        bbuf.get(this.digest = new byte[digestLength]);

    // highly variable length fields

    // requestValue
    int reqValLen = bbuf.getInt();
    byte[] reqValBytes = new byte[reqValLen];
    bbuf.get(reqValBytes);
    this.requestValue = reqValBytes.length > 0 ? new String(reqValBytes, CHARSET) : null;
    exactLength += (4 + reqValBytes.length);

    // responseValue
    int respValLen = bbuf.getInt();
    byte[] respValBytes = new byte[respValLen];
    bbuf.get(respValBytes);
    this.responseValue = respValBytes.length > 0 ? new String(respValBytes, CHARSET) : null;
    exactLength += (4 + respValBytes.length);

    int numBatched = bbuf.getInt();
    if (numBatched == 0)
        return;
    // else
    // batched requests
    this.batched = new RequestPacket[numBatched];
    for (int i = 0; i < numBatched; i++) {
        int len = bbuf.getInt();
        byte[] element = new byte[len];
        bbuf.get(element);
        this.batched[i] = new RequestPacket(element);
    }
    assert (exactLength > 0);
}