List of usage examples for com.google.common.io OutputSupplier getOutput
T getOutput() throws IOException;
From source file:com.metamx.druid.index.v1.CompressedLongsSupplierSerializer.java
public void closeAndConsolidate(OutputSupplier<? extends OutputStream> consolidatedOut) throws IOException { endBuffer.limit(endBuffer.position()); endBuffer.rewind();//w w w. j a va2s.com flattener.write(StupidResourceHolder.create(endBuffer)); endBuffer = null; flattener.close(); OutputStream out = null; try { out = consolidatedOut.getOutput(); out.write(CompressedLongsIndexedSupplier.version); out.write(Ints.toByteArray(numInserted)); out.write(Ints.toByteArray(sizePer)); ByteStreams.copy(flattener.combineStreams(), out); } finally { Closeables.closeQuietly(out); } }
From source file:com.metamx.druid.index.v1.CompressedFloatsSupplierSerializer.java
public void closeAndConsolidate(OutputSupplier<? extends OutputStream> consolidatedOut) throws IOException { endBuffer.limit(endBuffer.position()); endBuffer.rewind();// w ww . jav a 2 s . co m flattener.write(StupidResourceHolder.create(endBuffer)); endBuffer = null; flattener.close(); OutputStream out = null; try { out = consolidatedOut.getOutput(); out.write(CompressedFloatsIndexedSupplier.version); out.write(Ints.toByteArray(numInserted)); out.write(Ints.toByteArray(sizePer)); ByteStreams.copy(flattener.combineStreams(), out); } finally { Closeables.closeQuietly(out); } }
From source file:co.cask.cdap.data.stream.StreamDataFileWriter.java
/** * Constructs a new instance that writes to given outputs. * * @param eventOutputSupplier the provider of the {@link OutputStream} for writing events * @param indexOutputSupplier the provider of the {@link OutputStream} for writing the index * @param indexInterval the time interval in milliseconds for emitting a new index entry * @param properties the property set that will be stored as file properties * @throws IOException if there is an error in preparing the output streams *///from w w w . jav a 2s .c o m public StreamDataFileWriter(OutputSupplier<? extends OutputStream> eventOutputSupplier, OutputSupplier<? extends OutputStream> indexOutputSupplier, long indexInterval, Map<String, String> properties) throws IOException { this.eventOutput = eventOutputSupplier.getOutput(); try { this.indexOutput = indexOutputSupplier.getOutput(); } catch (IOException e) { Closeables.closeQuietly(this.eventOutput); throw e; } this.indexInterval = indexInterval; this.currentTimestamp = -1L; this.closeTimestamp = -1L; Function<OutputStream, Encoder> encoderFactory = createEncoderFactory(); this.encoder = new BufferedEncoder(BUFFER_SIZE, encoderFactory); this.lengthEncoder = new BufferedEncoder(5, encoderFactory); try { init(properties); } catch (IOException e) { Closeables.closeQuietly(eventOutput); Closeables.closeQuietly(indexOutput); throw e; } }
From source file:bammerbom.ultimatecore.bukkit.resources.utils.AttributeUtil.java
/** * Save the content of a NBT compound to a stream. * <p>// w w w.j ava 2s.c om * Use {@link Files#newOutputStreamSupplier(java.io.File)} to provide a * stream supplier to a file. * * @param source - the NBT compound to save. * @param stream - the stream. * @param option - whether or not to compress the output. * @throws IOException If anything went wrong. */ public static void saveStream(NbtCompound source, OutputSupplier<? extends OutputStream> stream, StreamOptions option) throws IOException { OutputStream output = null; DataOutputStream data = null; boolean suppress = true; try { output = stream.getOutput(); data = new DataOutputStream( option == StreamOptions.GZIP_COMPRESSION ? new GZIPOutputStream(output) : output); invokeMethod(get().SAVE_COMPOUND, null, source.getHandle(), data); suppress = false; } finally { if (data != null) { Closeables.close(data, suppress); } else if (output != null) { Closeables.close(output, suppress); } } }
From source file:edu.cmu.cs.lti.ark.fn.Semafor.java
/** * Reads conll sentences, parses them, and writes the json-serialized results. * * @param inputSupplier where to read conll sentences from * @param outputSupplier where to write the results to * @param numThreads the number of threads to use * @throws IOException/* www . j a v a 2 s. c o m*/ * @throws InterruptedException */ public void runParser(final InputSupplier<? extends Readable> inputSupplier, final OutputSupplier<? extends Writer> outputSupplier, final int numThreads) throws IOException, InterruptedException { // use the producer-worker-consumer pattern to parse all sentences in multiple threads, while keeping // output in order. final BlockingQueue<Future<Optional<SemaforParseResult>>> results = Queues .newLinkedBlockingDeque(5 * numThreads); final ExecutorService workerThreadPool = newFixedThreadPool(numThreads); // try to shutdown gracefully. don't worry too much if it doesn't work Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { try { workerThreadPool.shutdown(); workerThreadPool.awaitTermination(5, TimeUnit.SECONDS); } catch (InterruptedException ignored) { } } })); final PrintWriter output = new PrintWriter(outputSupplier.getOutput()); try { // Start thread to fetch computed results and write to file final Thread consumer = new Thread(new Runnable() { @Override public void run() { while (!Thread.currentThread().isInterrupted()) { try { final Optional<SemaforParseResult> oResult = results.take().get(); if (!oResult.isPresent()) break; // got poison pill. we're done output.println(oResult.get().toJson()); output.flush(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } } }); consumer.start(); // in main thread, put placeholders on results queue (so results stay in order), then // tell a worker thread to fill up the placeholder final SentenceCodec.SentenceIterator sentences = ConllCodec.readInput(inputSupplier.getInput()); try { int i = 0; while (sentences.hasNext()) { final Sentence sentence = sentences.next(); final int sentenceId = i; results.put(workerThreadPool.submit(new Callable<Optional<SemaforParseResult>>() { @Override public Optional<SemaforParseResult> call() throws Exception { final long start = System.currentTimeMillis(); try { final SemaforParseResult result = parseSentence(sentence); final long end = System.currentTimeMillis(); System.err.printf("parsed sentence %d in %d millis.%n", sentenceId, end - start); return Optional.of(result); } catch (Exception e) { e.printStackTrace(); throw e; } } })); i++; } // put a poison pill on the queue to signal that we're done results.put(workerThreadPool.submit(new Callable<Optional<SemaforParseResult>>() { @Override public Optional<SemaforParseResult> call() throws Exception { return Optional.absent(); } })); workerThreadPool.shutdown(); } finally { closeQuietly(sentences); } // wait for consumer to finish consumer.join(); } finally { closeQuietly(output); } System.err.println("Done."); }
From source file:co.cask.cdap.gateway.handlers.AppFabricHttpHandler.java
/** * Saves the {@link SessionInfo} to the filesystem. * * @param info to be saved.// w w w .j av a2 s . co m * @return true if and only if successful; false otherwise. */ private boolean save(SessionInfo info, String accountId) { try { Gson gson = new GsonBuilder().registerTypeAdapter(Location.class, new LocationCodec(locationFactory)) .create(); Location outputDir = locationFactory.create(archiveDir + "/" + accountId); if (!outputDir.exists()) { return false; } final Location sessionInfoFile = outputDir.append("session.json"); OutputSupplier<Writer> writer = new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return new OutputStreamWriter(sessionInfoFile.getOutputStream(), "UTF-8"); } }; Writer w = writer.getOutput(); try { gson.toJson(info, w); } finally { Closeables.closeQuietly(w); } } catch (IOException e) { LOG.warn(e.getMessage(), e); return false; } return true; }