Example usage for org.apache.commons.codec.binary Hex encodeHex

List of usage examples for org.apache.commons.codec.binary Hex encodeHex

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Hex encodeHex.

Prototype

public static char[] encodeHex(byte[] data) 

Source Link

Document

Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.

Usage

From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java

/**
 * get the value from the recordDelimiter field
 *
 * @return recordDelimiter - the recordDelimiter as a Hex encoded String
 *//*from w  w w .  j a  v  a  2s .  c  o m*/
public String getRecordDelimiterAsHexString() {
    this.recordDelimiter.flip();
    String delim1 = new String(Hex.encodeHex(new byte[] { this.recordDelimiter.get() }));
    String delim2 = new String(Hex.encodeHex(new byte[] { this.recordDelimiter.get() }));

    String recordDelimiter = delim1 + delim2;
    return recordDelimiter;
}

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

void decodeRecordType7(BufferedInputStream stream) throws IOException {
    dbgLog.fine("***** decodeRecordType7(): start *****");
    int counter = 0;
    int[] headerSection = new int[2];

    // the variables below may no longer needed; 
    // but they may be useful for debugging/logging purposes.

    /// // RecordType 7 
    /// // Subtype 3
    /// List<Integer> releaseMachineSpecificInfo = new ArrayList<Integer>();
    /// List<String> releaseMachineSpecificInfoHex = new ArrayList<String>();

    /// // Subytpe 4
    /// Map<String, Double> OBSTypeValue = new LinkedHashMap<String, Double>();
    /// Map<String, String> OBSTypeHexValue = new LinkedHashMap<String, String>();    
    //Subtype 11/*www  . j a v a  2  s  . c om*/
    /// List<Integer> measurementLevel = new ArrayList<Integer>();
    /// List<Integer> columnWidth = new ArrayList<Integer>();
    /// List<Integer> alignment = new ArrayList<Integer>();

    Map<String, String> shortToLongVarialbeNameTable = new LinkedHashMap<String, String>();

    while (true) {
        try {
            if (stream == null) {
                throw new IllegalArgumentException("RT7: stream == null!");
            }
            // first check the 4-byte header value
            //if (stream.markSupported()){
            stream.mark(1000);
            //}
            // 7.0 check the first 4 bytes
            byte[] headerCodeRt7 = new byte[LENGTH_RECORD_TYPE7_CODE];

            int nbytes_rt7 = stream.read(headerCodeRt7, 0, LENGTH_RECORD_TYPE7_CODE);
            // to-do check against nbytes
            //printHexDump(headerCodeRt7, "RT7 header test");
            ByteBuffer bb_header_code_rt7 = ByteBuffer.wrap(headerCodeRt7, 0, LENGTH_RECORD_TYPE7_CODE);
            if (isLittleEndian) {
                bb_header_code_rt7.order(ByteOrder.LITTLE_ENDIAN);
            }

            int intRT7test = bb_header_code_rt7.getInt();
            dbgLog.fine("RT7: header test value=" + intRT7test);
            if (intRT7test != 7) {
                //if (stream.markSupported()){
                //out.print("iteration="+safteyCounter);
                //dbgLog.fine("iteration="+safteyCounter);
                dbgLog.fine("intRT7test failed=" + intRT7test);
                dbgLog.fine("counter=" + counter);
                stream.reset();
                return;
                //}
            }

            // 7.1 check 4-byte integer Sub-Type Code

            byte[] length_sub_type_code = new byte[LENGTH_RT7_SUB_TYPE_CODE];

            int nbytes_rt7_1 = stream.read(length_sub_type_code, 0, LENGTH_RT7_SUB_TYPE_CODE);
            // to-do check against nbytes

            //printHexDump(length_how_many_line_bytes, "RT7 how_many_line_bytes");
            ByteBuffer bb_sub_type_code = ByteBuffer.wrap(length_sub_type_code, 0, LENGTH_RT7_SUB_TYPE_CODE);
            if (isLittleEndian) {
                bb_sub_type_code.order(ByteOrder.LITTLE_ENDIAN);
            }

            int subTypeCode = bb_sub_type_code.getInt();
            dbgLog.fine("RT7: subTypeCode=" + subTypeCode);

            switch (subTypeCode) {
            case 3:
                // 3: Release andMachine-Specific Integer Information

                //parseRT7SubTypefield(stream);

                headerSection = parseRT7SubTypefieldHeader(stream);
                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    int numberOfUnits = headerSection[1];

                    for (int i = 0; i < numberOfUnits; i++) {
                        dbgLog.finer(i + "-th fieldData");
                        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);
                        }
                        String dataInHex = new String(Hex.encodeHex(bb_field.array()));
                        /// releaseMachineSpecificInfoHex.add(dataInHex);

                        dbgLog.finer("raw bytes in Hex:" + dataInHex);
                        if (unitLength == 4) {
                            int fieldData = bb_field.getInt();
                            dbgLog.finer("fieldData(int)=" + fieldData);
                            dbgLog.finer("fieldData in Hex=0x" + Integer.toHexString(fieldData));
                            /// releaseMachineSpecificInfo.add(fieldData);
                        }

                    }

                    /// dbgLog.fine("releaseMachineSpecificInfo="+releaseMachineSpecificInfo);
                    /// dbgLog.fine("releaseMachineSpecificInfoHex="+releaseMachineSpecificInfoHex);

                } else {
                    // throw new IOException
                }

                dbgLog.fine("***** end of subType 3 ***** \n");

                break;
            case 4:
                // Release andMachine-SpecificOBS-Type Information
                headerSection = parseRT7SubTypefieldHeader(stream);
                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    int numberOfUnits = headerSection[1];

                    for (int i = 0; i < numberOfUnits; i++) {
                        dbgLog.finer(i + "-th fieldData:" + RecordType7SubType4Fields.get(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);
                        dbgLog.finer("byte order=" + bb_field.order().toString());
                        if (isLittleEndian) {
                            bb_field.order(ByteOrder.LITTLE_ENDIAN);
                        }
                        ByteBuffer bb_field_dup = bb_field.duplicate();
                        OBSTypeHexValue.put(RecordType7SubType4Fields.get(i),
                                new String(Hex.encodeHex(bb_field.array())));
                        //                            dbgLog.finer("raw bytes in Hex:"+
                        //                                OBSTypeHexValue.get(RecordType7SubType4Fields.get(i)));
                        if (unitLength == 8) {
                            double fieldData = bb_field.getDouble();
                            /// OBSTypeValue.put(RecordType7SubType4Fields.get(i), fieldData);
                            dbgLog.finer("fieldData(double)=" + fieldData);
                            OBSTypeHexValue.put(RecordType7SubType4Fields.get(i),
                                    Double.toHexString(fieldData));
                            dbgLog.fine("fieldData in Hex=" + Double.toHexString(fieldData));
                        }
                    }
                    /// dbgLog.fine("OBSTypeValue="+OBSTypeValue);
                    /// dbgLog.fine("OBSTypeHexValue="+OBSTypeHexValue);

                } else {
                    // throw new IOException
                }

                dbgLog.fine("***** end of subType 4 ***** \n");
                break;
            case 5:
                // Variable Sets Information
                parseRT7SubTypefield(stream);
                break;
            case 6:
                // Trends date information
                parseRT7SubTypefield(stream);
                break;
            case 7:
                // Multiple response groups
                parseRT7SubTypefield(stream);
                break;
            case 8:
                // Windows Data Entry data
                parseRT7SubTypefield(stream);
                break;
            case 9:
                //
                parseRT7SubTypefield(stream);
                break;
            case 10:
                // TextSmart data
                parseRT7SubTypefield(stream);
                break;
            case 11:
                // Msmt level, col width, & alignment
                //parseRT7SubTypefield(stream);

                headerSection = parseRT7SubTypefieldHeader(stream);
                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    int numberOfUnits = headerSection[1];

                    for (int i = 0; i < numberOfUnits; i++) {
                        dbgLog.finer(i + "-th fieldData");
                        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.finer("raw bytes in Hex:" + new String(Hex.encodeHex(bb_field.array())));

                        if (unitLength == 4) {
                            int fieldData = bb_field.getInt();
                            dbgLog.finer("fieldData(int)=" + fieldData);
                            dbgLog.finer("fieldData in Hex=0x" + Integer.toHexString(fieldData));

                            int remainder = i % 3;
                            dbgLog.finer("remainder=" + remainder);
                            if (remainder == 0) {
                                /// measurementLevel.add(fieldData);
                            } else if (remainder == 1) {
                                /// columnWidth.add(fieldData);
                            } else if (remainder == 2) {
                                /// alignment.add(fieldData);
                            }
                        }

                    }

                } else {
                    // throw new IOException
                }
                /// dbgLog.fine("measurementLevel="+measurementLevel);
                /// dbgLog.fine("columnWidth="+columnWidth);
                /// dbgLog.fine("alignment="+alignment);
                dbgLog.fine("***** end of subType 11 ***** \n");

                break;
            case 12:
                // Windows Data Entry GUID
                parseRT7SubTypefield(stream);
                break;
            case 13:
                // Extended variable names
                // parseRT7SubTypefield(stream);
                headerSection = parseRT7SubTypefieldHeader(stream);

                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    dbgLog.fine("RT7: unitLength=" + unitLength);
                    int numberOfUnits = headerSection[1];
                    dbgLog.fine("RT7: numberOfUnits=" + numberOfUnits);
                    byte[] work = new byte[unitLength * numberOfUnits];
                    int nbtyes13 = stream.read(work);

                    String[] variableShortLongNamePairs = new String(work, "US-ASCII").split("\t");

                    for (int i = 0; i < variableShortLongNamePairs.length; i++) {
                        dbgLog.fine("RT7: " + i + "-th pair" + variableShortLongNamePairs[i]);
                        String[] pair = variableShortLongNamePairs[i].split("=");
                        shortToLongVarialbeNameTable.put(pair[0], pair[1]);
                    }

                    dbgLog.fine("RT7: shortToLongVarialbeNameTable" + shortToLongVarialbeNameTable);
                    smd.setShortToLongVarialbeNameTable(shortToLongVarialbeNameTable);
                } else {
                    // throw new IOException
                }

                break;
            case 14:
                // Extended strings
                //parseRT7SubTypefield(stream);
                headerSection = parseRT7SubTypefieldHeader(stream);

                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    dbgLog.fine("RT7.14: unitLength=" + unitLength);
                    int numberOfUnits = headerSection[1];
                    dbgLog.fine("RT7.14: numberOfUnits=" + numberOfUnits);
                    byte[] work = new byte[unitLength * numberOfUnits];
                    int nbtyes13 = stream.read(work);

                    String[] extendedVariablesSizePairs = new String(work, defaultCharSet).split("\000\t");

                    for (int i = 0; i < extendedVariablesSizePairs.length; i++) {
                        dbgLog.fine("RT7.14: " + i + "-th pair" + extendedVariablesSizePairs[i]);
                        if (extendedVariablesSizePairs[i].indexOf("=") > 0) {
                            String[] pair = extendedVariablesSizePairs[i].split("=");
                            extendedVariablesSizeTable.put(pair[0], Integer.valueOf(pair[1]));
                        }
                    }

                    dbgLog.fine("RT7.14: extendedVariablesSizeTable" + extendedVariablesSizeTable);
                } else {
                    // throw new IOException
                }

                break;
            case 15:
                // Clementine Metadata
                parseRT7SubTypefield(stream);
                break;
            case 16:
                // 64 bit N of cases
                parseRT7SubTypefield(stream);
                break;
            case 17:
                // File level attributes
                parseRT7SubTypefield(stream);
                break;
            case 18:
                // Variable attributes
                parseRT7SubTypefield(stream);
                break;
            case 19:
                // Extended multiple response groups
                parseRT7SubTypefield(stream);
                break;
            case 20:
                // Encoding, aka code page
                parseRT7SubTypefield(stream);
                /* TODO: This needs to be researched; 
                 * Is this field really used, ever?
                headerSection = parseRT7SubTypefieldHeader(stream);
                        
                if (headerSection != null){
                int unitLength = headerSection[0];
                dbgLog.fine("RT7-20: unitLength="+unitLength);
                int numberOfUnits = headerSection[1];
                dbgLog.fine("RT7-20: numberOfUnits="+numberOfUnits);
                byte[] rt7st20bytes = new byte[unitLength*numberOfUnits];
                int nbytes20 = stream.read(rt7st20bytes);
                        
                String dataCharSet = new String(rt7st20bytes,"US-ASCII");
                        
                if (dataCharSet != null && !(dataCharSet.equals(""))) {
                    dbgLog.fine("RT7-20: data charset: "+ dataCharSet);
                    defaultCharSet = dataCharSet; 
                }
                } else {
                // throw new IOException
                }
                 * 
                 */

                break;
            case 21:
                // Value labels for long strings
                parseRT7SubTypefield(stream);
                break;
            case 22:
                // Missing values for long strings
                parseRT7SubTypefield(stream);
                break;
            default:
                parseRT7SubTypefield(stream);
            }

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

        counter++;

        if (counter > 20) {
            break;
        }
    }

    dbgLog.fine("RT7: counter=" + counter);
    dbgLog.fine("RT7: ***** decodeRecordType7(): end *****");
}

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

