1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.util.io;
18 
19 import androidx.annotation.NonNull;
20 
21 import com.android.systemui.dagger.SysUISingleton;
22 
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.LinkOption;
27 import java.nio.file.OpenOption;
28 import java.nio.file.Path;
29 import java.nio.file.attribute.BasicFileAttributes;
30 import java.util.stream.Stream;
31 
32 import javax.inject.Inject;
33 
34 /**
35  * Wrapper around {@link java.nio.file.Files} that can be mocked in tests.
36  */
37 @SysUISingleton
38 public class Files {
39     @Inject
Files()40     public Files() { }
41 
42     /** See {@link java.nio.file.Files#newBufferedWriter} */
newBufferedWriter(Path path, OpenOption... options)43     public BufferedWriter newBufferedWriter(Path path, OpenOption... options) throws IOException {
44         return java.nio.file.Files.newBufferedWriter(path, StandardCharsets.UTF_8, options);
45     }
46 
47     /** See {@link java.nio.file.Files#lines} */
lines(Path path)48     public Stream<String> lines(Path path) throws IOException {
49         return java.nio.file.Files.lines(path);
50     }
51 
52     /** See {@link java.nio.file.Files#readAttributes} */
readAttributes( @onNull Path path, @NonNull Class<A> type, @NonNull LinkOption... options)53     public <A extends BasicFileAttributes> A readAttributes(
54             @NonNull Path path,
55             @NonNull Class<A> type,
56             @NonNull LinkOption... options) throws IOException {
57         return java.nio.file.Files.readAttributes(path, type, options);
58     }
59 }
60