438 lines
16 KiB
Java
438 lines
16 KiB
Java
|
package com.google.common.io;
|
||
|
|
||
|
import com.airbnb.deeplinkdispatch.base.MatchIndex;
|
||
|
import com.google.common.base.Joiner;
|
||
|
import com.google.common.base.Optional;
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import com.google.common.base.Predicate;
|
||
|
import com.google.common.base.Splitter;
|
||
|
import com.google.common.collect.ImmutableList;
|
||
|
import com.google.common.collect.ImmutableSet;
|
||
|
import com.google.common.collect.Lists;
|
||
|
import com.google.common.graph.SuccessorsFunction;
|
||
|
import com.google.common.graph.Traverser;
|
||
|
import com.google.common.hash.HashCode;
|
||
|
import com.google.common.hash.HashFunction;
|
||
|
import java.io.BufferedReader;
|
||
|
import java.io.BufferedWriter;
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.FileNotFoundException;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStreamReader;
|
||
|
import java.io.OutputStream;
|
||
|
import java.io.OutputStreamWriter;
|
||
|
import java.io.RandomAccessFile;
|
||
|
import java.nio.MappedByteBuffer;
|
||
|
import java.nio.channels.FileChannel;
|
||
|
import java.nio.charset.Charset;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Arrays;
|
||
|
import java.util.Collections;
|
||
|
import java.util.List;
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public final class Files {
|
||
|
private static final SuccessorsFunction<File> FILE_TREE = new SuccessorsFunction<File>() { // from class: com.google.common.io.Files.2
|
||
|
@Override // com.google.common.graph.SuccessorsFunction
|
||
|
public Iterable<File> successors(File file) {
|
||
|
File[] listFiles;
|
||
|
if (file.isDirectory() && (listFiles = file.listFiles()) != null) {
|
||
|
return Collections.unmodifiableList(Arrays.asList(listFiles));
|
||
|
}
|
||
|
return ImmutableList.of();
|
||
|
}
|
||
|
};
|
||
|
private static final int TEMP_DIR_ATTEMPTS = 10000;
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
enum FilePredicate implements Predicate<File> {
|
||
|
IS_DIRECTORY { // from class: com.google.common.io.Files.FilePredicate.1
|
||
|
@Override // com.google.common.base.Predicate
|
||
|
public final boolean apply(File file) {
|
||
|
return file.isDirectory();
|
||
|
}
|
||
|
|
||
|
@Override // java.lang.Enum
|
||
|
public final String toString() {
|
||
|
return "Files.isDirectory()";
|
||
|
}
|
||
|
},
|
||
|
IS_FILE { // from class: com.google.common.io.Files.FilePredicate.2
|
||
|
@Override // com.google.common.base.Predicate
|
||
|
public final boolean apply(File file) {
|
||
|
return file.isFile();
|
||
|
}
|
||
|
|
||
|
@Override // java.lang.Enum
|
||
|
public final String toString() {
|
||
|
return "Files.isFile()";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private Files() {
|
||
|
}
|
||
|
|
||
|
public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException {
|
||
|
Preconditions.checkNotNull(file);
|
||
|
Preconditions.checkNotNull(charset);
|
||
|
return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
|
||
|
}
|
||
|
|
||
|
public static BufferedWriter newWriter(File file, Charset charset) throws FileNotFoundException {
|
||
|
Preconditions.checkNotNull(file);
|
||
|
Preconditions.checkNotNull(charset);
|
||
|
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
|
||
|
}
|
||
|
|
||
|
public static ByteSource asByteSource(File file) {
|
||
|
return new FileByteSource(file);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* loaded from: classes2.dex */
|
||
|
public static final class FileByteSource extends ByteSource {
|
||
|
private final File file;
|
||
|
|
||
|
private FileByteSource(File file) {
|
||
|
this.file = (File) Preconditions.checkNotNull(file);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.io.ByteSource
|
||
|
public final FileInputStream openStream() throws IOException {
|
||
|
return new FileInputStream(this.file);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.io.ByteSource
|
||
|
public final Optional<Long> sizeIfKnown() {
|
||
|
if (this.file.isFile()) {
|
||
|
return Optional.of(Long.valueOf(this.file.length()));
|
||
|
}
|
||
|
return Optional.absent();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.io.ByteSource
|
||
|
public final long size() throws IOException {
|
||
|
if (!this.file.isFile()) {
|
||
|
throw new FileNotFoundException(this.file.toString());
|
||
|
}
|
||
|
return this.file.length();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.io.ByteSource
|
||
|
public final byte[] read() throws IOException {
|
||
|
try {
|
||
|
FileInputStream fileInputStream = (FileInputStream) Closer.create().register(openStream());
|
||
|
return ByteStreams.toByteArray(fileInputStream, fileInputStream.getChannel().size());
|
||
|
} finally {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public final String toString() {
|
||
|
String valueOf = String.valueOf(this.file);
|
||
|
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 20);
|
||
|
sb.append("Files.asByteSource(");
|
||
|
sb.append(valueOf);
|
||
|
sb.append(")");
|
||
|
return sb.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static ByteSink asByteSink(File file, FileWriteMode... fileWriteModeArr) {
|
||
|
return new FileByteSink(file, fileWriteModeArr);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* loaded from: classes2.dex */
|
||
|
public static final class FileByteSink extends ByteSink {
|
||
|
private final File file;
|
||
|
private final ImmutableSet<FileWriteMode> modes;
|
||
|
|
||
|
private FileByteSink(File file, FileWriteMode... fileWriteModeArr) {
|
||
|
this.file = (File) Preconditions.checkNotNull(file);
|
||
|
this.modes = ImmutableSet.copyOf(fileWriteModeArr);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.io.ByteSink
|
||
|
public final FileOutputStream openStream() throws IOException {
|
||
|
return new FileOutputStream(this.file, this.modes.contains(FileWriteMode.APPEND));
|
||
|
}
|
||
|
|
||
|
public final String toString() {
|
||
|
String valueOf = String.valueOf(this.file);
|
||
|
String valueOf2 = String.valueOf(this.modes);
|
||
|
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 20 + String.valueOf(valueOf2).length());
|
||
|
sb.append("Files.asByteSink(");
|
||
|
sb.append(valueOf);
|
||
|
sb.append(", ");
|
||
|
sb.append(valueOf2);
|
||
|
sb.append(")");
|
||
|
return sb.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static CharSource asCharSource(File file, Charset charset) {
|
||
|
return asByteSource(file).asCharSource(charset);
|
||
|
}
|
||
|
|
||
|
public static CharSink asCharSink(File file, Charset charset, FileWriteMode... fileWriteModeArr) {
|
||
|
return asByteSink(file, fileWriteModeArr).asCharSink(charset);
|
||
|
}
|
||
|
|
||
|
public static byte[] toByteArray(File file) throws IOException {
|
||
|
return asByteSource(file).read();
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static String toString(File file, Charset charset) throws IOException {
|
||
|
return asCharSource(file, charset).read();
|
||
|
}
|
||
|
|
||
|
public static void write(byte[] bArr, File file) throws IOException {
|
||
|
asByteSink(file, new FileWriteMode[0]).write(bArr);
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static void write(CharSequence charSequence, File file, Charset charset) throws IOException {
|
||
|
asCharSink(file, charset, new FileWriteMode[0]).write(charSequence);
|
||
|
}
|
||
|
|
||
|
public static void copy(File file, OutputStream outputStream) throws IOException {
|
||
|
asByteSource(file).copyTo(outputStream);
|
||
|
}
|
||
|
|
||
|
public static void copy(File file, File file2) throws IOException {
|
||
|
Preconditions.checkArgument(!file.equals(file2), "Source %s and destination %s must be different", file, file2);
|
||
|
asByteSource(file).copyTo(asByteSink(file2, new FileWriteMode[0]));
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static void copy(File file, Charset charset, Appendable appendable) throws IOException {
|
||
|
asCharSource(file, charset).copyTo(appendable);
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static void append(CharSequence charSequence, File file, Charset charset) throws IOException {
|
||
|
asCharSink(file, charset, FileWriteMode.APPEND).write(charSequence);
|
||
|
}
|
||
|
|
||
|
public static boolean equal(File file, File file2) throws IOException {
|
||
|
Preconditions.checkNotNull(file);
|
||
|
Preconditions.checkNotNull(file2);
|
||
|
if (file == file2 || file.equals(file2)) {
|
||
|
return true;
|
||
|
}
|
||
|
long length = file.length();
|
||
|
long length2 = file2.length();
|
||
|
if (length == 0 || length2 == 0 || length == length2) {
|
||
|
return asByteSource(file).contentEquals(asByteSource(file2));
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static File createTempDir() {
|
||
|
File file = new File(System.getProperty("java.io.tmpdir"));
|
||
|
long currentTimeMillis = System.currentTimeMillis();
|
||
|
StringBuilder sb = new StringBuilder(21);
|
||
|
sb.append(currentTimeMillis);
|
||
|
sb.append("-");
|
||
|
String obj = sb.toString();
|
||
|
for (int i = 0; i < 10000; i++) {
|
||
|
StringBuilder sb2 = new StringBuilder(String.valueOf(obj).length() + 11);
|
||
|
sb2.append(obj);
|
||
|
sb2.append(i);
|
||
|
File file2 = new File(file, sb2.toString());
|
||
|
if (file2.mkdir()) {
|
||
|
return file2;
|
||
|
}
|
||
|
}
|
||
|
StringBuilder sb3 = new StringBuilder(String.valueOf(obj).length() + 66 + String.valueOf(obj).length());
|
||
|
sb3.append("Failed to create directory within 10000 attempts (tried ");
|
||
|
sb3.append(obj);
|
||
|
sb3.append("0 to ");
|
||
|
sb3.append(obj);
|
||
|
sb3.append("9999)");
|
||
|
throw new IllegalStateException(sb3.toString());
|
||
|
}
|
||
|
|
||
|
public static void touch(File file) throws IOException {
|
||
|
Preconditions.checkNotNull(file);
|
||
|
if (file.createNewFile() || file.setLastModified(System.currentTimeMillis())) {
|
||
|
return;
|
||
|
}
|
||
|
String valueOf = String.valueOf(file);
|
||
|
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 38);
|
||
|
sb.append("Unable to update modification time of ");
|
||
|
sb.append(valueOf);
|
||
|
throw new IOException(sb.toString());
|
||
|
}
|
||
|
|
||
|
public static void createParentDirs(File file) throws IOException {
|
||
|
Preconditions.checkNotNull(file);
|
||
|
File parentFile = file.getCanonicalFile().getParentFile();
|
||
|
if (parentFile == null) {
|
||
|
return;
|
||
|
}
|
||
|
parentFile.mkdirs();
|
||
|
if (parentFile.isDirectory()) {
|
||
|
return;
|
||
|
}
|
||
|
String valueOf = String.valueOf(file);
|
||
|
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 39);
|
||
|
sb.append("Unable to create parent directories of ");
|
||
|
sb.append(valueOf);
|
||
|
throw new IOException(sb.toString());
|
||
|
}
|
||
|
|
||
|
public static void move(File file, File file2) throws IOException {
|
||
|
Preconditions.checkNotNull(file);
|
||
|
Preconditions.checkNotNull(file2);
|
||
|
Preconditions.checkArgument(!file.equals(file2), "Source %s and destination %s must be different", file, file2);
|
||
|
if (file.renameTo(file2)) {
|
||
|
return;
|
||
|
}
|
||
|
copy(file, file2);
|
||
|
if (file.delete()) {
|
||
|
return;
|
||
|
}
|
||
|
if (!file2.delete()) {
|
||
|
String valueOf = String.valueOf(file2);
|
||
|
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 17);
|
||
|
sb.append("Unable to delete ");
|
||
|
sb.append(valueOf);
|
||
|
throw new IOException(sb.toString());
|
||
|
}
|
||
|
String valueOf2 = String.valueOf(file);
|
||
|
StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf2).length() + 17);
|
||
|
sb2.append("Unable to delete ");
|
||
|
sb2.append(valueOf2);
|
||
|
throw new IOException(sb2.toString());
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static String readFirstLine(File file, Charset charset) throws IOException {
|
||
|
return asCharSource(file, charset).readFirstLine();
|
||
|
}
|
||
|
|
||
|
public static List<String> readLines(File file, Charset charset) throws IOException {
|
||
|
return (List) asCharSource(file, charset).readLines(new LineProcessor<List<String>>() { // from class: com.google.common.io.Files.1
|
||
|
final List<String> result = Lists.newArrayList();
|
||
|
|
||
|
@Override // com.google.common.io.LineProcessor
|
||
|
public boolean processLine(String str) {
|
||
|
this.result.add(str);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.io.LineProcessor
|
||
|
public List<String> getResult() {
|
||
|
return this.result;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static <T> T readLines(File file, Charset charset, LineProcessor<T> lineProcessor) throws IOException {
|
||
|
return (T) asCharSource(file, charset).readLines(lineProcessor);
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static <T> T readBytes(File file, ByteProcessor<T> byteProcessor) throws IOException {
|
||
|
return (T) asByteSource(file).read(byteProcessor);
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static HashCode hash(File file, HashFunction hashFunction) throws IOException {
|
||
|
return asByteSource(file).hash(hashFunction);
|
||
|
}
|
||
|
|
||
|
public static MappedByteBuffer map(File file) throws IOException {
|
||
|
Preconditions.checkNotNull(file);
|
||
|
return map(file, FileChannel.MapMode.READ_ONLY);
|
||
|
}
|
||
|
|
||
|
public static MappedByteBuffer map(File file, FileChannel.MapMode mapMode) throws IOException {
|
||
|
return mapInternal(file, mapMode, -1L);
|
||
|
}
|
||
|
|
||
|
public static MappedByteBuffer map(File file, FileChannel.MapMode mapMode, long j) throws IOException {
|
||
|
Preconditions.checkArgument(j >= 0, "size (%s) may not be negative", j);
|
||
|
return mapInternal(file, mapMode, j);
|
||
|
}
|
||
|
|
||
|
private static MappedByteBuffer mapInternal(File file, FileChannel.MapMode mapMode, long j) throws IOException {
|
||
|
Preconditions.checkNotNull(file);
|
||
|
Preconditions.checkNotNull(mapMode);
|
||
|
Closer create = Closer.create();
|
||
|
try {
|
||
|
FileChannel fileChannel = (FileChannel) create.register(((RandomAccessFile) create.register(new RandomAccessFile(file, mapMode == FileChannel.MapMode.READ_ONLY ? MatchIndex.ROOT_VALUE : "rw"))).getChannel());
|
||
|
if (j == -1) {
|
||
|
j = fileChannel.size();
|
||
|
}
|
||
|
return fileChannel.map(mapMode, 0L, j);
|
||
|
} finally {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static String simplifyPath(String str) {
|
||
|
Preconditions.checkNotNull(str);
|
||
|
if (str.length() == 0) {
|
||
|
return ".";
|
||
|
}
|
||
|
Iterable<String> split = Splitter.on('/').omitEmptyStrings().split(str);
|
||
|
ArrayList arrayList = new ArrayList();
|
||
|
for (String str2 : split) {
|
||
|
str2.hashCode();
|
||
|
if (!str2.equals(".")) {
|
||
|
if (str2.equals("..")) {
|
||
|
if (arrayList.size() > 0 && !((String) arrayList.get(arrayList.size() - 1)).equals("..")) {
|
||
|
arrayList.remove(arrayList.size() - 1);
|
||
|
} else {
|
||
|
arrayList.add("..");
|
||
|
}
|
||
|
} else {
|
||
|
arrayList.add(str2);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
String join = Joiner.on('/').join(arrayList);
|
||
|
if (str.charAt(0) == '/') {
|
||
|
String valueOf = String.valueOf(join);
|
||
|
join = valueOf.length() != 0 ? "/".concat(valueOf) : new String("/");
|
||
|
}
|
||
|
while (join.startsWith("/../")) {
|
||
|
join = join.substring(3);
|
||
|
}
|
||
|
return join.equals("/..") ? "/" : "".equals(join) ? "." : join;
|
||
|
}
|
||
|
|
||
|
public static String getFileExtension(String str) {
|
||
|
Preconditions.checkNotNull(str);
|
||
|
String name = new File(str).getName();
|
||
|
int lastIndexOf = name.lastIndexOf(46);
|
||
|
return lastIndexOf == -1 ? "" : name.substring(lastIndexOf + 1);
|
||
|
}
|
||
|
|
||
|
public static String getNameWithoutExtension(String str) {
|
||
|
Preconditions.checkNotNull(str);
|
||
|
String name = new File(str).getName();
|
||
|
int lastIndexOf = name.lastIndexOf(46);
|
||
|
return lastIndexOf != -1 ? name.substring(0, lastIndexOf) : name;
|
||
|
}
|
||
|
|
||
|
public static Traverser<File> fileTraverser() {
|
||
|
return Traverser.forTree(FILE_TREE);
|
||
|
}
|
||
|
|
||
|
public static Predicate<File> isDirectory() {
|
||
|
return FilePredicate.IS_DIRECTORY;
|
||
|
}
|
||
|
|
||
|
public static Predicate<File> isFile() {
|
||
|
return FilePredicate.IS_FILE;
|
||
|
}
|
||
|
}
|