/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }
/** * Copies all the bytes from one file to another. * * <p>Copying is not an atomic operation - in the case of an I/O error, power loss, process * termination, or other problems, {@code to} may not be a complete copy of {@code from}. If you * need to guard against those conditions, you should employ other file-level synchronization. * * <p><b>Warning:</b> If {@code to} represents an existing file, that file will be overwritten * with the contents of {@code from}. If {@code to} and {@code from} refer to the <i>same</i> * file, the contents of that file will be deleted. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)}. * * @param from the source file * @param to the destination file * @throws IOException if an I/O error occurs * @throws IllegalArgumentException if {@code from.equals(to)} */ public static void copy(File from, File to) throws IOException { checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to); asByteSource(from).copyTo(asByteSink(to)); }
/** * 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); }
/** * 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); }
@Override public ByteSink createSink() throws IOException { File file = createFile(); if (initialBytes != null) { FileOutputStream out = new FileOutputStream(file); try { out.write(initialBytes); } finally { out.close(); } return Files.asByteSink(file, FileWriteMode.APPEND); } return Files.asByteSink(file); }
/** * Copies all the bytes from one file to another. * * <p>Copying is not an atomic operation - in the case of an I/O error, power loss, process * termination, or other problems, {@code to} may not be a complete copy of {@code from}. If you * need to guard against those conditions, you should employ other file-level synchronization. * * <p><b>Warning:</b> If {@code to} represents an existing file, that file will be overwritten * with the contents of {@code from}. If {@code to} and {@code from} refer to the <i>same</i> * file, the contents of that file will be deleted. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)}. * * @param from the source file * @param to the destination file * @throws IOException if an I/O error occurs * @throws IllegalArgumentException if {@code from.equals(to)} */ public static void copy(File from, File to) throws IOException { checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to); asByteSource(from).copyTo(asByteSink(to)); }
/** * Copy input byte source to outFile. If outFile exists, it is attempted to be deleted. * * @param byteSource Supplier for an input stream that is to be copied. The resulting stream is closed each iteration * @param outFile Where the file should be written to. * @param shouldRetry Predicate indicating if an error is recoverable and should be retried. * @param maxAttempts The maximum number of assumed recoverable attempts to try before completely failing. * * @throws RuntimeException wrapping the inner exception on failure. */ public static FileCopyResult retryCopy( final ByteSource byteSource, final File outFile, final Predicate<Throwable> shouldRetry, final int maxAttempts ) { try { StreamUtils.retryCopy( byteSource, Files.asByteSink(outFile), shouldRetry, maxAttempts ); return new FileCopyResult(outFile); } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Copies all the bytes from one file to another. * * <p>Copying is not an atomic operation - in the case of an I/O error, power loss, process * termination, or other problems, {@code to} may not be a complete copy of {@code from}. If you * need to guard against those conditions, you should employ other file-level synchronization. * * <p><b>Warning:</b> If {@code to} represents an existing file, that file will be overwritten * with the contents of {@code from}. If {@code to} and {@code from} refer to the <i>same</i> * file, the contents of that file will be deleted. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)}. * * @param from the source file * @param to the destination file * @throws IOException if an I/O error occurs * @throws IllegalArgumentException if {@code from.equals(to)} */ public static void copy(File from, File to) throws IOException { checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to); asByteSource(from).copyTo(asByteSink(to)); }
/** * Gzips the input file to the output * * @param inFile The file to gzip * @param outFile A target file to copy the uncompressed contents of inFile to * @param shouldRetry Predicate on a potential throwable to determine if the copy should be attempted again. * * @return The result of the file copy * * @throws IOException */ public static FileUtils.FileCopyResult gzip(final File inFile, final File outFile, Predicate<Throwable> shouldRetry) { gzip(Files.asByteSource(inFile), Files.asByteSink(outFile), shouldRetry); return new FileUtils.FileCopyResult(outFile); }
@Test public void testGoodGzipByteSource() throws IOException { final File tmpDir = temporaryFolder.newFolder("testGoodGzipByteSource"); final File gzFile = new File(tmpDir, testFile.getName() + ".gz"); Assert.assertFalse(gzFile.exists()); CompressionUtils.gzip(Files.asByteSource(testFile), Files.asByteSink(gzFile), Predicates.alwaysTrue()); Assert.assertTrue(gzFile.exists()); try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(gzFile), gzFile.getName())) { assertGoodDataStream(inputStream); } if (!testFile.delete()) { throw new IOE("Unable to delete file [%s]", testFile.getAbsolutePath()); } Assert.assertFalse(testFile.exists()); CompressionUtils.gunzip(Files.asByteSource(gzFile), testFile); Assert.assertTrue(testFile.exists()); try (final InputStream inputStream = new FileInputStream(testFile)) { assertGoodDataStream(inputStream); } }
closer.register(segmentWriteOutMedium); long startTime = System.currentTimeMillis(); Files.asByteSink(new File(outDir, "version.bin")).write(Ints.toByteArray(IndexIO.V9_VERSION)); log.info("Completed version.bin in %,d millis.", System.currentTimeMillis() - startTime);
boolean runFailed = true; final ByteSink logSink = Files.asByteSink(logFile, FileWriteMode.APPEND);
final File gzFile = new File(tmpDir, testFile.getName() + ".gz"); Assert.assertFalse(gzFile.exists()); CompressionUtils.gzip(Files.asByteSource(testFile), Files.asByteSink(gzFile), Predicates.alwaysTrue()); Assert.assertTrue(gzFile.exists()); try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(gzFile), "file.gz")) {
@Before public void setUp() throws IOException { config = new LocalDataSegmentPusherConfig(); config.storageDirectory = temporaryFolder.newFolder(); localDataSegmentPusher = new LocalDataSegmentPusher(config, TestHelper.makeJsonMapper()); dataSegmentFiles = temporaryFolder.newFolder(); Files.asByteSink(new File(dataSegmentFiles, "version.bin")).write(Ints.toByteArray(0x9)); }
@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")))); }
}.copyTo(Files.asByteSink(new File(persistTmpDir, file)));
Files.asByteSink(outFile).writeFrom(inputStream); mCopied++; } catch (FileNotFoundException e) {