List of usage examples for java.util BitSet get
public boolean get(int bitIndex)
From source file:Exec.java
/** * Description of the Method/* w ww . ja v a2 s . c om*/ * * @param command * Description of the Parameter * @param input * Description of the Parameter * @param successCode * Description of the Parameter * @param timeout * Description of the Parameter * @param lazy * Description of the Parameter * @return Description of the Return Value */ public static ExecResults execOptions(String[] command, String input, int successCode, int timeout, boolean lazy) { Process child = null; ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream errors = new ByteArrayOutputStream(); ExecResults results = new ExecResults(command[0], input, successCode, timeout); BitSet interrupted = new BitSet(1); boolean lazyQuit = false; ThreadWatcher watcher; try { // start the command child = Runtime.getRuntime().exec(command); // get the streams in and out of the command InputStream processIn = child.getInputStream(); InputStream processError = child.getErrorStream(); OutputStream processOut = child.getOutputStream(); // start the clock running if (timeout > 0) { watcher = new ThreadWatcher(child, interrupted, timeout); new Thread(watcher).start(); } // Write to the child process' input stream if ((input != null) && !input.equals("")) { try { processOut.write(input.getBytes()); processOut.flush(); processOut.close(); } catch (IOException e1) { results.setThrowable(e1); } } // Read from the child process' output stream // The process may get killed by the watcher at any time int c = 0; try { while (true) { if (interrupted.get(0) || lazyQuit) { break; } // interrupted c = processIn.read(); if (c == -1) { break; } // end of stream output.write(c); if (lazy && (processIn.available() < 1)) { lazyQuit = true; } // if lazy and nothing then quit (after at least one read) } processIn.close(); } catch (IOException e2) { results.setThrowable(e2); } finally { if (interrupted.get(0)) { results.setInterrupted(); } results.setOutput(output.toString()); } // Read from the child process' error stream // The process may get killed by the watcher at any time try { while (true) { if (interrupted.get(0) || lazyQuit) { break; } // interrupted c = processError.read(); if (c == -1) { break; } // end of stream output.write(c); if (lazy && (processError.available() < 1)) { lazyQuit = true; } // if lazy and nothing then quit (after at least one read) } processError.close(); } catch (IOException e3) { results.setThrowable(e3); } finally { if (interrupted.get(0)) { results.setInterrupted(); } results.setErrors(errors.toString()); } // wait for the return value of the child process. if (!interrupted.get(0) && !lazyQuit) { int returnCode = child.waitFor(); results.setReturnCode(returnCode); if (returnCode != successCode) { results.setError(ExecResults.BADRETURNCODE); } } } catch (InterruptedException i) { results.setInterrupted(); } catch (Throwable t) { results.setThrowable(t); } finally { if (child != null) { child.destroy(); } } return (results); }
From source file:Exec.java
/** * Description of the Method/*w w w . j a va 2s . c o m*/ * * @param command * Description of the Parameter * @param input * Description of the Parameter * @param successCode * Description of the Parameter * @param timeout * Description of the Parameter * @param lazy * Description of the Parameter * @return Description of the Return Value */ public static ExecResults execOptions(String command, String input, int successCode, int timeout, boolean lazy) { Process child = null; ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream errors = new ByteArrayOutputStream(); ExecResults results = new ExecResults(command, input, successCode, timeout); BitSet interrupted = new BitSet(1); boolean lazyQuit = false; ThreadWatcher watcher; try { // start the command child = Runtime.getRuntime().exec(command); // get the streams in and out of the command InputStream processIn = child.getInputStream(); InputStream processError = child.getErrorStream(); OutputStream processOut = child.getOutputStream(); // start the clock running if (timeout > 0) { watcher = new ThreadWatcher(child, interrupted, timeout); new Thread(watcher).start(); } // Write to the child process' input stream if ((input != null) && !input.equals("")) { try { processOut.write(input.getBytes()); processOut.flush(); processOut.close(); } catch (IOException e1) { results.setThrowable(e1); } } // Read from the child process' output stream // The process may get killed by the watcher at any time int c = 0; try { while (true) { if (interrupted.get(0) || lazyQuit) { break; } // interrupted c = processIn.read(); if (c == -1) { break; } // end of stream output.write(c); if (lazy && (processIn.available() < 1)) { lazyQuit = true; } // if lazy and nothing then quit (after at least one read) } processIn.close(); } catch (IOException e2) { results.setThrowable(e2); } finally { if (interrupted.get(0)) { results.setInterrupted(); } results.setOutput(output.toString()); } // Read from the child process' error stream // The process may get killed by the watcher at any time try { while (true) { if (interrupted.get(0) || lazyQuit) { break; } // interrupted c = processError.read(); if (c == -1) { break; } // end of stream output.write(c); if (lazy && (processError.available() < 1)) { lazyQuit = true; } // if lazy and nothing then quit (after at least one read) } processError.close(); } catch (IOException e3) { results.setThrowable(e3); } finally { if (interrupted.get(0)) { results.setInterrupted(); } results.setErrors(errors.toString()); } // wait for the return value of the child process. if (!interrupted.get(0) && !lazyQuit) { int returnCode = child.waitFor(); results.setReturnCode(returnCode); if (returnCode != successCode) { results.setError(ExecResults.BADRETURNCODE); } } } catch (InterruptedException i) { results.setInterrupted(); } catch (Throwable t) { results.setThrowable(t); } finally { if (child != null) { child.destroy(); } } return (results); }
From source file:com.microsoft.azure.management.datalake.store.uploader.UploadMetadata.java
/** * Verifies the given metadata for consistency. Checks include: * Completeness//from ww w . ja va 2 s .c o m * Existence and consistency with local file * Segment data consistency * * @throws InvalidMetadataException Thrown if the metadata is invalid. */ public void validateConsistency() throws InvalidMetadataException { if (this.segments == null || this.segments.length != this.segmentCount) { throw new InvalidMetadataException("Inconsistent number of segments"); } long sum = 0; int lastSegmentNumber = -1; BitSet segments = new BitSet(this.segmentCount); for (UploadSegmentMetadata segment : this.segments) { if (segment.getSegmentNumber() < 0 || segment.getSegmentNumber() >= this.segmentCount) { throw new InvalidMetadataException(MessageFormat.format( "Segment numbers must be at least 0 and less than {0}. Found segment number {1}.", this.segmentCount, segment.getSegmentNumber())); } if (segment.getSegmentNumber() <= lastSegmentNumber) { throw new InvalidMetadataException(MessageFormat.format("Segment number {0} appears out of order.", segment.getSegmentNumber())); } if (segments.get(segment.getSegmentNumber())) { throw new InvalidMetadataException( MessageFormat.format("Segment number {0} appears twice", segment.getSegmentNumber())); } if (segment.getOffset() != sum) { throw new InvalidMetadataException(MessageFormat.format( "Segment number {0} has an invalid starting offset ({1}). Expected {2}.", segment.getSegmentNumber(), segment.getOffset(), sum)); } segments.set(segment.getSegmentNumber()); sum += segment.getLength(); lastSegmentNumber = segment.getSegmentNumber(); } if (sum != this.fileLength) { throw new InvalidMetadataException( "The individual segment lengths do not add up to the input File length"); } }
From source file:org.dataconservancy.packaging.tool.integration.PackageGenerationTest.java
/** * Reads in any BagIt file that uses a ':' to delimit a keyword and value pair. * * @param bagItFile the file to read/* www . j av a 2 s. co m*/ * @return a Map keyed by the keywords, with the List of values as they appear in the file * @throws IOException */ private Map<String, List<String>> parseBagItKeyValuesFile(File bagItFile) throws IOException { Map<String, List<String>> result = new HashMap<>(); // Used to track state; a streams no-no. Probably should do this the old-fashioned way. BitSet bitSet = new BitSet(1); bitSet.set(0); StringBuilder key = new StringBuilder(); Files.lines(bagItFile.toPath(), Charset.forName("UTF-8")).flatMap(line -> Stream .of(line.substring(0, line.indexOf(":")), line.substring(line.indexOf(":") + 1).trim())) .forEach(token -> { if (bitSet.get(0)) { // key key.delete(0, key.length()); result.putIfAbsent(token, new ArrayList<>()); key.append(token); bitSet.clear(0); } else { // value result.get(key.toString()).add(token); bitSet.set(0); } }); return result; }
From source file:model.DecomposableModel.java
public BitSet findSab(GraphAction action) { int a = action.getV1(); int b = action.getV2(); BitSet Sab = null;//ww w . j av a 2 s. c o m for (CliqueGraphEdge e : graph.cg.edgeSet()) { BitSet clique1 = e.getClique1(); BitSet clique2 = e.getClique2(); if ((clique1.get(a) && clique2.get(b)) || (clique2.get(a) && clique1.get(b))) { Sab = e.getSeparator(); break; } } if (Sab == null) {// disconnected components Sab = new BitSet(dimensionsForVariables.length); } if (!Sab.equals(graph.getSeparator(a, b))) { System.err.println("Ouch"); } return Sab; }
From source file:srebrinb.compress.sevenzip.SevenZFile.java
private void readUnpackInfo(final ByteBuffer header, final Archive archive) throws IOException { int nid = getUnsignedByte(header); if (nid != NID.kFolder) { throw new IOException("Expected kFolder, got " + nid); }/*from w w w . jav a 2 s . co m*/ final long numFolders = readUint64(header); final Folder[] folders = new Folder[(int) numFolders]; archive.folders = folders; final int external = getUnsignedByte(header); if (external != 0) { throw new IOException("External unsupported"); } for (int i = 0; i < (int) numFolders; i++) { folders[i] = readFolder(header); } nid = getUnsignedByte(header); if (nid != NID.kCodersUnpackSize) { throw new IOException("Expected kCodersUnpackSize, got " + nid); } for (final Folder folder : folders) { folder.unpackSizes = new long[(int) folder.totalOutputStreams]; for (int i = 0; i < folder.totalOutputStreams; i++) { folder.unpackSizes[i] = readUint64(header); } } nid = getUnsignedByte(header); if (nid == NID.kCRC) { final BitSet crcsDefined = readAllOrBits(header, (int) numFolders); for (int i = 0; i < (int) numFolders; i++) { if (crcsDefined.get(i)) { folders[i].hasCrc = true; folders[i].crc = 0xffffFFFFL & header.getInt(); } else { folders[i].hasCrc = false; } } nid = getUnsignedByte(header); } if (nid != NID.kEnd) { throw new IOException("Badly terminated UnpackInfo"); } }
From source file:org.lockss.crawler.TestFollowLinkCrawler2.java
public void testPassesParamsToUrlFetcherDefault() { mau.addUrl(startUrl);// w ww.jav a 2s.co m MockUrlFetcher muf = (MockUrlFetcher) crawler.makeUrlFetcher(startUrl); BitSet fetchFlags = muf.getFetchFlags(); assertFalse(fetchFlags.get(UrlCacher.REFETCH_FLAG)); }
From source file:bobs.is.compress.sevenzip.SevenZFile.java
private void readUnpackInfo(final DataInput header, final Archive archive) throws IOException { int nid = header.readUnsignedByte(); if (nid != NID.kFolder) { throw new IOException("Expected kFolder, got " + nid); }/*from ww w . ja v a 2 s . c o m*/ final long numFolders = readUint64(header); final Folder[] folders = new Folder[(int) numFolders]; archive.folders = folders; final int external = header.readUnsignedByte(); if (external != 0) { throw new IOException("External unsupported"); } for (int i = 0; i < (int) numFolders; i++) { folders[i] = readFolder(header); } nid = header.readUnsignedByte(); if (nid != NID.kCodersUnpackSize) { throw new IOException("Expected kCodersUnpackSize, got " + nid); } for (final Folder folder : folders) { folder.unpackSizes = new long[(int) folder.totalOutputStreams]; for (int i = 0; i < folder.totalOutputStreams; i++) { folder.unpackSizes[i] = readUint64(header); } } nid = header.readUnsignedByte(); if (nid == NID.kCRC) { final BitSet crcsDefined = readAllOrBits(header, (int) numFolders); for (int i = 0; i < (int) numFolders; i++) { if (crcsDefined.get(i)) { folders[i].hasCrc = true; folders[i].crc = 0xffffFFFFL & Integer.reverseBytes(header.readInt()); } else { folders[i].hasCrc = false; } } nid = header.readUnsignedByte(); } if (nid != NID.kEnd) { throw new IOException("Badly terminated UnpackInfo"); } }
From source file:com.indeed.lsmtree.core.TestStore.java
public void testStore(StorageType storageType, CompressionCodec codec) throws Exception { File indexDir = new File(tmpDir, "index"); indexDir.mkdirs();/*from www .jav a 2 s . c o m*/ File indexLink = new File(tmpDir, "indexlink"); PosixFileOperations.link(indexDir, indexLink); File storeDir = new File(indexLink, "store"); Store<Integer, Long> store = new StoreBuilder<Integer, Long>(storeDir, new IntSerializer(), new LongSerializer()).setMaxVolatileGenerationSize(8 * 1024 * 1024).setStorageType(storageType) .setCodec(codec).build(); final Random r = new Random(0); final int[] ints = new int[treeSize]; for (int i = 0; i < ints.length; i++) { ints[i] = r.nextInt(); } for (final int i : ints) { store.put(i, (long) i); assertTrue(store.get(i) == i); } for (final int i : ints) { assertTrue(store.get(i) == i); } store.close(); store.waitForCompactions(); store = new StoreBuilder<Integer, Long>(storeDir, new IntSerializer(), new LongSerializer()) .setMaxVolatileGenerationSize(8 * 1024 * 1024).setStorageType(storageType).setCodec(codec).build(); Arrays.sort(ints); Iterator<Store.Entry<Integer, Long>> iterator = store.iterator(); int index = 0; while (iterator.hasNext()) { Store.Entry<Integer, Long> next = iterator.next(); int current = ints[index]; assertTrue(next.getKey() == ints[index]); assertTrue(next.getValue() == ints[index]); while (index < ints.length && ints[index] == current) { index++; } } assertTrue(index == ints.length); final BitSet deleted = new BitSet(); for (int i = 0; i < ints.length / 10; i++) { int deletionIndex = r.nextInt(ints.length); deleted.set(deletionIndex, true); for (int j = deletionIndex - 1; j >= 0; j--) { if (ints[j] == ints[deletionIndex]) { deleted.set(j, true); } else { break; } } for (int j = deletionIndex + 1; j < ints.length; j++) { if (ints[j] == ints[deletionIndex]) { deleted.set(j, true); } else { break; } } store.delete(ints[deletionIndex]); assertNull(store.get(ints[deletionIndex])); } iterator = store.iterator(); index = 0; while (iterator.hasNext()) { Store.Entry<Integer, Long> next = iterator.next(); while (deleted.get(index)) index++; int current = ints[index]; assertTrue(next.getKey() == ints[index]); assertTrue(next.getValue() == ints[index]); while (index < ints.length && ints[index] == current) { index++; } } while (deleted.get(index)) index++; assertTrue(index == ints.length); final int max = ints[ints.length - 1]; final AtomicInteger done = new AtomicInteger(8); for (int i = 0; i < done.get(); i++) { final int thread = i; final Store<Integer, Long> finalStore = store; new Thread(new Runnable() { @Override public void run() { try { Random r = new Random(thread); for (int i = 0; i < treeSize; i++) { int rand = r.nextInt(); int insertionindex = Arrays.binarySearch(ints, rand); Store.Entry<Integer, Long> next = finalStore.ceil(rand); boolean found = next != null; if (insertionindex >= 0 && deleted.get(insertionindex)) { assertNull(finalStore.get(ints[insertionindex])); } else { assertTrue(found == (rand <= max)); if (found) { assertTrue(next.getKey() >= rand); assertTrue(next.getKey().longValue() == next.getValue()); if (insertionindex >= 0) { assertTrue(rand == ints[insertionindex]); assertTrue(next.getKey() == rand); Long result = finalStore.get(rand); assertTrue(result == rand); } else { int nextIndex = ~insertionindex; while (deleted.get(nextIndex) && nextIndex < ints.length) nextIndex++; if (nextIndex < ints.length) { if (insertionindex != -1) assertTrue(ints[(~insertionindex) - 1] < rand); assertTrue(ints[nextIndex] + " != " + next.getKey(), ints[nextIndex] == next.getKey()); } Long result = finalStore.get(rand); assertTrue(result == null); } } } } } catch (IOException e) { throw new RuntimeException(e); } finally { done.decrementAndGet(); } } }).start(); } while (done.get() > 0) { Thread.yield(); } store.close(); }
From source file:org.apache.hadoop.mapreduce.lib.input.TestCombineSequenceFileInputFormat.java
@Test(timeout = 10000) public void testFormat() throws IOException, InterruptedException { Job job = Job.getInstance(conf);/* w w w . j a va2s . c om*/ Random random = new Random(); long seed = random.nextLong(); random.setSeed(seed); localFs.delete(workDir, true); FileInputFormat.setInputPaths(job, workDir); final int length = 10000; final int numFiles = 10; // create files with a variety of lengths createFiles(length, numFiles, random, job); TaskAttemptContext context = MapReduceTestUtil.createDummyMapTaskAttemptContext(job.getConfiguration()); // create a combine split for the files InputFormat<IntWritable, BytesWritable> format = new CombineSequenceFileInputFormat<IntWritable, BytesWritable>(); for (int i = 0; i < 3; i++) { int numSplits = random.nextInt(length / (SequenceFile.SYNC_INTERVAL / 20)) + 1; LOG.info("splitting: requesting = " + numSplits); List<InputSplit> splits = format.getSplits(job); LOG.info("splitting: got = " + splits.size()); // we should have a single split as the length is comfortably smaller than // the block size assertEquals("We got more than one splits!", 1, splits.size()); InputSplit split = splits.get(0); assertEquals("It should be CombineFileSplit", CombineFileSplit.class, split.getClass()); // check the split BitSet bits = new BitSet(length); RecordReader<IntWritable, BytesWritable> reader = format.createRecordReader(split, context); MapContext<IntWritable, BytesWritable, IntWritable, BytesWritable> mcontext = new MapContextImpl<IntWritable, BytesWritable, IntWritable, BytesWritable>( job.getConfiguration(), context.getTaskAttemptID(), reader, null, null, MapReduceTestUtil.createDummyReporter(), split); reader.initialize(split, mcontext); assertEquals("reader class is CombineFileRecordReader.", CombineFileRecordReader.class, reader.getClass()); try { while (reader.nextKeyValue()) { IntWritable key = reader.getCurrentKey(); BytesWritable value = reader.getCurrentValue(); assertNotNull("Value should not be null.", value); final int k = key.get(); LOG.debug("read " + k); assertFalse("Key in multiple partitions.", bits.get(k)); bits.set(k); } } finally { reader.close(); } assertEquals("Some keys in no partition.", length, bits.cardinality()); } }