List of usage examples for java.nio ByteOrder LITTLE_ENDIAN
ByteOrder LITTLE_ENDIAN
To view the source code for java.nio ByteOrder LITTLE_ENDIAN.
Click Source Link
From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java
/** * get the value from the outsideTemperature field * * @return outsideTemperature - the outsideTemperature as a float *///from w w w. j ava2 s.c o m public float getOutsideTemperature() { this.outsideTemperature.flip(); return (float) (this.outsideTemperature.order(ByteOrder.LITTLE_ENDIAN).getShort()) / 10; }
From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.dta.DTAFileReader.java
private void decodeDescriptorVarSortList(BufferedInputStream stream, int nvar) throws IOException { /* /*w w w . ja v a 2 s . c om*/ * Whatever this "var sort list" is, we don't seem to be using this * information for any purposes in particular. However, we need to read * the bytes, to skip to the next section in the stream, if nothing else. * -- L.A. 4.0 */ int length_var_sort_list = VAR_SORT_FIELD_LENGTH * (nvar + 1); if (dbgLog.isLoggable(Level.FINE)) dbgLog.fine("length_var_sort_list=" + length_var_sort_list); byte[] varSortList = new byte[length_var_sort_list]; short[] variableSortList = new short[nvar + 1]; int nbytes = stream.read(varSortList, 0, length_var_sort_list); if (nbytes == 0) { throw new IOException("reading error: the varSortList"); } int offset_start = 0; for (int i = 0; i <= nvar; i++) { ByteBuffer bb_varSortList = ByteBuffer.wrap(varSortList, offset_start, VAR_SORT_FIELD_LENGTH); if (isLittleEndian) { bb_varSortList.order(ByteOrder.LITTLE_ENDIAN); } variableSortList[i] = bb_varSortList.getShort(); offset_start += VAR_SORT_FIELD_LENGTH; } if (dbgLog.isLoggable(Level.FINE)) dbgLog.fine("variableSortList=" + Arrays.toString(variableSortList)); }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private byte[] encodeTimelinePin(UUID uuid, int timestamp, short duration, int icon_id, String title, String subtitle) {//from w w w. j ava 2 s. com final short TIMELINE_PIN_LENGTH = 46; icon_id |= 0x80000000; byte attributes_count = 2; byte actions_count = 0; int attributes_length = 10 + title.getBytes().length; if (subtitle != null && !subtitle.isEmpty()) { attributes_length += 3 + subtitle.getBytes().length; attributes_count += 1; } int pin_length = TIMELINE_PIN_LENGTH + attributes_length; ByteBuffer buf = ByteBuffer.allocate(pin_length); // pin - 46 bytes buf.order(ByteOrder.BIG_ENDIAN); buf.putLong(uuid.getMostSignificantBits()); buf.putLong(uuid.getLeastSignificantBits()); buf.putLong(0); // parent buf.putLong(0); buf.order(ByteOrder.LITTLE_ENDIAN); buf.putInt(timestamp); // 32-bit timestamp buf.putShort(duration); buf.put((byte) 0x02); // type (0x02 = pin) buf.putShort((short) 0x0001); // flags 0x0001 = ? buf.put((byte) 0x01); // layout was (0x02 = pin?), 0x01 needed for subtitle but seems to do no harm if there isn't one buf.putShort((short) attributes_length); // total length of all attributes and actions in bytes buf.put(attributes_count); buf.put(actions_count); buf.put((byte) 4); // icon buf.putShort((short) 4); // length of int buf.putInt(icon_id); buf.put((byte) 1); // title buf.putShort((short) title.getBytes().length); buf.put(title.getBytes()); if (subtitle != null && !subtitle.isEmpty()) { buf.put((byte) 2); //subtitle buf.putShort((short) subtitle.getBytes().length); buf.put(subtitle.getBytes()); } return encodeBlobdb(uuid, BLOBDB_INSERT, BLOBDB_PIN, buf.array()); }
From source file:gephi.spade.panel.fcsFile.java
/** * readDoubleData ---//from w w w . j a v a2 s . c o m * <p> * Reads double precision floating point values in list mode in the DATA * segment and updates eventList with the integer values of the values. * </p> * * @param data * <code>ByteBuffer</code> containing the DATA segment of the * underlying file. */ private void readDoubleData(ByteBuffer data) { // Allocate the eventList eventList = new double[parameters][totalEvents]; if (littleEndianP) { data.order(ByteOrder.LITTLE_ENDIAN); } // Convert the byte buffer into a double buffer - doesn't get any easier DoubleBuffer db = data.asDoubleBuffer(); final int totalEvents = this.totalEvents; final int parameters = this.parameters; for (int i = 0; i < totalEvents; i++) { for (int j = 0; j < parameters; j++) { // Store the value into the array eventList[j][i] = db.get(); } } }
From source file:org.jtrfp.trcl.core.ResourceManager.java
public ByteBuffer[] getFontBIN(String fontPath, NDXFile ndx) { try {/*from w w w .j a va2 s . c o m*/ InputStream is = getInputStreamFromResource(fontPath); List<Integer> widths = ndx.getWidths(); ByteBuffer[] result = new ByteBuffer[widths.size()]; for (int c = 0; c < ndx.getWidths().size(); c++) { final int len = 23 * widths.get(c); result[c] = ByteBuffer.allocate(23 * widths.get(c) * 4); result[c].order(ByteOrder.LITTLE_ENDIAN); for (int i = 0; i < len; i++) { byte b = (byte) (is.read()); result[c].put(b); result[c].put(b); result[c].put(b); result[c].put((byte) ((b & 0xFF) > 1 ? 255 : b));//Less than 1 is alpha'd out } result[c].clear(); } //end for(chars) return result; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:org.eclipse.paho.android.service.MqttConnection.java
private void parseMqttMessageV2(String topic, MqttMessage mqttMessage) throws Exception { Log.v("mqtt", "parseMqttMessageV2"); Context ctx = NanuService.getContext(); byte origMqttMsgByte[] = mqttMessage.getPayload(); int mqttIndex = 0; boolean processVTagSuccess = false; boolean processPTagSuccess = false; long mqttPacketValue = 0; boolean processTTagSuccess = false; long mqttTimestampValue = 0; boolean processLTagSuccess = false; int mqttMsgLengthValue = 0; boolean processMTagSuccess = false; String mqttMessageValue = ""; String mqttMembersValue = ""; boolean processGTagSuccess = false; long mqttGroupIDValue = 0; boolean processSTagSuccess = false; String mqttSubjectValue = ""; boolean processCTagSuccess = false; int mqttMemberCountValue = 0; boolean processNTagSuccess = false; int mqttAdminCountValue = 0; boolean processATagSuccess = false; String mqttAdminsValue = ""; String[] topicArray = topic.split("\\/"); String sender = topicArray[2]; if (topicArray.length == 4) { processGTagSuccess = true;/*from w w w. j av a2 s. c o m*/ try { mqttGroupIDValue = Long.parseLong(topicArray[3].toString().trim()); } catch (NumberFormatException nfe) { processGTagSuccess = false; nfe.printStackTrace(); } if (mqttGroupIDValue == 0) { try { mqttGroupIDValue = Long.valueOf(topicArray[3].trim()); } catch (Exception err) { processGTagSuccess = false; err.printStackTrace(); } } } String mqttMsgDateValue = ""; for (int indexMqttCounter = 0; indexMqttCounter < origMqttMsgByte.length; indexMqttCounter++) { /* Log.v(SettingsManager.TAG, "MqttService origMqttMsgByte[" + indexMqttCounter + "] = " + origMqttMsgByte[indexMqttCounter]); */ } for (int indexMqttCounter = 0; indexMqttCounter < origMqttMsgByte.length; indexMqttCounter++) { if (indexMqttCounter == 0) { mqttIndex = indexMqttCounter; long mqttVTag = getMqttTag(origMqttMsgByte, mqttIndex); if (mqttVTag != -1) { if (mqttVTag == 86) // "V" { processVTagSuccess = true; mqttIndex = mqttIndex + 2; } else { processVTagSuccess = false; break; } } } else { if (mqttIndex == indexMqttCounter) { long mqttTag = getMqttTag(origMqttMsgByte, mqttIndex); if (mqttTag != -1) { if (mqttTag == 80) /* "P" */ { mqttIndex = mqttIndex + 1; long mPValue = origMqttMsgByte[mqttIndex]; mqttPacketValue = mPValue; mqttIndex = mqttIndex + 1; processPTagSuccess = true; } else if (mqttTag == 84) /* "T" */ { mqttIndex = mqttIndex + 1; byte timeStampArray[] = new byte[8]; for (int i = 0; i < 8; i++) { timeStampArray[i] = origMqttMsgByte[mqttIndex + i]; } mqttTimestampValue = ByteBuffer.wrap(timeStampArray).order(ByteOrder.LITTLE_ENDIAN) .getLong(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); String messageYear = sdf.format(mqttTimestampValue); if (messageYear.length() != 4) { mqttTimestampValue = ByteBuffer.wrap(timeStampArray).order(ByteOrder.BIG_ENDIAN) .getLong(); } SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String messageDate = sdfDate.format(mqttTimestampValue); processTTagSuccess = true; mqttIndex = mqttIndex + 8; } else if (mqttTag == 76) /* "L" */ { if (processPTagSuccess) { if (mqttPacketValue == -128 || (mqttPacketValue == -117) || (mqttPacketValue == -115) || (mqttPacketValue == -126)) { mqttIndex = mqttIndex + 1; mqttMsgLengthValue = origMqttMsgByte[mqttIndex]; processLTagSuccess = true; mqttIndex = mqttIndex + 1; } else if (mqttPacketValue == 0) { mqttIndex = mqttIndex + 1; byte msgLengthArray[] = new byte[4]; for (int i = 0; i < 4; i++) { msgLengthArray[i] = origMqttMsgByte[mqttIndex + i]; } mqttMsgLengthValue = ByteBuffer.wrap(msgLengthArray) .order(ByteOrder.LITTLE_ENDIAN).getInt(); processLTagSuccess = true; mqttIndex = mqttIndex + 4; } } } else if (mqttTag == 77) /* "M" */ { if (processPTagSuccess) { if ((mqttPacketValue == -128) || (mqttPacketValue == -124) || (mqttPacketValue == -126) || (mqttPacketValue == -117)) { if (processCTagSuccess) { mqttIndex = mqttIndex + 1; for (int i = 0; i < mqttMemberCountValue; i++) { byte groupMembersArray[] = new byte[8]; for (int j = 0; j < 8; j++) { groupMembersArray[j] = origMqttMsgByte[mqttIndex + j]; } long participants = ByteBuffer.wrap(groupMembersArray) .order(ByteOrder.LITTLE_ENDIAN).getLong(); mqttIndex = mqttIndex + 8; if (i == (mqttMemberCountValue - 1)) { mqttMembersValue = mqttMembersValue + participants; } else { mqttMembersValue = mqttMembersValue + participants + ","; } } processMTagSuccess = true; } else { break; } } else if (mqttPacketValue == 0) { if (processLTagSuccess) { mqttIndex = mqttIndex + 1; if (mqttMsgLengthValue > 0) { byte messageArray[] = null; try { messageArray = new byte[mqttMsgLengthValue]; } catch (Exception err) { err.printStackTrace(); processMTagSuccess = false; break; } for (int i = 0; i < mqttMsgLengthValue; i++) { messageArray[i] = origMqttMsgByte[mqttIndex + i]; } mqttMessageValue = new String(messageArray); processMTagSuccess = true; mqttIndex = mqttIndex + mqttMsgLengthValue + 1; } } else { break; } } } } else if (mqttTag == 71) /* "G" */ { mqttIndex = mqttIndex + 1; byte groupIDArray[] = new byte[8]; for (int i = 0; i < 8; i++) { groupIDArray[i] = origMqttMsgByte[mqttIndex + i]; } mqttGroupIDValue = ByteBuffer.wrap(groupIDArray).order(ByteOrder.LITTLE_ENDIAN) .getLong(); processGTagSuccess = true; mqttIndex = mqttIndex + 8; } else if (mqttTag == 83) /* "S" */ { if (processLTagSuccess) { mqttIndex = mqttIndex + 1; if (mqttMsgLengthValue > 0) { byte subjectArray[] = null; try { subjectArray = new byte[mqttMsgLengthValue]; } catch (Exception err) { err.printStackTrace(); processSTagSuccess = false; break; } for (int i = 0; i < mqttMsgLengthValue; i++) { subjectArray[i] = origMqttMsgByte[mqttIndex + i]; } mqttSubjectValue = new String(subjectArray); processSTagSuccess = true; mqttIndex = mqttIndex + mqttMsgLengthValue; } } else { break; } } else if (mqttTag == 67) /* "C" */ { mqttIndex = mqttIndex + 1; mqttMemberCountValue = origMqttMsgByte[mqttIndex]; processCTagSuccess = true; mqttIndex = mqttIndex + 1; } else if (mqttTag == 78) /* "N" */ { mqttIndex = mqttIndex + 1; mqttAdminCountValue = origMqttMsgByte[mqttIndex]; processNTagSuccess = true; mqttIndex = mqttIndex + 1; } else if (mqttTag == 65) /* "A" */ { if (processPTagSuccess) { if (mqttPacketValue == -117) { if (processNTagSuccess) { mqttIndex = mqttIndex + 1; for (int i = 0; i < mqttAdminCountValue; i++) { byte groupAdminsArray[] = new byte[8]; for (int j = 0; j < 8; j++) { groupAdminsArray[j] = origMqttMsgByte[mqttIndex + j]; } long admins = ByteBuffer.wrap(groupAdminsArray) .order(ByteOrder.LITTLE_ENDIAN).getLong(); mqttIndex = mqttIndex + 8; if (i == (mqttAdminCountValue - 1)) { mqttAdminsValue = mqttAdminsValue + admins; } else { mqttAdminsValue = mqttAdminsValue + admins + ","; } } processATagSuccess = true; } else { break; } } } } else { break; } } else { break; } } } } if (!processVTagSuccess) { return; } PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); boolean isScreenOn = pm.isScreenOn(); if (isScreenOn == false) { WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock"); if (wl.isHeld()) { wl.release(); } wl.acquire(10000); WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock"); if (wl_cpu.isHeld()) { wl_cpu.release(); } wl_cpu.acquire(10000); } String message = mqttMessageValue; Log.v("mqtt", "from: " + sender); Log.v("mqtt", "message: " + message); Intent intent = new Intent(); intent.setClassName(ctx, "org.eclipse.paho.android.service.sample.MainActivity"); intent.putExtra("handle", clientHandle); String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns); int messageNotificationId = 1; mNotificationManager.cancel(messageNotificationId); Calendar.getInstance().getTime().toString(); long when = System.currentTimeMillis(); String ticker = sender + " " + mqttMessageValue; PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 3, intent, 0); NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(ctx); notificationCompat.setAutoCancel(true).setContentTitle(sender).setContentIntent(pendingIntent) .setContentText(mqttMessageValue).setTicker(ticker).setWhen(when) .setSmallIcon(R.drawable.ic_launcher); // Notification notification = notificationCompat.build(); Bitmap iconLarge = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_launcher); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx) .setSmallIcon(R.drawable.ic_launcher).setLargeIcon(iconLarge).setContentTitle(sender) .setContentText(mqttMessageValue); mBuilder.setContentIntent(pendingIntent); mBuilder.setTicker(message); mBuilder.setAutoCancel(true); mBuilder.setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS); mNotificationManager.notify(messageNotificationId, mBuilder.build()); }
From source file:au.org.ala.layers.intersect.Grid.java
public void writeGrid(String newfilename, float[] dfiltered, double xmin, double ymin, double xmax, double ymax, double xres, double yres, int nrows, int ncols) { int size, i, length = dfiltered.length; double maxvalue = Double.MAX_VALUE * -1; double minvalue = Double.MAX_VALUE; //write data as whole file RandomAccessFile afile = null; try { //read of random access file can throw an exception afile = new RandomAccessFile(newfilename + ".gri", "rw"); size = 4;// ww w . j a v a 2s.c om byte[] b = new byte[size * length]; ByteBuffer bb = ByteBuffer.wrap(b); if (byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } else { bb.order(ByteOrder.BIG_ENDIAN); } for (i = 0; i < length; i++) { if (Double.isNaN(dfiltered[i])) { bb.putFloat((float) noDataValueDefault); } else { if (minvalue > dfiltered[i]) { minvalue = dfiltered[i]; } if (maxvalue < dfiltered[i]) { maxvalue = dfiltered[i]; } bb.putFloat((float) dfiltered[i]); } } afile.write(b); } catch (Exception e) { logger.error("error writing grid file", e); } finally { if (afile != null) { try { afile.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } writeHeader(newfilename, xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows, ncols, minvalue, maxvalue, "FLT4BYTES", String.valueOf(noDataValueDefault)); }
From source file:edu.vu.isis.ammo.dash.provider.IncidentSyncAdaptor.java
public ArrayList<File> categorySerialize(Cursor cursor) { logger.debug("::categorySerialize"); ArrayList<File> paths = new ArrayList<File>(); if (1 > cursor.getCount()) return paths; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream eos = new DataOutputStream(baos); for (boolean more = cursor.moveToFirst(); more; more = cursor.moveToNext()) { CategoryWrapper iw = new CategoryWrapper(); iw.setMainCategory(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.MAIN_CATEGORY))); iw.setSubCategory(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.SUB_CATEGORY))); iw.setTigrId(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.TIGR_ID))); iw.setIconType(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.ICON_TYPE))); iw.setIcon(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.ICON))); iw.set_ReceivedDate(cursor.getLong(cursor.getColumnIndex(CategoryTableSchemaBase._RECEIVED_DATE))); iw.set_Disposition(cursor.getInt(cursor.getColumnIndex(CategoryTableSchemaBase._DISPOSITION))); Gson gson = new Gson(); try {//from ww w . ja va 2s. c o m eos.writeBytes(gson.toJson(iw)); eos.writeByte(0); } catch (IOException ex) { ex.printStackTrace(); } // not a reference field name :main category mainCategory main_category\n // not a reference field name :sub category subCategory sub_category\n // not a reference field name :tigr id tigrId tigr_id\n try { String fileName = iw.getIcon(); File dataFile = new File(fileName); int dataSize = (int) dataFile.length(); byte[] buffData = new byte[dataSize]; FileInputStream fileStream = new FileInputStream(dataFile); int ret = 0; for (int position = 0; (ret > -1 && dataSize > position); position += ret) { ret = fileStream.read(buffData, position, dataSize - position); } fileStream.close(); eos.writeBytes("icon"); eos.writeByte(0); ByteBuffer dataSizeBuf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE); dataSizeBuf.order(ByteOrder.LITTLE_ENDIAN); dataSizeBuf.putInt(dataSize); // write the category back out eos.write(dataSizeBuf.array()); eos.write(buffData); eos.write(dataSizeBuf.array()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // CategoryTableSchemaBase._DISPOSITION; // try { // if (!applCacheCategoryDir.exists() ) applCacheCategoryDir.mkdirs(); // // File outfile = new File(applCacheCategoryDir, Integer.toHexString((int) System.currentTimeMillis())); // BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(outfile), 8192); // bufferedOutput.write(baos.toByteArray()); // bufferedOutput.flush(); // bufferedOutput.close(); // // paths.add(outfile); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } } return paths; }
From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.sav.SAVFileReader.java
void decodeRecordType2(BufferedInputStream stream) throws IOException { dbgLog.fine("decodeRecordType2(): start"); if (stream == null) { throw new IllegalArgumentException("stream == null!"); }// w w w. j ava 2s.c o m Map<String, String> printFormatNameTable = new LinkedHashMap<String, String>(); Map<String, String> variableLabelMap = new LinkedHashMap<String, String>(); Map<String, List<String>> missingValueTable = new LinkedHashMap<String, List<String>>(); List<Integer> printFormatList = new ArrayList<Integer>(); String caseWeightVariableName = null; int caseWeightVariableIndex = 0; boolean lastVariableIsExtendable = false; boolean extendedVariableMode = false; boolean obs255 = false; String lastVariableName = null; String lastExtendedVariable = null; // this field repeats as many as the number of variables in // this sav file // (note that the above statement is not technically correct, this // record repeats not just for every variable in the file, but for // every OBS (8 byte unit); i.e., if a string is split into multiple // OBS units, each one will have its own RT2 record -- L.A.). // Each field constists of a fixed (32-byte) segment and // then a few variable segments: // if the variable has a label (3rd INT4 set to 1), then there's 4 more // bytes specifying the length of the label, and then that many bytes // holding the label itself (no more than 256). // Then if there are optional missing value units (4th INT4 set to 1) // there will be 3 more OBS units attached = 24 extra bytes. int variableCounter = 0; int obsSeqNumber = 0; int j; dbgLog.fine("RT2: Reading " + OBSUnitsPerCase + " OBS units."); for (j = 0; j < OBSUnitsPerCase; j++) { dbgLog.fine("RT2: " + j + "-th RT2 unit is being decoded."); // 2.0: read the fixed[=non-optional] 32-byte segment byte[] recordType2Fixed = new byte[LENGTH_RECORDTYPE2_FIXED]; try { int nbytes = stream.read(recordType2Fixed, 0, LENGTH_RECORDTYPE2_FIXED); //printHexDump(recordType2Fixed, "recordType2 part 1"); if (nbytes == 0) { throw new IOException("reading recordType2: no bytes read!"); } int offset = 0; // 2.1: create int-view of the bytebuffer for the first 16-byte segment int rt2_1st_4_units = 4; ByteBuffer[] bb_record_type2_fixed_part1 = new ByteBuffer[rt2_1st_4_units]; int[] recordType2FixedPart1 = new int[rt2_1st_4_units]; for (int i = 0; i < rt2_1st_4_units; i++) { bb_record_type2_fixed_part1[i] = ByteBuffer.wrap(recordType2Fixed, offset, LENGTH_SAV_INT_BLOCK); offset += LENGTH_SAV_INT_BLOCK; if (isLittleEndian) { bb_record_type2_fixed_part1[i].order(ByteOrder.LITTLE_ENDIAN); } recordType2FixedPart1[i] = bb_record_type2_fixed_part1[i].getInt(); } ///dbgLog.fine("recordType2FixedPart="+ /// ReflectionToStringBuilder.toString(recordType2FixedPart1, ToStringStyle.MULTI_LINE_STYLE)); // 1st ([0]) element must be 2 otherwise no longer Record Type 2 if (recordType2FixedPart1[0] != 2) { dbgLog.warning(j + "-th RT header value is no longet RT2! " + recordType2FixedPart1[0]); break; } dbgLog.fine("variable type[must be 2]=" + recordType2FixedPart1[0]); // 2.3 variable name: 8 byte(space[x20]-padded) // This field is located at the very end of the 32 byte // fixed-size RT2 header (bytes 24-31). // We are processing it now, so that // we can make the decision on whether this variable is part // of a compound variable: String RawVariableName = getNullStrippedString(new String( Arrays.copyOfRange(recordType2Fixed, 24, (24 + LENGTH_VARIABLE_NAME)), defaultCharSet)); //offset +=LENGTH_VARIABLE_NAME; String variableName = null; if (RawVariableName.indexOf(' ') >= 0) { variableName = RawVariableName.substring(0, RawVariableName.indexOf(' ')); } else { variableName = RawVariableName; } // 2nd ([1]) element: numeric variable = 0 :for string variable // this block indicates its datum-length, i.e, >0 ; // if -1, this RT2 unit is a non-1st RT2 unit for a string variable // whose value is longer than 8 character. boolean isNumericVariable = false; dbgLog.fine("variable type(0: numeric; > 0: String;-1 continue )=" + recordType2FixedPart1[1]); //OBSwiseTypelList.add(recordType2FixedPart1[1]); int HowManyRt2Units = 1; if (recordType2FixedPart1[1] == -1) { dbgLog.fine("this RT2 is an 8 bit continuation chunk of an earlier string variable"); if (obs255) { if (obsSeqNumber < 30) { OBSwiseTypelList.add(recordType2FixedPart1[1]); obsSeqNumber++; } else { OBSwiseTypelList.add(-2); obs255 = false; obsSeqNumber = 0; } } else { OBSwiseTypelList.add(recordType2FixedPart1[1]); } obsNonVariableBlockSet.add(j); continue; } else if (recordType2FixedPart1[1] == 0) { // This is a numeric variable extendedVariableMode = false; // And as such, it cannot be an extension of a // previous, long string variable. OBSwiseTypelList.add(recordType2FixedPart1[1]); variableCounter++; isNumericVariable = true; variableTypelList.add(recordType2FixedPart1[1]); } else if (recordType2FixedPart1[1] > 0) { // This looks like a regular string variable. However, // it may still be a part of a compound variable // (a String > 255 bytes that was split into 255 byte // chunks, stored as individual String variables). if (recordType2FixedPart1[1] == 255) { obs255 = true; } if (lastVariableIsExtendable) { String varNameBase = null; if (lastVariableName.length() > 5) { varNameBase = lastVariableName.substring(0, 5); } else { varNameBase = lastVariableName; } if (extendedVariableMode) { if (variableNameIsAnIncrement(varNameBase, lastExtendedVariable, variableName)) { OBSwiseTypelList.add(-1); lastExtendedVariable = variableName; // OK, we stay in the "extended variable" mode; // but we can't move on to the next OBS (hence the commented out // "continue" below: //continue; // see the next comment below for the explanation. // // Should we also set "extendable" flag to false at this point // if it's shorter than 255 bytes, i.e. the last extended chunk? } else { extendedVariableMode = false; } } else { if (variableNameIsAnIncrement(varNameBase, variableName)) { OBSwiseTypelList.add(-1); extendedVariableMode = true; dbgLog.fine("RT2: in extended variable mode; variable " + variableName); lastExtendedVariable = variableName; // Before we move on to the next OBS unit, we need to check // if this current extended variable has its own label specified; // If so, we need to determine its length, then read and skip // that many bytes. // Hence the commented out "continue" below: //continue; } } } if (!extendedVariableMode) { // OK, this is a "real" // string variable, and not a continuation chunk of a compound // string. OBSwiseTypelList.add(recordType2FixedPart1[1]); variableCounter++; if (recordType2FixedPart1[1] == 255) { // This variable is 255 bytes long, i.e. this is // either the single "atomic" variable of the // max allowed size, or it's a 255 byte segment // of a compound variable. So we will check // the next variable and see if it is the continuation // of this one. lastVariableIsExtendable = true; } else { lastVariableIsExtendable = false; } if (recordType2FixedPart1[1] % LENGTH_SAV_OBS_BLOCK == 0) { HowManyRt2Units = recordType2FixedPart1[1] / LENGTH_SAV_OBS_BLOCK; } else { HowManyRt2Units = recordType2FixedPart1[1] / LENGTH_SAV_OBS_BLOCK + 1; } variableTypelList.add(recordType2FixedPart1[1]); } } if (!extendedVariableMode) { // Again, we only want to do the following steps for the "real" // variables, not the chunks of split mega-variables: dbgLog.fine("RT2: HowManyRt2Units for this variable=" + HowManyRt2Units); lastVariableName = variableName; // caseWeightVariableOBSIndex starts from 1: 0 is used for does-not-exist cases if (j == (caseWeightVariableOBSIndex - 1)) { caseWeightVariableName = variableName; // TODO: do we need this "index"? -- 4.0 alpha caseWeightVariableIndex = variableCounter; ///smd.setCaseWeightVariableName(caseWeightVariableName); ///smd.getFileInformation().put("caseWeightVariableIndex", caseWeightVariableIndex); } OBSIndexToVariableName.put(j, variableName); //dbgLog.fine("\nvariable name="+variableName+"<-"); dbgLog.fine("RT2: " + j + "-th variable name=" + variableName + "<-"); dbgLog.fine("RT2: raw variable: " + RawVariableName); variableNameList.add(variableName); } // 3rd ([2]) element: = 1 variable-label block follows; 0 = no label // dbgLog.fine("RT: variable label follows?(1:yes; 0: no)=" + recordType2FixedPart1[2]); boolean hasVariableLabel = recordType2FixedPart1[2] == 1 ? true : false; if ((recordType2FixedPart1[2] != 0) && (recordType2FixedPart1[2] != 1)) { throw new IOException("RT2: reading error: value is neither 0 or 1" + recordType2FixedPart1[2]); } // 2.4 [optional]The length of a variable label followed: 4-byte int // 3rd element of 2.1 indicates whether this field exists // *** warning: The label block is padded to a multiple of the 4-byte // NOT the raw integer value of this 4-byte block if (hasVariableLabel) { byte[] length_variable_label = new byte[4]; int nbytes_2_4 = stream.read(length_variable_label); if (nbytes_2_4 == 0) { throw new IOException("RT 2: error reading recordType2.4: no bytes read!"); } else { dbgLog.fine("nbytes_2_4=" + nbytes_2_4); } ByteBuffer bb_length_variable_label = ByteBuffer.wrap(length_variable_label, 0, LENGTH_VARIABLE_LABEL); if (isLittleEndian) { bb_length_variable_label.order(ByteOrder.LITTLE_ENDIAN); } int rawVariableLabelLength = bb_length_variable_label.getInt(); dbgLog.fine("rawVariableLabelLength=" + rawVariableLabelLength); int variableLabelLength = getSAVintAdjustedBlockLength(rawVariableLabelLength); dbgLog.fine("RT2: variableLabelLength=" + variableLabelLength); // 2.5 [optional]variable label whose length is found at 2.4 String variableLabel = ""; if (rawVariableLabelLength > 0) { byte[] variable_label = new byte[variableLabelLength]; int nbytes_2_5 = stream.read(variable_label); if (nbytes_2_5 == 0) { throw new IOException("RT 2: error reading recordType2.5: " + variableLabelLength + " bytes requested, no bytes read!"); } else { dbgLog.fine("nbytes_2_5=" + nbytes_2_5); } variableLabel = getNullStrippedString(new String( Arrays.copyOfRange(variable_label, 0, rawVariableLabelLength), defaultCharSet)); dbgLog.fine("RT2: variableLabel=" + variableLabel + "<-"); dbgLog.fine(variableName + " => " + variableLabel); } else { dbgLog.fine("RT2: defaulting to empty variable label."); } if (!extendedVariableMode) { // We only have any use for this label if it's a "real" variable. // Thinking about it, it doesn't make much sense for the "fake" // variables that are actually chunks of large strings to store // their own labels. But in some files they do. Then failing to read // the bytes would result in getting out of sync with the RT record // borders. So we always read the bytes, but only use them for // the real variable entries. /*String variableLabel = new String(Arrays.copyOfRange(variable_label, 0, rawVariableLabelLength),"US-ASCII");*/ variableLabelMap.put(variableName, variableLabel); } } if (extendedVariableMode) { // there's nothing else left for us to do in this iteration of the loop. // Once again, this was not a real variable, but a dummy variable entry // created for a chunk of a string variable longer than 255 bytes -- // that's how SPSS stores them. continue; } // 4th ([3]) element: Missing value type code // 0[none], 1, 2, 3 [point-type],-2[range], -3 [range type+ point] dbgLog.fine("RT: missing value unit follows?(if 0, none)=" + recordType2FixedPart1[3]); boolean hasMissingValues = (validMissingValueCodeSet.contains(recordType2FixedPart1[3]) && (recordType2FixedPart1[3] != 0)) ? true : false; InvalidData invalidDataInfo = null; if (recordType2FixedPart1[3] != 0) { invalidDataInfo = new InvalidData(recordType2FixedPart1[3]); dbgLog.fine("RT: missing value type=" + invalidDataInfo.getType()); } // 2.2: print/write formats: 4-byte each = 8 bytes byte[] printFormt = Arrays.copyOfRange(recordType2Fixed, offset, offset + LENGTH_PRINT_FORMAT_CODE); dbgLog.fine("printFrmt=" + new String(Hex.encodeHex(printFormt))); offset += LENGTH_PRINT_FORMAT_CODE; int formatCode = isLittleEndian ? printFormt[2] : printFormt[1]; int formatWidth = isLittleEndian ? printFormt[1] : printFormt[2]; // TODO: // What should we be doing with these "format decimal positions" // in 4.0? // -- L.A. 4.0 alpha int formatDecimalPointPosition = isLittleEndian ? printFormt[0] : printFormt[3]; dbgLog.fine("RT2: format code{5=F, 1=A[String]}=" + formatCode); formatDecimalPointPositionList.add(formatDecimalPointPosition); if (!SPSSConstants.FORMAT_CODE_TABLE_SAV.containsKey(formatCode)) { throw new IOException("Unknown format code was found = " + formatCode); } else { printFormatList.add(formatCode); } byte[] writeFormt = Arrays.copyOfRange(recordType2Fixed, offset, offset + LENGTH_WRITE_FORMAT_CODE); dbgLog.fine("RT2: writeFrmt=" + new String(Hex.encodeHex(writeFormt))); if (writeFormt[3] != 0x00) { dbgLog.fine("byte-order(write format): reversal required"); } offset += LENGTH_WRITE_FORMAT_CODE; if (!SPSSConstants.ORDINARY_FORMAT_CODE_SET.contains(formatCode)) { StringBuilder sb = new StringBuilder( SPSSConstants.FORMAT_CODE_TABLE_SAV.get(formatCode) + formatWidth); if (formatDecimalPointPosition > 0) { sb.append("." + formatDecimalPointPosition); } dbgLog.fine("formattable[i] = " + variableName + " -> " + sb.toString()); printFormatNameTable.put(variableName, sb.toString()); } printFormatTable.put(variableName, SPSSConstants.FORMAT_CODE_TABLE_SAV.get(formatCode)); // 2.6 [optional] missing values:4-byte each if exists // 4th element of 2.1 indicates the structure of this sub-field // Should we perhaps check for this for the "fake" variables too? // if (hasMissingValues) { dbgLog.fine("RT2: decoding missing value: type=" + recordType2FixedPart1[3]); int howManyMissingValueUnits = missingValueCodeUnits.get(recordType2FixedPart1[3]); //int howManyMissingValueUnits = recordType2FixedPart1[3] > 0 ? recordType2FixedPart1[3] : 0; dbgLog.fine("RT2: howManyMissingValueUnits=" + howManyMissingValueUnits); byte[] missing_value_code_units = new byte[LENGTH_SAV_OBS_BLOCK * howManyMissingValueUnits]; int nbytes_2_6 = stream.read(missing_value_code_units); if (nbytes_2_6 == 0) { throw new IOException("RT 2: reading recordType2.6: no byte was read"); } else { dbgLog.fine("nbytes_2_6=" + nbytes_2_6); } //printHexDump(missing_value_code_units, "missing value"); if (isNumericVariable) { double[] missingValues = new double[howManyMissingValueUnits]; //List<String> mvp = new ArrayList<String>(); List<String> mv = new ArrayList<String>(); ByteBuffer[] bb_missig_value_code = new ByteBuffer[howManyMissingValueUnits]; int offset_start = 0; for (int i = 0; i < howManyMissingValueUnits; i++) { bb_missig_value_code[i] = ByteBuffer.wrap(missing_value_code_units, offset_start, LENGTH_SAV_OBS_BLOCK); offset_start += LENGTH_SAV_OBS_BLOCK; if (isLittleEndian) { bb_missig_value_code[i].order(ByteOrder.LITTLE_ENDIAN); } ByteBuffer temp = bb_missig_value_code[i].duplicate(); missingValues[i] = bb_missig_value_code[i].getDouble(); if (Double.toHexString(missingValues[i]).equals("-0x1.ffffffffffffep1023")) { dbgLog.fine("1st value is LOWEST"); mv.add(Double.toHexString(missingValues[i])); } else if (Double.valueOf(missingValues[i]).equals(Double.MAX_VALUE)) { dbgLog.fine("2nd value is HIGHEST"); mv.add(Double.toHexString(missingValues[i])); } else { mv.add(doubleNumberFormatter.format(missingValues[i])); } dbgLog.fine(i + "-th missing value=" + Double.toHexString(missingValues[i])); } dbgLog.fine("variableName=" + variableName); if (recordType2FixedPart1[3] > 0) { // point cases only dbgLog.fine("mv(>0)=" + mv); missingValueTable.put(variableName, mv); invalidDataInfo.setInvalidValues(mv); } else if (recordType2FixedPart1[3] == -2) { dbgLog.fine("mv(-2)=" + mv); // range invalidDataInfo.setInvalidRange(mv); } else if (recordType2FixedPart1[3] == -3) { // mixed case dbgLog.fine("mv(-3)=" + mv); invalidDataInfo.setInvalidRange(mv.subList(0, 2)); invalidDataInfo.setInvalidValues(mv.subList(2, 3)); missingValueTable.put(variableName, mv.subList(2, 3)); } dbgLog.fine("missing value=" + StringUtils.join(missingValueTable.get(variableName), "|")); dbgLog.fine("invalidDataInfo(Numeric):\n" + invalidDataInfo); invalidDataTable.put(variableName, invalidDataInfo); } else { // string variable case String[] missingValues = new String[howManyMissingValueUnits]; List<String> mv = new ArrayList<String>(); int offset_start = 0; int offset_end = LENGTH_SAV_OBS_BLOCK; for (int i = 0; i < howManyMissingValueUnits; i++) { missingValues[i] = StringUtils.stripEnd(new String( Arrays.copyOfRange(missing_value_code_units, offset_start, offset_end), defaultCharSet), " "); dbgLog.fine("missing value=" + missingValues[i] + "<-"); offset_start = offset_end; offset_end += LENGTH_SAV_OBS_BLOCK; mv.add(missingValues[i]); } invalidDataInfo.setInvalidValues(mv); missingValueTable.put(variableName, mv); invalidDataTable.put(variableName, invalidDataInfo); dbgLog.fine( "missing value(str)=" + StringUtils.join(missingValueTable.get(variableName), "|")); dbgLog.fine("invalidDataInfo(String):\n" + invalidDataInfo); } // string case dbgLog.fine("invalidDataTable:\n" + invalidDataTable); } // if msv } catch (IOException ex) { //ex.printStackTrace(); throw ex; } catch (Exception ex) { ex.printStackTrace(); // should we be throwing some exception here? } } // j-loop if (j != OBSUnitsPerCase) { dbgLog.fine("RT2: attention! didn't reach the end of the OBS list!"); throw new IOException("RT2: didn't reach the end of the OBS list!"); } dbgLog.fine("RT2 metadata-related exit-chores"); ///smd.getFileInformation().put("varQnty", variableCounter); dataTable.setVarQuantity(new Long(variableCounter)); dbgLog.fine("RT2: varQnty=" + variableCounter); // 4.0 Initialize variables: List<DataVariable> variableList = new ArrayList<DataVariable>(); for (int i = 0; i < variableCounter; i++) { DataVariable dv = new DataVariable(); String varName = variableNameList.get(i); dbgLog.fine("name: " + varName); dv.setName(varName); String varLabel = variableLabelMap.get(varName); if (varLabel != null && varLabel.length() > 255) { // TODO: // variable labels will be changed into type 'TEXT' in the // database - this will eliminate the 255 char. limit. // -- L.A. 4.0 beta11 dbgLog.fine("Have to truncate label: " + varLabel); varLabel = varLabel.substring(0, 255); } dbgLog.fine("label: " + varLabel); dv.setLabel(varLabel); dv.setInvalidRanges(new ArrayList<VariableRange>()); dv.setSummaryStatistics(new ArrayList<SummaryStatistic>()); dv.setUnf("UNF:6:"); dv.setCategories(new ArrayList<VariableCategory>()); variableList.add(dv); dv.setFileOrder(i); dv.setDataTable(dataTable); } dataTable.setDataVariables(variableList); ///smd.setVariableName(variableNameList.toArray(new String[variableNameList.size()])); ///smd.setVariableLabel(variableLabelMap); // TODO: // figure out what to do with the missing value table! // -- 4.0 alpha // well, they were used to generate merged summary statistics for // the variable. So need to verify what the DDI import was doing // with them and replicate the same in 4.0. // (add appropriate value labels?) ///TODO: 4.0 smd.setMissingValueTable(missingValueTable); ///smd.getFileInformation().put("caseWeightVariableName", caseWeightVariableName); dbgLog.fine("sumstat:long case=" + Arrays.deepToString(variableTypelList.toArray())); dbgLog.fine("RT2: OBSwiseTypelList=" + OBSwiseTypelList); dbgLog.fine("decodeRecordType2(): end"); }
From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java
/** * get the value from the rainRate field * * @return rainRate - the rainRate as a float */// w ww .j av a 2 s.co m public float getRainRate() { this.rainRate.flip(); return (float) (this.rainRate.order(ByteOrder.LITTLE_ENDIAN).getShort()) / 100; }