/** * Returns a new {@link CharSource} for reading character data from the given file using the given * character set. * * @since 14.0 */ public static CharSource asCharSource(File file, Charset charset) { return asByteSource(file).asCharSource(charset); }
/** * Copies all bytes from a file to an output stream. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#copy(java.nio.file.Path, OutputStream)}. * * @param from the source file * @param to the output stream * @throws IOException if an I/O error occurs */ public static void copy(File from, OutputStream to) throws IOException { asByteSource(from).copyTo(to); }
/** * Reads all bytes from a file into a byte array. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#readAllBytes}. * * @param file the file to read from * @return a byte array containing all the bytes from file * @throws IllegalArgumentException if the file is bigger than the largest possible byte array * (2^31 - 1) * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(File file) throws IOException { return asByteSource(file).read(); }
/** * Computes the hash code of the {@code file} using {@code hashFunction}. * * @param file the file to read * @param hashFunction the hash function to use to hash the data * @return the {@link HashCode} of all of the bytes in the file * @throws IOException if an I/O error occurs * @since 12.0 * @deprecated Prefer {@code asByteSource(file).hash(hashFunction)}. This method is scheduled to * be removed in January 2019. */ @Deprecated public static HashCode hash(File file, HashFunction hashFunction) throws IOException { return asByteSource(file).hash(hashFunction); }
/** * Returns true if the given files exist, are not directories, and contain the same bytes. * * @throws IOException if an I/O error occurs */ public static boolean equal(File file1, File file2) throws IOException { checkNotNull(file1); checkNotNull(file2); if (file1 == file2 || file1.equals(file2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ long len1 = file1.length(); long len2 = file2.length(); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return asByteSource(file1).contentEquals(asByteSource(file2)); }
/** * Returns a new {@link CharSource} for reading character data from the given file using the given * character set. * * @since 14.0 */ public static CharSource asCharSource(File file, Charset charset) { return asByteSource(file).asCharSource(charset); }
/** * gunzip the file to the output file. * * @param pulledFile The source of the gz data * @param outFile A target file to put the contents * * @return The result of the file copy * * @throws IOException */ public static FileUtils.FileCopyResult gunzip(final File pulledFile, File outFile) { return gunzip(Files.asByteSource(pulledFile), outFile); }
/** * Copies all bytes from a file to an output stream. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#copy(java.nio.file.Path, OutputStream)}. * * @param from the source file * @param to the output stream * @throws IOException if an I/O error occurs */ public static void copy(File from, OutputStream to) throws IOException { asByteSource(from).copyTo(to); }
/** * Reads all bytes from a file into a byte array. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#readAllBytes}. * * @param file the file to read from * @return a byte array containing all the bytes from file * @throws IllegalArgumentException if the file is bigger than the largest possible byte array * (2^31 - 1) * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(File file) throws IOException { return asByteSource(file).read(); }
/** * Computes the hash code of the {@code file} using {@code hashFunction}. * * @param file the file to read * @param hashFunction the hash function to use to hash the data * @return the {@link HashCode} of all of the bytes in the file * @throws IOException if an I/O error occurs * @since 12.0 * @deprecated Prefer {@code asByteSource(file).hash(hashFunction)}. This method is scheduled to * be removed in January 2019. */ @Deprecated public static HashCode hash(File file, HashFunction hashFunction) throws IOException { return asByteSource(file).hash(hashFunction); }
/** * Process the bytes of a file. * * <p>(If this seems too complicated, maybe you're looking for {@link #toByteArray}.) * * @param file the file to read * @param processor the object to which the bytes of the file are passed. * @return the result of the byte processor * @throws IOException if an I/O error occurs * @deprecated Prefer {@code asByteSource(file).read(processor)}. This method is scheduled to be * removed in January 2019. */ @Deprecated @CanIgnoreReturnValue // some processors won't return a useful result public static <T> T readBytes(File file, ByteProcessor<T> processor) throws IOException { return asByteSource(file).read(processor); }
/** * 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 CharSource} for reading character data from the given file using the given * character set. * * @since 14.0 */ public static CharSource asCharSource(File file, Charset charset) { return asByteSource(file).asCharSource(charset); }
static void uploadJar(File jarFile, final Path path, final FileSystem fs) throws IOException { log.info("Uploading jar to path[%s]", path); try (OutputStream os = fs.create(path)) { Files.asByteSource(jarFile).copyTo(os); } }
public CloudFilesObject(final String basePath, final File file, final String region, final String container) { this(region, container); ByteSource byteSource = Files.asByteSource(file); this.payload = Payloads.newByteSourcePayload(byteSource); this.path = CloudFilesUtils.buildCloudFilesPath(basePath, file.getName()); }
@Override public ByteSource createSource(byte[] bytes) throws IOException { checkNotNull(bytes); File file = createFile(); OutputStream out = new FileOutputStream(file); try { out.write(bytes); } finally { out.close(); } return Files.asByteSource(file); }
/** * 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); }
public void testToByteArray() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII), Files.toByteArray(asciiFile))); assertTrue(Arrays.equals(I18N.getBytes(Charsets.UTF_8), Files.toByteArray(i18nFile))); assertTrue(Arrays.equals(I18N.getBytes(Charsets.UTF_8), Files.asByteSource(i18nFile).read())); }
public void testRoundTripSources() throws Exception { File asciiFile = getTestFile("ascii.txt"); ByteSource byteSource = Files.asByteSource(asciiFile); assertSame(byteSource, byteSource.asCharSource(Charsets.UTF_8).asByteSource(Charsets.UTF_8)); }
public void testEqual() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); assertFalse(Files.equal(asciiFile, i18nFile)); assertTrue(Files.equal(asciiFile, asciiFile)); File temp = createTempFile(); Files.copy(asciiFile, temp); assertTrue(Files.equal(asciiFile, temp)); Files.copy(i18nFile, temp); assertTrue(Files.equal(i18nFile, temp)); Files.copy(asciiFile, temp); RandomAccessFile rf = new RandomAccessFile(temp, "rw"); rf.writeByte(0); rf.close(); assertEquals(asciiFile.length(), temp.length()); assertFalse(Files.equal(asciiFile, temp)); assertTrue(Files.asByteSource(asciiFile).contentEquals(Files.asByteSource(asciiFile))); // 0-length files have special treatment (/proc, etc.) assertTrue(Files.equal(asciiFile, new BadLengthFile(asciiFile, 0))); }