类com.google.common.io.MoreFiles源码实例Demo

下面列出了怎么用com.google.common.io.MoreFiles的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: airsonic-advanced   文件: CoverArtController.java
/**
 * Returns an input stream to the image in the given file.  If the file is an audio file,
 * the embedded album art is returned. In addition returns the mime type
 */
private Pair<InputStream, String> getImageInputStreamWithType(Path file) throws IOException {
    InputStream is;
    String mimeType;
    if (jaudiotaggerParser.isApplicable(file)) {
        LOG.trace("Using Jaudio Tagger for reading artwork from {}", file);
        MediaFile mediaFile = mediaFileService.getMediaFile(file);
        try {
            LOG.trace("Reading artwork from file {}", mediaFile);
            Artwork artwork = jaudiotaggerParser.getArtwork(mediaFile);
            is = new ByteArrayInputStream(artwork.getBinaryData());
            mimeType = artwork.getMimeType();
        } catch (Exception e) {
            LOG.debug("Could not read artwork from file {}", mediaFile);
            throw new RuntimeException(e);
        }
    } else {
        is = new BufferedInputStream(Files.newInputStream(file));
        mimeType = StringUtil.getMimeType(MoreFiles.getFileExtension(file));
    }
    return Pair.of(is, mimeType);
}
 
源代码2 项目: size-analyzer   文件: WebpAutoFix.java
/**
 * Writes the provided bytes into a new file path, based on the initial image's filepath, but with
 * a .webp extension.
 */
public void apply() {
  Path newFilePath =
      filePath.resolveSibling(MoreFiles.getNameWithoutExtension(filePath) + ".webp");
  try (InputStream inputStream = new FileInputStream(new File(filePath.toString()))) {
    CountingInputStream countingStream = new CountingInputStream(inputStream);
    BufferedImage bufferedImage = WebpSuggester.safelyParseImage(countingStream);

    long oldSize = countingStream.getCount();
    byte[] webpBytes = webpConverter.encodeLosslessWebp(bufferedImage);
    Files.write(newFilePath, webpBytes);
    Files.delete(filePath);
  } catch (IOException | ImageReadException e) {
    throw new RuntimeException(e);
  }
}
 
源代码3 项目: size-analyzer   文件: WebpAutoFixTest.java
@Test
public void appliesFix() throws Exception {
  FakeWebpConverter fakeConverter = new FakeWebpConverter();
  byte[] bytes = new byte[] {0, 1, 2, 4, 8};
  fakeConverter.setFakeData(bytes);
  File pngFile = File.createTempFile("foo", ".png");
  Files.copy(getTestDataFile("webp/drawing.png"), pngFile);
  Path pngPath = pngFile.toPath();
  new WebpAutoFix(pngPath, fakeConverter).apply();

  File webpFile =
      new File(
          pngPath
              .resolveSibling(MoreFiles.getNameWithoutExtension(pngPath) + ".webp")
              .toString());
  assertThat(pngFile.exists()).isFalse();
  assertThat(webpFile.exists()).isTrue();
  assertThat(Files.asByteSource(webpFile).read()).isEqualTo(bytes);
  webpFile.delete();
}
 
源代码4 项目: j2cl   文件: TranspilerTester.java
public TranspileResult assertOutputFilesAreSame(TranspileResult other) throws IOException {
  List<Path> actualPaths =
      ImmutableList.copyOf(MoreFiles.fileTraverser().depthFirstPreOrder(outputPath));
  List<Path> expectedPaths =
      ImmutableList.copyOf(MoreFiles.fileTraverser().depthFirstPreOrder(other.outputPath));

  // Compare simple names.
  assertThat(toFileNames(actualPaths))
      .containsExactlyElementsIn(toFileNames(expectedPaths))
      .inOrder();

  // Compare file contents.
  for (int i = 0; i < expectedPaths.size(); i++) {
    Path expectedPath = expectedPaths.get(i);
    Path actualPath = actualPaths.get(i);
    if (Files.isDirectory(expectedPath)) {
      assertThat(Files.isDirectory(actualPath)).isTrue();
    } else {
      assertThat(Files.readAllLines(actualPath))
          .containsExactlyElementsIn(Files.readAllLines(expectedPath))
          .inOrder();
    }
  }

  return this;
}
 
