Minor optimizations

This commit is contained in:
Reinhard Pointner 2016-11-07 22:17:37 +08:00
parent 838fd5b155
commit 09ff31d88e
5 changed files with 23 additions and 18 deletions

View File

@ -14,7 +14,7 @@
<classpathentry kind="lib" path="lib/ivy/jar/junit.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/miglayout-core.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/miglayout-swing.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/rsyntaxtextarea.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/rsyntaxtextarea.jar" sourcepath="lib/ivy/source/rsyntaxtextarea.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/xz.jar" sourcepath="lib/ivy/source/xz.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/slf4j-api.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/commons-io.jar" sourcepath="lib/ivy/source/commons-io.jar"/>

View File

@ -17,6 +17,7 @@ import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.text.Collator;
import java.text.Normalizer;
import java.text.Normalizer.Form;
@ -36,6 +37,7 @@ import java.util.function.IntFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.tukaani.xz.XZInputStream;
@ -493,8 +495,9 @@ public class ReleaseInfo {
byte[] bytes = cache.bytes(name, n -> new URL(getProperty(n)), XZInputStream::new).expire(refreshDuration.optional().orElse(expirationTime)).get();
// all data files are UTF-8 encoded XZ compressed text files
String text = new String(bytes, UTF_8);
return NEWLINE.splitAsStream(text).filter(s -> s.length() > 0).map(parse).filter(Objects::nonNull).toArray(generator);
Stream<String> lines = NEWLINE.splitAsStream(UTF_8.decode(ByteBuffer.wrap(bytes)));
return lines.filter(s -> s.length() > 0).map(parse).filter(Objects::nonNull).toArray(generator);
};
}

View File

@ -1,11 +1,9 @@
package net.filebot.ui.rename;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import static net.filebot.Logging.*;
import static net.filebot.MediaTypes.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.RegularExpressions.*;
import java.awt.datatransfer.Transferable;
import java.io.File;
@ -66,8 +64,7 @@ class FilesListTransferablePolicy extends BackgroundFileTransferablePolicy<File>
// load file paths from text files
if (recursive && LIST_FILES.accept(f)) {
try {
String[] lines = NEWLINE.split(readTextFile(f));
List<File> paths = stream(lines).filter(s -> s.length() > 0).map(path -> {
List<File> paths = readLines(f).stream().filter(s -> s.length() > 0).map(path -> {
try {
File file = new File(path);
return file.isAbsolute() && file.exists() ? file : null;

View File

@ -49,11 +49,11 @@ public class FileSet extends AbstractSet<Path> {
}
public boolean add(File e) {
return add(getPath(e));
return add(e.toPath());
}
public boolean add(String e) {
return add(getPath(e));
return add(Paths.get(e));
}
private boolean contains(Path e, int depth) {
@ -160,7 +160,7 @@ public class FileSet extends AbstractSet<Path> {
public void load(File f) throws IOException {
for (String path : readLines(f)) {
try {
add(Paths.get(path));
add(path);
} catch (InvalidPathException e) {
debug.warning(e::toString);
}

View File

@ -47,6 +47,7 @@ import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
@ -186,22 +187,26 @@ public final class FileUtilities {
private static final String WIN_THUMBNAIL_STORE = "Thumbs.db";
private static final String MAC_THUMBNAIL_STORE = ".DS_Store";
public static boolean isThumbnailStore(File f) {
return MAC_THUMBNAIL_STORE.equals(f.getName()) || WIN_THUMBNAIL_STORE.equalsIgnoreCase(f.getName());
public static boolean isThumbnailStore(File file) {
return MAC_THUMBNAIL_STORE.equals(file.getName()) || WIN_THUMBNAIL_STORE.equalsIgnoreCase(file.getName());
}
public static byte[] readFile(File source) throws IOException {
return Files.readAllBytes(source.toPath());
public static byte[] readFile(File file) throws IOException {
return Files.readAllBytes(file.toPath());
}
public static List<String> readLines(File file) throws IOException {
try (BufferedReader reader = new BufferedReader(new UnicodeReader(new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE), false, UTF_8), BUFFER_SIZE)) {
return reader.lines().collect(toList());
public static <R, A> R readLines(File file, Collector<? super String, A, R> collector) throws IOException {
try (BufferedReader reader = new BufferedReader(new UnicodeReader(new ByteArrayInputStream(readFile(file)), false, UTF_8))) {
return reader.lines().collect(collector);
}
}
public static List<String> readLines(File file) throws IOException {
return readLines(file, toList());
}
public static String readTextFile(File file) throws IOException {
return String.join(System.lineSeparator(), readLines(file));
return readLines(file, joining(System.lineSeparator()));
}
public static File writeFile(ByteBuffer data, File destination) throws IOException {