/** * Sets the (average) bit length for this data set. * * <p>A bit length is used to determine the baudrate of this data set. If there is already a bit * length available from an earlier call to this method, it will be used to average the resulting * bit length if both the current and the given bit length are "close". This allows you to use the * bit lengths of both the RxD- and TxD-lines to get a good approximation of the actual baudrate. * * @param aBitLength the bit length to set/add, should be >= 0. */ public void setSampledBitLength(final int aBitLength) { // If the given bit length is "much" smaller (smaller bit length means // higher baudrate) than the current one, switch to that one instead; this // way we can recover from bad values... if ((this.bitLength <= 0) || ((10.0 * aBitLength) < this.bitLength)) { // First time being called, take the given bit length as our "truth"... this.bitLength = aBitLength; } else { // Take the average as the current and the given bit lengths are "close" // to each other; ignore the given bit length otherwise, as it clobbers // our earlier results... final int diff = Math.abs(aBitLength - this.bitLength); if ((diff >= 0) && (diff < 50)) { this.bitLength = (int) ((aBitLength + this.bitLength) / 2.0); } else { LOG.log( Level.INFO, "Ignoring sampled bit length ({0}) as it deviates " + "too much from current bit length ({1}).", new Object[] {Integer.valueOf(aBitLength), Integer.valueOf(this.bitLength)}); } } }
/** * Reads the data from a given reader. * * @param aProject the project to read the settings to; * @param aReader the reader to read the data from, cannot be <code>null</code>. * @throws IOException in case of I/O problems. */ @SuppressWarnings("boxing") public static void read(final StubDataSet aDataSet, final Reader aReader) throws IOException { int size = -1; Integer rate = null, channels = null, enabledChannels = null; long triggerPos = -1L; long absLen = -1L; // assume 'new' file format is in use, don't support uncompressed ones... boolean compressed = true; final BufferedReader br = new BufferedReader(aReader); final List<String[]> dataValues = new ArrayList<String[]>(); String line; while ((line = br.readLine()) != null) { // Determine whether the line is an instruction, or data... final Matcher instructionMatcher = OLS_INSTRUCTION_PATTERN.matcher(line); final Matcher dataMatcher = OLS_DATA_PATTERN.matcher(line); if (dataMatcher.matches()) { final String[] dataPair = new String[] {dataMatcher.group(1), dataMatcher.group(2)}; dataValues.add(dataPair); } else if (instructionMatcher.matches()) { // Ok; found an instruction... final String instrKey = instructionMatcher.group(1); final String instrValue = instructionMatcher.group(2); if ("Size".equals(instrKey)) { size = safeParseInt(instrValue); } else if ("Rate".equals(instrKey)) { rate = safeParseInt(instrValue); } else if ("Channels".equals(instrKey)) { channels = safeParseInt(instrValue); } else if ("TriggerPosition".equals(instrKey)) { triggerPos = Long.parseLong(instrValue); } else if ("EnabledChannels".equals(instrKey)) { enabledChannels = safeParseInt(instrValue); } else if ("CursorEnabled".equals(instrKey)) { aDataSet.setCursorsEnabled(Boolean.parseBoolean(instrValue)); } else if ("Compressed".equals(instrKey)) { compressed = Boolean.parseBoolean(instrValue); } else if ("AbsoluteLength".equals(instrKey)) { absLen = Long.parseLong(instrValue); } else if ("CursorA".equals(instrKey)) { final long value = safeParseLong(instrValue); if (value > Long.MIN_VALUE) { aDataSet.getCursor(0).setTimestamp(value); } } else if ("CursorB".equals(instrKey)) { final long value = safeParseLong(instrValue); if (value > Long.MIN_VALUE) { aDataSet.getCursor(1).setTimestamp(value); } } else if (instrKey.startsWith("Cursor")) { final int idx = safeParseInt(instrKey.substring(6)); final long pos = Long.parseLong(instrValue); if (pos > Long.MIN_VALUE) { aDataSet.getCursor(idx).setTimestamp(pos); } } } } // Perform some sanity checks, make it not possible to import invalid // data... if (dataValues.isEmpty()) { throw new IOException("Data file does not contain any sample data!"); } if (!compressed) { throw new IOException( "Uncompressed data file found! Please send this file to the OLS developers!"); } // In case the size is not provided (as of 0.9.4 no longer mandatory), // take the length of the data values as size indicator... if (size < 0) { size = dataValues.size(); } if (size != dataValues.size()) { throw new IOException("Data file is corrupt?! Data size does not match sample count!"); } if (rate == null) { throw new IOException("Data file is corrupt?! Sample rate is not provided!"); } if ((channels == null) || (channels <= 0) || (channels > 32)) { throw new IOException("Data file is corrupt?! Channel count is not provided!"); } // Make sure the enabled channels are defined... if (enabledChannels == null) { enabledChannels = NumberUtils.getBitMask(channels); } int[] values = new int[size]; long[] timestamps = new long[size]; try { for (int i = 0; i < size; i++) { final String[] dataPair = dataValues.get(i); values[i] = (int) Long.parseLong(dataPair[0], 16); timestamps[i] = Long.parseLong(dataPair[1], 10) & Long.MAX_VALUE; } } catch (final NumberFormatException exception) { throw new IOException("Invalid data encountered.", exception); } // Allow the absolute length to be undefined, in which case the last // time stamp is used (+ some margin to be able to see the last // sample)... long absoluteLength = Math.max(absLen, timestamps[size - 1]); // Finally set the captured data, and notify all event listeners... aDataSet.setCapturedData( new CapturedData( values, timestamps, triggerPos, rate, channels, enabledChannels, absoluteLength)); }