Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

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

Prototype

public abstract byte get();

Source Link

Document

Returns the byte at the current position and increases the position by 1.

Usage

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

protected PaxosPacket(ByteBuffer bbuf) throws UnsupportedEncodingException, UnknownHostException {
    super(PaxosPacketType.PAXOS_PACKET);

    bbuf.getInt(); // packet type
    this.packetType = PaxosPacketType.getPaxosPacketType(bbuf.getInt());
    this.version = bbuf.getInt();
    byte paxosIDLength = bbuf.get();
    byte[] paxosIDBytes = new byte[paxosIDLength];
    bbuf.get(paxosIDBytes);/*  w w  w. j  a va  2 s .  c  o  m*/
    this.paxosID = paxosIDBytes.length > 0 ? new String(paxosIDBytes, CHARSET) : null;
    int exactLength = (4 + 4 + 4 + 1 + paxosIDBytes.length);
    assert (bbuf.position() == exactLength);
}

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

/**
 * Read in the page numbers in this inline map
 *//*from  w  w w .j  a va  2  s. c  o m*/
protected void processMap(ByteBuffer buffer, int bufferStartPage) {
    int byteCount = 0;
    while (buffer.hasRemaining()) {
        byte b = buffer.get();
        if (b != (byte) 0) {
            for (int i = 0; i < 8; i++) {
                if ((b & (1 << i)) != 0) {
                    int pageNumberOffset = (byteCount * 8 + i) + bufferStartPage;
                    int pageNumber = bitIndexToPageNumber(pageNumberOffset, PageChannel.INVALID_PAGE_NUMBER);
                    if (!isPageWithinRange(pageNumber)) {
                        throw new IllegalStateException("found page number " + pageNumber
                                + " in usage map outside of expected range " + _startPage + " to " + _endPage);
                    }
                    _pageNumbers.set(pageNumberOffset);
                }
            }
        }
        byteCount++;
    }
}

From source file:com.sonymobile.android.media.internal.VUParser.java

