public void saveTo(ByteSink sink) throws IOException { final PrintWriter out = new PrintWriter(sink.asCharSink(Charsets.UTF_8).openBufferedStream()); out.println(docIdToBannedRegions.size()); for (final Map.Entry<Symbol, ImmutableRangeSet<Integer>> entry : docIdToBannedRegions .entrySet()) { out.println(entry.getKey()); final List<String> parts = Lists.newArrayList(); for (final Range<Integer> r : entry.getValue().asRanges()) { // we know by construction these ranges are bounded above and below parts.add(String.format("%d-%d", r.lowerEndpoint(), r.upperEndpoint())); } out.println(StringUtils.SpaceJoiner.join(parts)); } out.close(); }
public void testGet_io() throws IOException { assertEquals(-1, ArbitraryInstances.get(InputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(ByteArrayInputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(Readable.class).read(CharBuffer.allocate(1))); assertEquals(-1, ArbitraryInstances.get(Reader.class).read()); assertEquals(-1, ArbitraryInstances.get(StringReader.class).read()); assertEquals(0, ArbitraryInstances.get(Buffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(CharBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ByteBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ShortBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(IntBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(LongBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(FloatBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(DoubleBuffer.class).capacity()); ArbitraryInstances.get(PrintStream.class).println("test"); ArbitraryInstances.get(PrintWriter.class).println("test"); assertNotNull(ArbitraryInstances.get(File.class)); assertFreshInstanceReturned( ByteArrayOutputStream.class, OutputStream.class, Writer.class, StringWriter.class, PrintStream.class, PrintWriter.class); assertEquals(ByteSource.empty(), ArbitraryInstances.get(ByteSource.class)); assertEquals(CharSource.empty(), ArbitraryInstances.get(CharSource.class)); assertNotNull(ArbitraryInstances.get(ByteSink.class)); assertNotNull(ArbitraryInstances.get(CharSink.class)); }
public static Single<WriteFileEvent> writeFile(final String content, final Path path, final boolean overwrite) { Preconditions.checkNotNull(content); Preconditions.checkNotNull(path); return Single.fromCallable(() -> { if (path.getParent() != null && !Files.exists(path.getParent())) { Files.createDirectories(path.getParent()); } if (overwrite) { Files.deleteIfExists(path); } else if (Files.isDirectory(path)) { throw new IOException("There is already a directory at " + path); } else if (Files.exists(path)) { throw new IOException("There is already a file at " + path); } final ByteSink sink = MoreFiles.asByteSink(path); sink.write(content.getBytes()); return WriteFileEvent.of(path); }).subscribeOn(Schedulers.io()); }
private static void runScenario( final Class<? extends Scenario> scenarioClass ) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException, InterruptedException, ExecutionException { final File scenarioOutputFile = new File( TARGET_DIRECTORY, scenarioClass.getCanonicalName() ); final ScenarioRunner scenarioRunner = new ScenarioRunner( scenarioClass, new ByteSink() { @Override public OutputStream openStream() throws IOException { final ByteSink fileSink = Files.asByteSink( scenarioOutputFile ); return new MultiplexingOutputStream( System.out, fileSink.openStream() ); } }, EXTENDED_RUN_PARAMETERS ); scenarioRunner.run(); }
/** * Returns a byte sink for generating output. If the name is "-" then bytes will go to the standard output * stream, otherwise name is taken as a file path relative to the current working directory. */ protected static ByteSink getOutput(String name) { try { if (name.equals("-")) { return new ByteSink() { @Override public OutputStream openStream() throws IOException { // TODO Block closing? return System.out; } }; } else { File file = new File(name); if (file.isDirectory()) { throw new IOException(name); // TODO Message? Exception? } else if (file.exists() && !file.canWrite()) { throw new IOException(name); // TODO Message? Exception? } else { return Files.asByteSink(file); } } } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public void finish() throws IOException { final CharSink textSink = Files.asCharSink(new File(outputDir, outputName + FILE_SUFFIX), Charsets.UTF_8); final ByteSink jsonSink = Files.asByteSink(new File(outputDir, outputName + FILE_SUFFIX + ".json")); final SummaryConfusionMatrix summaryConfusionMatrix = summaryConfusionMatrixB.build(); // Output the summaries and add a final newline final FMeasureCounts fMeasure = SummaryConfusionMatrices.FMeasureVsAllOthers(summaryConfusionMatrix, PRESENT); textSink.write(StringUtils.NewlineJoiner.join( SummaryConfusionMatrices.prettyPrint(summaryConfusionMatrix), fMeasure.compactPrettyString(), "")); // Empty string creates a bare newline at the end JacksonSerializer.builder().forJson().prettyOutput().build().serializeTo(fMeasure, jsonSink); // Call finish on the observers for (final ScoringEventObserver observer : scoringEventObservers) { observer.finish(outputDir); } }
public static void writeBinary(OffsetIndex offsetIndex, ByteSink sink) throws IOException { final DataOutputStream out = new DataOutputStream(sink.openBufferedStream()); boolean threw = true; try { out.writeInt(offsetIndex.keySet().size()); // use a fixed key order to be diff-friendly. final Iterable<Symbol> keyOrder = Ordering.usingToString().immutableSortedCopy(offsetIndex.keySet()); for (final Symbol key : keyOrder) { out.writeUTF(key.asString()); // get is safe because we are iterating over the mapping's key set final OffsetRange<ByteOffset> range = offsetIndex.byteOffsetsOf(key).get(); out.writeInt(range.startInclusive().asInt()); out.writeInt(range.endInclusive().asInt()); } threw = false; } finally { Closeables.close(out, threw); } }
/** * Compiles all the templates in the given registry to a jar file written to the given output * stream. * * <p>If errors are encountered, the error reporter will be updated and we will return. The * contents of any data written to the sink at that point are undefined. * * @param registry All the templates to compile * @param reporter The error reporter * @param sink The output sink to write the JAR to. */ public static void compileToJar(TemplateRegistry registry, ErrorReporter reporter, ByteSink sink) throws IOException { ErrorReporter.Checkpoint checkpoint = reporter.checkpoint(); if (reporter.errorsSince(checkpoint)) { return; } CompiledTemplateRegistry compilerRegistry = new CompiledTemplateRegistry(registry); if (reporter.errorsSince(checkpoint)) { return; } try (final SoyJarFileWriter writer = new SoyJarFileWriter(sink.openStream())) { compileTemplates( compilerRegistry, reporter, new CompilerListener<Void>() { @Override void onCompile(ClassData clazz) throws IOException { writer.writeEntry( clazz.type().internalName() + ".class", ByteSource.wrap(clazz.data())); } }); } }
/** * Writes the source files out to a {@code -src.jar}. This places the soy files at the same * classpath relative location as their generated classes. Ultimately this can be used by * debuggers for source level debugging. * * <p>It is a little weird that the relative locations of the generated classes are not identical * to the input source files. This is due to the disconnect between java packages and soy * namespaces. We should consider using the soy namespace directly as a java package in the * future. * * @param registry All the templates in the current compilation unit * @param files The source files by file path * @param sink The source to write the jar file */ public static void writeSrcJar( TemplateRegistry registry, ImmutableMap<String, SoyFileSupplier> files, ByteSink sink) throws IOException { Set<SoyFileNode> seenFiles = new HashSet<>(); try (SoyJarFileWriter writer = new SoyJarFileWriter(sink.openStream())) { for (TemplateNode template : registry.getAllTemplates()) { SoyFileNode file = template.getParent(); if (file.getSoyFileKind() == SoyFileKind.SRC && seenFiles.add(file)) { String namespace = file.getNamespace(); String fileName = file.getFileName(); writer.writeEntry( Names.javaFileName(namespace, fileName), files.get(file.getFilePath()).asCharSource().asByteSource(UTF_8)); } } } }
/** * Save the content of a NBT compound to a stream. * * @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, ByteSink stream, StreamOptions option) throws IOException { OutputStream output = null; DataOutputStream data = null; boolean suppress = true; try { output = stream.openStream(); 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); } } }
private void writeArray(DoubleArrayList numbers, File file) throws IOException { final ByteSink sink = GZIPByteSink.gzipCompress(Files.asByteSink(file)); final PrintWriter out = new PrintWriter(sink.asCharSink(Charsets.UTF_8).openBufferedStream()); for (final DoubleCursor cursor : numbers) { out.println(cursor.value); } out.close(); ; }
public ScenarioRunner( final Class<? extends Scenario> scenarioClass, final ByteSink reportTo, final ScenarioRun[] extendedRunArguments ) throws IllegalAccessException, InstantiationException { checkArgument( scenarioClass != null, "scenarioClass can't be null" ); checkArgument( reportTo != null, "reportTo can't be null" ); //just to check class have 0-arg ctor and can be cast to Scenario final Scenario scenario = scenarioClass.newInstance(); this.scenarioClass = scenarioClass; this.reportTo = reportTo; this.extendedRunArguments = extendedRunArguments; }
final void stream( ByteSource input, ByteSink output, ByteSink error) throws IOException, MojoExecutionException { try (InputStream in = input.openBufferedStream()) { try (OutputStream out = output.openBufferedStream()) { try (PrintStream pout = new PrintStream(out, true, "UTF-8")) { try (OutputStream err = error.openBufferedStream()) { try (PrintStream perr = new PrintStream(err, true, "UTF-8")) { stream(in, pout, perr); } } } } } }
@Test public void copyToByteSinkTest() throws Exception { File dest = new File("src/test/resources/sampleCompany.pdf"); dest.deleteOnExit(); File source = new File("src/main/resources/sample.pdf"); ByteSource byteSource = Files.asByteSource(source); ByteSink byteSink = Files.asByteSink(dest); byteSource.copyTo(byteSink); assertThat(Files.toByteArray(dest), is(Files.toByteArray(source))); }
/** * Creates a new keystore that will contain a single entry with given {@code entryAlias} and {@code entryPassword} * and stores it to a temporary file. The entry will contain the private key and the certificate. */ public File toTmpKeyStoreFile(String entryAlias, String password) throws IOException, GeneralSecurityException { KeyStore keyStore = toKeyStore(entryAlias, password); File keyStoreFile = File.createTempFile("key-" + entryAlias, ".keystore", new File("target")); ByteSink keyStoreSink = Files.asByteSink(keyStoreFile); OutputStream keyStoreStream = keyStoreSink.openStream(); try { keyStore.store(keyStoreStream, password.toCharArray()); } finally { Closeables.close(keyStoreStream, true); } return keyStoreFile; }
/** * Creates a new truststore that will contain a single entry with given {@code entryAlias} and stores it * to a temporary file. The entry will contain the certificate. */ public File toTmpTrustStoreFile(String entryAlias, String password) throws IOException, GeneralSecurityException { KeyStore trustStore = toTrustStore(entryAlias); File trustStoreFile = File.createTempFile("trust-" + entryAlias, ".truststore", new File("target")); ByteSink trustStoreSink = Files.asByteSink(trustStoreFile); OutputStream trustStoreStream = trustStoreSink.openStream(); try { trustStore.store(trustStoreStream, password.toCharArray()); } finally { Closeables.close(trustStoreStream, true); } return trustStoreFile; }
@Test public void whenWriteUsingByteSink_thenWritten() throws IOException { final String expectedValue = "Hello world"; final File file = new File("src/test/resources/test.out"); final ByteSink sink = Files.asByteSink(file); sink.write(expectedValue.getBytes()); final String result = Files.toString(file, Charsets.UTF_8); assertEquals(expectedValue, result); }
public static void writeBinaryIntArray(final int[] arr, final ByteSink outSup) throws IOException { try (OutputStream out = outSup.openBufferedStream()) { try (DataOutputStream dos = new DataOutputStream(out)) { dos.writeInt(arr.length); for (final int x : arr) { dos.writeInt(x); } } } }
public long copyTo(ByteSink sink) { try { return delegate.copyTo(sink); } catch (IOException ex) { throw new AssertionError("An illegal IOException was throw"); } }
@Override void compile(SoyFileSet.Builder sfsBuilder) throws IOException { Optional<ByteSink> srcJarSink = Optional.absent(); if (outputSrcJar != null) { srcJarSink = Optional.of(Files.asByteSink(outputSrcJar)); } compile(sfsBuilder.build(), Files.asByteSink(output), srcJarSink); }
/** * Extracts all messages from this Soy file set and writes the messages to an output sink. * * @param msgBundleHandler Handler to write the messages. * @param options Options to configure how to write the extracted messages. * @param output Where to write the extracted messages. * @throws IOException If there are errors writing to the output. */ public void extractAndWriteMsgs( SoyMsgBundleHandler msgBundleHandler, OutputFileOptions options, ByteSink output) throws IOException { resetErrorReporter(); SoyMsgBundle bundle = doExtractMsgs(); msgBundleHandler.writeExtractedMsgs(bundle, options, output, errorReporter); throwIfErrorsPresent(); reportWarnings(); }
/** * Compiles this Soy file set into a set of java classes implementing the {@link CompiledTemplate} * interface and writes them out to the given ByteSink as a JAR file. * * @throws SoyCompilationException If compilation fails. */ void compileToJar(ByteSink jarTarget, Optional<ByteSink> srcJarTarget) throws IOException { resetErrorReporter(); disallowExternalCalls(); ServerCompilationPrimitives primitives = compileForServerRendering(); BytecodeCompiler.compileToJar(primitives.registry, errorReporter, jarTarget); if (srcJarTarget.isPresent()) { BytecodeCompiler.writeSrcJar(primitives.registry, soyFileSuppliers, srcJarTarget.get()); } throwIfErrorsPresent(); reportWarnings(); }
public static ByteSink asByteSink(final Path path, final boolean append) { return new ByteSink() { @Override public OutputStream openStream() throws IOException { return path.getOutputStream(append); } }; }
private static void prepareTemplate(File templateDir, String template) throws IOException { URL src = OutputTemplates.class.getClassLoader().getResource("templates/" + template); if (src == null) { throw new IOException("Could not find template " + template); } ByteSink dest = Files.asByteSink(new File(templateDir, template)); Resources.asByteSource(src).copyTo(dest); }
@Override public void read(ByteString key, ByteSink byteSink) throws IOException { File path = toPath(key); path.getParentFile().mkdirs(); ByteSource source = Files.asByteSource(path); source.copyTo(byteSink); }
public String writeSnapshot(ReadOnlyTransaction txn, SnapshotStorage snapshotDest) throws IOException { PageStore pageStore = db.getPageStore(); try (SnapshotStorage.SnapshotUpload upload = snapshotDest.doUpload()) { ByteSource byteSource = pageStore.asByteSource(txn); ByteSink sink = upload.asSink(); byteSource.copyTo(sink); return upload.getId(); } }
/** * Writes the specified string to stream with buffering and closes the stream. * * @param str String to write. * @param outputStream Stream to write to. * @param charset The charset to write in. * @throws IOException If there is an exception while writing. */ public static void write(String str, final OutputStream outputStream, Charset charset) throws IOException { new ByteSink() { @Override public OutputStream openStream() { return outputStream; } }.asCharSink(charset).write(str); }
/** * Copies the {@code inputStream} into the {@code outputSteam} and finally * closes the both streams. */ public static void copy(final InputStream inputStream, final OutputStream outputStream) throws IOException { new ByteSource() { @Override public InputStream openStream() { return inputStream; } }.copyTo(new ByteSink() { @Override public OutputStream openStream() { return outputStream; } }); }
/** * Writes the given {@code byte[]} to a stream. * * @throws IOException if the steam cannot be written to */ @VisibleForTesting static void writeBytesToStream(byte[] bytes, final OutputStream outputStream) throws IOException { new ByteSink() { @Override public OutputStream openStream() { return outputStream; } }.write(bytes); }
@Test public void downloadProductImage() throws Exception { when(productImageSeeker.seekProductImage(anyLong())) .thenReturn(Resources.getResource("Large_Scaled_Forest_Lizard.jpg").openStream()); AtomicBoolean completed = new AtomicBoolean(false); AtomicBoolean error = new AtomicBoolean(false); File imageFile = File.createTempFile("image", ".jpg"); imageFile.deleteOnExit(); Files.touch(imageFile); ByteSink byteSink = Files.asByteSink(imageFile, FileWriteMode.APPEND); StreamObserver<DataChunk> streamObserver = new StreamObserver<DataChunk>() { @Override public void onNext(DataChunk dataChunk) { try { byteSink.write(dataChunk.getData().toByteArray()); } catch (IOException e) { log.error("error on write files", e); onError(e); } } @Override public void onError(Throwable t) { error.compareAndSet(false, true); } @Override public void onCompleted() { log.info("write image to {}", imageFile.getAbsoluteFile()); completed.compareAndSet(false, true); } }; stub.downloadProductImage(DownloadProductImageRequest.getDefaultInstance(), streamObserver); while (!completed.get() && !error.get()) { Thread.sleep(500); } assertThat(completed.get()).isTrue(); assertThat(error.get()).isFalse(); try (InputStream destImageStream = new FileInputStream(imageFile); InputStream origImageStream = Resources.getResource("Large_Scaled_Forest_Lizard.jpg").openStream()) { assertThat(DigestUtils.md5Hex(destImageStream)).isEqualTo( DigestUtils.md5Hex(origImageStream) ); } }
public Completable write(Object o, ByteSink sink) { return write(o).map(String::getBytes).flatMapCompletable(bytes -> ioAsync(() -> sink.write(bytes))); }
/** * Output to an output stream supplied by the specified byte sink. */ public static OutputSink toStream(ByteSink byteSink) { return create(Kind.STREAM, byteSink); }
private static OutputSink create(Kind kind, Optional<ByteSink> byteSink) { return new AutoValue_OutputSink(kind, byteSink); }
private static OutputSink create(Kind kind, ByteSink byteSink) { return create(kind, Optional.of(byteSink)); }
public void setOutput(ByteSink output) { this.output = Objects.requireNonNull(output); }
public void serializeTo(final Object o, final ByteSink out) throws IOException { final RootObject rootObj = RootObject.forObject(o); final OutputStream bufStream = out.openBufferedStream(); mapper.writeValue(bufStream, rootObj); bufStream.close(); }