1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-11-10 20:09:58 +00:00

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
This commit is contained in:
Jonathan Coates 2021-05-05 21:26:17 +01:00
parent fb9590467d
commit 1fecb995c9

View File

@ -12,6 +12,7 @@ import com.google.common.io.ByteStreams;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.core.apis.handles.ArrayByteChannel; import dan200.computercraft.core.apis.handles.ArrayByteChannel;
import dan200.computercraft.shared.util.IoUtil;
import net.minecraft.resources.IReloadableResourceManager; import net.minecraft.resources.IReloadableResourceManager;
import net.minecraft.resources.IResource; import net.minecraft.resources.IResource;
import net.minecraft.resources.IResourceManager; import net.minecraft.resources.IResourceManager;
@ -244,11 +245,20 @@ public final class ResourceMount implements IMount
byte[] contents = CONTENTS_CACHE.getIfPresent( file ); byte[] contents = CONTENTS_CACHE.getIfPresent( file );
if( contents != null ) return new ArrayByteChannel( contents ); 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 ); if( stream.available() > MAX_CACHED_SIZE ) return Channels.newChannel( stream );
try
{
contents = ByteStreams.toByteArray( stream ); contents = ByteStreams.toByteArray( stream );
}
finally
{
IoUtil.closeQuietly( stream );
}
CONTENTS_CACHE.put( file, contents ); CONTENTS_CACHE.put( file, contents );
return new ArrayByteChannel( contents ); return new ArrayByteChannel( contents );
} }