源代码5 项目: j2cl   文件: GwtIncompatibleStripper.java
/** Preprocess all provided files and put them to provided output path. */
private static void preprocessFiles(List<FileInfo> fileInfos, Path output, Problems problems) {
  for (FileInfo fileInfo : fileInfos) {
    String processedFileContent;
    try {
      String fileContent =
          MoreFiles.asCharSource(Paths.get(fileInfo.sourcePath()), StandardCharsets.UTF_8).read();
      processedFileContent = processFile(fileContent);
    } catch (IOException e) {
      problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
      return;
    }

    // Write the processed file to output
    J2clUtils.writeToFile(
        output.resolve(fileInfo.originalPath()), processedFileContent, problems);
  }
}
 
源代码6 项目: turbine   文件: MainTest.java
@Test
public void packageInfo() throws IOException {
  Path src = temporaryFolder.newFile("package-info.jar").toPath();
  MoreFiles.asCharSink(src, UTF_8).write("@Deprecated package test;");

  Path output = temporaryFolder.newFile("output.jar").toPath();

  Main.compile(
      optionsWithBootclasspath()
          .setSources(ImmutableList.of(src.toString()))
          .setOutput(output.toString())
          .build());

  Map<String, byte[]> data = readJar(output);
  assertThat(data.keySet()).containsExactly("test/package-info.class");
}
 
源代码7 项目: turbine   文件: MainTest.java
@Test
public void emptyBootClassPath() throws IOException {
  Path src = temporaryFolder.newFolder().toPath().resolve("java/lang/Object.java");
  Files.createDirectories(src.getParent());
  MoreFiles.asCharSink(src, UTF_8).write("package java.lang; public class Object {}");

  Path output = temporaryFolder.newFile("output.jar").toPath();

  Main.compile(
      TurbineOptions.builder()
          .setSources(ImmutableList.of(src.toString()))
          .setOutput(output.toString())
          .build());

  Map<String, byte[]> data = readJar(output);
  assertThat(data.keySet()).containsExactly("java/lang/Object.class");
}
 
源代码8 项目: turbine   文件: MainTest.java
@Test
public void emptyBootClassPath_noJavaLang() throws IOException {
  Path src = temporaryFolder.newFile("Test.java").toPath();
  MoreFiles.asCharSink(src, UTF_8).write("public class Test {}");

  Path output = temporaryFolder.newFile("output.jar").toPath();

  try {
    Main.compile(
        TurbineOptions.builder()
            .setSources(ImmutableList.of(src.toString()))
            .setOutput(output.toString())
            .build());
    fail();
  } catch (IllegalArgumentException expected) {
    assertThat(expected).hasMessageThat().contains("java.lang");
  }
}
 
源代码9 项目: turbine   文件: MainTest.java
@Test
public void classGeneration() throws IOException {
  Path src = temporaryFolder.newFile("package-info.jar").toPath();
  MoreFiles.asCharSink(src, UTF_8).write("@Deprecated package test;");
  File resources = temporaryFolder.newFile("resources.jar");
  Main.compile(
      optionsWithBootclasspath()
          .setProcessors(ImmutableList.of(ClassGeneratingProcessor.class.getName()))
          .setSources(ImmutableList.of(src.toString()))
          .setResourceOutput(resources.toString())
          .build());
  try (JarFile jarFile = new JarFile(resources);
      Stream<JarEntry> entries = jarFile.stream()) {
    assertThat(entries.map(JarEntry::getName)).containsExactly("g/Gen.class");
  }
}
 
源代码10 项目: pulsar   文件: ProcessRuntimeTest.java
@Test
public void testJavaConstructorWithDeps() throws Exception {
    InstanceConfig config = createJavaInstanceConfig(FunctionDetails.Runtime.JAVA);

    Path dir = Files.createTempDirectory("test-empty-dir");
    assertTrue(Files.exists(dir));
    try {
        int numFiles = 3;
        for (int i = 0; i < numFiles; i++) {
            Path file = Files.createFile(Paths.get(dir.toAbsolutePath().toString(), "file-" + i));
            assertTrue(Files.exists(file));
        }

        factory = createProcessRuntimeFactory(dir.toAbsolutePath().toString());

        verifyJavaInstance(config, dir);
    } finally {
        MoreFiles.deleteRecursively(dir, RecursiveDeleteOption.ALLOW_INSECURE);
    }
}
 
