From 430a70049bb85d948267d382e837d534b39f63ed Mon Sep 17 00:00:00 2001 From: Pinghao Wu Date: Sat, 26 Oct 2024 13:37:11 +0800 Subject: [PATCH] adopt try-with-resources --- .../safcephfs/CephFSDocumentsProvider.java | 20 +++++------ .../org/safcephfs/MediaMetadataReader.java | 34 +++++++++++-------- .../org/safcephfs/UncheckedAutoCloseable.java | 13 +++++++ 3 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 src/main/java/org/safcephfs/UncheckedAutoCloseable.java diff --git a/src/main/java/org/safcephfs/CephFSDocumentsProvider.java b/src/main/java/org/safcephfs/CephFSDocumentsProvider.java index 4f600f9..cd9af77 100644 --- a/src/main/java/org/safcephfs/CephFSDocumentsProvider.java +++ b/src/main/java/org/safcephfs/CephFSDocumentsProvider.java @@ -371,27 +371,23 @@ public class CephFSDocumentsProvider extends DocumentsProvider { String mimeType = getDocumentType(documentId); if (MetadataReader.isSupportedMimeType(mimeType)) { ParcelFileDescriptor fd = openDocument(documentId, "r", null); - AutoCloseInputStream stream = new AutoCloseInputStream(fd); - Bundle metadata = new Bundle(); - try { - MetadataReader.getMetadata(metadata, stream, mimeType, null); + + try (var stream = new UncheckedAutoCloseable( + new AutoCloseInputStream(fd))) { + MetadataReader.getMetadata(metadata, stream.c(), mimeType, null); } catch (IOException e) { Log.e(APP_NAME, "getMetadata: ", e); return null; } return metadata; } else if (MediaMetadataReader.isSupportedMimeType(mimeType)) { - ParcelFileDescriptor fd = openDocument(documentId, "r", null); Bundle metadata = new Bundle(); - MediaMetadataReader.getMetadata(metadata, fd.getFileDescriptor(), - mimeType); - try { - fd.close(); - } catch (IOException e) { - Log.e(APP_NAME, "getMetadata: video fd close: ", e); - return null; + try (var fd = new UncheckedAutoCloseable( + openDocument(documentId, "r", null))){ + MediaMetadataReader.getMetadata(metadata, + fd.c().getFileDescriptor(), mimeType); } return metadata; } diff --git a/src/main/java/org/safcephfs/MediaMetadataReader.java b/src/main/java/org/safcephfs/MediaMetadataReader.java index aee94d7..e321d45 100644 --- a/src/main/java/org/safcephfs/MediaMetadataReader.java +++ b/src/main/java/org/safcephfs/MediaMetadataReader.java @@ -7,6 +7,7 @@ import android.os.Bundle; import android.provider.DocumentsContract; import java.io.FileDescriptor; +import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -68,23 +69,26 @@ public final class MediaMetadataReader { return; } Bundle typeSpecificMetadata = new Bundle(); - MediaMetadataRetriever retriever = new MediaMetadataRetriever(); - retriever.setDataSource(fd); - for (int key: nameMapping.keySet()) { - String raw = retriever.extractMetadata(key); - if (raw == null) { - continue; - } + try (var retriever = new UncheckedAutoCloseable( + new MediaMetadataRetriever())) { + retriever.c().setDataSource(fd); + + for (int key: nameMapping.keySet()) { + String raw = retriever.c().extractMetadata(key); + if (raw == null) { + continue; + } - Integer type = TYPE_MAPPING.get(key); - if (type == null) { - typeSpecificMetadata.putString(nameMapping.get(key), raw); - } else { - switch (type) { - case TYPE_INT: - typeSpecificMetadata.putInt(nameMapping.get(key), Integer.parseInt(raw)); - break; + Integer type = TYPE_MAPPING.get(key); + if (type == null) { + typeSpecificMetadata.putString(nameMapping.get(key), raw); + } else { + switch (type) { + case TYPE_INT: + typeSpecificMetadata.putInt(nameMapping.get(key), Integer.parseInt(raw)); + break; + } } } } diff --git a/src/main/java/org/safcephfs/UncheckedAutoCloseable.java b/src/main/java/org/safcephfs/UncheckedAutoCloseable.java new file mode 100644 index 0000000..d7c491d --- /dev/null +++ b/src/main/java/org/safcephfs/UncheckedAutoCloseable.java @@ -0,0 +1,13 @@ +package org.safcephfs; + +public record UncheckedAutoCloseable(T c) + implements AutoCloseable { + + public void close() { + try { + c.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} -- 2.45.2