Merge pull request #188 from KAMKEEL/codex/find-and-fix-memory-leak-issues

Fix memory leaks by closing streams
This commit is contained in:
Kam authored and GitHub committed 2025-07-23 05:44:27 -04:00
commit 15dee0e5b8
18 files changed
+96 -60

No files matched your search

@@ -59,21 +59,23 @@ public class CustomNpcResourceListener implements
try {
IResourceManager manager = Minecraft.getMinecraft().getResourceManager();
ResourceLocation location = new ResourceLocation("textures/blocks/" + texture + ".png");
BufferedImage bufferedimage = ImageIO.read(manager.getResource(location).getInputStream());
int i = bufferedimage.getWidth();
int j = bufferedimage.getHeight();
try (InputStream stream = manager.getResource(location).getInputStream()) {
BufferedImage bufferedimage = ImageIO.read(stream);
int i = bufferedimage.getWidth();
int j = bufferedimage.getHeight();
BufferedImage image = new BufferedImage(i * 4, j * 2, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.drawImage(bufferedimage, 0, 0, null);
g.drawImage(bufferedimage, i, 0, null);
g.drawImage(bufferedimage, i * 2, 0, null);
g.drawImage(bufferedimage, i * 3, 0, null);
g.drawImage(bufferedimage, 0, i, null);
g.drawImage(bufferedimage, i, j, null);
g.drawImage(bufferedimage, i * 2, j, null);
g.drawImage(bufferedimage, i * 3, j, null);
ImageIO.write(image, "png", new File(dir, texture + ".png"));
BufferedImage image = new BufferedImage(i * 4, j * 2, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.drawImage(bufferedimage, 0, 0, null);
g.drawImage(bufferedimage, i, 0, null);
g.drawImage(bufferedimage, i * 2, 0, null);
g.drawImage(bufferedimage, i * 3, 0, null);
g.drawImage(bufferedimage, 0, i, null);
g.drawImage(bufferedimage, i, j, null);
g.drawImage(bufferedimage, i * 2, j, null);
g.drawImage(bufferedimage, i * 3, j, null);
ImageIO.write(image, "png", new File(dir, texture + ".png"));
}
} catch (Exception e) {
LogWriter.error("Failed caching texture: " + texture, e);
}
@@ -122,10 +122,14 @@ public class ImageDownloadAlt extends SimpleTexture {
BufferedImage bufferedimage;
if (ImageDownloadAlt.this.cacheFile != null) {
FileUtils.copyInputStreamToFile(connection.getInputStream(), ImageDownloadAlt.this.cacheFile);
try (InputStream in = connection.getInputStream()) {
FileUtils.copyInputStreamToFile(in, ImageDownloadAlt.this.cacheFile);
}
bufferedimage = ImageIO.read(ImageDownloadAlt.this.cacheFile);
} else {
bufferedimage = ImageIO.read(connection.getInputStream());
try (InputStream in = connection.getInputStream()) {
bufferedimage = ImageIO.read(in);
}
}
if (ImageDownloadAlt.this.imageBuffer != null) {
@@ -52,7 +52,9 @@ public class PresetController {
if (!file.exists()) {
return null;
}
return CompressedStreamTools.readCompressed(new FileInputStream(file));
try (FileInputStream fis = new FileInputStream(file)) {
return CompressedStreamTools.readCompressed(fis);
}
} catch (Exception e) {
LogWriter.except(e);
}
@@ -61,7 +63,9 @@ public class PresetController {
if (!file.exists()) {
return null;
}
return CompressedStreamTools.readCompressed(new FileInputStream(file));
try (FileInputStream fis = new FileInputStream(file)) {
return CompressedStreamTools.readCompressed(fis);
}
} catch (Exception e) {
LogWriter.except(e);
@@ -461,8 +461,8 @@ public class CustomGuiLabel extends Gui implements IGuiComponent {
private void readFontTexture() {
BufferedImage bufferedimage;
try {
bufferedimage = ImageIO.read(getResourceInputStream(this.locationFontTexture));
try (InputStream stream = getResourceInputStream(this.locationFontTexture)) {
bufferedimage = ImageIO.read(stream);
} catch (IOException ioexception) {
throw new RuntimeException(ioexception);
}
@@ -515,8 +515,7 @@ public class CustomGuiLabel extends Gui implements IGuiComponent {
}
private void readGlyphSizes() {
try {
InputStream inputstream = getResourceInputStream(new ResourceLocation("font/glyph_sizes.bin"));
try (InputStream inputstream = getResourceInputStream(new ResourceLocation("font/glyph_sizes.bin"))) {
inputstream.read(this.glyphWidth);
} catch (IOException ioexception) {
throw new RuntimeException(ioexception);
@@ -400,8 +400,8 @@ public class CustomOverlayLabel extends Gui implements IOverlayComponent {
private void readFontTexture() {
BufferedImage bufferedimage;
try {
bufferedimage = ImageIO.read(getResourceInputStream(this.locationFontTexture));
try (InputStream stream = getResourceInputStream(this.locationFontTexture)) {
bufferedimage = ImageIO.read(stream);
} catch (IOException ioexception) {
throw new RuntimeException(ioexception);
}
@@ -454,8 +454,7 @@ public class CustomOverlayLabel extends Gui implements IOverlayComponent {
}
private void readGlyphSizes() {
try {
InputStream inputstream = getResourceInputStream(new ResourceLocation("font/glyph_sizes.bin"));
try (InputStream inputstream = getResourceInputStream(new ResourceLocation("font/glyph_sizes.bin"))) {
inputstream.read(this.glyphWidth);
} catch (IOException ioexception) {
throw new RuntimeException(ioexception);
@@ -756,11 +756,12 @@ public class ModelPony extends ModelBase {
if (pony.textureLocation != pony.checked && pony.textureLocation != null) {
try {
IResource resource = Minecraft.getMinecraft().getResourceManager().getResource(pony.textureLocation);
BufferedImage bufferedimage = ImageIO.read(resource.getInputStream());
try (InputStream stream = resource.getInputStream()) {
BufferedImage bufferedimage = ImageIO.read(stream);
pony.isPegasus = false;
pony.isUnicorn = false;
Color color = new Color(bufferedimage.getRGB(0, 0), true);
pony.isPegasus = false;
pony.isUnicorn = false;
Color color = new Color(bufferedimage.getRGB(0, 0), true);
Color color1 = new Color(249, 177, 49, 255);
Color color2 = new Color(136, 202, 240, 255);
Color color3 = new Color(209, 159, 228, 255);
@@ -778,7 +779,7 @@ public class ModelPony extends ModelBase {
pony.isUnicorn = true;
}
pony.checked = pony.textureLocation;
}
} catch (IOException e) {
}
@@ -93,8 +93,9 @@ public class ImageData {
if (!this.isUrl) {
try {
IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(location);
InputStream inputstream = iresource.getInputStream();
this.bufferedImage = ImageIO.read(inputstream);
try (InputStream inputstream = iresource.getInputStream()) {
this.bufferedImage = ImageIO.read(inputstream);
}
} catch (IOException ignored) {
}
} else {
@@ -93,10 +93,11 @@ public class RenderNPCPony extends RenderNPCInterface {
if (check) {
try {
IResource resource = Minecraft.getMinecraft().getResourceManager().getResource(loc);
BufferedImage bufferedimage = ImageIO.read(resource.getInputStream());
try (InputStream stream = resource.getInputStream()) {
BufferedImage bufferedimage = ImageIO.read(stream);
pony.isPegasus = false;
pony.isUnicorn = false;
pony.isPegasus = false;
pony.isUnicorn = false;
Color color = new Color(bufferedimage.getRGB(0, 0), true);
Color color1 = new Color(249, 177, 49, 255);
Color color2 = new Color(136, 202, 240, 255);
@@ -114,8 +115,8 @@ public class RenderNPCPony extends RenderNPCInterface {
pony.isPegasus = true;
pony.isUnicorn = true;
}
pony.checked = loc;
pony.checked = loc;
}
} catch (IOException e) {
}
@@ -253,19 +253,19 @@ public class GlyphCache {
}
void setCustomFont(ResourceLocation location, int size, boolean antiAlias) throws Exception {
InputStream stream = Minecraft.getMinecraft().getResourceManager().getResource(location).getInputStream();
try (InputStream stream = Minecraft.getMinecraft().getResourceManager().getResource(location).getInputStream()) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
ge.registerFont(font);
font = font.deriveFont(Font.PLAIN, 72);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
ge.registerFont(font);
font = font.deriveFont(Font.PLAIN, 72);
usedFonts.clear();
usedFonts.add(font);
usedFonts.clear();
usedFonts.add(font);
fontSize = size;
antiAliasEnabled = antiAlias;
setRenderingHints();
fontSize = size;
antiAliasEnabled = antiAlias;
setRenderingHints();
}
}
int fontHeight(String s) {
@@ -79,7 +79,9 @@ public class BankController {
}
private void loadBanks(File file) throws IOException {
loadBanks(CompressedStreamTools.readCompressed(new FileInputStream(file)));
try (FileInputStream fis = new FileInputStream(file)) {
loadBanks(CompressedStreamTools.readCompressed(fis));
}
}
public void loadBanks(NBTTagCompound nbttagcompound1) throws IOException {
@@ -120,7 +120,10 @@ public class DialogController implements IDialogHandler {
if (!dir.exists()) {
dir.mkdir();
}
NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file));
NBTTagCompound nbttagcompound1;
try (FileInputStream fis = new FileInputStream(file)) {
nbttagcompound1 = CompressedStreamTools.readCompressed(fis);
}
NBTTagList list = nbttagcompound1.getTagList("Data", 10);
if (list == null)
return;
@@ -40,7 +40,10 @@ public class GlobalDataController {
}
private void loadData(File file) throws Exception {
NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file));
NBTTagCompound nbttagcompound1;
try (FileInputStream fis = new FileInputStream(file)) {
nbttagcompound1 = CompressedStreamTools.readCompressed(fis);
}
itemGiverId = nbttagcompound1.getInteger("itemGiverId");
}
@@ -184,7 +184,10 @@ public class PlayerDataController {
try {
File file = new File(saveDir, filename);
if (file.exists()) {
NBTTagCompound comp = CompressedStreamTools.readCompressed(new FileInputStream(file));
NBTTagCompound comp;
try (FileInputStream fis = new FileInputStream(file)) {
comp = CompressedStreamTools.readCompressed(fis);
}
file.delete();
file = new File(saveDir, filename + "_old");
if (file.exists())
@@ -197,7 +200,9 @@ public class PlayerDataController {
try {
File file = new File(saveDir, filename + "_old");
if (file.exists()) {
return CompressedStreamTools.readCompressed(new FileInputStream(file));
try (FileInputStream fis = new FileInputStream(file)) {
return CompressedStreamTools.readCompressed(fis);
}
}
} catch (Exception e) {
@@ -112,7 +112,10 @@ public class QuestController implements IQuestHandler {
if (!dir.exists()) {
dir.mkdir();
}
NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file));
NBTTagCompound nbttagcompound1;
try (FileInputStream fis = new FileInputStream(file)) {
nbttagcompound1 = CompressedStreamTools.readCompressed(fis);
}
lastUsedCatID = nbttagcompound1.getInteger("lastID");
lastUsedQuestID = nbttagcompound1.getInteger("lastQuestID");
NBTTagList list = nbttagcompound1.getTagList("Data", 10);
@@ -94,7 +94,10 @@ public class RecipeController implements IRecipeHandler {
}
private void loadCategories(File file) throws Exception {
NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file));
NBTTagCompound nbttagcompound1;
try (FileInputStream fis = new FileInputStream(file)) {
nbttagcompound1 = CompressedStreamTools.readCompressed(fis);
}
nextId = nbttagcompound1.getInteger("LastId");
nextAnvilId = nbttagcompound1.getInteger("LastAnvilId");
NBTTagList list = nbttagcompound1.getTagList("Data", 10);
@@ -63,7 +63,10 @@ public class ServerCloneController implements ICloneHandler {
private Map<Integer, Map<String, NBTTagCompound>> loadOldClones(File file) throws Exception {
Map<Integer, Map<String, NBTTagCompound>> clones = new HashMap<Integer, Map<String, NBTTagCompound>>();
NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file));
NBTTagCompound nbttagcompound1;
try (FileInputStream fis = new FileInputStream(file)) {
nbttagcompound1 = CompressedStreamTools.readCompressed(fis);
}
NBTTagList list = nbttagcompound1.getTagList("Data", 10);
if (list == null) {
return clones;
@@ -66,7 +66,10 @@ public class TransportController implements ITransportHandler {
public void loadCategories(File file) throws IOException {
HashMap<Integer, TransportLocation> locations = new HashMap<Integer, TransportLocation>();
HashMap<Integer, TransportCategory> categories = new HashMap<Integer, TransportCategory>();
NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file));
NBTTagCompound nbttagcompound1;
try (FileInputStream fis = new FileInputStream(file)) {
nbttagcompound1 = CompressedStreamTools.readCompressed(fis);
}
lastUsedID = nbttagcompound1.getInteger("lastID");
NBTTagList list = nbttagcompound1.getTagList("NPCTransportCategories", 10);
if (list == null) {
@@ -213,8 +213,8 @@ public class NBTJsonUtil {
}
public static NBTTagCompound loadNBTData(File file) {
try {
return CompressedStreamTools.readCompressed(new FileInputStream(file));
try (FileInputStream fis = new FileInputStream(file)) {
return CompressedStreamTools.readCompressed(fis);
} catch (Exception e) {
LogWriter.error("Error loading: " + file.getName(), e);
}