void decodeRecordType7(BufferedInputStream stream) throws IOException {
    dbgLog.fine("decodeRecordType7(): start");
    int counter = 0;
    int[] headerSection = new int[2];

    // the variables below may no longer needed; 
    // but they may be useful for debugging/logging purposes.

    /// // RecordType 7 
    /// // Subtype 3
    /// List<Integer> releaseMachineSpecificInfo = new ArrayList<Integer>();
    /// List<String> releaseMachineSpecificInfoHex = new ArrayList<String>();

    /// // Subytpe 4
    /// Map<String, Double> OBSTypeValue = new LinkedHashMap<String, Double>();
    /// Map<String, String> OBSTypeHexValue = new LinkedHashMap<String, String>();    
    //Subtype 11//from   w w w  . j  av a 2s  .  c o  m
    /// List<Integer> measurementLevel = new ArrayList<Integer>();
    /// List<Integer> columnWidth = new ArrayList<Integer>();
    /// List<Integer> alignment = new ArrayList<Integer>();

    while (true) {
        try {
            if (stream == null) {
                throw new IllegalArgumentException("RT7: stream == null!");
            }
            // first check the 4-byte header value
            //if (stream.markSupported()){
            stream.mark(1000);
            //}
            // 7.0 check the first 4 bytes
            byte[] headerCodeRt7 = new byte[LENGTH_RECORD_TYPE7_CODE];

            int nbytes_rt7 = stream.read(headerCodeRt7, 0, LENGTH_RECORD_TYPE7_CODE);
            // to-do check against nbytes
            //printHexDump(headerCodeRt7, "RT7 header test");
            ByteBuffer bb_header_code_rt7 = ByteBuffer.wrap(headerCodeRt7, 0, LENGTH_RECORD_TYPE7_CODE);
            if (isLittleEndian) {
                bb_header_code_rt7.order(ByteOrder.LITTLE_ENDIAN);
            }

            int intRT7test = bb_header_code_rt7.getInt();
            dbgLog.fine("RT7: header test value=" + intRT7test);
            if (intRT7test != 7) {
                //if (stream.markSupported()){
                //out.print("iteration="+safteyCounter);
                //dbgLog.fine("iteration="+safteyCounter);
                dbgLog.fine("intRT7test failed=" + intRT7test);
                dbgLog.fine("counter=" + counter);
                stream.reset();
                return;
                //}
            }

            // 7.1 check 4-byte integer Sub-Type Code

            byte[] length_sub_type_code = new byte[LENGTH_RT7_SUB_TYPE_CODE];

            int nbytes_rt7_1 = stream.read(length_sub_type_code, 0, LENGTH_RT7_SUB_TYPE_CODE);
            // to-do check against nbytes

            //printHexDump(length_how_many_line_bytes, "RT7 how_many_line_bytes");
            ByteBuffer bb_sub_type_code = ByteBuffer.wrap(length_sub_type_code, 0, LENGTH_RT7_SUB_TYPE_CODE);
            if (isLittleEndian) {
                bb_sub_type_code.order(ByteOrder.LITTLE_ENDIAN);
            }

            int subTypeCode = bb_sub_type_code.getInt();
            dbgLog.fine("RT7: subTypeCode=" + subTypeCode);

            switch (subTypeCode) {
            case 3:
                // 3: Release andMachine-Specific Integer Information

                //parseRT7SubTypefield(stream);

                headerSection = parseRT7SubTypefieldHeader(stream);
                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    int numberOfUnits = headerSection[1];

                    for (int i = 0; i < numberOfUnits; i++) {
                        dbgLog.finer(i + "-th fieldData");
                        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);
                        }
                        String dataInHex = new String(Hex.encodeHex(bb_field.array()));
                        /// releaseMachineSpecificInfoHex.add(dataInHex);

                        dbgLog.finer("raw bytes in Hex:" + dataInHex);
                        if (unitLength == 4) {
                            int fieldData = bb_field.getInt();
                            dbgLog.finer("fieldData(int)=" + fieldData);
                            dbgLog.finer("fieldData in Hex=0x" + Integer.toHexString(fieldData));
                            /// releaseMachineSpecificInfo.add(fieldData);
                        }

                    }

                    /// dbgLog.fine("releaseMachineSpecificInfo="+releaseMachineSpecificInfo);
                    /// dbgLog.fine("releaseMachineSpecificInfoHex="+releaseMachineSpecificInfoHex);

                } else {
                    // throw new IOException
                }

                dbgLog.fine("***** end of subType 3 ***** \n");

                break;
            case 4:
                // Release andMachine-SpecificOBS-Type Information
                headerSection = parseRT7SubTypefieldHeader(stream);
                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    int numberOfUnits = headerSection[1];

                    for (int i = 0; i < numberOfUnits; i++) {
                        dbgLog.finer(i + "-th fieldData:" + RecordType7SubType4Fields.get(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);
                        dbgLog.finer("byte order=" + bb_field.order().toString());
                        if (isLittleEndian) {
                            bb_field.order(ByteOrder.LITTLE_ENDIAN);
                        }
                        ByteBuffer bb_field_dup = bb_field.duplicate();
                        OBSTypeHexValue.put(RecordType7SubType4Fields.get(i),
                                new String(Hex.encodeHex(bb_field.array())));
                        //                            dbgLog.finer("raw bytes in Hex:"+
                        //                                OBSTypeHexValue.get(RecordType7SubType4Fields.get(i)));
                        if (unitLength == 8) {
                            double fieldData = bb_field.getDouble();
                            /// OBSTypeValue.put(RecordType7SubType4Fields.get(i), fieldData);
                            dbgLog.finer("fieldData(double)=" + fieldData);
                            OBSTypeHexValue.put(RecordType7SubType4Fields.get(i),
                                    Double.toHexString(fieldData));
                            dbgLog.fine("fieldData in Hex=" + Double.toHexString(fieldData));
                        }
                    }
                    /// dbgLog.fine("OBSTypeValue="+OBSTypeValue);
                    /// dbgLog.fine("OBSTypeHexValue="+OBSTypeHexValue);

                } else {
                    // throw new IOException
                }

                dbgLog.fine("***** end of subType 4 ***** \n");
                break;
            case 5:
                // Variable Sets Information
                parseRT7SubTypefield(stream);
                break;
            case 6:
                // Trends date information
                parseRT7SubTypefield(stream);
                break;
            case 7:
                // Multiple response groups
                parseRT7SubTypefield(stream);
                break;
            case 8:
                // Windows Data Entry data
                parseRT7SubTypefield(stream);
                break;
            case 9:
                //
                parseRT7SubTypefield(stream);
                break;
            case 10:
                // TextSmart data
                parseRT7SubTypefield(stream);
                break;
            case 11:
                // Msmt level, col width, & alignment
                //parseRT7SubTypefield(stream);

                headerSection = parseRT7SubTypefieldHeader(stream);
                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    int numberOfUnits = headerSection[1];

                    for (int i = 0; i < numberOfUnits; i++) {
                        dbgLog.finer(i + "-th fieldData");
                        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.finer("raw bytes in Hex:" + new String(Hex.encodeHex(bb_field.array())));

                        if (unitLength == 4) {
                            int fieldData = bb_field.getInt();
                            dbgLog.finer("fieldData(int)=" + fieldData);
                            dbgLog.finer("fieldData in Hex=0x" + Integer.toHexString(fieldData));

                            int remainder = i % 3;
                            dbgLog.finer("remainder=" + remainder);
                            if (remainder == 0) {
                                /// measurementLevel.add(fieldData);
                            } else if (remainder == 1) {
                                /// columnWidth.add(fieldData);
                            } else if (remainder == 2) {
                                /// alignment.add(fieldData);
                            }
                        }

                    }

                } else {
                    // throw new IOException
                }
                /// dbgLog.fine("measurementLevel="+measurementLevel);
                /// dbgLog.fine("columnWidth="+columnWidth);
                /// dbgLog.fine("alignment="+alignment);
                dbgLog.fine("end of subType 11\n");

                break;
            case 12:
                // Windows Data Entry GUID
                parseRT7SubTypefield(stream);
                break;
            case 13:
                // Extended variable names
                // parseRT7SubTypefield(stream);
                headerSection = parseRT7SubTypefieldHeader(stream);

                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    dbgLog.fine("RT7: unitLength=" + unitLength);
                    int numberOfUnits = headerSection[1];
                    dbgLog.fine("RT7: numberOfUnits=" + numberOfUnits);
                    byte[] work = new byte[unitLength * numberOfUnits];
                    int nbtyes13 = stream.read(work);

                    String[] variableShortLongNamePairs = new String(work, "US-ASCII").split("\t");

                    for (int i = 0; i < variableShortLongNamePairs.length; i++) {
                        dbgLog.fine("RT7: " + i + "-th pair" + variableShortLongNamePairs[i]);
                        String[] pair = variableShortLongNamePairs[i].split("=");
                        shortToLongVariableNameTable.put(pair[0], pair[1]);
                    }

                    dbgLog.fine("RT7: shortToLongVarialbeNameTable" + shortToLongVariableNameTable);
                    // We are saving the short-to-long name map; at the
                    // end of ingest, we'll go through the data variables and
                    // change the names accordingly. 

                    // smd.setShortToLongVarialbeNameTable(shortToLongVarialbeNameTable);
                } else {
                    // throw new IOException
                }

                break;
            case 14:
                // Extended strings
                //parseRT7SubTypefield(stream);
                headerSection = parseRT7SubTypefieldHeader(stream);

                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    dbgLog.fine("RT7.14: unitLength=" + unitLength);
                    int numberOfUnits = headerSection[1];
                    dbgLog.fine("RT7.14: numberOfUnits=" + numberOfUnits);
                    byte[] work = new byte[unitLength * numberOfUnits];
                    int nbtyes13 = stream.read(work);

                    String[] extendedVariablesSizePairs = new String(work, defaultCharSet).split("\000\t");

                    for (int i = 0; i < extendedVariablesSizePairs.length; i++) {
                        dbgLog.fine("RT7.14: " + i + "-th pair" + extendedVariablesSizePairs[i]);
                        if (extendedVariablesSizePairs[i].indexOf("=") > 0) {
                            String[] pair = extendedVariablesSizePairs[i].split("=");
                            extendedVariablesSizeTable.put(pair[0], Integer.valueOf(pair[1]));
                        }
                    }

                    dbgLog.fine("RT7.14: extendedVariablesSizeTable" + extendedVariablesSizeTable);
                } else {
                    // throw new IOException
                }

                break;
            case 15:
                // Clementine Metadata
                parseRT7SubTypefield(stream);
                break;
            case 16:
                // 64 bit N of cases
                parseRT7SubTypefield(stream);
                break;
            case 17:
                // File level attributes
                parseRT7SubTypefield(stream);
                break;
            case 18:
                // Variable attributes
                parseRT7SubTypefield(stream);
                break;
            case 19:
                // Extended multiple response groups
                parseRT7SubTypefield(stream);
                break;
            case 20:
                // Character encoding, aka code page.
                // Must be a version 16+ feature (?).
                // Starting v.16, the default character encoding for SAV
                // files is UTF-8; but then it is possible to specify an 
                // alternative encoding here. 
                // A typical use case would be people setting it to "ISO-Latin" 
                // or "windows-1252", or a similar 8-bit encoding to store 
                // text with standard Western European accents.
                // -- L.A.

                headerSection = parseRT7SubTypefieldHeader(stream);

                if (headerSection != null) {
                    int unitLength = headerSection[0];
                    dbgLog.fine("RT7-20: unitLength=" + unitLength);
                    int numberOfUnits = headerSection[1];
                    dbgLog.fine("RT7-20: numberOfUnits=" + numberOfUnits);
                    byte[] rt7st20bytes = new byte[unitLength * numberOfUnits];
                    int nbytes20 = stream.read(rt7st20bytes);

                    String dataCharSet = new String(rt7st20bytes, "US-ASCII");

                    if (dataCharSet != null && !(dataCharSet.equals(""))) {
                        dbgLog.fine("RT7-20: data charset: " + dataCharSet);
                        defaultCharSet = dataCharSet;
                    }
                } /*else {
                  // TODO: 
                  // decide if the exception should actually be thrown here!
                  // -- L.A. 4.0 beta
                  // throw new IOException
                  }*/

                break;
            case 21:
                // Value labels for long strings
                parseRT7SubTypefield(stream);
                break;
            case 22:
                // Missing values for long strings
                parseRT7SubTypefield(stream);
                break;
            default:
                parseRT7SubTypefield(stream);
            }

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

        counter++;

        if (counter > 20) {
            break;
        }
    }

    dbgLog.fine("RT7: counter=" + counter);
    dbgLog.fine("RT7: decodeRecordType7(): end");
}

