From 2769f93f71ff0934930428f2aec1c102a5cb6d22 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 5 Aug 2018 09:30:42 -0400 Subject: [PATCH] streams --- .../java/baritone/bot/chunk/CachedRegion.java | 46 +++++++++---------- .../java/baritone/bot/utils/GZIPUtils.java | 6 +-- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/main/java/baritone/bot/chunk/CachedRegion.java b/src/main/java/baritone/bot/chunk/CachedRegion.java index 9be288f0..79a92b7d 100644 --- a/src/main/java/baritone/bot/chunk/CachedRegion.java +++ b/src/main/java/baritone/bot/chunk/CachedRegion.java @@ -3,13 +3,15 @@ package baritone.bot.chunk; import baritone.bot.pathing.util.PathingBlockType; import baritone.bot.utils.GZIPUtils; -import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.BitSet; +import java.util.zip.GZIPOutputStream; /** * @author Brady @@ -19,7 +21,7 @@ public final class CachedRegion implements ICachedChunkAccess { /** * All of the chunks in this region. A 16x16 array of them. - * + *

* I would make these 32x32 regions to be in line with the Anvil format, but 16 is a nice number. */ private final CachedChunk[][] chunks = new CachedChunk[32][32]; @@ -67,28 +69,24 @@ public final class CachedRegion implements ICachedChunkAccess { if (!Files.exists(path)) Files.createDirectories(path); - ByteArrayOutputStream bos = new ByteArrayOutputStream(32 * 32 * CachedChunk.SIZE_IN_BYTES); - for (int z = 0; z < 32; z++) { - for (int x = 0; x < 32; x++) { - CachedChunk chunk = this.chunks[x][z]; - if (chunk == null) { - bos.write(CachedChunk.EMPTY_CHUNK); - } else { - byte[] chunkBytes = chunk.toByteArray(); - bos.write(chunkBytes); - // Messy, but fills the empty 0s that should be trailing to fill up the space. - bos.write(new byte[CachedChunk.SIZE_IN_BYTES - chunkBytes.length]); - } - } - } - Path regionFile = getRegionFile(path, this.x, this.z); if (!Files.exists(regionFile)) Files.createFile(regionFile); - - byte[] compressed = GZIPUtils.compress(bos.toByteArray()); - if (compressed != null) - Files.write(regionFile, compressed); + try (FileOutputStream fileOut = new FileOutputStream(regionFile.toFile()); GZIPOutputStream out = new GZIPOutputStream(fileOut)) { + for (int z = 0; z < 32; z++) { + for (int x = 0; x < 32; x++) { + CachedChunk chunk = this.chunks[x][z]; + if (chunk == null) { + out.write(CachedChunk.EMPTY_CHUNK); + } else { + byte[] chunkBytes = chunk.toByteArray(); + out.write(chunkBytes); + // Messy, but fills the empty 0s that should be trailing to fill up the space. + out.write(new byte[CachedChunk.SIZE_IN_BYTES - chunkBytes.length]); + } + } + } + } } catch (IOException ignored) {} } @@ -101,9 +99,11 @@ public final class CachedRegion implements ICachedChunkAccess { Path regionFile = getRegionFile(path, this.x, this.z); if (!Files.exists(regionFile)) return; + byte[] decompressed; + try (FileInputStream in = new FileInputStream(regionFile.toFile())) { + decompressed = GZIPUtils.decompress(in); + } - byte[] fileBytes = Files.readAllBytes(regionFile); - byte[] decompressed = GZIPUtils.decompress(fileBytes); if (decompressed == null) return; diff --git a/src/main/java/baritone/bot/utils/GZIPUtils.java b/src/main/java/baritone/bot/utils/GZIPUtils.java index 00b894bf..a2b68a18 100644 --- a/src/main/java/baritone/bot/utils/GZIPUtils.java +++ b/src/main/java/baritone/bot/utils/GZIPUtils.java @@ -1,8 +1,8 @@ package baritone.bot.utils; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -24,10 +24,10 @@ public final class GZIPUtils { return byteStream.toByteArray(); } - public static byte[] decompress(byte[] in) throws IOException { + public static byte[] decompress(InputStream in) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); - try (GZIPInputStream gzipStream = new GZIPInputStream(new ByteArrayInputStream(in))) { + try (GZIPInputStream gzipStream = new GZIPInputStream(in)) { byte[] buffer = new byte[1024]; int len; while ((len = gzipStream.read(buffer)) > 0) {