List of usage examples for com.google.common.io InputSupplier InputSupplier
InputSupplier
From source file:kn.uni.gis.dataimport.GenerateCSV.java
public static void main(String[] args) throws IOException, InterruptedException { for (File file : new File(PATH).listFiles(new FilenameFilter() { @Override//from w w w . j av a2s . c o m public boolean accept(File dir, String name) { return name.endsWith(".shp"); } })) { ProcessBuilder builder = new ProcessBuilder("lib/shp2text", "--spreadsheet", file.getAbsolutePath()); System.out.println("executing: " + builder.command()); final Process start = builder.start(); new StreamConsumer(new InputSupplier<Reader>() { public Reader getInput() throws IOException { return new InputStreamReader(start.getInputStream()); } }, Files.newWriterSupplier(new File(OUTPUT + file.getName() + ".csv"), Charsets.UTF_8)).start(); new StreamConsumer(new InputSupplier<Reader>() { public Reader getInput() throws IOException { return new InputStreamReader(start.getErrorStream()); } }, Files.newWriterSupplier(new File(OUTPUT + file.getName() + ".csv"), Charsets.UTF_8)).start(); Preconditions.checkState(start.waitFor() == 0, "error "); } }
From source file:org.commoncrawl.service.parser.client.Dispatcher.java
public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); CrawlEnvironment.setHadoopConfig(conf); String baseURL = "http://unknown.com/"; if (args.length != 0) { baseURL = args[0];/*from w ww.j a v a 2 s . c om*/ } URL baseURLObj; try { baseURLObj = new URL(baseURL); } catch (MalformedURLException e2) { throw new IOException("Invalid Base Link"); } final URL finalBaseURL = (baseURLObj != null) ? baseURLObj : null; final DataOutputBuffer headerBuffer = new DataOutputBuffer(); final DataOutputBuffer contentBuffer = new DataOutputBuffer(); try { ByteStreams.readBytes(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return System.in; } }, new ByteProcessor<Long>() { @Override public Long getResult() { return 0L; } int currLineCharCount = 0; boolean processingHeaders = true; @Override public boolean processBytes(byte[] buf, int start, int length) throws IOException { if (processingHeaders) { int current = start; int end = current + length; while (processingHeaders && current != end) { if (buf[current] != '\r' && buf[current] != '\n') { currLineCharCount++; } else if (buf[current] == '\n') { if (currLineCharCount == 0) { headerBuffer.write(buf, start, current - start + 1); processingHeaders = false; } currLineCharCount = 0; } current++; } if (processingHeaders) { headerBuffer.write(buf, start, length); } else { length -= current - start; start = current; } } if (!processingHeaders) { contentBuffer.write(buf, start, length); } return true; } }); LOG.info("HEADER LEN:" + headerBuffer.getLength()); // System.out.println(new String(headerBuffer.getData(),0,headerBuffer.getLength(),Charset.forName("UTF-8"))); LOG.info("CONTENT LEN:" + contentBuffer.getLength()); //System.out.println(new String(contentBuffer.getData(),0,contentBuffer.getLength(),Charset.forName("UTF-8"))); // decode header bytes ... String header = ""; if (headerBuffer.getLength() != 0) { try { header = new String(headerBuffer.getData(), 0, headerBuffer.getLength(), Charset.forName("UTF-8")); } catch (Exception e) { LOG.warn(CCStringUtils.stringifyException(e)); header = new String(headerBuffer.getData(), 0, headerBuffer.getLength(), Charset.forName("ASCII")); } } final String headersFinal = (header != null) ? header : ""; LOG.info("Starting Event Loop"); final EventLoop eventLoop = new EventLoop(); eventLoop.start(); try { // create fake hosts file ... //String hosts = "10.0.20.101:8072"; // reader //Reader reader = new StringReader(hosts); // dispatcher init LOG.info("initializing Dispatcher"); final Dispatcher dispatcher = new Dispatcher(eventLoop, "parserNodes"); LOG.info("Waiting for a few seconds"); Thread.sleep(5000); Thread threads[] = new Thread[TEST_THREAD_COUNT]; final Semaphore threadWaitSem = new Semaphore(-TEST_THREAD_COUNT - 1); // start 100 threads for (int threadIdx = 0; threadIdx < TEST_THREAD_COUNT; ++threadIdx) { threads[threadIdx] = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < ITERATIONS_PER_THREAD; ++i) { // build parse request ParseRequest request = new ParseRequest(); request.setDocId(1); request.setDomainId(1); request.setDocURL(finalBaseURL.toString()); request.setDocHeaders(headersFinal); request.setDocContent( new FlexBuffer(contentBuffer.getData(), 0, contentBuffer.getLength())); //LOG.info("Dispatching parse request"); ParseResult result = dispatcher.dispatchRequest(request); LOG.info("TID[" + Thread.currentThread().getId() + "]ReqID[" + i + "]" + " Success:" + ((result != null) ? result.getParseSuccessful() : false) + " LinkCount:" + ((result != null) ? result.getExtractedLinks().size() : 0)); } LOG.info("Thread:" + Thread.currentThread().getId() + " Exiting"); threadWaitSem.release(); } }); threads[threadIdx].start(); } LOG.info("Waiting for threads to die"); threadWaitSem.acquireUninterruptibly(); LOG.info("All Threads dead."); } finally { eventLoop.stop(); } } catch (IOException e) { LOG.error(CCStringUtils.stringifyException(e)); } catch (InterruptedException e) { } }
From source file:com.facebook.buck.testutil.FakeInputStreams.java
public static InputSupplier<? extends InputStream> createInputSupplierFromString(String data) { final InputStream inputStream = createInputStreamFromString(data); return new InputSupplier<InputStream>() { @Override/* www .j a va 2 s .c o m*/ public InputStream getInput() throws IOException { return inputStream; } }; }
From source file:org.jclouds.io.InputSuppliers.java
public static InputSupplier<? extends InputStream> of(final InputStream in) { checkNotNull(in, "in"); return new InputSupplier<InputStream>() { @Override//from w w w .j ava 2 s . c o m public InputStream getInput() throws IOException { return in; } }; }
From source file:com.complexible.common.io.IOSuppliers.java
/** * Return a {@link InputSupplier} that will provide the constant value as the input channel * @param theObj the input//w w w . j a va 2s . c o m * @param <T> the input type * @return the new InputSupplier */ public static <T> InputSupplier<T> inputOf(final T theObj) { return new InputSupplier<T>() { @Override public T getInput() throws IOException { return theObj; } }; }
From source file:org.apache.jackrabbit.oak.plugins.memory.AbstractBlob.java
private static InputSupplier<InputStream> supplier(final Blob blob) { return new InputSupplier<InputStream>() { @Override//from w ww . ja v a 2s. co m public InputStream getInput() throws IOException { return blob.getNewStream(); } }; }
From source file:com.github.nethad.clustermeister.provisioning.utils.FileUtils.java
/** * Compute Cyclic Redundancy Check (CRC32). * @param in the InputStream to compute/*from www . j a v a2 s. c o m*/ * @return CRC32 for the given InputStream * @throws IOException */ public static synchronized long getCRC32(final InputStream in) throws IOException { return ByteStreams.getChecksum(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return in; } }, new CRC32()); }
From source file:com.netflix.exhibitor.core.index.IndexerUtil.java
public static void startIndexing(Exhibitor exhibitor, final File path, IndexActivity.CompletionListener listener) throws Exception { if (path.isDirectory()) { final DirectoryInputStream stream = new DirectoryInputStream(path); // doesn't actually open any streams until reading starts InputSupplier<InputStream> source = new InputSupplier<InputStream>() { @Override//from w w w . j av a 2 s .c o m public InputStream getInput() throws IOException { return stream; } }; startIndexing(exhibitor, source, path.getName(), stream.length(), listener); } else { InputSupplier<InputStream> source = new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return new BufferedInputStream(new FileInputStream(path)); } }; startIndexing(exhibitor, source, path.getName(), path.length(), listener); } }
From source file:com.xebialabs.overthere.util.OverthereUtils.java
/** * Reads the contents of an {@link OverthereFile} into a byte array. * * @param from the file to read from.//from w w w.j a va 2 s . com * @returns the byte array. */ public static byte[] read(final OverthereFile from) { try { return ByteStreams.toByteArray(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return from.getInputStream(); } }); } catch (IOException exc) { throw new RuntimeException(exc); } }
From source file:co.cask.tigon.io.Locations.java
/** * Creates a new {@link com.google.common.io.InputSupplier} that can provides {@link SeekableInputStream} of * the given path.// www.ja v a 2 s . co m * * @param fs The {@link org.apache.hadoop.fs.FileSystem} for the given path. * @param path The path to create {@link co.cask.tigon.io.SeekableInputStream} when requested. * @return A {@link com.google.common.io.InputSupplier}. */ public static InputSupplier<? extends SeekableInputStream> newInputSupplier(final FileSystem fs, final Path path) { return new InputSupplier<SeekableInputStream>() { @Override public SeekableInputStream getInput() throws IOException { return SeekableInputStream.create(fs.open(path)); } }; }