源代码11 项目: pulsar   文件: FunctionActioner.java
private void cleanupFunctionFiles(FunctionRuntimeInfo functionRuntimeInfo) {
    Function.Instance instance = functionRuntimeInfo.getFunctionInstance();
    FunctionMetaData functionMetaData = instance.getFunctionMetaData();
    // clean up function package
    File pkgDir = new File(
            workerConfig.getDownloadDirectory(),
            getDownloadPackagePath(functionMetaData, instance.getInstanceId()));

    if (pkgDir.exists()) {
        try {
            MoreFiles.deleteRecursively(
                    Paths.get(pkgDir.toURI()), RecursiveDeleteOption.ALLOW_INSECURE);
        } catch (IOException e) {
            log.warn("Failed to delete package for function: {}",
                    FunctionCommon.getFullyQualifiedName(functionMetaData.getFunctionDetails()), e);
        }
    }
}
 
源代码12 项目: nomulus   文件: SetupOteCommand.java
/** Run any pre-execute command checks */
@Override
protected void init() throws Exception {
  checkArgument(
      certFile == null ^ certHash == null,
      "Must specify exactly one of client certificate file or client certificate hash.");

  password = passwordGenerator.createString(PASSWORD_LENGTH);
  oteAccountBuilder =
      OteAccountBuilder.forClientId(registrar)
          .addContact(email)
          .setPassword(password)
          .setIpAllowList(ipAllowList)
          .setReplaceExisting(overwrite);

  if (certFile != null) {
    String asciiCert = MoreFiles.asCharSource(certFile, US_ASCII).read();
    // Don't wait for create_registrar to fail if it's a bad certificate file.
    loadCertificate(asciiCert);
    oteAccountBuilder.setCertificate(asciiCert, clock.nowUtc());
  }

  if (certHash != null) {
    oteAccountBuilder.setCertificateHash(certHash);
  }
}
 
源代码13 项目: buck   文件: OutputsMaterializer.java
@Override
public WritableByteChannel getOutputChannel(Path path, boolean executable) throws IOException {
  path = root.resolve(path);
  MoreFiles.createParentDirectories(path);

  FileChannel channel =
      FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
  try {
    // Creating the FileOutputStream makes the file so we can now set it executable.
    setExecutable(executable, path);
  } catch (Exception e) {
    channel.close();
    throw e;
  }
  return channel;
}
 
源代码14 项目: besu   文件: FastSyncDownloader.java
public void deleteFastSyncState() {
  // Make sure downloader is stopped before we start cleaning up its dependencies
  worldStateDownloader.cancel();
  try {
    taskCollection.close();
    if (fastSyncDataDirectory.toFile().exists()) {
      // Clean up this data for now (until fast sync resume functionality is in place)
      MoreFiles.deleteRecursively(fastSyncDataDirectory, RecursiveDeleteOption.ALLOW_INSECURE);
    }
  } catch (final IOException e) {
    LOG.error("Unable to clean up fast sync state", e);
  }
}
 
源代码15 项目: besu   文件: WorldStateDownloaderBenchmark.java
@TearDown(Level.Invocation)
public void tearDown() throws Exception {
  ethProtocolManager.stop();
  ethProtocolManager.awaitStop();
  pendingRequests.close();
  storageProvider.close();
  MoreFiles.deleteRecursively(tempDir, RecursiveDeleteOption.ALLOW_INSECURE);
}
 
源代码16 项目: besu   文件: OrionTestHarness.java
public void close() {
  stop();
  try {
    MoreFiles.deleteRecursively(config.workDir(), RecursiveDeleteOption.ALLOW_INSECURE);
  } catch (final IOException e) {
    LOG.info("Failed to clean up temporary file: {}", config.workDir(), e);
  }
}
 
源代码17 项目: besu   文件: BesuNode.java
@Override
@SuppressWarnings("UnstableApiUsage")
public void close() {
  stop();
  try {
    MoreFiles.deleteRecursively(homeDirectory, RecursiveDeleteOption.ALLOW_INSECURE);
  } catch (final IOException e) {
    LOG.info("Failed to clean up temporary file: {}", homeDirectory, e);
  }
}
 
