com.google.common.io.Files.asByteSource()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(173)

本文整理了Java中com.google.common.io.Files.asByteSource()方法的一些代码示例,展示了Files.asByteSource()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.asByteSource()方法的具体详情如下:
包路径:com.google.common.io.Files
类名称:Files
方法名:asByteSource

Files.asByteSource介绍

[英]Returns a new ByteSource for reading bytes from the given file.
[中]返回用于从给定文件读取字节的新ByteSource。

代码示例

代码示例来源:origin: google/guava

/**
 * 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);
}

代码示例来源:origin: google/guava

/**
 * 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);
}

代码示例来源:origin: google/guava

/**
 * 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();
}

代码示例来源:origin: google/guava

/**
 * 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);
}

代码示例来源:origin: google/guava

/**
 * 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));
}

代码示例来源:origin: google/j2objc

/**
 * 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);
}

代码示例来源:origin: apache/incubator-druid

/**
 * 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);
}

代码示例来源:origin: google/j2objc

/**
 * 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);
}

代码示例来源:origin: google/j2objc

/**
 * 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();
}

代码示例来源:origin: google/j2objc

/**
 * 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);
}

代码示例来源:origin: google/guava

/**
 * 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);
}

代码示例来源:origin: google/guava

/**
 * 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));
}

代码示例来源:origin: wildfly/wildfly

/**
 * 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);
}

代码示例来源:origin: apache/incubator-druid

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);
 }
}

代码示例来源:origin: apache/incubator-druid

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());
}

代码示例来源:origin: google/guava

@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);
}

代码示例来源:origin: apache/incubator-druid

/**
 * 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);
}

代码示例来源:origin: google/guava

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()));
}

代码示例来源:origin: google/guava

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));
}

代码示例来源:origin: google/guava

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)));
}

相关文章