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

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

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

Files.asByteSink介绍

[英]Returns a new ByteSink for writing bytes to the given file. The given modescontrol how the file is opened for writing. When no mode is provided, the file will be truncated before writing. When the FileWriteMode#APPEND mode is provided, writes will append to the end of the file without truncating it.
[中]返回用于将字节写入给定文件的新ByteSink。给定的模式控制如何打开文件进行写入。当没有提供模式时,文件将在写入之前被截断。当提供FileWriteMode#APPEND模式时,写入操作将附加到文件末尾,而不会截断它。

代码示例

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

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

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

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

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

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

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

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

代码示例来源: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 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);
}

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

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

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

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

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

/**
 * 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: apache/incubator-druid

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

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

/**
 * 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: 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: apache/incubator-druid

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

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

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

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

boolean runFailed = true;
final ByteSink logSink = Files.asByteSink(logFile, FileWriteMode.APPEND);

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

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")) {

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

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

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

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

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

}.copyTo(Files.asByteSink(new File(persistTmpDir, file)));

代码示例来源:origin: syncthing/syncthing-android

Files.asByteSink(outFile).writeFrom(inputStream);
  mCopied++;
} catch (FileNotFoundException e) {

相关文章