aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java
blob: 729bdf111c3b04bcbfcc34f79885277c5b16190f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
    }
}