aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/android/ziputils/FileSystem.java
blob: 2c749ac69fd8c559db43660e2ccef2f8f5c980a8 (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
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.android.ziputils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

/**
 * A simple file system abstraction, for testing. This library doesn't itself open files.
 * Clients are responsible for opening files and pass in file channels to the API.
 * This class is currently here to be able to use an common abstraction for testing the
 * library, and tools implemented using this library. This class may be removed in the future.
 */
class FileSystem  {

  protected static FileSystem fileSystem;

  /**
   * Returns the configured file system implementation. If no filesystem is configured, and
   * instance of this class is created and returned. The default filesystem implementation is
   * a simple wrapper of the standard Java file system.
   */
  public static FileSystem fileSystem() {
    if (fileSystem == null) {
      fileSystem = new FileSystem();
    }
    return fileSystem;
  }

  /**
   * Opens a file for reading, and returns a file channel.
   *
   * @param filename name of file to open.
   * @return file channel open for reading.
   * @throws java.io.IOException
   */
  public FileChannel getInputChannel(String filename) throws IOException {
    return new FileInputStream(filename).getChannel();
  }

  /**
   * Opens a file for writing, and returns a file channel.
   *
   * @param filename name of file to open.
   * @param append whether to open file in append mode.
   * @return  file channel open for write.
   * @throws java.io.IOException
   */
  public FileChannel getOutputChannel(String filename, boolean append) throws IOException {
    return new FileOutputStream(filename, append).getChannel();
  }

  /**
   * Opens a file for reading, and returns an input stream.
   *
   * @param filename name of file to open.
   * @return input stream reading from the specified file.
   * @throws java.io.IOException
   */
  public InputStream getInputStream(String filename) throws IOException {
    return new FileInputStream(filename);
  }
}