Example usage for java.nio ByteBuffer order

List of usage examples for java.nio ByteBuffer order

Introduction

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

Prototype

Endianness order

To view the source code for java.nio ByteBuffer order.

Click Source Link

Document

The byte order of this buffer, default is BIG_ENDIAN .

Usage

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private GBDeviceEvent[] decodeDatalog(ByteBuffer buf, short length) {
    boolean ack = true;
    byte command = buf.get();
    byte id = buf.get();
    GBDeviceEventDataLogging devEvtDataLogging = null;
    switch (command) {
    case DATALOG_TIMEOUT:
        LOG.info("DATALOG TIMEOUT. id=" + (id & 0xff) + " - ignoring");
        return null;
    case DATALOG_SENDDATA:
        buf.order(ByteOrder.LITTLE_ENDIAN);
        int items_left = buf.getInt();
        int crc = buf.getInt();
        DatalogSession datalogSession = mDatalogSessions.get(id);
        LOG.info("DATALOG SENDDATA. id=" + (id & 0xff) + ", items_left=" + items_left + ", total length="
                + (length - 10));//from   ww w.ja v a  2s.c o  m
        if (datalogSession != null) {
            LOG.info("DATALOG UUID=" + datalogSession.uuid + ", tag=" + datalogSession.tag
                    + datalogSession.getTaginfo() + ", itemSize=" + datalogSession.itemSize + ", itemType="
                    + datalogSession.itemType);
            if (!datalogSession.uuid.equals(UUID_ZERO) && datalogSession.getClass().equals(DatalogSession.class)
                    && mEnablePebbleKit) {
                devEvtDataLogging = datalogSession.handleMessageForPebbleKit(buf, length - 10);
                if (devEvtDataLogging == null) {
                    ack = false;
                }
            } else {
                ack = datalogSession.handleMessage(buf, length - 10);
            }
        }
        break;
    case DATALOG_OPENSESSION:
        UUID uuid = getUUID(buf);
        buf.order(ByteOrder.LITTLE_ENDIAN);
        int timestamp = buf.getInt();
        int log_tag = buf.getInt();
        byte item_type = buf.get();
        short item_size = buf.getShort();
        LOG.info("DATALOG OPENSESSION. id=" + (id & 0xff) + ", App UUID=" + uuid.toString() + ", log_tag="
                + log_tag + ", item_type=" + item_type + ", itemSize=" + item_size);
        if (!mDatalogSessions.containsKey(id)) {
            if (uuid.equals(UUID_ZERO) && log_tag == 81) {
                mDatalogSessions.put(id, new DatalogSessionHealthSteps(id, uuid, timestamp, log_tag, item_type,
                        item_size, getDevice()));
            } else if (uuid.equals(UUID_ZERO) && log_tag == 83) {
                mDatalogSessions.put(id, new DatalogSessionHealthSleep(id, uuid, timestamp, log_tag, item_type,
                        item_size, getDevice()));
            } else if (uuid.equals(UUID_ZERO) && log_tag == 84) {
                mDatalogSessions.put(id, new DatalogSessionHealthOverlayData(id, uuid, timestamp, log_tag,
                        item_type, item_size, getDevice()));
            } else if (uuid.equals(UUID_ZERO) && log_tag == 85) {
                mDatalogSessions.put(id, new DatalogSessionHealthHR(id, uuid, timestamp, log_tag, item_type,
                        item_size, getDevice()));
            } else {
                mDatalogSessions.put(id,
                        new DatalogSession(id, uuid, timestamp, log_tag, item_type, item_size));
            }
        }
        break;
    case DATALOG_CLOSE:
        LOG.info("DATALOG_CLOSE. id=" + (id & 0xff));
        datalogSession = mDatalogSessions.get(id);
        if (datalogSession != null) {
            if (!datalogSession.uuid.equals(UUID_ZERO) && datalogSession.getClass().equals(DatalogSession.class)
                    && mEnablePebbleKit) {
                GBDeviceEventDataLogging dataLogging = new GBDeviceEventDataLogging();
                dataLogging.command = GBDeviceEventDataLogging.COMMAND_FINISH_SESSION;
                dataLogging.appUUID = datalogSession.uuid;
                dataLogging.tag = datalogSession.tag;
                devEvtDataLogging = dataLogging;
            }
            mDatalogSessions.remove(id);
        }
        break;
    default:
        LOG.info("unknown DATALOG command: " + (command & 0xff));
        break;
    }
    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
    if (ack) {
        LOG.info("sending ACK (0x85)");
        sendBytes.encodedBytes = encodeDatalog(id, DATALOG_ACK);
    } else {
        LOG.info("sending NACK (0x86)");
        sendBytes.encodedBytes = encodeDatalog(id, DATALOG_NACK);
    }
    // append ack/nack
    return new GBDeviceEvent[] { devEvtDataLogging, sendBytes };
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.sav.SAVFileReader.java

private int[] parseRT7SubTypefieldHeader(BufferedInputStream stream) throws IOException {
    int length_unit_length = 4;
    int length_number_of_units = 4;
    int storage_size = length_unit_length + length_number_of_units;

    int[] headerSection = new int[2];

    byte[] byteStorage = new byte[storage_size];

    try {/*from  ww w .ja  v a 2  s  . c  o  m*/
        int nbytes = stream.read(byteStorage);
        // to-do check against nbytes

        //printHexDump(byteStorage, "RT7:storage");

        ByteBuffer bb_data_type = ByteBuffer.wrap(byteStorage, 0, length_unit_length);
        if (isLittleEndian) {
            bb_data_type.order(ByteOrder.LITTLE_ENDIAN);
        }

        int unitLength = bb_data_type.getInt();
        dbgLog.fine("parseRT7 SubTypefield: unitLength=" + unitLength);

        ByteBuffer bb_number_of_units = ByteBuffer.wrap(byteStorage, length_unit_length,
                length_number_of_units);
        if (isLittleEndian) {
            bb_number_of_units.order(ByteOrder.LITTLE_ENDIAN);
        }

        int numberOfUnits = bb_number_of_units.getInt();
        dbgLog.fine("parseRT7 SubTypefield: numberOfUnits=" + numberOfUnits);

        headerSection[0] = unitLength;
        headerSection[1] = numberOfUnits;
        return headerSection;
    } catch (IOException ex) {
        throw ex;
    }
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.sav.SAVFileReader.java

private List<byte[]> getRT7SubTypefieldData(BufferedInputStream stream) throws IOException {
    int length_unit_length = 4;
    int length_number_of_units = 4;
    int storage_size = length_unit_length + length_number_of_units;
    List<byte[]> dataList = new ArrayList<byte[]>();
    int[] headerSection = new int[2];

    byte[] byteStorage = new byte[storage_size];

    try {//from  w w  w.j  ava2 s . c om
        int nbytes = stream.read(byteStorage);
        // to-do check against nbytes

        //printHexDump(byteStorage, "RT7:storage");

        ByteBuffer bb_data_type = ByteBuffer.wrap(byteStorage, 0, length_unit_length);
        if (isLittleEndian) {
            bb_data_type.order(ByteOrder.LITTLE_ENDIAN);
        }

        int unitLength = bb_data_type.getInt();
        dbgLog.fine("parseRT7SubTypefield: unitLength=" + unitLength);

        ByteBuffer bb_number_of_units = ByteBuffer.wrap(byteStorage, length_unit_length,
                length_number_of_units);
        if (isLittleEndian) {
            bb_number_of_units.order(ByteOrder.LITTLE_ENDIAN);
        }

        int numberOfUnits = bb_number_of_units.getInt();
        dbgLog.fine("parseRT7SubTypefield: numberOfUnits=" + numberOfUnits);

        headerSection[0] = unitLength;
        headerSection[1] = numberOfUnits;

        for (int i = 0; i < numberOfUnits; i++) {

            byte[] work = new byte[unitLength];
            int nb = stream.read(work);
            dbgLog.finer(new String(Hex.encodeHex(work)));
            dataList.add(work);
        }

    } catch (IOException ex) {
        //ex.printStackTrace();
        throw ex;
    }
    return dataList;
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.sav.SAVFileReader.java

private void parseRT7SubTypefield(BufferedInputStream stream) throws IOException {
    int length_unit_length = 4;
    int length_number_of_units = 4;
    int storage_size = length_unit_length + length_number_of_units;

    int[] headerSection = new int[2];

    byte[] byteStorage = new byte[storage_size];

    try {/*from   w w  w  .  j a  v  a2  s.  c o  m*/
        int nbytes = stream.read(byteStorage);
        // to-do check against nbytes

        //printHexDump(byteStorage, "RT7:storage");

        ByteBuffer bb_data_type = ByteBuffer.wrap(byteStorage, 0, length_unit_length);
        if (isLittleEndian) {
            bb_data_type.order(ByteOrder.LITTLE_ENDIAN);
        }

        int unitLength = bb_data_type.getInt();
        dbgLog.fine("parseRT7 SubTypefield: unitLength=" + unitLength);

        ByteBuffer bb_number_of_units = ByteBuffer.wrap(byteStorage, length_unit_length,
                length_number_of_units);
        if (isLittleEndian) {
            bb_number_of_units.order(ByteOrder.LITTLE_ENDIAN);
        }

        int numberOfUnits = bb_number_of_units.getInt();
        dbgLog.fine("parseRT7 SubTypefield: numberOfUnits=" + numberOfUnits);

        headerSection[0] = unitLength;
        headerSection[1] = numberOfUnits;

        for (int i = 0; i < numberOfUnits; i++) {
            byte[] work = new byte[unitLength];

            int nb = stream.read(work);
            dbgLog.finer("raw bytes in Hex:" + new String(Hex.encodeHex(work)));
            ByteBuffer bb_field = ByteBuffer.wrap(work);
            if (isLittleEndian) {
                bb_field.order(ByteOrder.LITTLE_ENDIAN);
            }
            dbgLog.fine("RT7ST: raw bytes in Hex:" + new String(Hex.encodeHex(bb_field.array())));
            if (unitLength == 4) {
                int fieldData = bb_field.getInt();
                dbgLog.fine("RT7ST: " + i + "-th fieldData=" + fieldData);
                dbgLog.fine("RT7ST: fieldData in Hex=" + Integer.toHexString(fieldData));
            } else if (unitLength == 8) {
                double fieldData = bb_field.getDouble();
                dbgLog.finer("RT7ST: " + i + "-th fieldData=" + fieldData);
                dbgLog.finer("RT7ST: fieldData in Hex=" + Double.toHexString(fieldData));

            }
            dbgLog.finer("");
        }

    } catch (IOException ex) {
        //ex.printStackTrace();
        throw ex;
    }

}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.sav.SAVFileReader.java

void decodeRecordType6(BufferedInputStream stream) throws IOException {
    dbgLog.fine("***** decodeRecordType6(): start *****");
    try {/*from  ww w  .ja v  a  2 s.  c  o  m*/
        if (stream == null) {
            throw new IllegalArgumentException("stream == null!");
        }
        // this section is optional; so let's first check the 4-byte header 
        // value and see what type it is. 
        //if (stream.markSupported()){ // -- ? L.A. 4.0 alpha
        stream.mark(1000);
        //}
        // 6.0 check the first 4 bytes
        byte[] headerCodeRt6 = new byte[LENGTH_RECORD_TYPE6_CODE];

        int nbytes_rt6 = stream.read(headerCodeRt6, 0, LENGTH_RECORD_TYPE6_CODE);
        // to-do check against nbytes
        //printHexDump(headerCodeRt6, "RT6 header test");
        ByteBuffer bb_header_code_rt6 = ByteBuffer.wrap(headerCodeRt6, 0, LENGTH_RECORD_TYPE6_CODE);
        if (isLittleEndian) {
            bb_header_code_rt6.order(ByteOrder.LITTLE_ENDIAN);
        }

        int intRT6test = bb_header_code_rt6.getInt();
        dbgLog.fine("RT6: header test value=" + intRT6test);
        if (intRT6test != 6) {
            //if (stream.markSupported()){
            //out.print("iteration="+safteyCounter);
            //dbgLog.fine("iteration="+safteyCounter);
            dbgLog.fine("intRT6test failed=" + intRT6test);

            stream.reset();
            return;
            //}
        }
        // 6.1 check 4-byte integer that tells how many lines follow

        byte[] length_how_many_line_bytes = new byte[LENGTH_RT6_HOW_MANY_LINES];

        int nbytes_rt6_1 = stream.read(length_how_many_line_bytes, 0, LENGTH_RT6_HOW_MANY_LINES);
        // to-do check against nbytes

        //printHexDump(length_how_many_line_bytes, "RT6 how_many_line_bytes");
        ByteBuffer bb_how_many_lines = ByteBuffer.wrap(length_how_many_line_bytes, 0,
                LENGTH_RT6_HOW_MANY_LINES);
        if (isLittleEndian) {
            bb_how_many_lines.order(ByteOrder.LITTLE_ENDIAN);
        }

        int howManyLinesRt6 = bb_how_many_lines.getInt();
        dbgLog.fine("how Many lines follow=" + howManyLinesRt6);

        // 6.2 read 80-char-long lines 
        String[] documentRecord = new String[howManyLinesRt6];

        for (int i = 0; i < howManyLinesRt6; i++) {

            byte[] line = new byte[80];
            int nbytes_rt6_line = stream.read(line);

            documentRecord[i] = StringUtils.stripEnd(
                    new String(Arrays.copyOfRange(line, 0, LENGTH_RT6_DOCUMENT_LINE), defaultCharSet), " ");

            dbgLog.fine(i + "-th line =" + documentRecord[i] + "<-");
        }
        dbgLog.fine("documentRecord:\n" + StringUtils.join(documentRecord, "\n"));

    } catch (IOException ex) {
        //ex.printStackTrace();
        throw ex;
    }

    dbgLog.fine("decodeRecordType6(): end");
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.sav.SAVFileReader.java

void decodeRecordType999(BufferedInputStream stream) throws IOException {
    dbgLog.fine("decodeRecordType999(): start");
    try {/*from   ww w. java2s. c om*/
        if (stream == null) {
            throw new IllegalArgumentException("RT999: stream == null!");
        }
        // first check the 4-byte header value
        //if (stream.markSupported()){
        stream.mark(1000);
        //}
        // 999.0 check the first 4 bytes
        byte[] headerCodeRt999 = new byte[LENGTH_RECORD_TYPE999_CODE];

        //dbgLog.fine("RT999: stream position="+stream.pos);

        int nbytes_rt999 = stream.read(headerCodeRt999, 0, LENGTH_RECORD_TYPE999_CODE);
        // to-do check against nbytes
        //printHexDump(headerCodeRt999, "RT999 header test");
        ByteBuffer bb_header_code_rt999 = ByteBuffer.wrap(headerCodeRt999, 0, LENGTH_RECORD_TYPE999_CODE);
        if (isLittleEndian) {
            bb_header_code_rt999.order(ByteOrder.LITTLE_ENDIAN);
        }

        int intRT999test = bb_header_code_rt999.getInt();
        dbgLog.fine("header test value: RT999=" + intRT999test);
        if (intRT999test != 999) {
            //if (stream.markSupported()){
            dbgLog.fine("intRT999test failed=" + intRT999test);
            stream.reset();
            throw new IOException("RT999:Header value(999) was not correctly detected:" + intRT999test);
            //}
        }

        // 999.1 check 4-byte integer Filler block

        byte[] length_filler = new byte[LENGTH_RT999_FILLER];

        int nbytes_rt999_1 = stream.read(length_filler, 0, LENGTH_RT999_FILLER);
        // to-do check against nbytes

        //printHexDump(length_how_many_line_bytes, "RT999 how_many_line_bytes");
        ByteBuffer bb_filler = ByteBuffer.wrap(length_filler, 0, LENGTH_RT999_FILLER);
        if (isLittleEndian) {
            bb_filler.order(ByteOrder.LITTLE_ENDIAN);
        }

        int rt999filler = bb_filler.getInt();
        dbgLog.fine("rt999filler=" + rt999filler);

        if (rt999filler == 0) {
            dbgLog.fine("the end of the dictionary section");
        } else {
            throw new IOException("RT999: failed to detect the end mark(0): value=" + rt999filler);
        }

        // missing value processing concerning HIGHEST/LOWEST values

        Set<Map.Entry<String, InvalidData>> msvlc = invalidDataTable.entrySet();
        for (Iterator<Map.Entry<String, InvalidData>> itc = msvlc.iterator(); itc.hasNext();) {
            Map.Entry<String, InvalidData> et = itc.next();
            String variable = et.getKey();
            dbgLog.fine("variable=" + variable);
            InvalidData invalidDataInfo = et.getValue();

            if (invalidDataInfo.getInvalidRange() != null && !invalidDataInfo.getInvalidRange().isEmpty()) {
                if (invalidDataInfo.getInvalidRange().get(0).equals(OBSTypeHexValue.get("LOWEST"))) {
                    dbgLog.fine("1st value is LOWEST");
                    invalidDataInfo.getInvalidRange().set(0, "LOWEST");
                } else if (invalidDataInfo.getInvalidRange().get(1).equals(OBSTypeHexValue.get("HIGHEST"))) {
                    dbgLog.fine("2nd value is HIGHEST");
                    invalidDataInfo.getInvalidRange().set(1, "HIGHEST");
                }
            }
        }
        dbgLog.fine("invalidDataTable:\n" + invalidDataTable);
        // TODO: take care of the invalid data! - add the appropriate 
        // value labels (?) 
        // should it be done here, or at the end of ingest?
        // -- L.A. 4.0 alpha
        ///smd.setInvalidDataTable(invalidDataTable);
    } catch (IOException ex) {
        //ex.printStackTrace();
        //exit(1);
        throw ex;
    }

    dbgLog.fine("decodeRecordType999(): end");
}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Writes a numeric value./* w  w w.jav a2  s. c  om*/
 */
private void writeNumericValue(ByteBuffer buffer, Object value) throws IOException {
    Object inValue = value;
    try {
        BigDecimal decVal = toBigDecimal(value);
        inValue = decVal;

        boolean negative = (decVal.compareTo(BigDecimal.ZERO) < 0);
        if (negative) {
            decVal = decVal.negate();
        }

        // write sign byte
        buffer.put(negative ? (byte) 0x80 : (byte) 0);

        // adjust scale according to this column type (will cause the an
        // ArithmeticException if number has too many decimal places)
        decVal = decVal.setScale(getScale());

        // check precision
        if (decVal.precision() > getPrecision()) {
            throw new IOException(
                    "Numeric value is too big for specified precision " + getPrecision() + ": " + decVal);
        }

        // convert to unscaled BigInteger, big-endian bytes
        byte[] intValBytes = decVal.unscaledValue().toByteArray();
        int maxByteLen = getType().getFixedSize() - 1;
        if (intValBytes.length > maxByteLen) {
            throw new IOException("Too many bytes for valid BigInteger?");
        }
        if (intValBytes.length < maxByteLen) {
            byte[] tmpBytes = new byte[maxByteLen];
            System.arraycopy(intValBytes, 0, tmpBytes, (maxByteLen - intValBytes.length), intValBytes.length);
            intValBytes = tmpBytes;
        }
        if (buffer.order() != ByteOrder.BIG_ENDIAN) {
            fixNumericByteOrder(intValBytes);
        }
        buffer.put(intValBytes);
    } catch (ArithmeticException e) {
        throw (IOException) new IOException("Numeric value '" + inValue + "' out of range").initCause(e);
    }
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.sav.SAVFileReader.java

void decodeRecordType6(BufferedInputStream stream) throws IOException {
    dbgLog.fine("***** decodeRecordType6(): start *****");
    try {//  w ww  .  j av  a 2 s . com
        if (stream == null) {
            throw new IllegalArgumentException("stream == null!");
        }
        // this secton may not exit so first check the 4-byte header value
        //if (stream.markSupported()){
        stream.mark(1000);
        //}
        // 6.0 check the first 4 bytes
        byte[] headerCodeRt6 = new byte[LENGTH_RECORD_TYPE6_CODE];

        int nbytes_rt6 = stream.read(headerCodeRt6, 0, LENGTH_RECORD_TYPE6_CODE);
        // to-do check against nbytes
        //printHexDump(headerCodeRt6, "RT6 header test");
        ByteBuffer bb_header_code_rt6 = ByteBuffer.wrap(headerCodeRt6, 0, LENGTH_RECORD_TYPE6_CODE);
        if (isLittleEndian) {
            bb_header_code_rt6.order(ByteOrder.LITTLE_ENDIAN);
        }

        int intRT6test = bb_header_code_rt6.getInt();
        dbgLog.fine("RT6: header test value=" + intRT6test);
        if (intRT6test != 6) {
            //if (stream.markSupported()){
            //out.print("iteration="+safteyCounter);
            //dbgLog.fine("iteration="+safteyCounter);
            dbgLog.fine("intRT6test failed=" + intRT6test);

            stream.reset();
            return;
            //}
        }
        // 6.1 check 4-byte integer that tells how many lines follow

        byte[] length_how_many_line_bytes = new byte[LENGTH_RT6_HOW_MANY_LINES];

        int nbytes_rt6_1 = stream.read(length_how_many_line_bytes, 0, LENGTH_RT6_HOW_MANY_LINES);
        // to-do check against nbytes

        //printHexDump(length_how_many_line_bytes, "RT6 how_many_line_bytes");
        ByteBuffer bb_how_many_lines = ByteBuffer.wrap(length_how_many_line_bytes, 0,
                LENGTH_RT6_HOW_MANY_LINES);
        if (isLittleEndian) {
            bb_how_many_lines.order(ByteOrder.LITTLE_ENDIAN);
        }

        int howManyLinesRt6 = bb_how_many_lines.getInt();
        dbgLog.fine("how Many lines follow=" + howManyLinesRt6);

        // 6.2 read 80-char-long lines 
        String[] documentRecord = new String[howManyLinesRt6];

        for (int i = 0; i < howManyLinesRt6; i++) {

            byte[] line = new byte[80];
            int nbytes_rt6_line = stream.read(line);

            documentRecord[i] = StringUtils.stripEnd(
                    new String(Arrays.copyOfRange(line, 0, LENGTH_RT6_DOCUMENT_LINE), defaultCharSet), " ");

            dbgLog.fine(i + "-th line =" + documentRecord[i] + "<-");
        }
        dbgLog.fine("documentRecord:\n" + StringUtils.join(documentRecord, "\n"));

    } catch (IOException ex) {
        //ex.printStackTrace();
        throw ex;
    }

    dbgLog.fine("***** decodeRecordType6(): end *****");
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.sav.SAVFileReader.java

void decodeRecordType999(BufferedInputStream stream) throws IOException {
    dbgLog.fine("***** decodeRecordType999(): start *****");
    try {/* w w w . ja  va2  s .  c  o  m*/
        if (stream == null) {
            throw new IllegalArgumentException("RT999: stream == null!");
        }
        // first check the 4-byte header value
        //if (stream.markSupported()){
        stream.mark(1000);
        //}
        // 999.0 check the first 4 bytes
        byte[] headerCodeRt999 = new byte[LENGTH_RECORD_TYPE999_CODE];

        //dbgLog.fine("RT999: stream position="+stream.pos);

        int nbytes_rt999 = stream.read(headerCodeRt999, 0, LENGTH_RECORD_TYPE999_CODE);
        // to-do check against nbytes
        //printHexDump(headerCodeRt999, "RT999 header test");
        ByteBuffer bb_header_code_rt999 = ByteBuffer.wrap(headerCodeRt999, 0, LENGTH_RECORD_TYPE999_CODE);
        if (isLittleEndian) {
            bb_header_code_rt999.order(ByteOrder.LITTLE_ENDIAN);
        }

        int intRT999test = bb_header_code_rt999.getInt();
        dbgLog.fine("header test value: RT999=" + intRT999test);
        if (intRT999test != 999) {
            //if (stream.markSupported()){
            dbgLog.fine("intRT999test failed=" + intRT999test);
            stream.reset();
            throw new IOException("RT999:Header value(999) was not correctly detected:" + intRT999test);
            //}
        }

        // 999.1 check 4-byte integer Filler block

        byte[] length_filler = new byte[LENGTH_RT999_FILLER];

        int nbytes_rt999_1 = stream.read(length_filler, 0, LENGTH_RT999_FILLER);
        // to-do check against nbytes

        //printHexDump(length_how_many_line_bytes, "RT999 how_many_line_bytes");
        ByteBuffer bb_filler = ByteBuffer.wrap(length_filler, 0, LENGTH_RT999_FILLER);
        if (isLittleEndian) {
            bb_filler.order(ByteOrder.LITTLE_ENDIAN);
        }

        int rt999filler = bb_filler.getInt();
        dbgLog.fine("rt999filler=" + rt999filler);

        if (rt999filler == 0) {
            dbgLog.fine("the end of the dictionary section");
        } else {
            throw new IOException("RT999: failed to detect the end mark(0): value=" + rt999filler);
        }

        // missing value processing concerning HIGHEST/LOWEST values

        Set<Map.Entry<String, InvalidData>> msvlc = invalidDataTable.entrySet();
        for (Iterator<Map.Entry<String, InvalidData>> itc = msvlc.iterator(); itc.hasNext();) {
            Map.Entry<String, InvalidData> et = itc.next();
            String variable = et.getKey();
            dbgLog.fine("variable=" + variable);
            InvalidData invalidDataInfo = et.getValue();

            if (invalidDataInfo.getInvalidRange() != null && !invalidDataInfo.getInvalidRange().isEmpty()) {
                if (invalidDataInfo.getInvalidRange().get(0).equals(OBSTypeHexValue.get("LOWEST"))) {
                    dbgLog.fine("1st value is LOWEST");
                    invalidDataInfo.getInvalidRange().set(0, "LOWEST");
                } else if (invalidDataInfo.getInvalidRange().get(1).equals(OBSTypeHexValue.get("HIGHEST"))) {
                    dbgLog.fine("2nd value is HIGHEST");
                    invalidDataInfo.getInvalidRange().set(1, "HIGHEST");
                }
            }
        }
        dbgLog.fine("invalidDataTable:\n" + invalidDataTable);
        smd.setInvalidDataTable(invalidDataTable);
    } catch (IOException ex) {
        //ex.printStackTrace();
        //exit(1);
        throw ex;
    }

    dbgLog.fine("***** decodeRecordType999(): end *****");
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.sav.SAVFileReader.java

void decodeRecordType3and4(BufferedInputStream stream) throws IOException {
    dbgLog.fine("decodeRecordType3and4(): start");
    Map<String, Map<String, String>> valueLabelTable = new LinkedHashMap<String, Map<String, String>>();

    int safteyCounter = 0;
    while (true) {
        try {/*from   w ww.  j a  v  a2s  . c o m*/
            if (stream == null) {
                throw new IllegalArgumentException("stream == null!");
            }
            // this secton may not exit so first check the 4-byte header value
            //if (stream.markSupported()){
            stream.mark(1000);
            //}
            // 3.0 check the first 4 bytes
            byte[] headerCode = new byte[LENGTH_RECORD_TYPE3_CODE];

            int nbytes_rt3 = stream.read(headerCode, 0, LENGTH_RECORD_TYPE3_CODE);
            // to-do check against nbytes
            //printHexDump(headerCode, "RT3 header test");
            ByteBuffer bb_header_code = ByteBuffer.wrap(headerCode, 0, LENGTH_RECORD_TYPE3_CODE);
            if (isLittleEndian) {
                bb_header_code.order(ByteOrder.LITTLE_ENDIAN);
            }

            int intRT3test = bb_header_code.getInt();
            dbgLog.fine("header test value: RT3=" + intRT3test);
            if (intRT3test != 3) {
                //if (stream.markSupported()){
                dbgLog.fine("iteration=" + safteyCounter);

                // We have encountered a record that's not type 3. This means we've
                // processed all the type 3/4 record pairs. So we want to rewind
                // the stream and return -- so that the appropriate record type
                // reader can be called on it.
                // But before we return, we need to save all the value labels
                // we have found:
                //smd.setValueLabelTable(valueLabelTable);
                assignValueLabels(valueLabelTable);

                stream.reset();
                return;
                //}
            }
            // 3.1 how many value-label pairs follow
            byte[] number_of_labels = new byte[LENGTH_RT3_HOW_MANY_LABELS];

            int nbytes_3_1 = stream.read(number_of_labels);
            if (nbytes_3_1 == 0) {
                throw new IOException("RT 3: reading recordType3.1: no byte was read");
            }
            ByteBuffer bb_number_of_labels = ByteBuffer.wrap(number_of_labels, 0, LENGTH_RT3_HOW_MANY_LABELS);
            if (isLittleEndian) {
                bb_number_of_labels.order(ByteOrder.LITTLE_ENDIAN);
            }

            int numberOfValueLabels = bb_number_of_labels.getInt();
            dbgLog.fine("number of value-label pairs=" + numberOfValueLabels);

            ByteBuffer[] tempBB = new ByteBuffer[numberOfValueLabels];

            String valueLabel[] = new String[numberOfValueLabels];

            for (int i = 0; i < numberOfValueLabels; i++) {

                // read 8-byte as value          
                byte[] value = new byte[LENGTH_RT3_VALUE];
                int nbytes_3_value = stream.read(value);

                if (nbytes_3_value == 0) {
                    throw new IOException("RT 3: reading recordType3 value: no byte was read");
                }
                // note these 8 bytes are interpreted later
                // currently no information about which variable's (=> type unknown)
                ByteBuffer bb_value = ByteBuffer.wrap(value, 0, LENGTH_RT3_VALUE);
                if (isLittleEndian) {
                    bb_value.order(ByteOrder.LITTLE_ENDIAN);
                }
                tempBB[i] = bb_value;
                dbgLog.fine("bb_value=" + Hex.encodeHex(bb_value.array()));
                /*
                 double valueD = bb_value.getDouble();                
                 dbgLog.fine("value="+valueD);
                 */
                // read 1st byte as unsigned integer = label_length

                // read label_length byte as label
                byte[] labelLengthByte = new byte[LENGTH_RT3_LABEL_LENGTH];

                int nbytes_3_label_length = stream.read(labelLengthByte);

                // add check-routine here
                dbgLog.fine("labelLengthByte" + Hex.encodeHex(labelLengthByte));
                dbgLog.fine("label length = " + labelLengthByte[0]);
                // the net-length of a value label is saved as
                // unsigned byte; however, the length is less than 127
                // byte should be ok
                int rawLabelLength = labelLengthByte[0] & 0xFF;
                dbgLog.fine("rawLabelLength=" + rawLabelLength);
                // -1 =>1-byte already read
                int labelLength = getSAVobsAdjustedBlockLength(rawLabelLength + 1) - 1;
                byte[] valueLabelBytes = new byte[labelLength];
                int nbytes_3_value_label = stream.read(valueLabelBytes);

                // ByteBuffer bb_label = ByteBuffer.wrap(valueLabel,0,labelLength);
                valueLabel[i] = StringUtils.stripEnd(
                        new String(Arrays.copyOfRange(valueLabelBytes, 0, rawLabelLength), defaultCharSet),
                        " ");
                dbgLog.fine(i + "-th valueLabel=" + valueLabel[i] + "<-");

            } // iter rt3

            dbgLog.fine("end of RT3 block");
            dbgLog.fine("start of RT4 block");

            // 4.0 check the first 4 bytes
            byte[] headerCode4 = new byte[LENGTH_RECORD_TYPE4_CODE];

            int nbytes_rt4 = stream.read(headerCode4, 0, LENGTH_RECORD_TYPE4_CODE);

            if (nbytes_rt4 == 0) {
                throw new IOException("RT4: reading recordType4 value: no byte was read");
            }

            //printHexDump(headerCode4, "RT4 header test");
            ByteBuffer bb_header_code_4 = ByteBuffer.wrap(headerCode4, 0, LENGTH_RECORD_TYPE4_CODE);
            if (isLittleEndian) {
                bb_header_code_4.order(ByteOrder.LITTLE_ENDIAN);
            }

            int intRT4test = bb_header_code_4.getInt();
            dbgLog.fine("header test value: RT4=" + intRT4test);

            if (intRT4test != 4) {
                throw new IOException("RT 4: reading recordType4 header: no byte was read");
            }

            // 4.1 read the how-many-variables bytes
            byte[] howManyVariablesfollow = new byte[LENGTH_RT4_HOW_MANY_VARIABLES];

            int nbytes_rt4_1 = stream.read(howManyVariablesfollow, 0, LENGTH_RT4_HOW_MANY_VARIABLES);

            ByteBuffer bb_howManyVariablesfollow = ByteBuffer.wrap(howManyVariablesfollow, 0,
                    LENGTH_RT4_HOW_MANY_VARIABLES);
            if (isLittleEndian) {
                bb_howManyVariablesfollow.order(ByteOrder.LITTLE_ENDIAN);
            }

            int howManyVariablesRT4 = bb_howManyVariablesfollow.getInt();
            dbgLog.fine("how many variables follow: RT4=" + howManyVariablesRT4);

            int length_indicies = LENGTH_RT4_VARIABLE_INDEX * howManyVariablesRT4;
            byte[] variableIdicesBytes = new byte[length_indicies];

            int nbytes_rt4_2 = stream.read(variableIdicesBytes, 0, length_indicies);

            // !!!!! Caution: variableIndex in RT4 starts from 1 NOT ** 0 **
            int[] variableIndex = new int[howManyVariablesRT4];
            int offset = 0;
            for (int i = 0; i < howManyVariablesRT4; i++) {

                ByteBuffer bb_variable_index = ByteBuffer.wrap(variableIdicesBytes, offset,
                        LENGTH_RT4_VARIABLE_INDEX);
                offset += LENGTH_RT4_VARIABLE_INDEX;

                if (isLittleEndian) {
                    bb_variable_index.order(ByteOrder.LITTLE_ENDIAN);
                }

                variableIndex[i] = bb_variable_index.getInt();
                dbgLog.fine(i + "-th variable index number=" + variableIndex[i]);
            }

            dbgLog.fine("variable index set=" + ArrayUtils.toString(variableIndex));
            dbgLog.fine("subtract 1 from variableIndex for getting a variable info");

            boolean isNumeric = OBSwiseTypelList.get(variableIndex[0] - 1) == 0 ? true : false;

            Map<String, String> valueLabelPair = new LinkedHashMap<String, String>();
            if (isNumeric) {
                // numeric variable
                dbgLog.fine("processing of a numeric value-label table");
                for (int j = 0; j < numberOfValueLabels; j++) {
                    valueLabelPair.put(doubleNumberFormatter.format(tempBB[j].getDouble()), valueLabel[j]);
                }
            } else {
                // String variable
                dbgLog.fine("processing of a string value-label table");
                for (int j = 0; j < numberOfValueLabels; j++) {
                    valueLabelPair.put(
                            StringUtils.stripEnd(new String((tempBB[j].array()), defaultCharSet), " "),
                            valueLabel[j]);
                }
            }

            dbgLog.fine("valueLabePair=" + valueLabelPair);
            dbgLog.fine("key variable's (raw) index =" + variableIndex[0]);

            valueLabelTable.put(OBSIndexToVariableName.get(variableIndex[0] - 1), valueLabelPair);

            dbgLog.fine("valueLabelTable=" + valueLabelTable);

            // create a mapping table that finds the key variable for this mapping table
            String keyVariableName = OBSIndexToVariableName.get(variableIndex[0] - 1);
            for (int vn : variableIndex) {
                valueVariableMappingTable.put(OBSIndexToVariableName.get(vn - 1), keyVariableName);
            }

            dbgLog.fine("valueVariableMappingTable:\n" + valueVariableMappingTable);
        } catch (IOException ex) {
            //ex.printStackTrace();
            throw ex;
        }

        safteyCounter++;
        if (safteyCounter >= 1000000) {
            break;
        }
    } //while

    ///smd.setValueLabelTable(valueLabelTable);
    assignValueLabels(valueLabelTable);

    dbgLog.fine("***** decodeRecordType3and4(): end *****");
}