From 1fecb995c909b32a65c9337623b03c635cd2f2d3 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 5 May 2021 21:26:17 +0100 Subject: [PATCH] Don't close file handles from ResourceMounts Unlike short handles, we don't read these immediately, and so we can't close it right away. Otherwise the file is considered empty! Fixes SquidDev-CC/treasure-programs#1 --- .../core/filesystem/ResourceMount.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java b/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java index 279871e50..583d1e26c 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java +++ b/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java @@ -12,6 +12,7 @@ import com.google.common.io.ByteStreams; import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.core.apis.handles.ArrayByteChannel; +import dan200.computercraft.shared.util.IoUtil; import net.minecraft.resources.IReloadableResourceManager; import net.minecraft.resources.IResource; import net.minecraft.resources.IResourceManager; @@ -244,11 +245,20 @@ public final class ResourceMount implements IMount byte[] contents = CONTENTS_CACHE.getIfPresent( file ); if( contents != null ) return new ArrayByteChannel( contents ); - try( InputStream stream = manager.getResource( file.identifier ).getInputStream() ) + try { + InputStream stream = manager.getResource( file.identifier ).getInputStream(); if( stream.available() > MAX_CACHED_SIZE ) return Channels.newChannel( stream ); - contents = ByteStreams.toByteArray( stream ); + try + { + contents = ByteStreams.toByteArray( stream ); + } + finally + { + IoUtil.closeQuietly( stream ); + } + CONTENTS_CACHE.put( file, contents ); return new ArrayByteChannel( contents ); }