Skip to content

Commit 43b7699

Browse files
authored
使用 readAllBytes 和 transferTo 简化代码 (#4115)
1 parent fe3fc04 commit 43b7699

File tree

10 files changed

+17
-58
lines changed

10 files changed

+17
-58
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/upgrade/ExecutableHeaderHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static Optional<byte[]> readHeader(ZipFile zip, String suffix) throws IO
6464
ZipEntry entry = zip.getEntry(location);
6565
if (entry != null && !entry.isDirectory()) {
6666
try (InputStream in = zip.getInputStream(entry)) {
67-
return Optional.of(IOUtils.readFullyAsByteArray(in));
67+
return Optional.of(IOUtils.readFully(in));
6868
}
6969
}
7070
}

HMCL/src/main/java/org/jackhuang/hmcl/upgrade/IntegrityChecker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static PublicKey getPublicKey() throws IOException {
5555
if (in == null) {
5656
throw new IOException("Public key not found");
5757
}
58-
return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(IOUtils.readFullyAsByteArray(in)));
58+
return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(IOUtils.readFully(in)));
5959
} catch (GeneralSecurityException e) {
6060
throw new IOException("Failed to load public key", e);
6161
}
@@ -76,7 +76,7 @@ static void verifyJar(Path jarPath) throws IOException {
7676
}
7777

