M src/main/java/org/safcephfs/CephFSDocumentsProvider.java => src/main/java/org/safcephfs/CephFSDocumentsProvider.java +8 -12
@@ 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<AutoCloseInputStream>(
+ 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<ParcelFileDescriptor>(
+ openDocument(documentId, "r", null))){
+ MediaMetadataReader.getMetadata(metadata,
+ fd.c().getFileDescriptor(), mimeType);
}
return metadata;
}
M src/main/java/org/safcephfs/MediaMetadataReader.java => src/main/java/org/safcephfs/MediaMetadataReader.java +19 -15
@@ 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<MediaMetadataRetriever>(
+ 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;
+ }
}
}
}
A src/main/java/org/safcephfs/UncheckedAutoCloseable.java => src/main/java/org/safcephfs/UncheckedAutoCloseable.java +13 -0
@@ 0,0 1,13 @@
+package org.safcephfs;
+
+public record UncheckedAutoCloseable<T extends AutoCloseable>(T c)
+ implements AutoCloseable {
+
+ public void close() {
+ try {
+ c.close();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+}