aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/android/tools/r8/utils/DirectoryOutputSink.java
blob: 56e0ea1498993e15ba7c1672acaa661bf00c9195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DirectoryOutputSink extends FileSystemOutputSink {

  private final Path outputDirectory;

  public DirectoryOutputSink(Path outputDirectory, InternalOptions options) throws IOException {
    super(options);
    this.outputDirectory = outputDirectory;
    cleanUpOutputDirectory();
  }

  private void cleanUpOutputDirectory() throws IOException {
    if (getOutputMode() == OutputMode.Indexed) {
      try (Stream<Path> filesInDir = Files.list(outputDirectory)) {
        for (Path path : filesInDir.collect(Collectors.toList())) {
          if (FileUtils.isClassesDexFile(path)) {
            Files.delete(path);
          }
        }
      }
    }
  }

  @Override
  public void writeDexFile(byte[] contents, Set<String> classDescriptors, int fileId)
      throws IOException {
    Path target = outputDirectory.resolve(getOutputFileName(fileId));
    Files.createDirectories(target.getParent());
    writeToFile(target, null, contents);
  }

  @Override
  public void writeDexFile(byte[] contents, Set<String> classDescriptors, String primaryClassName)
      throws IOException {
    Path target = outputDirectory.resolve(getOutputFileName(primaryClassName));
    Files.createDirectories(target.getParent());
    writeToFile(target, null, contents);
  }

  @Override
  public void close() throws IOException {
    // Intentionally left empty.
  }
}