7878
if (SIGNATURE_FILE.equals(filename)) {
79-
signature = IOUtils.readFullyAsByteArray(in);
79+
signature = IOUtils.readFully(in);
8080
} else {
8181
md.reset();
8282
fileFingerprints.put(filename, DigestUtils.digest(md, in));

HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.jackhuang.hmcl.auth.authlibinjector;
1919

20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021
import static java.util.Collections.emptyMap;
2122
import static org.jackhuang.hmcl.util.Lang.tryCast;
2223
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
@@ -32,7 +33,6 @@
3233

3334
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilService;
3435
import org.jackhuang.hmcl.util.io.HttpRequest;
35-
import org.jackhuang.hmcl.util.io.IOUtils;
3636
import org.jackhuang.hmcl.util.javafx.ObservableHelper;
3737
import org.jetbrains.annotations.Nullable;
3838

@@ -77,7 +77,7 @@ public static AuthlibInjectorServer locateServer(String url) throws IOException
7777

7878
try {
7979
AuthlibInjectorServer server = new AuthlibInjectorServer(url);
80-
server.refreshMetadata(IOUtils.readFullyAsStringWithClosing(conn.getInputStream()));
80+
server.refreshMetadata(new String(conn.getInputStream().readAllBytes(), UTF_8));
8181
return server;
8282
} finally {
8383
conn.disconnect();

HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeOldInstallTask.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.jackhuang.hmcl.task.Task;
2626
import org.jackhuang.hmcl.util.gson.JsonUtils;
2727
import org.jackhuang.hmcl.util.io.FileUtils;
28-
import org.jackhuang.hmcl.util.io.IOUtils;
2928

3029
import java.io.*;
3130
import java.nio.file.Path;
@@ -78,7 +77,7 @@ public void execute() throws Exception {
7877

7978
ZipEntry forgeEntry = zipFile.getEntry(installProfile.getInstall().getFilePath());
8079
try (InputStream is = zipFile.getInputStream(forgeEntry); OutputStream os = new FileOutputStream(forgeFile)) {
81-
IOUtils.copyTo(is, os);
80+
is.transferTo(os);
8281
}
8382

8483
setResult(installProfile.getVersionInfo()

HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/LibraryDownloadTask.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.jackhuang.hmcl.task.Task;
2727
import org.jackhuang.hmcl.util.DigestUtils;
2828
import org.jackhuang.hmcl.util.io.FileUtils;
29-
import org.jackhuang.hmcl.util.io.IOUtils;
3029

3130
import java.io.ByteArrayInputStream;
3231
import java.io.File;
@@ -164,7 +163,7 @@ private static boolean validateJar(byte[] data, List<String> checksums) throws I
164163
JarInputStream jar = new JarInputStream(new ByteArrayInputStream(data));
165164
JarEntry entry = jar.getNextJarEntry();
166165
while (entry != null) {
167-
byte[] eData = IOUtils.readFullyWithoutClosing(jar);
166+
byte[] eData = jar.readAllBytes();
168167
if (entry.getName().equals("checksums.sha1")) {
169168
hashes = new String(eData, StandardCharsets.UTF_8).split("\n");
170169
}

HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.jackhuang.hmcl.util.StringUtils;
2626
import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter;
2727
import org.jackhuang.hmcl.util.io.FileUtils;
28-
import org.jackhuang.hmcl.util.io.IOUtils;
2928
import org.jackhuang.hmcl.util.io.Unzipper;
3029
import org.jackhuang.hmcl.util.platform.Bits;
3130
import org.jackhuang.hmcl.util.platform.*;
@@ -420,7 +419,7 @@ public void extractLog4jConfigurationFile() throws IOException {
420419
}
421420

422421
try (InputStream input = source; OutputStream output = new FileOutputStream(targetFile)) {
423-
IOUtils.copyTo(input, output);
422+
input.transferTo(output);
424423
}
425424
}
426425

HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.gson.annotations.SerializedName;
2222
import org.jackhuang.hmcl.util.function.ExceptionalSupplier;
2323
import org.jackhuang.hmcl.util.io.FileUtils;
24-
import org.jackhuang.hmcl.util.io.IOUtils;
2524

2625
import java.io.FileNotFoundException;
2726
import java.io.IOException;
@@ -288,7 +287,7 @@ public void saveETagIndex() throws IOException {
288287
try (FileChannel channel = FileChannel.open(indexFile, StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
289288
FileLock lock = channel.lock();
290289
try {
291-
ETagIndex indexOnDisk = fromMaybeMalformedJson(IOUtils.readFullyAsStringWithClosing(Channels.newInputStream(channel)), ETagIndex.class);
290+
ETagIndex indexOnDisk = fromMaybeMalformedJson(new String(Channels.newInputStream(channel).readAllBytes(), UTF_8), ETagIndex.class);
292291
Map<String, ETagItem> newIndex = joinETagIndexes(indexOnDisk == null ? null : indexOnDisk.eTag, index.values());
293292
channel.truncate(0);
294293
ByteBuffer writeTo = ByteBuffer.wrap(GSON.toJson(new ETagIndex(newIndex.values())).getBytes(UTF_8));
@@ -424,7 +423,7 @@ public void saveToFile() {
424423
try (FileChannel channel = FileChannel.open(indexFile, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
425424
FileLock lock = channel.lock();
426425
try {
427-
Map<String, Object> indexOnDisk = fromMaybeMalformedJson(IOUtils.readFullyAsStringWithClosing(Channels.newInputStream(channel)), mapTypeOf(String.class, Object.class));
426+
Map<String, Object> indexOnDisk = fromMaybeMalformedJson(new String(Channels.newInputStream(channel).readAllBytes(), UTF_8), mapTypeOf(String.class, Object.class));
428427
if (indexOnDisk == null) indexOnDisk = new HashMap<>();
429428
indexOnDisk.putAll(storage);
430429
channel.truncate(0);

HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpMultipartRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public HttpMultipartRequest file(String name, String filename, String contentTyp
5252
addLine(String.format("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"", name, filename));
5353
addLine("Content-Type: " + contentType);
5454
addLine("");
55-
IOUtils.copyTo(inputStream, stream);
55+
inputStream.transferTo(stream);
5656
addLine("");
5757
return this;
5858
}

HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/IOUtils.java

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -62,54 +62,18 @@ public static BufferedReader newBufferedReaderMaybeNativeEncoding(Path file) thr
6262
}
6363
}
6464

65-
/**
66-
* Read all bytes to a buffer from given input stream. The stream will not be closed.
67-
*
68-
* @param stream the InputStream being read.
69-
* @return all bytes read from the stream
70-
* @throws IOException if an I/O error occurs.
71-
*/
72-
public static byte[] readFullyWithoutClosing(InputStream stream) throws IOException {
73-
ByteArrayOutputStream result = new ByteArrayOutputStream(Math.max(stream.available(), 32));
74-
copyTo(stream, result);
75-
return result.toByteArray();
76-
}
77-
78-
public static String readFullyAsStringWithClosing(InputStream stream) throws IOException {
79-
ByteArrayOutputStream result = new ByteArrayOutputStream(Math.max(stream.available(), 32));
80-
copyTo(stream, result);
81-
return result.toString(UTF_8);
82-
}
83-
84-
/**
85-
* Read all bytes to a buffer from given input stream, and close the input stream finally.
86-
*
87-
* @param stream the InputStream being read, closed finally.
88-
* @return all bytes read from the stream
89-
* @throws IOException if an I/O error occurs.
90-
*/
91-
public static ByteArrayOutputStream readFully(InputStream stream) throws IOException {
92-
try (InputStream is = stream) {
93-
ByteArrayOutputStream result = new ByteArrayOutputStream(Math.max(is.available(), 32));
94-
copyTo(is, result);
95-
return result;
65+
public static byte[] readFully(InputStream stream) throws IOException {
66+
try (stream) {
67+
return stream.readAllBytes();
9668
}
9769
}
9870

99-
public static byte[] readFullyAsByteArray(InputStream stream) throws IOException {
100-
return readFully(stream).toByteArray();
101-
}
102-
10371
public static String readFullyAsString(InputStream stream) throws IOException {
104-
return readFully(stream).toString(UTF_8);
72+
return new String(readFully(stream), UTF_8);
10573
}
10674

10775
public static String readFullyAsString(InputStream stream, Charset charset) throws IOException {
108-
return readFully(stream).toString(charset);
109-
}
110-
111-
public static void copyTo(InputStream src, OutputStream dest) throws IOException {
112-
copyTo(src, dest, new byte[DEFAULT_BUFFER_SIZE]);
76+
return new String(readFully(stream), charset);
11377
}
11478

11579
public static void copyTo(InputStream src, OutputStream dest, byte[] buf) throws IOException {

HMCLCore/src/main/java/org/jackhuang/hmcl/util/tree/TarFileTree.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import kala.compress.archivers.tar.TarArchiveEntry;
2222
import kala.compress.archivers.tar.TarArchiveReader;
23-
import org.jackhuang.hmcl.util.io.IOUtils;
2423

2524
import java.io.*;
2625
import java.nio.file.Files;
@@ -42,7 +41,7 @@ public static TarFileTree open(Path file) throws IOException {
4241
try (GZIPInputStream input = new GZIPInputStream(Files.newInputStream(file));
4342
OutputStream output = Files.newOutputStream(tempFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
4443
) {
45-
IOUtils.copyTo(input, output);
44+
input.transferTo(output);
4645
tarFile = new TarArchiveReader(tempFile);
4746
} catch (Throwable e) {
4847
try {

0 commit comments

Comments
 (0)