aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java
new file mode 100644
index 00000000..729bdf11
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java
@@ -0,0 +1,63 @@
+package de.thedevstack.conversationsplus.utils;
+
+import android.net.Uri;
+
+import java.io.Closeable;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.Socket;
+
+import de.thedevstack.conversationsplus.ConversationsPlusApplication;
+
+/**
+ * Util to handle streams.
+ */
+public final class StreamUtil {
+
+ /**
+ * Opens an InputStream from Uri using the ContentResolver from application.
+ * @see android.content.ContentResolver#openInputStream(Uri)
+ * @param uri the uri to open
+ * @return the InputStream for given uri
+ * @throws FileNotFoundException if the provided URI could not be opened.
+ */
+ public static InputStream openInputStreamFromContentResolver(Uri uri) throws FileNotFoundException {
+ return ConversationsPlusApplication.getInstance().getContentResolver().openInputStream(uri);
+ }
+
+ /**
+ * Closes a stream.
+ * IOException is silently ignored.
+ * @param stream the stream to close
+ */
+ public static void close(Closeable stream) {
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ /**
+ * Closes a socket.
+ * IOException is silently ignored.
+ * @param socket the socket to close
+ */
+ public static void close(Socket socket) {
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ /**
+ * Avoid instantiation of util class.
+ */
+ private StreamUtil() {
+ // Static helper class
+ }
+}