protected boolean parseODSMData(IsoTrack odsmTrack) {
    int kObjectSize = 11;
    SampleTable sampleTable = odsmTrack.getSampleTable();
    if (sampleTable.getSampleCount() > 1) {
        // TODO: Should multiple entries be supported?
        return false;
    }/*  w w w . j a  va2  s.c o m*/
    mSinfList = new ArrayList<SinfData>(2);

    ByteBuffer stszData = sampleTable.getStszData();
    stszData.rewind();
    stszData.getInt(); // version and flags

    int dataSize = stszData.getInt();
    if (dataSize == 0) {
        stszData.getInt(); // sample_count
        dataSize = stszData.getInt();
    }

    byte[] data = new byte[dataSize];
    try {
        ByteBuffer stcoData = sampleTable.getStcoData();
        stcoData.rewind();

        stcoData.getInt(); // version and flags
        stcoData.getInt(); // entry_count

        long sampleOffset = 0;
        if (sampleTable.isUsingLongChunkOffsets()) {
            sampleOffset = stcoData.getLong();
        } else {
            sampleOffset = 0xFFFFFFFFL & stcoData.getInt();
        }

        mDataSource.readAt(sampleOffset, data, dataSize);
        ByteBuffer dataBuffer = ByteBuffer.wrap(data);
        byte updateTag = dataBuffer.get();
        if (updateTag != 1) {
            return false;
        }
        int size = 0;
        int sizePart = 0;
        do {
            sizePart = (dataBuffer.get() & 0xFF);
            size = ((size << 7) & 0xFFFFFF80) | (sizePart & 0x7F);
        } while (sizePart > 128);
        while (size >= kObjectSize) {
            byte descriptorTag = dataBuffer.get();
            if (descriptorTag != 17) {
                // not mp4 descriptor
                return false;
            }
            dataBuffer.get(); // ODLength
            dataBuffer.getShort(); // 10 bit ObjectDescriptorID, 1 bit
                                   // URL_FLAG and 5 bit reserved

            byte esTag = dataBuffer.get();
            if (esTag != 0x0F) {
                return false;
            }
            dataBuffer.get(); // ES Length
            short esTrackReferenceIndex = dataBuffer.getShort();
            byte ipmpDescriptorPointer = dataBuffer.get();
            if (ipmpDescriptorPointer != 0x0A) {
                // unexpected pointer
                return false;
            }
            dataBuffer.get(); // ipmpLength
            byte ipmpDescriptorId = dataBuffer.get();
            SinfData sinfData = new SinfData();
            sinfData.esIdReference = esTrackReferenceIndex;
            sinfData.ipmpDescriptorId = ipmpDescriptorId;
            mSinfList.add(sinfData);
            size -= kObjectSize;
        }
        dataBuffer.get(); // IPMP Descriptor Update Tag
        int sinfCount = mSinfList.size();
        size = 0;
        sizePart = 0;
        do {
            sizePart = (dataBuffer.get() & 0xFF);
            size = ((size << 7) & 0xFFFFFF80) | (sizePart & 0x7F);
        } while (sizePart > 128);
        while (size > 0) {
            dataBuffer.get(); // IPMP Descriptor Tag
            int ipmpByteCount = 1;
            int ipmpLength = 0;
            sizePart = 0;
            do {
                sizePart = (dataBuffer.get() & 0xFF);
                ipmpByteCount++;
                ipmpLength = ((ipmpLength << 7) & 0xFFFFFF80) | (sizePart & 0x7F);
            } while (sizePart > 128);
            ipmpByteCount += ipmpLength;
            byte ipmpDescriptorId = dataBuffer.get();
            dataBuffer.getShort(); // IPMPS Type
            byte[] ipmpData = new byte[ipmpLength - 3];
            dataBuffer.get(ipmpData);
            SinfData sinfData = null;
            for (int i = 0; i < sinfCount; i++) {
                sinfData = mSinfList.get(i);
                if (sinfData.ipmpDescriptorId == ipmpDescriptorId) {
                    sinfData.ipmpData = new byte[ipmpData.length];
                    for (int j = 0; j < ipmpData.length; j++) {
                        sinfData.ipmpData[j] = ipmpData[j];
                    }
                    break;
                }
            }
            size -= ipmpByteCount;
        }
        int ipmpDataLength = 0;
        for (int i = 0; i < sinfCount; i++) {
            SinfData sinfData = mSinfList.get(i);
            ipmpDataLength += sinfData.ipmpData.length;
        }

        int ipmpMetaDataLength = 16 // MARLIN_SYSTEM_ID
                + 4 // size of all SINF data
                + 4 // size of SINF box id
                + 4 * sinfCount // trackIndex * sinfCount
                + 4 * sinfCount // ipmpLength * sinfCount
                + ipmpDataLength; // size of ipmpData
        byte[] ipmpMetaData = new byte[ipmpMetaDataLength];
        int offset = 16;

        for (int i = 0; i < offset; i++) {
            int hexVal = Integer.parseInt(Util.MARLIN_SYSTEM_ID.substring(i * 2, i * 2 + 2), 16);
            ipmpMetaData[i] = (byte) hexVal;
        }
        ipmpMetaData[offset++] = (byte) ((ipmpDataLength >> 24) & 0xFF);
        ipmpMetaData[offset++] = (byte) ((ipmpDataLength >> 16) & 0xFF);
        ipmpMetaData[offset++] = (byte) ((ipmpDataLength >> 8) & 0xFF);
        ipmpMetaData[offset++] = (byte) (ipmpDataLength & 0xFF);
        ipmpMetaData[offset++] = 0x73; // S
        ipmpMetaData[offset++] = 0x69; // I
        ipmpMetaData[offset++] = 0x6E; // N
        ipmpMetaData[offset++] = 0x66; // F

        int numTracks = mTracks.size();
        for (int i = 0; i < numTracks; i++) {
            IsoTrack track = (IsoTrack) mTracks.get(i);
            for (int j = 0; j < sinfCount; j++) {
                SinfData sinfData = mSinfList.get(j);
                if (sinfData.esIdReference == track.getTrackId()) {
                    track.getMetaData().addValue(MetaData.KEY_IPMP_DATA, sinfData.ipmpData);
                    // track index
                    ipmpMetaData[offset++] = (byte) ((i >> 24) & 0xFF);
                    ipmpMetaData[offset++] = (byte) ((i >> 16) & 0xFF);
                    ipmpMetaData[offset++] = (byte) ((i >> 8) & 0xFF);
                    ipmpMetaData[offset++] = (byte) (i & 0xFF);

                    // sinf data length
                    ipmpMetaData[offset++] = (byte) ((sinfData.ipmpData.length >> 24) & 0xFF);
                    ipmpMetaData[offset++] = (byte) ((sinfData.ipmpData.length >> 16) & 0xFF);
                    ipmpMetaData[offset++] = (byte) ((sinfData.ipmpData.length >> 8) & 0xFF);
                    ipmpMetaData[offset++] = (byte) (sinfData.ipmpData.length & 0xFF);

                    System.arraycopy(sinfData.ipmpData, 0, ipmpMetaData, offset, sinfData.ipmpData.length);

                    byte[] tempData = new byte[4 + sinfData.ipmpData.length];
                    tempData[0] = (byte) ((sinfData.ipmpData.length >> 24) & 0xFF);
                    tempData[1] = (byte) ((sinfData.ipmpData.length >> 16) & 0xFF);
                    tempData[2] = (byte) ((sinfData.ipmpData.length >> 8) & 0xFF);
                    tempData[3] = (byte) (sinfData.ipmpData.length & 0xFF);
                    System.arraycopy(sinfData.ipmpData, 0, tempData, 4, sinfData.ipmpData.length);

                    // Create JSON for this track
                    String jsonData = null;
                    try {
                        jsonData = Util.getJSONIPMPData(tempData);
                    } catch (JSONException e) {
                        if (LOGS_ENABLED)
                            Log.e(TAG, "Exception when creating JSON object" + e);
                        return false;
                    }
                    track.getMediaFormat().setString(KEY_DRM_UUID, Util.MARLIN_SYSTEM_ID);
                    track.getMediaFormat().setString(KEY_MARLIN_JSON, jsonData);

                    offset += sinfData.ipmpData.length;
                    break;
                }
            }
        }

        mIpmpMetaData = ipmpMetaData;

        addMetaDataValue(KEY_IPMP_DATA, mIpmpMetaData);

        mCurrentTrack.getMetaData().addValue(KEY_MIME_TYPE, MimeType.OCTET_STREAM);

    } catch (IOException e) {
        if (LOGS_ENABLED)
            Log.e(TAG, "IOException when parsing ODSM data");
    }
    return true;
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReaderSpi.java

@Override
public boolean canDecodeInput(BufferedInputStream stream) throws IOException {
    if (stream == null) {
        throw new IllegalArgumentException("file == null!");
    }//from  www . j a  va  2 s  .c  om

    dbgLog.fine("applying the por test\n");

    byte[] b = new byte[POR_HEADER_SIZE];

    if (stream.markSupported()) {
        stream.mark(0);
    }

    int nbytes = stream.read(b, 0, POR_HEADER_SIZE);

    //printHexDump(b, "hex dump of the byte-array");

    if (nbytes == 0) {
        throw new IOException();
    } else if (nbytes < 491) {
        // size test
        dbgLog.fine("this file is NOT spss-por type");
        return false;
    }

    if (stream.markSupported()) {
        stream.reset();
    }

    boolean DEBUG = false;

    //windows [0D0A]=>   [1310] = [CR/LF]
    //unix    [0A]  =>   [10]
    //mac     [0D]  =>   [13]
    // 3char  [0D0D0A]=> [131310] spss for windows rel 15
    // expected results
    // unix    case: [0A]   : [80], [161], [242], [323], [404], [485]
    // windows case: [0D0A] : [81], [163], [245], [327], [409], [491]
    //  : [0D0D0A] : [82], [165], [248], [331], [414], [495]

    // convert b into a ByteBuffer

    ByteBuffer buff = ByteBuffer.wrap(b);
    byte[] nlch = new byte[36];
    int pos1;
    int pos2;
    int pos3;
    int ucase = 0;
    int wcase = 0;
    int mcase = 0;
    int three = 0;
    int nolines = 6;
    int nocols = 80;
    for (int i = 0; i < nolines; ++i) {
        int baseBias = nocols * (i + 1);
        // 1-char case
        pos1 = baseBias + i;
        buff.position(pos1);
        dbgLog.finer("\tposition(1)=" + buff.position());
        int j = 6 * i;
        nlch[j] = buff.get();

        if (nlch[j] == 10) {
            ucase++;
        } else if (nlch[j] == 13) {
            mcase++;
        }

        // 2-char case
        pos2 = baseBias + 2 * i;
        buff.position(pos2);
        dbgLog.finer("\tposition(2)=" + buff.position());

        nlch[j + 1] = buff.get();
        nlch[j + 2] = buff.get();

        // 3-char case
        pos3 = baseBias + 3 * i;
        buff.position(pos3);
        dbgLog.finer("\tposition(3)=" + buff.position());

        nlch[j + 3] = buff.get();
        nlch[j + 4] = buff.get();
        nlch[j + 5] = buff.get();

        dbgLog.finer(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]);
        dbgLog.finer(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]);

        if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) {
            three++;
        } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) {
            wcase++;
        }

        buff.rewind();
    }
    if (three == nolines) {
        dbgLog.fine("0D0D0A case");
        windowsNewLine = false;
    } else if ((ucase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0A case");
        windowsNewLine = false;
    } else if ((ucase < nolines) && (wcase == nolines)) {
        dbgLog.fine("0D0A case");
    } else if ((mcase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0D case");
        windowsNewLine = false;
    }

    buff.rewind();
    int PORmarkPosition = POR_MARK_POSITION_DEFAULT;
    if (windowsNewLine) {
        PORmarkPosition = PORmarkPosition + 5;
    } else if (three == nolines) {
        PORmarkPosition = PORmarkPosition + 10;
    }

    byte[] pormark = new byte[8];
    buff.position(PORmarkPosition);
    buff.get(pormark, 0, 8);
    String pormarks = new String(pormark);

    //dbgLog.fine("pormark =>" + pormarks + "<-");
    dbgLog.fine(
            "pormark[hex: 53 50 53 53 50 4F 52 54 == SPSSPORT] =>" + new String(Hex.encodeHex(pormark)) + "<-");

    if (pormarks.equals(POR_MARK)) {
        dbgLog.fine("this file is spss-por type");
        return true;
    } else {
        dbgLog.fine("this file is NOT spss-por type");
    }
    return false;
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReaderSpi.java

@Override
public boolean canDecodeInput(Object source) throws IOException {
    if (!(source instanceof BufferedInputStream)) {
        return false;
    }/*from w  w  w .  j  av a  2 s.c o  m*/
    if (source == null) {
        throw new IllegalArgumentException("source == null!");
    }
    BufferedInputStream stream = (BufferedInputStream) source;
    dbgLog.fine("applying the por test\n");

    byte[] b = new byte[POR_HEADER_SIZE];

    if (stream.markSupported()) {
        stream.mark(0);
    }

    int nbytes = stream.read(b, 0, POR_HEADER_SIZE);

    //printHexDump(b, "hex dump of the byte-array");

    if (nbytes == 0) {
        throw new IOException();
    } else if (nbytes < 491) {
        // size test
        dbgLog.fine("this file is NOT spss-por type");
        return false;
    }

    if (stream.markSupported()) {
        stream.reset();
    }

    boolean DEBUG = false;

    //windows [0D0A]=>   [1310] = [CR/LF]
    //unix    [0A]  =>   [10]
    //mac     [0D]  =>   [13]
    // 3char  [0D0D0A]=> [131310] spss for windows rel 15
    // expected results
    // unix    case: [0A]   : [80], [161], [242], [323], [404], [485]
    // windows case: [0D0A] : [81], [163], [245], [327], [409], [491]
    //  : [0D0D0A] : [82], [165], [248], [331], [414], [495]

    // convert b into a ByteBuffer

    ByteBuffer buff = ByteBuffer.wrap(b);
    byte[] nlch = new byte[36];
    int pos1;
    int pos2;
    int pos3;
    int ucase = 0;
    int wcase = 0;
    int mcase = 0;
    int three = 0;
    int nolines = 6;
    int nocols = 80;
    for (int i = 0; i < nolines; ++i) {
        int baseBias = nocols * (i + 1);
        // 1-char case
        pos1 = baseBias + i;
        buff.position(pos1);
        dbgLog.finer("\tposition(1)=" + buff.position());
        int j = 6 * i;
        nlch[j] = buff.get();

        if (nlch[j] == 10) {
            ucase++;
        } else if (nlch[j] == 13) {
            mcase++;
        }

        // 2-char case
        pos2 = baseBias + 2 * i;
        buff.position(pos2);
        dbgLog.finer("\tposition(2)=" + buff.position());

        nlch[j + 1] = buff.get();
        nlch[j + 2] = buff.get();

        // 3-char case
        pos3 = baseBias + 3 * i;
        buff.position(pos3);
        dbgLog.finer("\tposition(3)=" + buff.position());

        nlch[j + 3] = buff.get();
        nlch[j + 4] = buff.get();
        nlch[j + 5] = buff.get();

        dbgLog.finer(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]);
        dbgLog.finer(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]);

        if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) {
            three++;
        } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) {
            wcase++;
        }

        buff.rewind();
    }
    if (three == nolines) {
        dbgLog.fine("0D0D0A case");
        windowsNewLine = false;
    } else if ((ucase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0A case");
        windowsNewLine = false;
    } else if ((ucase < nolines) && (wcase == nolines)) {
        dbgLog.fine("0D0A case");
    } else if ((mcase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0D case");
        windowsNewLine = false;
    }

    buff.rewind();
    int PORmarkPosition = POR_MARK_POSITION_DEFAULT;
    if (windowsNewLine) {
        PORmarkPosition = PORmarkPosition + 5;
    } else if (three == nolines) {
        PORmarkPosition = PORmarkPosition + 10;
    }

    byte[] pormark = new byte[8];
    buff.position(PORmarkPosition);
    buff.get(pormark, 0, 8);
    String pormarks = new String(pormark);

    dbgLog.fine(
            "pormark[hex: 53 50 53 53 50 4F 52 54 == SPSSPORT] =>" + new String(Hex.encodeHex(pormark)) + "<-");

    if (pormarks.equals(POR_MARK)) {
        dbgLog.fine("this file is spss-por type");
        return true;
    } else {
        dbgLog.fine("this file is NOT spss-por type");
    }
    return false;
}

From source file:com.l2jfree.network.mmocore.ReadWriteThread.java

private void parseClientPacket(ByteBuffer buf, int dataSize, T client) {
    final int pos = buf.position();
    final DataSizeHolder dsh = getDataSizeHolder().init(dataSize);

    if (client.decipher(buf, dsh) && buf.hasRemaining()) {
        // remove useless bytes
        dsh.decreaseSize(dsh.getMinPadding());
        // calculate possibly remaining useless bytes
        final int maxPossiblePadding = dsh.getMaxPadding() - dsh.getMinPadding();

        // apply limit
        final int limit = buf.limit();
        buf.limit(pos + dsh.getSize());/*w  w w  .  ja va2 s.  com*/

        final int opcode = buf.get() & 0xFF;

        if (getMMOController().canReceivePacketFrom(client, opcode)) {
            RP cp = getPacketHandler().handlePacket(buf, client, opcode);

            if (cp != null) {
                System.out.println("READ: " + client.getState() + " " + cp.getClass().getSimpleName());

                // remove useless bytes #2, using packet specs
                int maxLeftoverPadding = maxPossiblePadding;
                final int overflow = buf.remaining() - cp.getMaximumLength();
                if (maxPossiblePadding > 0 && // there may be useless bytes
                        overflow > 0) // and we have too much
                {
                    // avoid any damage to the packet body
                    final int removable = Math.min(overflow, maxPossiblePadding);
                    buf.limit(buf.limit() - removable);
                    maxLeftoverPadding -= removable;
                }

                getMmoBuffer().setByteBuffer(buf);
                cp.setClient(client);

                try {
                    if (getMmoBuffer().getAvailableBytes() < cp.getMinimumLength()) {
                        getMMOController().report(ErrorMode.BUFFER_UNDER_FLOW, client, cp, null);
                    } else if (getMmoBuffer().getAvailableBytes() > cp.getMaximumLength()) {
                        getMMOController().report(ErrorMode.BUFFER_OVER_FLOW, client, cp, null);
                    } else {
                        cp.read(getMmoBuffer());

                        client.executePacket(cp);

                        if (buf.hasRemaining() && // some unused data, a bad sign
                                buf.remaining() > maxLeftoverPadding) // and definitely not padded bytes
                        {
                            // FIXME disabled until packet structures updated properly
                            //report(ErrorMode.BUFFER_OVER_FLOW, client, cp, null);

                            MMOController._log.info("Invalid packet format (buf: " + buf + ", dataSize: "
                                    + dataSize + ", pos: " + pos + ", limit: " + limit + ", opcode: 0x"
                                    + HexUtil.fillHex(opcode, 2) + ") used for reading - " + client + " - "
                                    + cp.getType() + " - " + getMMOController().getVersionInfo());
                        }
                    }
                } catch (BufferUnderflowException e) {
                    getMMOController().report(ErrorMode.BUFFER_UNDER_FLOW, client, cp, e);
                } catch (RuntimeException e) {
                    getMMOController().report(ErrorMode.FAILED_READING, client, cp, e);
                }

                getMmoBuffer().setByteBuffer(null);
            }
        }

        buf.limit(limit);
    }
}

From source file:com.yobidrive.diskmap.needles.Needle.java

public boolean getNeedleHeaderFromBuffer(ByteBuffer input) throws Exception {
    try {/*  ww  w.  j a  v a 2 s . c om*/
        // Reinit needle
        keyBytes = null;
        version = null;
        flags = 0x00;
        size = 0;
        data = null;
        previousNeedle = null; // Chaining
        readBytes = 0;
        // Processes reading
        input.rewind();
        int startPosition = input.position();
        int magic = input.getInt();
        if (magic == MAGICSTART_BADENDIAN) {
            if (input.order().equals(ByteOrder.BIG_ENDIAN))
                input.order(ByteOrder.LITTLE_ENDIAN);
            else
                input.order(ByteOrder.BIG_ENDIAN);
        } else if (magic != MAGICSTART) {
            logger.error("Buffer not starting with needle");
            return false;
        }
        needleNumber = input.getLong();
        flags = input.get();
        int keyLen = input.getInt();
        if (keyLen > 2028) {
            logger.error("Crazy needle key len");
            return false;
        }
        keyBytes = new byte[keyLen];
        input.get(keyBytes);
        int versionLen = input.getInt();
        if (versionLen > 1024 * 16) {
            logger.error("Crazy needle version len");
            return false;
        }
        if (versionLen == 0)
            version = null;
        else {
            byte[] versionBytes = new byte[versionLen];
            input.get(versionBytes);
            version = new VectorClock(versionBytes);
        }
        int previousLogNumber = input.getInt(); // Chaining
        long previousNeedleOffset = input.getLong(); // Chaining
        if (previousLogNumber != -1 && previousNeedleOffset != -1L) {
            previousNeedle = new NeedlePointer();
            previousNeedle.setNeedleFileNumber(previousLogNumber);
            previousNeedle.setNeedleOffset(previousNeedleOffset);
        }
        originalFileNumber = input.getInt(); // Original needle location (for cleaning)
        originalSize = input.getInt(); // Original needle size (for cleaning)
        size = input.getInt();

        readBytes = input.position() - startPosition;
        input.rewind();
        // input.mark() ;
        return true;
    } catch (BufferUnderflowException bue) {
        return false;
    }
}

From source file:edu.hawaii.soest.kilonalu.flntu.FLNTUSource.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.
 *//* w w w. ja va 2 s .com*/
protected boolean execute() {
    logger.debug("FLNTUSource.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.
                switch (state) {

                case 0:

                    // sample sets begin with 'mvs 1\r\n' and end with 'mvs 0\r\n'.  Find the 
                    // beginning of the sample set using the 4-byte window (s 1\r\n)
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0A && byteTwo == 0x0D && byteThree == 0x31 && byteFour == 0x20) {
                        // we've found the beginning of a sample set, 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 byte (\r\n)
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0A && byteTwo == 0x0D && byteThree == 0x30 && byteFour == 0x20) {

                        // we've found the sample set ending, clear buffers and return
                        // to state 0 to wait for the next set
                        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;

                        // if we're not at the sample set end, look for individual samples    
                    } else if (byteOne == 0x0A && byteTwo == 0x0D) {

                        // found the sample ending delimiter
                        // add in the sample delimiter to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);
                            sampleByteCount++;
                        } else {
                            sampleBuffer.compact();
                            logger.debug("Compacting sampleBuffer ...");
                            sampleBuffer.put(byteOne);
                            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.");
                        break;

                    } else { // not 0x0

                        // 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:com.koda.integ.hbase.blockcache.OffHeapBlockCacheOld.java

/**
 * Read external with codec./*from  w ww.jav  a  2  s .c o m*/
 *
 * @param blockName the block name
 * @return the cacheable
 * @throws IOException Signals that an I/O exception has occurred.
 */
private Cacheable readExternalWithCodec(String blockName) 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 = (StorageHandle) extStorageCache.get(hashed);
        if (handle == null)
            return null;
        ByteBuffer buffer = extStorageCache.getLocalBufferWithAddress().getBuffer();
        SerDe serde = extStorageCache.getSerDe();
        @SuppressWarnings("unused")
        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)
            return null;
        // Skip key
        int keySize = buffer.getInt();
        buffer.position(8 + keySize);
        boolean inMemory = buffer.get() == (byte) 1;

        //buffer.position(5);
        buffer.limit(size + 4);
        Cacheable obj = (Cacheable) serde.readCompressed(buffer/*, codec*/);
        if (inMemory) {
            permGenCache.put(blockName, obj);
        } else {
            tenGenCache.put(blockName, obj);
        }
        if (newHandle.equals(handle) == false) {
            extStorageCache.put(hashed, newHandle);
        }

        return obj;

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

}

From source file:org.dcache.chimera.JdbcFs.java

@Override
public FsInode inodeFromBytes(byte[] handle) throws ChimeraFsException {
    FsInode inode;/*  www  .  j a va  2  s.  c o  m*/

    if (handle.length < MIN_HANDLE_LEN) {
        throw new FileNotFoundHimeraFsException("File handle too short");
    }

    ByteBuffer b = ByteBuffer.wrap(handle);
    int fsid = b.get();
    int type = b.get();
    int len = b.get(); // eat the file id size.
    long ino = b.getLong();
    int opaqueLen = b.get();
    if (opaqueLen > b.remaining()) {
        throw new FileNotFoundHimeraFsException("Bad Opaque len");
    }

    byte[] opaque = new byte[opaqueLen];
    b.get(opaque);

    FsInodeType inodeType = FsInodeType.valueOf(type);

    switch (inodeType) {
    case INODE:
        int level = Integer.parseInt(new String(opaque));
        inode = new FsInode(this, ino, level);
        break;

    case ID:
        inode = new FsInode_ID(this, ino);
        break;

    case TAGS:
        inode = new FsInode_TAGS(this, ino);
        break;

    case TAG:
        String tag = new String(opaque);
        inode = new FsInode_TAG(this, ino, tag);
        break;

    case NAMEOF:
        inode = new FsInode_NAMEOF(this, ino);
        break;
    case PARENT:
        inode = new FsInode_PARENT(this, ino);
        break;

    case PATHOF:
        inode = new FsInode_PATHOF(this, ino);
        break;

    case CONST:
        inode = new FsInode_CONST(this, ino);
        break;

    case PSET:
        inode = new FsInode_PSET(this, ino, getArgs(opaque));
        break;

    case PCUR:
        inode = new FsInode_PCUR(this, ino);
        break;

    case PLOC:
        inode = new FsInode_PLOC(this, ino);
        break;

    case PCRC:
        inode = new FsInode_PCRC(this, ino);
        break;

    default:
        throw new FileNotFoundHimeraFsException("Unsupported file handle type: " + inodeType);
    }
    return inode;
}