From source file:com.android.server.MountService.java

private String toHex(String password) {
    if (password == null) {
        return new String();
    }//from  ww  w .  j  a  v  a2s .  c o m
    byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
    return new String(Hex.encodeHex(bytes));
}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.UmlsCommon.UMLSBaseCode.java

/**
 * Generates a unique but reproducible key based on the given base of string
 * values./*from   w  ww.ja  va  2 s. co m*/
 * 
 * @param components
 * @return A unique string based on 20-byte output from a SHA-1 message
 *         digest.
 * @throws SQLException
 */
protected String generateUniqueKey(String[] basis) {
    MessageDigest md = getSHA1();
    md.reset();
    for (int i = 0; i < basis.length; i++)
        if (basis[i] != null)
            md.update(basis[i].getBytes());
    byte[] bytes = md.digest();
    return String.valueOf(Hex.encodeHex(bytes));
}

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

void decodeRecordTypeDataUnCompressed(BufferedInputStream stream) throws IOException {
    dbgLog.fine("***** decodeRecordTypeDataUnCompressed(): start *****");

    if (stream == null) {
        throw new IllegalArgumentException("decodeRecordTypeDataUnCompressed: stream == null!");
    }/*from w  w w.  ja  va2  s .  c  o m*/

    Map<String, String> formatCategoryTable = new LinkedHashMap<String, String>();

    // 
    // set-up tab file

    PrintWriter pwout = createOutputWriter(stream);

    boolean hasStringVarContinuousBlock = obsNonVariableBlockSet.size() > 0 ? true : false;
    dbgLog.fine("hasStringVarContinuousBlock=" + hasStringVarContinuousBlock);

    int ii = 0;

    int OBS = LENGTH_SAV_OBS_BLOCK;
    int nOBS = OBSUnitsPerCase;

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

    int caseIndex = 0;

    dbgLog.fine("printFormatTable:\n" + printFormatTable);

    dbgLog.fine("printFormatNameTable:\n" + printFormatNameTable);
    variableFormatTypeList = new String[varQnty];

    for (int i = 0; i < varQnty; i++) {
        variableFormatTypeList[i] = SPSSConstants.FORMAT_CATEGORY_TABLE
                .get(printFormatTable.get(variableNameList.get(i)));
        dbgLog.fine("i=" + i + "th variableFormatTypeList=" + variableFormatTypeList[i]);
        formatCategoryTable.put(variableNameList.get(i), variableFormatTypeList[i]);
    }
    dbgLog.fine("variableFormatType:\n" + Arrays.deepToString(variableFormatTypeList));
    dbgLog.fine("formatCategoryTable:\n" + formatCategoryTable);

    // contents (variable) checker concering decimals
    variableTypeFinal = new int[varQnty];
    Arrays.fill(variableTypeFinal, 0);

    int numberOfDecimalVariables = 0;

    List<String> casewiseRecordForTabFile = new ArrayList<String>();
    String[] caseWiseDateFormatForUNF = null;
    List<String> casewiseRecordForUNF = new ArrayList<String>();

    // missing values are written to the tab-delimited file by
    // using the default or user-specified missing-value  strings;
    // however, to calculate UNF/summary statistics,
    // classes for these calculations require their specific 
    // missing values that differ from the above missing-value
    // strings; therefore, after row data for the tab-delimited 
    // file are written, missing values in a row are changed to
    // UNF/summary-statistics-OK ones.

    // data-storage object for sumStat
    dataTable2 = new Object[varQnty][caseQnty];
    // storage of date formats to pass to UNF   
    dateFormats = new String[varQnty][caseQnty];

    try {
        for (int i = 0;; i++) { // case-wise loop

            byte[] buffer = new byte[OBS * nOBS];

            int nbytesuc = stream.read(buffer);

            StringBuilder sb_stringStorage = new StringBuilder("");

            for (int k = 0; k < nOBS; k++) {
                int offset = OBS * k;

                // uncompressed case
                // numeric missing value == sysmis
                // FF FF FF FF FF FF eF FF(little endian)
                // string missing value
                // 20 20 20 20 20 20 20 20
                // cf: compressed case 
                // numeric type:sysmis == 0xFF
                // string type: missing value == 0xFE
                // 

                boolean isNumeric = OBSwiseTypelList.get(k) == 0 ? true : false;

                if (isNumeric) {
                    dbgLog.finer(k + "-th variable is numeric");
                    // interprete as double
                    ByteBuffer bb_double = ByteBuffer.wrap(buffer, offset, LENGTH_SAV_OBS_BLOCK);
                    if (isLittleEndian) {
                        bb_double.order(ByteOrder.LITTLE_ENDIAN);
                    }
                    //char[] hexpattern =
                    String dphex = new String(Hex.encodeHex(
                            Arrays.copyOfRange(bb_double.array(), offset, offset + LENGTH_SAV_OBS_BLOCK)));
                    dbgLog.finer("dphex=" + dphex);

                    if ((dphex.equals("ffffffffffffefff")) || (dphex.equals("ffefffffffffffff"))) {
                        //casewiseRecordForTabFile.add(systemMissingValue);
                        // add the numeric missing value
                        dbgLog.fine("SAV Reader: adding: Missing Value (numeric)");
                        casewiseRecordForTabFile.add(MissingValueForTextDataFileNumeric);
                    } else {
                        Double ddatum = bb_double.getDouble();
                        dbgLog.fine("SAV Reader: adding: ddatum=" + ddatum);

                        // add this non-missing-value numeric datum
                        casewiseRecordForTabFile.add(doubleNumberFormatter.format(ddatum));
                    }

                } else {
                    dbgLog.finer(k + "-th variable is string");
                    // string case
                    // strip space-padding
                    // do not trim: string might have spaces within it
                    // the missing value (hex) for a string variable is:
                    // "20 20 20 20 20 20 20 20"

                    String strdatum = new String(
                            Arrays.copyOfRange(buffer, offset, (offset + LENGTH_SAV_OBS_BLOCK)),
                            defaultCharSet);
                    dbgLog.finer("str_datum=" + strdatum);
                    // add this non-missing-value string datum 
                    casewiseRecordForTabFile.add(strdatum);

                } // if isNumeric

            } // k-loop

            // String-variable's continuous block exits:
            if (hasStringVarContinuousBlock) {
                // continuous blocks: string case
                // concatenating process
                //dbgLog.fine("concatenating process starts");

                //dbgLog.fine("casewiseRecordForTabFile(before)="+casewiseRecordForTabFile);
                //dbgLog.fine("casewiseRecordForTabFile(before:size)="+casewiseRecordForTabFile.size());

                StringBuilder sb = new StringBuilder("");
                int firstPosition = 0;

                Set<Integer> removeJset = new HashSet<Integer>();
                for (int j = 0; j < nOBS; j++) {
                    dbgLog.finer("j=" + j + "-th type =" + OBSwiseTypelList.get(j));
                    if (OBSwiseTypelList.get(j) == -1) {
                        // String continued fount at j-th 
                        // look back the j-1 
                        firstPosition = j - 1;
                        int lastJ = j;
                        String concatanated = null;

                        removeJset.add(j);
                        sb.append(casewiseRecordForTabFile.get(j - 1));
                        sb.append(casewiseRecordForTabFile.get(j));
                        for (int jc = 1;; jc++) {
                            if (OBSwiseTypelList.get(j + jc) != -1) {
                                // j is the end unit of this string variable
                                concatanated = sb.toString();
                                sb.setLength(0);
                                lastJ = j + jc;
                                break;
                            } else {
                                sb.append(casewiseRecordForTabFile.get(j + jc));
                                removeJset.add(j + jc);
                            }
                        }
                        casewiseRecordForTabFile.set(j - 1, concatanated);

                        //out.println(j-1+"th concatanated="+concatanated);
                        j = lastJ - 1;

                    } // end-of-if: continuous-OBS only
                } // end of loop-j

                List<String> newDataLine = new ArrayList<String>();

                for (int jl = 0; jl < casewiseRecordForTabFile.size(); jl++) {
                    //out.println("jl="+jl+"-th datum =["+casewiseRecordForTabFile.get(jl)+"]");

                    if (!removeJset.contains(jl)) {
                        newDataLine.add(casewiseRecordForTabFile.get(jl));
                    }
                }

                dbgLog.fine("new casewiseRecordForTabFile=" + newDataLine);
                dbgLog.fine("new casewiseRecordForTabFile(size)=" + newDataLine.size());

                casewiseRecordForTabFile = newDataLine;

            } // end-if: stringContinuousVar-exist case

            for (int el = 0; el < casewiseRecordForTabFile.size(); el++) {
                casewiseRecordForUNF.add(casewiseRecordForTabFile.get(el));
            }

            caseWiseDateFormatForUNF = new String[casewiseRecordForTabFile.size()];

            caseIndex++;
            dbgLog.finer("caseIndex=" + caseIndex);
            for (int k = 0; k < casewiseRecordForTabFile.size(); k++) {

                if (variableTypelList.get(k) > 0) {
                    // String variable case: set to  -1
                    variableTypeFinal[k] = -1;

                    // See my comments for this padding removal logic
                    // in the "compressed" method -- L.A.

                    String paddRemoved = StringUtils.stripEnd(casewiseRecordForTabFile.get(k).toString(), null);
                    // TODO: clean this up.  For now, just make sure that strings contain at least one blank space.
                    if (paddRemoved.equals("")) {
                        paddRemoved = " ";
                    }

                    casewiseRecordForUNF.set(k, paddRemoved);
                    casewiseRecordForTabFile.set(k,
                            "\"" + paddRemoved.replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"");

                    // end of String var case

                } else {
                    // numeric var case
                    if (casewiseRecordForTabFile.get(k).equals(MissingValueForTextDataFileNumeric)) {
                        casewiseRecordForUNF.set(k, null);
                    }

                } // end of variable-type check

                if (casewiseRecordForTabFile.get(k) != null
                        && !casewiseRecordForTabFile.get(k).equals(MissingValueForTextDataFileNumeric)) {

                    // to do date conversion
                    String variableFormatType = variableFormatTypeList[k];
                    dbgLog.finer("k=" + k + "th variable format=" + variableFormatType);

                    int formatDecimalPointPosition = formatDecimalPointPositionList.get(k);

                    if (variableFormatType.equals("date")) {
                        dbgLog.finer("date case");

                        long dateDatum = Long.parseLong(casewiseRecordForTabFile.get(k).toString()) * 1000L
                                - SPSS_DATE_OFFSET;

                        String newDatum = sdf_ymd.format(new Date(dateDatum));
                        dbgLog.finer("k=" + k + ":" + newDatum);
                        caseWiseDateFormatForUNF[k] = sdf_ymd.toPattern();

                        casewiseRecordForTabFile.set(k, newDatum);
                        casewiseRecordForUNF.set(k, newDatum);
                        //formatCategoryTable.put(variableNameList.get(k), "date");
                    } else if (variableFormatType.equals("time")) {
                        dbgLog.finer("time case:DTIME or DATETIME or TIME");
                        //formatCategoryTable.put(variableNameList.get(k), "time");

                        if (printFormatTable.get(variableNameList.get(k)).equals("DTIME")) {

                            if (casewiseRecordForTabFile.get(k).toString().indexOf(".") < 0) {
                                long dateDatum = Long.parseLong(casewiseRecordForTabFile.get(k).toString())
                                        * 1000L - SPSS_DATE_BIAS;
                                String newDatum = sdf_dhms.format(new Date(dateDatum));
                                // Note: DTIME is not a complete date, so we don't save a date format with it
                                dbgLog.finer("k=" + k + ":" + newDatum);
                                casewiseRecordForTabFile.set(k, newDatum);
                                casewiseRecordForUNF.set(k, newDatum);
                            } else {
                                // decimal point included
                                String[] timeData = casewiseRecordForTabFile.get(k).toString().split("\\.");

                                dbgLog.finer(StringUtils.join(timeData, "|"));
                                long dateDatum = Long.parseLong(timeData[0]) * 1000L - SPSS_DATE_BIAS;
                                StringBuilder sb_time = new StringBuilder(sdf_dhms.format(new Date(dateDatum)));

                                if (formatDecimalPointPosition > 0) {
                                    sb_time.append("." + timeData[1].substring(0, formatDecimalPointPosition));
                                }

                                dbgLog.finer("k=" + k + ":" + sb_time.toString());
                                casewiseRecordForTabFile.set(k, sb_time.toString());
                                casewiseRecordForUNF.set(k, sb_time.toString());
                            }
                        } else if (printFormatTable.get(variableNameList.get(k)).equals("DATETIME")) {

                            if (casewiseRecordForTabFile.get(k).toString().indexOf(".") < 0) {
                                long dateDatum = Long.parseLong(casewiseRecordForTabFile.get(k).toString())
                                        * 1000L - SPSS_DATE_OFFSET;
                                String newDatum = sdf_ymdhms.format(new Date(dateDatum));
                                caseWiseDateFormatForUNF[k] = sdf_ymdhms.toPattern();
                                dbgLog.finer("k=" + k + ":" + newDatum);
                                casewiseRecordForTabFile.set(k, newDatum);
                                casewiseRecordForUNF.set(k, newDatum);
                            } else {
                                // decimal point included
                                String[] timeData = casewiseRecordForTabFile.get(k).toString().split("\\.");

                                //dbgLog.finer(StringUtils.join(timeData, "|"));
                                long dateDatum = Long.parseLong(timeData[0]) * 1000L - SPSS_DATE_OFFSET;
                                StringBuilder sb_time = new StringBuilder(
                                        sdf_ymdhms.format(new Date(dateDatum)));
                                //dbgLog.finer(sb_time.toString());

                                if (formatDecimalPointPosition > 0) {
                                    sb_time.append("." + timeData[1].substring(0, formatDecimalPointPosition));
                                }
                                caseWiseDateFormatForUNF[k] = sdf_ymdhms.toPattern()
                                        + (formatDecimalPointPosition > 0 ? ".S" : "");
                                dbgLog.finer("k=" + k + ":" + sb_time.toString());
                                casewiseRecordForTabFile.set(k, sb_time.toString());
                                casewiseRecordForUNF.set(k, sb_time.toString());
                            }
                        } else if (printFormatTable.get(variableNameList.get(k)).equals("TIME")) {
                            if (casewiseRecordForTabFile.get(k).toString().indexOf(".") < 0) {
                                long dateDatum = Long.parseLong(casewiseRecordForTabFile.get(k).toString())
                                        * 1000L;
                                String newDatum = sdf_hms.format(new Date(dateDatum));
                                caseWiseDateFormatForUNF[k] = sdf_hms.toPattern();
                                dbgLog.finer("k=" + k + ":" + newDatum);
                                casewiseRecordForTabFile.set(k, newDatum);
                                casewiseRecordForUNF.set(k, newDatum);
                            } else {
                                // decimal point included
                                String[] timeData = casewiseRecordForTabFile.get(k).toString().split("\\.");

                                //dbgLog.finer(StringUtils.join(timeData, "|"));
                                long dateDatum = Long.parseLong(timeData[0]) * 1000L;
                                StringBuilder sb_time = new StringBuilder(sdf_hms.format(new Date(dateDatum)));
                                //dbgLog.finer(sb_time.toString());

                                if (formatDecimalPointPosition > 0) {
                                    sb_time.append("." + timeData[1].substring(0, formatDecimalPointPosition));
                                }
                                caseWiseDateFormatForUNF[k] = this.sdf_hms.toPattern()
                                        + (formatDecimalPointPosition > 0 ? ".S" : "");
                                dbgLog.finer("k=" + k + ":" + sb_time.toString());
                                casewiseRecordForTabFile.set(k, sb_time.toString());
                                casewiseRecordForUNF.set(k, sb_time.toString());
                            }
                        }
                    } else if (variableFormatType.equals("other")) {
                        dbgLog.finer("other non-date/time case");

                        if (printFormatTable.get(variableNameList.get(k)).equals("WKDAY")) {
                            // day of week
                            dbgLog.finer("data k=" + k + ":" + casewiseRecordForTabFile.get(k));
                            dbgLog.finer("data k=" + k + ":" + SPSSConstants.WEEKDAY_LIST
                                    .get(Integer.valueOf(casewiseRecordForTabFile.get(k).toString()) - 1));
                            String newDatum = SPSSConstants.WEEKDAY_LIST
                                    .get(Integer.valueOf(casewiseRecordForTabFile.get(k).toString()) - 1);
                            casewiseRecordForTabFile.set(k, newDatum);
                            casewiseRecordForUNF.set(k, newDatum);
                            dbgLog.finer("wkday:k=" + k + ":" + casewiseRecordForTabFile.get(k));
                        } else if (printFormatTable.get(variableNameList.get(k)).equals("MONTH")) {
                            // month
                            dbgLog.finer("data k=" + k + ":" + casewiseRecordForTabFile.get(k));
                            dbgLog.finer("data k=" + k + ":" + SPSSConstants.MONTH_LIST
                                    .get(Integer.valueOf(casewiseRecordForTabFile.get(k).toString()) - 1));
                            String newDatum = SPSSConstants.MONTH_LIST
                                    .get(Integer.valueOf(casewiseRecordForTabFile.get(k).toString()) - 1);
                            casewiseRecordForTabFile.set(k, newDatum);
                            casewiseRecordForUNF.set(k, newDatum);
                            dbgLog.finer("month:k=" + k + ":" + casewiseRecordForTabFile.get(k));

                        }
                    }
                    // end of date/time block
                } // end: date-time-datum check

            } // end: loop-k(2nd: variablte-wise-check)

            // write to tab file
            if (casewiseRecordForTabFile.size() > 0) {
                pwout.println(StringUtils.join(casewiseRecordForTabFile, "\t"));
            }

            if (casewiseRecordForTabFile.size() > 0) {
                for (int ij = 0; ij < varQnty; ij++) {
                    dataTable2[ij][caseIndex - 1] = casewiseRecordForUNF.get(ij);
                    if (variableFormatTypeList[ij].equals("date")
                            || variableFormatTypeList[ij].equals("time")) {
                        this.dateFormats[ij][caseIndex - 1] = caseWiseDateFormatForUNF[ij];
                    }
                }
            }

            // numeric contents-check
            for (int l = 0; l < casewiseRecordForTabFile.size(); l++) {
                if (variableFormatTypeList[l].equals("date") || variableFormatTypeList[l].equals("time")
                        || printFormatTable.get(variableNameList.get(l)).equals("WKDAY")
                        || printFormatTable.get(variableNameList.get(l)).equals("MONTH")) {
                    variableTypeFinal[l] = -1;
                }

                if (variableTypeFinal[l] == 0) {
                    if (casewiseRecordForTabFile.get(l).toString().indexOf(".") >= 0) {
                        // l-th variable is not integer
                        variableTypeFinal[l] = 1;
                        decimalVariableSet.add(l);
                    }
                }
            }

            // reset the case-wise working objects
            casewiseRecordForTabFile.clear();
            casewiseRecordForUNF.clear();

            if (stream.available() == 0) {
                // reached the end of this file
                // do exit-processing

                dbgLog.fine("***** reached the end of the file at " + ii + "th iteration *****");

                break;
            } // if eof processing
        } //i-loop: case(row) iteration

        // close the writer
        pwout.close();

    } catch (IOException ex) {
        throw ex;
    }

    smd.getFileInformation().put("caseQnty", caseQnty);
    smd.setDecimalVariables(decimalVariableSet);
    smd.setVariableFormatCategory(formatCategoryTable);

    // contents check
    dbgLog.fine("variableType=" + ArrayUtils.toString(variableTypeFinal));
    dbgLog.fine("numberOfDecimalVariables=" + numberOfDecimalVariables);
    dbgLog.fine("decimalVariableSet=" + decimalVariableSet);

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

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

void decodeRecordTypeDataUnCompressed(BufferedInputStream stream) throws IOException {
    dbgLog.fine("***** decodeRecordTypeDataUnCompressed(): start *****");

    if (stream == null) {
        throw new IllegalArgumentException("decodeRecordTypeDataUnCompressed: stream == null!");
    }/*from w  ww .  j ava 2  s . c  o m*/

    int varQnty = dataTable.getVarQuantity().intValue();

    // 
    // set-up tab file

    PrintWriter pwout = createOutputWriter(stream);

    boolean hasStringVarContinuousBlock = obsNonVariableBlockSet.size() > 0 ? true : false;
    dbgLog.fine("hasStringVarContinuousBlock=" + hasStringVarContinuousBlock);

    int ii = 0;

    int OBS = LENGTH_SAV_OBS_BLOCK;
    int nOBS = OBSUnitsPerCase;

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

    int caseIndex = 0;

    dbgLog.fine("printFormatTable:\n" + printFormatTable);

    variableFormatTypeList = new String[varQnty];
    dateFormatList = new String[varQnty];

    for (int i = 0; i < varQnty; i++) {
        variableFormatTypeList[i] = SPSSConstants.FORMAT_CATEGORY_TABLE
                .get(printFormatTable.get(variableNameList.get(i)));
        dbgLog.fine("i=" + i + "th variableFormatTypeList=" + variableFormatTypeList[i]);
        formatCategoryTable.put(variableNameList.get(i), variableFormatTypeList[i]);
    }
    dbgLog.fine("variableFormatType:\n" + Arrays.deepToString(variableFormatTypeList));
    dbgLog.fine("formatCategoryTable:\n" + formatCategoryTable);

    int numberOfDecimalVariables = 0;

    // TODO: 
    // Make sure the date formats are actually preserved! 
    // (this is something that was collected in the code below and passed
    // to the UNF calculator). 
    // -- L.A. 4.0 alpha

    List<String> casewiseRecordForTabFile = new ArrayList<String>();

    // missing values are written to the tab-delimited file by
    // using the default or user-specified missing-value  strings;
    // however, to calculate UNF/summary statistics,
    // classes for these calculations require their specific 
    // missing values that differ from the above missing-value
    // strings; therefore, after row data for the tab-delimited 
    // file are written, missing values in a row are changed to
    // UNF/summary-statistics-OK ones.

    // data-storage object for sumStat
    ///dataTable2 = new Object[varQnty][caseQnty];
    // storage of date formats to pass to UNF   
    ///dateFormats = new String[varQnty][caseQnty];

    try {
        for (int i = 0;; i++) { // case-wise loop

            byte[] buffer = new byte[OBS * nOBS];

            int nbytesuc = stream.read(buffer);

            StringBuilder sb_stringStorage = new StringBuilder("");

            for (int k = 0; k < nOBS; k++) {
                int offset = OBS * k;

                // uncompressed case
                // numeric missing value == sysmis
                // FF FF FF FF FF FF eF FF(little endian)
                // string missing value
                // 20 20 20 20 20 20 20 20
                // cf: compressed case 
                // numeric type:sysmis == 0xFF
                // string type: missing value == 0xFE
                // 

                boolean isNumeric = OBSwiseTypelList.get(k) == 0 ? true : false;

                if (isNumeric) {
                    dbgLog.finer(k + "-th variable is numeric");
                    // interprete as double
                    ByteBuffer bb_double = ByteBuffer.wrap(buffer, offset, LENGTH_SAV_OBS_BLOCK);
                    if (isLittleEndian) {
                        bb_double.order(ByteOrder.LITTLE_ENDIAN);
                    }
                    //char[] hexpattern =
                    String dphex = new String(Hex.encodeHex(
                            Arrays.copyOfRange(bb_double.array(), offset, offset + LENGTH_SAV_OBS_BLOCK)));
                    dbgLog.finer("dphex=" + dphex);

                    if ((dphex.equals("ffffffffffffefff")) || (dphex.equals("ffefffffffffffff"))) {
                        //casewiseRecordForTabFile.add(systemMissingValue);
                        // add the numeric missing value
                        dbgLog.fine("SAV Reader: adding: Missing Value (numeric)");
                        casewiseRecordForTabFile.add(MissingValueForTextDataFileNumeric);
                    } else {
                        Double ddatum = bb_double.getDouble();
                        dbgLog.fine("SAV Reader: adding: ddatum=" + ddatum);

                        // add this non-missing-value numeric datum
                        casewiseRecordForTabFile.add(doubleNumberFormatter.format(ddatum));
                    }

                } else {
                    dbgLog.finer(k + "-th variable is string");
                    // string case
                    // strip space-padding
                    // do not trim: string might have spaces within it
                    // the missing value (hex) for a string variable is:
                    // "20 20 20 20 20 20 20 20"

                    String strdatum = new String(
                            Arrays.copyOfRange(buffer, offset, (offset + LENGTH_SAV_OBS_BLOCK)),
                            defaultCharSet);
                    dbgLog.finer("str_datum=" + strdatum);
                    // add this non-missing-value string datum 
                    casewiseRecordForTabFile.add(strdatum);

                } // if isNumeric

            } // k-loop

            // String-variable's continuous block exits:
            if (hasStringVarContinuousBlock) {
                // continuous blocks: string case
                // concatenating process
                //dbgLog.fine("concatenating process starts");

                //dbgLog.fine("casewiseRecordForTabFile(before)="+casewiseRecordForTabFile);
                //dbgLog.fine("casewiseRecordForTabFile(before:size)="+casewiseRecordForTabFile.size());

                StringBuilder sb = new StringBuilder("");
                int firstPosition = 0;

                Set<Integer> removeJset = new HashSet<Integer>();
                for (int j = 0; j < nOBS; j++) {
                    dbgLog.finer("j=" + j + "-th type =" + OBSwiseTypelList.get(j));
                    if (OBSwiseTypelList.get(j) == -1) {
                        // String continued fount at j-th 
                        // look back the j-1 
                        firstPosition = j - 1;
                        int lastJ = j;
                        String concatanated = null;

                        removeJset.add(j);
                        sb.append(casewiseRecordForTabFile.get(j - 1));
                        sb.append(casewiseRecordForTabFile.get(j));
                        for (int jc = 1;; jc++) {
                            if (OBSwiseTypelList.get(j + jc) != -1) {
                                // j is the end unit of this string variable
                                concatanated = sb.toString();
                                sb.setLength(0);
                                lastJ = j + jc;
                                break;
                            } else {
                                sb.append(casewiseRecordForTabFile.get(j + jc));
                                removeJset.add(j + jc);
                            }
                        }
                        casewiseRecordForTabFile.set(j - 1, concatanated);

                        //out.println(j-1+"th concatanated="+concatanated);
                        j = lastJ - 1;

                    } // end-of-if: continuous-OBS only
                } // end of loop-j

                List<String> newDataLine = new ArrayList<String>();

                for (int jl = 0; jl < casewiseRecordForTabFile.size(); jl++) {
                    //out.println("jl="+jl+"-th datum =["+casewiseRecordForTabFile.get(jl)+"]");

                    if (!removeJset.contains(jl)) {
                        newDataLine.add(casewiseRecordForTabFile.get(jl));
                    }
                }

                dbgLog.fine("new casewiseRecordForTabFile=" + newDataLine);
                dbgLog.fine("new casewiseRecordForTabFile(size)=" + newDataLine.size());

                casewiseRecordForTabFile = newDataLine;

            } // end-if: stringContinuousVar-exist case

            caseIndex++;
            dbgLog.finer("caseIndex=" + caseIndex);
            for (int k = 0; k < casewiseRecordForTabFile.size(); k++) {

                if (variableTypelList.get(k) > 0) {

                    // See my comments for this padding removal logic
                    // in the "compressed" method -- L.A.

                    String paddRemoved = StringUtils.stripEnd(casewiseRecordForTabFile.get(k).toString(), null);
                    // TODO: clean this up.  For now, just make sure that strings contain at least one blank space.
                    if (paddRemoved.equals("")) {
                        paddRemoved = " ";
                    }

                    //casewiseRecordForTabFile.set(k, "\"" + paddRemoved.replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"");
                    casewiseRecordForTabFile.set(k, escapeCharacterString(paddRemoved));

                    // end of String var case

                } // end of variable-type check

                if (casewiseRecordForTabFile.get(k) != null
                        && !casewiseRecordForTabFile.get(k).equals(MissingValueForTextDataFileNumeric)) {

                    // to do date conversion
                    String variableFormatType = variableFormatTypeList[k];
                    dbgLog.finer("k=" + k + "th variable format=" + variableFormatType);

                    int formatDecimalPointPosition = formatDecimalPointPositionList.get(k);

                    if (variableFormatType.equals("date")) {
                        dbgLog.finer("date case");

                        long dateDatum = Long.parseLong(casewiseRecordForTabFile.get(k).toString()) * 1000L
                                - SPSS_DATE_OFFSET;

                        String newDatum = sdf_ymd.format(new Date(dateDatum));
                        dbgLog.finer("k=" + k + ":" + newDatum);

                        casewiseRecordForTabFile.set(k, newDatum);
                        dateFormatList[k] = sdf_ymd.toPattern();
                    } else if (variableFormatType.equals("time")) {
                        dbgLog.finer("time case:DTIME or DATETIME or TIME");
                        //formatCategoryTable.put(variableNameList.get(k), "time");
                        // not treating DTIME as date/time; see comment elsewhere in 
                        // the code; 
                        // (but we do need to remember to treat the resulting values 
                        // as character strings, not numerics!)

                        if (printFormatTable.get(variableNameList.get(k)).equals("DTIME")) {

                            if (casewiseRecordForTabFile.get(k).toString().indexOf(".") < 0) {
                                long dateDatum = Long.parseLong(casewiseRecordForTabFile.get(k).toString())
                                        * 1000L - SPSS_DATE_BIAS;
                                String newDatum = sdf_dhms.format(new Date(dateDatum));
                                // Note: DTIME is not a complete date, so we don't save a date format with it
                                dbgLog.finer("k=" + k + ":" + newDatum);
                                casewiseRecordForTabFile.set(k, newDatum);
                            } else {
                                // decimal point included
                                String[] timeData = casewiseRecordForTabFile.get(k).toString().split("\\.");

                                dbgLog.finer(StringUtils.join(timeData, "|"));
                                long dateDatum = Long.parseLong(timeData[0]) * 1000L - SPSS_DATE_BIAS;
                                StringBuilder sb_time = new StringBuilder(sdf_dhms.format(new Date(dateDatum)));

                                if (formatDecimalPointPosition > 0) {
                                    sb_time.append("." + timeData[1].substring(0, formatDecimalPointPosition));
                                }

                                dbgLog.finer("k=" + k + ":" + sb_time.toString());
                                casewiseRecordForTabFile.set(k, sb_time.toString());
                            }
                        } else if (printFormatTable.get(variableNameList.get(k)).equals("DATETIME")) {
                            // TODO: 
                            // (for both datetime and "dateless" time)
                            // keep the longest of the matching formats - i.e., if there are *some*
                            // values in the vector that have thousands of a second, that should be 
                            // part of the saved format!
                            //  -- L.A. Aug. 12 2014 

                            if (casewiseRecordForTabFile.get(k).toString().indexOf(".") < 0) {
                                long dateDatum = Long.parseLong(casewiseRecordForTabFile.get(k).toString())
                                        * 1000L - SPSS_DATE_OFFSET;
                                String newDatum = sdf_ymdhms.format(new Date(dateDatum));
                                dbgLog.finer("k=" + k + ":" + newDatum);
                                casewiseRecordForTabFile.set(k, newDatum);
                                dateFormatList[k] = sdf_ymdhms.toPattern();
                            } else {
                                // decimal point included
                                String[] timeData = casewiseRecordForTabFile.get(k).toString().split("\\.");

                                //dbgLog.finer(StringUtils.join(timeData, "|"));
                                long dateDatum = Long.parseLong(timeData[0]) * 1000L - SPSS_DATE_OFFSET;
                                StringBuilder sb_time = new StringBuilder(
                                        sdf_ymdhms.format(new Date(dateDatum)));
                                //dbgLog.finer(sb_time.toString());

                                if (formatDecimalPointPosition > 0) {
                                    sb_time.append("." + timeData[1].substring(0, formatDecimalPointPosition));
                                }
                                dbgLog.finer("k=" + k + ":" + sb_time.toString());
                                casewiseRecordForTabFile.set(k, sb_time.toString());
                                // datetime with milliseconds:
                                dateFormatList[k] = sdf_ymdhms.toPattern()
                                        + (formatDecimalPointPosition > 0 ? ".S" : "");
                            }
                        } else if (printFormatTable.get(variableNameList.get(k)).equals("TIME")) {
                            if (casewiseRecordForTabFile.get(k).toString().indexOf(".") < 0) {
                                long dateDatum = Long.parseLong(casewiseRecordForTabFile.get(k).toString())
                                        * 1000L;
                                String newDatum = sdf_hms.format(new Date(dateDatum));
                                dbgLog.finer("k=" + k + ":" + newDatum);
                                casewiseRecordForTabFile.set(k, newDatum);
                                if (dateFormatList[k] == null) {
                                    dateFormatList[k] = sdf_hms.toPattern();
                                }
                            } else {
                                // decimal point included
                                String[] timeData = casewiseRecordForTabFile.get(k).toString().split("\\.");

                                //dbgLog.finer(StringUtils.join(timeData, "|"));
                                long dateDatum = Long.parseLong(timeData[0]) * 1000L;
                                StringBuilder sb_time = new StringBuilder(sdf_hms.format(new Date(dateDatum)));
                                //dbgLog.finer(sb_time.toString());

                                if (formatDecimalPointPosition > 0) {
                                    sb_time.append("." + timeData[1].substring(0, formatDecimalPointPosition));
                                }
                                dbgLog.finer("k=" + k + ":" + sb_time.toString());
                                casewiseRecordForTabFile.set(k, sb_time.toString());
                                // time, possibly with milliseconds:
                                String format_hmsS = sdf_hms.toPattern()
                                        + (formatDecimalPointPosition > 0 ? ".S" : "");
                                if (dateFormatList[k] == null
                                        || (format_hmsS.length() > dateFormatList[k].length())) {
                                    dateFormatList[k] = format_hmsS;
                                }
                            }
                        }
                    } else if (variableFormatType.equals("other")) {
                        dbgLog.finer("other non-date/time case");

                        if (printFormatTable.get(variableNameList.get(k)).equals("WKDAY")) {
                            // day of week
                            dbgLog.finer("data k=" + k + ":" + casewiseRecordForTabFile.get(k));
                            dbgLog.finer("data k=" + k + ":" + SPSSConstants.WEEKDAY_LIST
                                    .get(Integer.valueOf(casewiseRecordForTabFile.get(k).toString()) - 1));
                            String newDatum = SPSSConstants.WEEKDAY_LIST
                                    .get(Integer.valueOf(casewiseRecordForTabFile.get(k).toString()) - 1);
                            casewiseRecordForTabFile.set(k, newDatum);
                            dbgLog.finer("wkday:k=" + k + ":" + casewiseRecordForTabFile.get(k));
                        } else if (printFormatTable.get(variableNameList.get(k)).equals("MONTH")) {
                            // month
                            dbgLog.finer("data k=" + k + ":" + casewiseRecordForTabFile.get(k));
                            dbgLog.finer("data k=" + k + ":" + SPSSConstants.MONTH_LIST
                                    .get(Integer.valueOf(casewiseRecordForTabFile.get(k).toString()) - 1));
                            String newDatum = SPSSConstants.MONTH_LIST
                                    .get(Integer.valueOf(casewiseRecordForTabFile.get(k).toString()) - 1);
                            casewiseRecordForTabFile.set(k, newDatum);
                            dbgLog.finer("month:k=" + k + ":" + casewiseRecordForTabFile.get(k));

                        }
                    }
                    // end of date/time block
                } // end: date-time-datum check

            } // end: loop-k(2nd: variablte-wise-check)

            // write to tab file
            if (casewiseRecordForTabFile.size() > 0) {
                pwout.println(StringUtils.join(casewiseRecordForTabFile, "\t"));
            }

            // numeric contents-check
            for (int l = 0; l < casewiseRecordForTabFile.size(); l++) {
                if (variableFormatTypeList[l].equals("date") || variableFormatTypeList[l].equals("time")
                        || printFormatTable.get(variableNameList.get(l)).equals("WKDAY")
                        || printFormatTable.get(variableNameList.get(l)).equals("MONTH")) {

                } else {
                    if (variableTypelList.get(l) <= 0) {
                        if (casewiseRecordForTabFile.get(l).toString().indexOf(".") >= 0) {
                            decimalVariableSet.add(l);
                        }
                    }
                }
            }

            // reset the case-wise working objects
            casewiseRecordForTabFile.clear();

            if (stream.available() == 0) {
                // reached the end of this file
                // do exit-processing

                dbgLog.fine("reached the end of the file at " + ii + "th iteration");

                break;
            } // if eof processing
        } //i-loop: case(row) iteration

        // close the writer
        pwout.close();

    } catch (IOException ex) {
        throw ex;
    }

    // contents check
    dbgLog.fine("numberOfDecimalVariables=" + numberOfDecimalVariables);
    dbgLog.fine("decimalVariableSet=" + decimalVariableSet);

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

From source file:com.zimbra.cs.account.ldap.LdapProvisioning.java

private Account createDummyAccount(Map<String, Object> attrs, String password, Domain domain)
        throws ServiceException {
    SecureRandom random = new SecureRandom();
    byte[] keyBytes = new byte[10];
    random.nextBytes(keyBytes);/*  w  w  w.j  av  a 2s  .  c o  m*/
    String dummyEmailAddr = String.valueOf(Hex.encodeHex(keyBytes)) + "@" + domain.getName();
    return createAccount(dummyEmailAddr, password, attrs);
}

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  a2s.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

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  ww w . j a v  a2s .  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("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;
}