Refine search
/** * Writes all the given bytes to this sink. * * @throws IOException if an I/O occurs while writing to this sink */ public void write(byte[] bytes) throws IOException { checkNotNull(bytes); Closer closer = Closer.create(); try { OutputStream out = closer.register(openStream()); out.write(bytes); out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public void testWriteFrom_inputStream() throws IOException { sink.writeFrom(new ByteArrayInputStream(data)); assertContainsExpectedBytes(); }
/** * Overwrites a file with the contents of a byte array. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#write(java.nio.file.Path, byte[], java.nio.file.OpenOption...)}. * * @param from the bytes to write * @param to the destination file * @throws IOException if an I/O error occurs */ public static void write(byte[] from, File to) throws IOException { asByteSink(to).write(from); }
/** * Writes all the bytes from the given {@code InputStream} to this sink. Does not close {@code * input}. * * @return the number of bytes written * @throws IOException if an I/O occurs while reading from {@code input} or writing to this sink */ @CanIgnoreReturnValue public long writeFrom(InputStream input) throws IOException { checkNotNull(input); Closer closer = Closer.create(); try { OutputStream out = closer.register(openStream()); long written = ByteStreams.copy(input, out); out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 return written; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
() -> { try (InputStream inputStream = byteSource.openStream()) { try (OutputStream outputStream = byteSink.openStream()) { final long retval = ByteStreams.copy(inputStream, outputStream); outputStream.flush(); return retval;
public void testOpenStream() throws IOException { OutputStream out = sink.openStream(); try { ByteStreams.copy(new ByteArrayInputStream(data), out); } finally { out.close(); } assertContainsExpectedBytes(); }
public void testOpenBufferedStream() throws IOException { OutputStream out = sink.openBufferedStream(); try { ByteStreams.copy(new ByteArrayInputStream(data), out); } finally { out.close(); } assertContainsExpectedBytes(); }
public void writeTo(@Nonnull File file) throws IOException { ByteSink sink = Files.asByteSink(file); OutputStream out = sink.openBufferedStream(); try { writeTo(out); } finally { out.close(); } }
ByteStreams.copy(in, Files.asByteSink(file).openBufferedStream()); } else { throw new FileNotFoundException("could not write to file "+path+"; it does not exist or is not writable");
/** * Save an object to file. * * @param object the object * @throws java.io.IOException on save error */ public static void commit(@NonNull Object object) throws IOException { ByteSink sink; synchronized (bound) { sink = bound.get(object); if (sink == null) { throw new IOException("Cannot persist unbound object: " + object); } } Closer closer = Closer.create(); try { OutputStream os = closer.register(sink.openBufferedStream()); mapper.writeValue(os, object); } finally { closer.close(); } }
boolean runFailed = true; final ByteSink logSink = Files.asByteSink(logFile, FileWriteMode.APPEND); Thread.currentThread().setName(StringUtils.format("%s-[%s]", priorThreadName, task.getId())); try (final OutputStream toLogfile = logSink.openStream()) { ByteStreams.copy(processHolder.process.getInputStream(), toLogfile); final int statusCode = processHolder.process.waitFor(); log.info("Process exited with status[%d] for task: %s", statusCode, task.getId());
Files.asByteSink(outFile).writeFrom(inputStream); mCopied++; } catch (FileNotFoundException e) {
/** * Returns a new {@link CharSink} for writing character data to the given file using the given * character set. The given {@code modes} control how the file is opened for writing. When no mode * is provided, the file will be truncated before writing. When the {@link FileWriteMode#APPEND * APPEND} mode is provided, writes will append to the end of the file without truncating it. * * @since 14.0 */ public static CharSink asCharSink(File file, Charset charset, FileWriteMode... modes) { return asByteSink(file, modes).asCharSink(charset); }
@VisibleForTesting public static void extractBundledProguardFile( @NonNull String name, @NonNull File proguardFile) throws IOException { Files.createParentDirs(proguardFile); URL proguardURL = ProguardFiles.class.getResource(name); URLConnection urlConnection = proguardURL.openConnection(); urlConnection.setUseCaches(false); try (InputStream is = urlConnection.getInputStream()) { Files.asByteSink(proguardFile).writeFrom(is); } } }
@Test public void testLastPushWinsForConcurrentPushes() throws IOException { File replicatedDataSegmentFiles = temporaryFolder.newFolder(); Files.asByteSink(new File(replicatedDataSegmentFiles, "version.bin")).write(Ints.toByteArray(0x8)); DataSegment returnSegment1 = localDataSegmentPusher.push(dataSegmentFiles, dataSegment, false); DataSegment returnSegment2 = localDataSegmentPusher.push(replicatedDataSegmentFiles, dataSegment2, false); Assert.assertEquals(dataSegment.getDimensions(), returnSegment1.getDimensions()); Assert.assertEquals(dataSegment2.getDimensions(), returnSegment2.getDimensions()); File unzipDir = new File(config.storageDirectory, "unzip"); FileUtils.forceMkdir(unzipDir); CompressionUtils.unzip( new File(config.storageDirectory, "/ds/1970-01-01T00:00:00.000Z_1970-01-01T00:00:00.001Z/v1/0/index.zip"), unzipDir ); Assert.assertEquals(0x8, Ints.fromByteArray(Files.toByteArray(new File(unzipDir, "version.bin")))); }
public static <T> void writeTo(T model, File outputFile) throws IOException { ByteSink sink = Files.asByteSink(outputFile); try (ObjectOutputStream oos = new ObjectOutputStream(sink.openBufferedStream())) { oos.writeObject(model); } }
public void writeResource(String skinName, Resource resource) throws IOException { final String requestPath = resource.getRequestPath(); final String requestPathWithSkin = skinName == null ? requestPath : ResourceSkinUtils.evaluateSkinInPath(requestPath, skinName); ResourceProcessor matchingProcessor = getMatchingResourceProcessor(requestPath); File outFile = createOutputFile(requestPathWithSkin); log.debug("Opening output stream for " + outFile); matchingProcessor.process(requestPathWithSkin, new ResourceInputStreamSupplier(resource).openStream(), Files.asByteSink(outFile).openStream(), true); processedResources.put(ResourceUtil.getResourceQualifier(resource), requestPath); }
/** * Opens an output stream from the supplier, copies all bytes from the input * to the output, and closes the output stream. Does not close or flush the * input stream. * * @param from the input stream to read from * @param to the output factory * @return the number of bytes copied * @throws IOException if an I/O error occurs * @since 10.0 */ public static long copy(InputStream from, OutputSupplier<? extends OutputStream> to) throws IOException { return asByteSink(to).writeFrom(from); }
/** * Writes a byte array to an output stream from the given supplier. * * @param from the bytes to write * @param to the output supplier * @throws IOException if an I/O error occurs */ public static void write(byte[] from, OutputSupplier<? extends OutputStream> to) throws IOException { asByteSink(to).write(from); }
@Override public Writer openStream() throws IOException { return new OutputStreamWriter(ByteSink.this.openStream(), charset); }