com.google.common.io.MoreFiles Java Examples

The following examples show how to use com.google.common.io.MoreFiles. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TranspilerTester.java    From j2cl with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: OutputsMaterializer.java    From buck with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: CoverArtController.java    From airsonic-advanced with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: WebpAutoFix.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example #5
Source File: WebpAutoFixTest.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #6
Source File: SetupOteCommand.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/** 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);
  }
}
 
Example #7
Source File: FunctionActioner.java    From pulsar with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
}
 
Example #8
Source File: ProcessRuntimeTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #9
Source File: MainTest.java    From turbine with Apache License 2.0 6 votes vote down vote up
@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");
  }
}
 
Example #10
Source File: MainTest.java    From turbine with Apache License 2.0 6 votes vote down vote up
@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");
  }
}
 
Example #11
Source File: MainTest.java    From turbine with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #12
Source File: MainTest.java    From turbine with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #13
Source File: GwtIncompatibleStripper.java    From j2cl with Apache License 2.0 6 votes vote down vote up
/** 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);
  }
}
 
Example #14
Source File: ProcessRuntimeTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testJavaConstructorWithEmptyDir() throws Exception {
    InstanceConfig config = createJavaInstanceConfig(FunctionDetails.Runtime.JAVA);

    Path dir = Files.createTempDirectory("test-empty-dir");
    assertTrue(Files.exists(dir));
    try {
        factory = createProcessRuntimeFactory(dir.toAbsolutePath().toString());

        verifyJavaInstance(config, dir);
    } finally {
        MoreFiles.deleteRecursively(dir, RecursiveDeleteOption.ALLOW_INSECURE);
    }
}
 
Example #15
Source File: PGPKeysCacheTest.java    From pgpverify-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void fileAsCacheDirThrowException() throws IOException {

    File fileAsCachePath = new File(cachePath.toFile(), "file.tmp");
    MoreFiles.touch(fileAsCachePath.toPath());

    assertThat(fileAsCachePath)
            .exists()
            .isFile();

    assertThatCode(() -> new PGPKeysCache(fileAsCachePath, keysServerClients, true))
            .isExactlyInstanceOf(IOException.class)
            .hasMessageStartingWith("PGP keys cache path exist but is not a directory:");
}
 
Example #16
Source File: FileOperationProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the file or directory at the given path recursively, allowing insecure delete according
 * to {@code allowInsecureDelete}.
 *
 * @see RecursiveDeleteOption#ALLOW_INSECURE
 */
public void deleteRecursively(File file, boolean allowInsecureDelete) throws IOException {
  if (allowInsecureDelete) {
    MoreFiles.deleteRecursively(file.toPath(), RecursiveDeleteOption.ALLOW_INSECURE);
  } else {
    MoreFiles.deleteRecursively(file.toPath());
  }
}
 
Example #17
Source File: ExtensionClassExporter.java    From Mixin with MIT License 5 votes vote down vote up
public ExtensionClassExporter(MixinEnvironment env) {
    this.decompiler = this.initDecompiler(env, new File(Constants.DEBUG_OUTPUT_DIR, ExtensionClassExporter.EXPORT_JAVA_DIR));

    try {
        MoreFiles.deleteRecursively(this.classExportDir.toPath(), RecursiveDeleteOption.ALLOW_INSECURE);
    } catch (IOException ex) {
        ExtensionClassExporter.logger.debug("Error cleaning class output directory: {}", ex.getMessage());
    }
}
 
Example #18
Source File: RuntimeDecompiler.java    From Mixin with MIT License 5 votes vote down vote up
public RuntimeDecompiler(File outputPath) {
    this.outputPath = outputPath;
    if (this.outputPath.exists()) {
        try {
            MoreFiles.deleteRecursively(this.outputPath.toPath(), RecursiveDeleteOption.ALLOW_INSECURE);
        } catch (IOException ex) {
            this.logger.debug("Error cleaning output directory: {}", ex.getMessage());
        }
    }
}
 
Example #19
Source File: SimpleJavaLibraryBuilder.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static void cleanupDirectory(@Nullable Path directory) throws IOException {
  if (directory == null) {
    return;
  }

  if (Files.exists(directory)) {
    try {
      MoreFiles.deleteRecursively(directory, RecursiveDeleteOption.ALLOW_INSECURE);
    } catch (IOException e) {
      throw new IOException("Cannot clean '" + directory + "'", e);
    }
  }

  Files.createDirectories(directory);
}
 
Example #20
Source File: ClassPathBinderTest.java    From turbine with Apache License 2.0 5 votes vote down vote up
@Test
public void nonJarFile() throws Exception {
  Path lib = temporaryFolder.newFile("NOT_A_JAR").toPath();
  MoreFiles.asCharSink(lib, UTF_8).write("hello");

  try {
    ClassPathBinder.bindClasspath(ImmutableList.of(lib));
    fail();
  } catch (IOException e) {
    assertThat(e).hasMessageThat().contains("NOT_A_JAR");
  }
}
 
Example #21
Source File: MainTest.java    From turbine with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #22
Source File: MainTest.java    From turbine with Apache License 2.0 5 votes vote down vote up
@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");
  }
}
 
Example #23
Source File: SafeFilesTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@AfterAll
public void tearDown() {
  try {
    MoreFiles.deleteRecursively(tmpDir);
  } catch (IOException ex) {
    // ignore
  }
}
 
Example #24
Source File: MainTest.java    From turbine with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example #25
Source File: BuildCommandIntegrationTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void buckBuildAndCopyOutputFileIntoDirectoryWithBuildTargetThatSupportsIt()
    throws IOException {
  workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "build_into", tmp);
  workspace.setUp();

  Path outputDir = tmp.newFolder("into-output");
  assertEquals(0, MoreFiles.listFiles(outputDir).size());
  workspace.runBuckBuild("//:example", "--out", outputDir.toString()).assertSuccess();
  assertTrue(outputDir.toFile().isDirectory());
  assertEquals(1, MoreFiles.listFiles(outputDir).size());
  assertTrue(Files.isRegularFile(outputDir.resolve("example.jar")));
}
 
Example #26
Source File: Files.java    From mojito with Apache License 2.0 5 votes vote down vote up
public static void deleteRecursivelyIfExists(Path path) {
    try {
        if (path.toFile().exists()) {
            MoreFiles.deleteRecursively(path, RecursiveDeleteOption.ALLOW_INSECURE);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example #27
Source File: BuildCommandIntegrationTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void buckBuildAndCopyOutputDirectoryIntoDirectoryWithBuildTargetThatSupportsIt()
    throws IOException {
  workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "build_into", tmp);
  workspace.setUp();

  Path outputDir = tmp.newFolder("into-output");
  assertEquals(0, MoreFiles.listFiles(outputDir).size());
  workspace.runBuckBuild("//:example_dir", "--out", outputDir.toString()).assertSuccess();
  assertTrue(Files.isDirectory(outputDir));
  File[] files = outputDir.toFile().listFiles();
  assertEquals(2, MoreFiles.listFiles(outputDir).size());
  assertTrue(Files.isRegularFile(outputDir.resolve("example.jar")));
  assertTrue(Files.isRegularFile(outputDir.resolve("example-2.jar")));
}
 
Example #28
Source File: WindowsBundledPythonCopier.java    From appengine-plugins-core with Apache License 2.0 5 votes vote down vote up
@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
    }
  }
}
 
Example #29
Source File: NativeJavaScriptFile.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #30
Source File: ZipUtilsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@AfterAll
public void tearDown() {
  try {
    MoreFiles.deleteRecursively(tmpDir);
  } catch (IOException ex) {
    // ignore
  }
}