源代码18 项目: judgels   文件: LocalFileSystem.java
@Override
public void writeByteArrayToFile(Path filePath, byte[] content) {
    InputStream stream = new ByteArrayInputStream(content);
    try {
        MoreFiles.createParentDirectories(baseDir.resolve(filePath));
        Files.copy(stream, baseDir.resolve(filePath), StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
源代码19 项目: teku   文件: FileUtil.java
public static void recursivelyDeleteDirectories(final List<File> directories) {
  for (File tmpDirectory : directories) {
    try {
      MoreFiles.deleteRecursively(tmpDirectory.toPath(), RecursiveDeleteOption.ALLOW_INSECURE);
    } catch (IOException e) {
      LOG.error("Failed to delete directory: " + tmpDirectory.getAbsolutePath(), e);
      throw new RuntimeException(e);
    }
  }
}
 
源代码20 项目: lemminx   文件: AbstractCacheBasedTest.java
@AfterEach
public final void clearCache() throws IOException {
	if (Files.exists(TEST_WORK_DIRECTORY)) {
		MoreFiles.deleteDirectoryContents(TEST_WORK_DIRECTORY,RecursiveDeleteOption.ALLOW_INSECURE);
	}
	System.clearProperty(FilesUtils.LEMMINX_WORKDIR_KEY);
	FilesUtils.resetDeployPath();
}
 
源代码21 项目: cloud-opensource-java   文件: DashboardTest.java
@AfterClass
public static void cleanUp() {
  try {
    // Mac's APFS fails with InsecureRecursiveDeleteException without ALLOW_INSECURE.
    // Still safe as this test does not use symbolic links
    if (outputDirectory != null) {
      MoreFiles.deleteRecursively(outputDirectory, RecursiveDeleteOption.ALLOW_INSECURE);
    }
  } catch (IOException ex) {
    // no big deal
  }
}
 
源代码22 项目: bundletool   文件: GetDeviceSpecCommand.java
public GetDeviceSpecCommand build() {
  GetDeviceSpecCommand command = autoBuild();

  if (!JSON_EXTENSION.equals(MoreFiles.getFileExtension(command.getOutputPath()))) {
    throw InvalidCommandException.builder()
        .withInternalMessage(
            "Flag --output should be the path where to generate the device spec file. "
                + "Its extension must be '.json'.")
        .build();
  }

  return command;
}
 
源代码23 项目: bundletool   文件: DeviceSpecParser.java
private static DeviceSpec parseDeviceSpecInternal(Path deviceSpecFile, boolean canSkipFields) {
  if (!JSON_EXTENSION.equals(MoreFiles.getFileExtension(deviceSpecFile))) {
    throw InvalidDeviceSpecException.builder()
        .withUserMessage("Expected .json extension for the device spec file.")
        .build();
  }
  try (Reader deviceSpecReader = BufferedIo.reader(deviceSpecFile)) {
    return parseDeviceSpecInternal(deviceSpecReader, canSkipFields);
  } catch (IOException e) {
    throw new UncheckedIOException(
        String.format("Error while reading the device spec file '%s'.", deviceSpecFile), e);
  }
}
 
源代码24 项目: bundletool   文件: Flag.java
private static String readPasswordFromFile(Path passwordFile) {
  try {
    return MoreFiles.asCharSource(passwordFile, UTF_8).readFirstLine();
  } catch (IOException e) {
    throw new UncheckedIOException(
        String.format("Unable to read password from file '%s'.", passwordFile), e);
  }
}
 
源代码25 项目: j2cl   文件: NativeJavaScriptFile.java
/**
 * Given a list of native files, return a map of file paths to NativeJavaScriptFile objects of the
 * form:
 *
 * <p>/com/google/example/nativejsfile1 => NativeJavaScriptFile
 *
 * <p>/com/google/example/nativejsfile2 => NativeJavaScriptFile
 */
public static Map<String, NativeJavaScriptFile> getMap(List<FileInfo> files, Problems problems) {
  Map<String, NativeJavaScriptFile> loadedFilesByPath = new LinkedHashMap<>();
  for (FileInfo file : files) {
    try {
      String content =
          MoreFiles.asCharSource(Paths.get(file.sourcePath()), StandardCharsets.UTF_8).read();
      NativeJavaScriptFile nativeFile = new NativeJavaScriptFile(file.targetPath(), content);
      loadedFilesByPath.put(nativeFile.getRelativePathWithoutExtension(), nativeFile);
    } catch (IOException e) {
      problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
    }
  }
  return loadedFilesByPath;
}
 
源代码26 项目: mojito   文件: Files.java
public static void deleteRecursivelyIfExists(Path path) {
    try {
        if (path.toFile().exists()) {
            MoreFiles.deleteRecursively(path, RecursiveDeleteOption.ALLOW_INSECURE);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
@VisibleForTesting
static void deleteCopiedPython(String pythonExePath) {
  // The path returned from gcloud points to the "python.exe" binary. Delete it from the path.
  String pythonHome = pythonExePath.replaceAll("[pP][yY][tT][hH][oO][nN]\\.[eE][xX][eE]$", "");
  boolean endsWithPythonExe = !pythonHome.equals(pythonExePath);

  if (endsWithPythonExe) { // just to be safe
    try {
      MoreFiles.deleteRecursively(Paths.get(pythonHome), RecursiveDeleteOption.ALLOW_INSECURE);
    } catch (IOException e) {
      // not critical to remove a temp directory
    }
  }
}
 
源代码28 项目: turbine   文件: MainTest.java
@Test
public void moduleInfos() throws IOException {
  if (Double.parseDouble(JAVA_CLASS_VERSION.value()) < 53) {
    // only run on JDK 9 and later
    return;
  }

  Path srcjar = temporaryFolder.newFile("lib.srcjar").toPath();
  try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(srcjar))) {
    jos.putNextEntry(new JarEntry("module-info.java"));
    jos.write("module foo {}".getBytes(UTF_8));
    jos.putNextEntry(new JarEntry("bar/module-info.java"));
    jos.write("module bar {}".getBytes(UTF_8));
  }

  Path src = temporaryFolder.newFile("module-info.java").toPath();
  MoreFiles.asCharSink(src, UTF_8).write("module baz {}");

  Path output = temporaryFolder.newFile("output.jar").toPath();

  Main.compile(
      TurbineOptions.builder()
          .setRelease("9")
          .setSources(ImmutableList.of(src.toString()))
          .setSourceJars(ImmutableList.of(srcjar.toString()))
          .setOutput(output.toString())
          .build());

  Map<String, byte[]> data = readJar(output);
  assertThat(data.keySet())
      .containsExactly("foo/module-info.class", "bar/module-info.class", "baz/module-info.class");
}
 
源代码29 项目: turbine   文件: MainTest.java
@Test
public void usage() throws IOException {
  Path src = temporaryFolder.newFile("Test.java").toPath();
  MoreFiles.asCharSink(src, UTF_8).write("public class Test {}");

  try {
    Main.compile(optionsWithBootclasspath().setSources(ImmutableList.of(src.toString())).build());
    fail();
  } catch (UsageException expected) {
    assertThat(expected)
        .hasMessageThat()
        .contains("at least one of --output, --gensrc_output, or --resource_output is required");
  }
}
 
源代码30 项目: turbine   文件: MainTest.java
@Test
public void testManifestProto() throws IOException {
  Path src = temporaryFolder.newFile("Foo.java").toPath();
  MoreFiles.asCharSink(src, UTF_8).write("package f; @Deprecated class Foo {}");

  Path output = temporaryFolder.newFile("output.jar").toPath();
  Path gensrcOutput = temporaryFolder.newFile("gensrcOutput.jar").toPath();
  Path manifestProtoOutput = temporaryFolder.newFile("manifest.proto").toPath();

  Main.compile(
      optionsWithBootclasspath()
          .setSources(ImmutableList.of(src.toString()))
          .setTargetLabel("//foo:foo")
          .setInjectingRuleKind("foo_library")
          .setOutput(output.toString())
          .setGensrcOutput(gensrcOutput.toString())
          .setOutputManifest(manifestProtoOutput.toString())
          .setProcessors(ImmutableList.of(SourceGeneratingProcessor.class.getName()))
          .build());

  assertThat(readManifestProto(manifestProtoOutput))
      .isEqualTo(
          ManifestProto.Manifest.newBuilder()
              .addCompilationUnit(
                  ManifestProto.CompilationUnit.newBuilder()
                      .setPkg("f")
                      .addTopLevel("Foo")
                      .setPath(src.toString())
                      .setGeneratedByAnnotationProcessor(false)
                      .build())
              .addCompilationUnit(
                  ManifestProto.CompilationUnit.newBuilder()
                      .setPkg("g")
                      .addTopLevel("Gen")
                      .setPath("g/Gen.java")
                      .setGeneratedByAnnotationProcessor(true)
                      .build())
              .build());
}