From 97d02148b96186c41ba83bc68d605acc58d47b50 Mon Sep 17 00:00:00 2001 From: Zhi Dou Date: Tue, 30 Jul 2024 17:07:56 +0000 Subject: [PATCH] close file stream Close file stream, since the in the restrict mode the runtime will check whether all the resource is closed when it is out of scope. Test: ABTD ImsStackJavaTests Bug: 349874828 Change-Id: Ib297622bae730bf99e4d5d1b3adeff3fee805a93 --- .../aconfig/storage/StorageInternalReader.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/aconfig/aconfig_storage_read_api/srcs/android/aconfig/storage/StorageInternalReader.java b/tools/aconfig/aconfig_storage_read_api/srcs/android/aconfig/storage/StorageInternalReader.java index bbb6813295..07558eee30 100644 --- a/tools/aconfig/aconfig_storage_read_api/srcs/android/aconfig/storage/StorageInternalReader.java +++ b/tools/aconfig/aconfig_storage_read_api/srcs/android/aconfig/storage/StorageInternalReader.java @@ -18,6 +18,7 @@ package android.aconfig.storage; import android.compat.annotation.UnsupportedAppUsage; +import java.io.Closeable; import java.io.FileInputStream; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; @@ -68,13 +69,26 @@ public class StorageInternalReader { // Map a storage file given file path private static MappedByteBuffer mapStorageFile(String file) { + FileInputStream stream = null; try { - FileInputStream stream = new FileInputStream(file); + stream = new FileInputStream(file); FileChannel channel = stream.getChannel(); return channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); } catch (Exception e) { throw new AconfigStorageException( String.format("Fail to mmap storage file %s", file), e); + } finally { + quietlyDispose(stream); + } + } + + private static void quietlyDispose(Closeable closable) { + try { + if (closable != null) { + closable.close(); + } + } catch (Exception e) { + // no need to care, at least as of now } } }