improve some util functions (potential memory leaks, etc)

This commit is contained in:
Bixilon 2020-10-02 22:03:34 +02:00
parent a143e79c25
commit bd1283c6d8
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 24 additions and 10 deletions

View File

@ -119,6 +119,5 @@
<artifactId>netty-all</artifactId>
<version>4.1.52.Final</version>
</dependency>
</dependencies>
</project>

View File

@ -16,7 +16,6 @@ package de.bixilon.minosoft.game.datatypes.objectLoader.versions;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.bixilon.minosoft.Config;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.config.GameConfiguration;
@ -153,15 +152,15 @@ public class Versions {
}
String fileName = Config.homeDir + String.format("assets/mapping/%s.tar.gz", version.getVersionName());
HashMap<String, String> files;
HashMap<String, JsonObject> files;
try {
files = Util.readTarGzFile(fileName);
files = Util.readJsonTarGzFile(fileName);
} catch (FileNotFoundException e) {
long downloadStartTime = System.currentTimeMillis();
Log.info(String.format("Mappings for %s are not available on disk. Downloading them...", version.getVersionName()));
Util.downloadFile(String.format(Minosoft.getConfig().getString(GameConfiguration.MAPPINGS_URL), version.getVersionName()), fileName);
try {
files = Util.readTarGzFile(fileName);
files = Util.readJsonTarGzFile(fileName);
} catch (ZipException e2) {
// bullshit downloaded, delete file
new File(fileName).delete();
@ -171,7 +170,7 @@ public class Versions {
}
for (Map.Entry<String, Mappings> mappingSet : mappingsHashMap.entrySet()) {
JsonObject data = JsonParser.parseString(files.get(mappingSet.getKey() + ".json")).getAsJsonObject().getAsJsonObject("minecraft");
JsonObject data = files.get(mappingSet.getKey() + ".json").getAsJsonObject("minecraft");
loadVersionMappings(mappingSet.getValue(), data, protocolId);
}

View File

@ -88,7 +88,7 @@ public final class Util {
public static byte[] decompressGzip(byte[] raw) throws IOException {
GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(raw));
ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int res = 0;
byte[] buf = new byte[1024];
@ -98,7 +98,10 @@ public final class Util {
outputStream.write(buf, 0, res);
}
}
return outputStream.toByteArray();
gzipInputStream.close();
byte[] ret = outputStream.toByteArray();
outputStream.close();
return ret;
}
public static String sha1(String string) {
@ -115,13 +118,26 @@ public final class Util {
public static HashMap<String, String> readTarGzFile(String fileName) throws IOException {
File inputFile = new File(fileName);
GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inputFile));
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream);
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(inputFile)));
HashMap<String, String> ret = new HashMap<>();
TarArchiveEntry entry;
while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
ret.put(entry.getName(), readFile(new BufferedReader(new InputStreamReader(tarArchiveInputStream)), false));
}
tarArchiveInputStream.close();
return ret;
}
public static HashMap<String, JsonObject> readJsonTarGzFile(String fileName) throws IOException {
File inputFile = new File(fileName);
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(inputFile)));
HashMap<String, JsonObject> ret = new HashMap<>();
TarArchiveEntry entry;
while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
ret.put(entry.getName(), JsonParser.parseReader(new InputStreamReader(tarArchiveInputStream)).getAsJsonObject());
}
tarArchiveInputStream.close();
return ret;
}