APISonar


com.google.common.io.Files.asByteSource

> com > google > common > io > Files > asByteSource
com APIs google APIs common APIs io APIs Files APIs asByteSource APIs

Example 1
public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception {
        final File temp1 = File.createTempFile("bael", "dung1");
        final File temp2 = File.createTempFile("bael", "dung2");

        assertThat(Files.asByteSource(temp1)).hasSize(0).hasSameContentAs(Files.asByteSource(temp2));
    }
Example 2
public static String shaSum256(File file) {
    try {
      return Files.asByteSource(file).hash(Hashing.sha256()).toString();
    } catch (IOException e) {
      throw new RuntimeException(String.format("Failed to calculate shaSum256 of %s", file));
    }
  }
Example 3
public final void givenUsingGuava_whenConvertingFileToInputStream_thenCorrect() throws IOException {
        final File initialFile = new File("src/main/resources/sample.txt");
        final InputStream targetStream = Files.asByteSource(initialFile).openStream();

        IOUtils.closeQuietly(targetStream);
    }
Example 4
private static void copyContent(String classpathElement, WritableByteChannel outputChannel)
      throws IOException {
    final File classpathElementFile = new File(classpathElement);
    if (classpathElementFile.isDirectory()) {
      ZipFiles.zipDirectory(classpathElementFile, Channels.newOutputStream(outputChannel));
    } else {
      Files.asByteSource(classpathElementFile).copyTo(Channels.newOutputStream(outputChannel));
    }
  }
Example 5
public void whenReadUsingByteSource_thenRead() throws IOException {
        final String expectedValue = "Hello world";
        final File file = new File("src/test/resources/test1.in");

        final ByteSource source = Files.asByteSource(file);

        final byte[] result = source.read();
        assertEquals(expectedValue, new String(result));
    }
Example 6
private static CharSource pathReader(Path path)
    {
        return Files.asByteSource(path.toFile()).asCharSource(Charsets.UTF_8);
    }
Example 7
public static FileUtils.FileCopyResult gunzip(final File pulledFile, File outFile) throws IOException
  {
    return gunzip(Files.asByteSource(pulledFile), outFile);
  }
Example 8
private void runDecrypt() throws IOException {
    try (InputStream in = Files.asByteSource(input.toFile()).openBufferedStream();
        InputStream ghostDecoder = Ghostryde.decoder(in, rdeStagingDecryptionKey.get())) {
      if (output == null) {
        ByteStreams.copy(ghostDecoder, System.out);
      } else {
        Path outFile =
            output.toFile().isDirectory()
                ? output.resolve(input.getFileName() + ".decrypt")
                : output;
        Files.asByteSink(outFile.toFile()).writeFrom(ghostDecoder);
      }
    }
  }
Example 9
public static FileUtils.FileCopyResult gzip(final File inFile, final File outFile, Predicate<Throwable> shouldRetry)
      throws IOException
  {
    gzip(Files.asByteSource(inFile), Files.asByteSink(outFile), shouldRetry);
    return new FileUtils.FileCopyResult(outFile);
  }
Example 10
public void testFailParseOnKeyMissing() throws Exception
  {
    final UriExtractionNamespace.JSONFlatDataParser parser = new UriExtractionNamespace.JSONFlatDataParser(
        MAPPER,
        "keyWHOOPS",
        "val"
    );
    final Map<String, String> map = new HashMap<>();

    expectedException.expect(NullPointerException.class);
    expectedException.expectMessage("Key column [keyWHOOPS] missing data in line");

    new MapPopulator<>(parser.getParser()).populate(Files.asByteSource(tmpFile), map);
  }
Example 11
void configureLogging() throws IOException {
    ByteSource baseConfig = (configFile != null)
        ? Files.asByteSource(configFile.toFile())
        : DEFAULT_LOG_CONFIG;
    if (logLevel != null) {
      configLines.add(".level = " + logLevel);
    }
    // Add an extra leading newline in case base properties file does not end in a newline.
    String customProperties = "\n" + Joiner.on('\n').join(configLines);
    ByteSource logConfig =
        ByteSource.concat(baseConfig, ByteSource.wrap(customProperties.getBytes()));
    try (InputStream input = logConfig.openStream()) {
      LogManager.getLogManager().readConfiguration(input);
    }
  }
Example 12
private HashCode getFileHashCode(Path path) throws IOException {
    // TODO(simons): Should be this.projectFilesystem.computeSha1(path);
    File file = this.projectFilesystem.resolve(path).toFile();
    ByteSource source = Files.asByteSource(file);
    return source.hash(Hashing.sha1());
  }
Example 13
public CloudFilesObject(final String basePath, final File file, final String region, final String container)
  {
    this(region, container);
    this.file = file;
    ByteSource byteSource = Files.asByteSource(file);
    this.payload = Payloads.newByteSourcePayload(byteSource);
    this.path = CloudFilesUtils.buildCloudFilesPath(basePath, file.getName());
  }
Example 14
public static ByteSource byteSource(File file, CompressionMode compression) {
        CompressionMode effectiveMode = compression.getEffectiveCompressionMode(file.getName());
        ByteSource source = Files.asByteSource(file);
        if (!effectiveMode.equals(CompressionMode.NONE)) {
            source = new CompressedByteSource(source, effectiveMode.getCompressorName());
        }
        return source;
    }
Example 15
public void sanitizeWorkingDirectoryWhenBuildingAssembly() throws IOException {
    BuildTarget target = BuildTargetFactory.newInstance("//:simple_assembly#default,static");
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    ProcessResult processResult = workspace.runBuckBuild(target.getFullyQualifiedName());
    processResult.assertSuccess();
    Path lib =
        workspace.getPath(BuildTargets.getGenPath(filesystem, target, "%s/libsimple_assembly.a"));
    String contents = Files.asByteSource(lib.toFile()).asCharSource(Charsets.ISO_8859_1).read();
    assertFalse(lib.toString(), contents.contains(tmp.getRoot().toString()));
  }