aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/android/tools/r8/utils/FileSystemOutputSink.java
blob: c0aef0a6ba995835c00bce88f45a6d554bf3893a (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 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 com.android.tools.r8.OutputSink;
import com.google.common.io.Closer;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public abstract class FileSystemOutputSink implements OutputSink {

  private final InternalOptions options;

  protected FileSystemOutputSink(InternalOptions options) {
    this.options = options;
  }

  public static FileSystemOutputSink create(Path outputPath, InternalOptions options)
      throws IOException {
    if (FileUtils.isArchive(outputPath)) {
      return new ZipFileOutputSink(outputPath, options);
    } else {
      return new DirectoryOutputSink(outputPath, options);
    }
  }

  String getOutputFileName(int index) {
    return index == 0 ? "classes.dex" : ("classes" + (index + 1) + FileUtils.DEX_EXTENSION);
  }

  String getOutputFileName(String classDescriptor) throws IOException {
    assert classDescriptor != null && DescriptorUtils.isClassDescriptor(classDescriptor);
    return DescriptorUtils.getClassBinaryNameFromDescriptor(classDescriptor)
        + FileUtils.DEX_EXTENSION;
  }


  @Override
  public void writePrintUsedInformation(byte[] contents) throws IOException {
    writeToFile(options.proguardConfiguration.getPrintUsageFile(), System.out, contents);
  }

  @Override
  public void writeProguardMapFile(byte[] contents) throws IOException {
    if (options.proguardConfiguration.getPrintMappingFile() != null) {
      writeToFile(options.proguardConfiguration.getPrintMappingFile(), System.out, contents);
    }
    if (options.proguardMapOutput != null) {
      writeToFile(options.proguardMapOutput, System.out, contents);
    }
  }

  @Override
  public void writeProguardSeedsFile(byte[] contents) throws IOException {
    writeToFile(options.proguardConfiguration.getSeedFile(), System.out, contents);
  }

  @Override
  public void writeMainDexListFile(byte[] contents) throws IOException {
    writeToFile(options.printMainDexListFile, System.out, contents);
  }

  protected void writeToFile(Path output, OutputStream defValue, byte[] contents)
      throws IOException {
    try (Closer closer = Closer.create()) {
      OutputStream outputStream =
          FileUtils.openPathWithDefault(
              closer,
              output,
              defValue,
              StandardOpenOption.CREATE,
              StandardOpenOption.TRUNCATE_EXISTING,
              StandardOpenOption.WRITE);
      outputStream.write(contents);
    }
  }

  protected OutputMode getOutputMode() {
    return options.outputMode;
  }
}