aboutsummaryrefslogtreecommitdiffstats
path: root/org_apg/src/org/apg/DataSource.java
blob: 340afa9f81f37abccb09bc65cb67e0e3f3e1c70f (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.apg;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apg.Apg.GeneralException;
import org.apg.R;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;

public class DataSource {
    private Uri mContentUri = null;
    private String mText = null;
    private byte[] mData = null;

    public DataSource() {

    }

    public void setUri(Uri uri) {
        mContentUri = uri;
        mText = null;
        mData = null;
    }

    public void setUri(String uri) {
        if (uri.startsWith("/")) {
            setUri(Uri.parse("file://" + uri));
        } else {
            setUri(Uri.parse(uri));
        }
    }

    public void setText(String text) {
        mText = text;
        mData = null;
        mContentUri = null;
    }

    public void setData(byte[] data) {
        mData = data;
        mText = null;
        mContentUri = null;
    }

    public boolean isText() {
        return mText != null;
    }

    public boolean isBinary() {
        return mData != null || mContentUri != null;
    }

    public InputData getInputData(Context context, boolean withSize) throws GeneralException,
            FileNotFoundException, IOException {
        InputStream in = null;
        long size = 0;

        if (mContentUri != null) {
            if (mContentUri.getScheme().equals("file")) {
                // get the rest after "file://"
                String path = Uri.decode(mContentUri.toString().substring(7));
                if (path.startsWith(Environment.getExternalStorageDirectory().getAbsolutePath())) {
                    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                        throw new GeneralException(
                                context.getString(R.string.error_externalStorageNotReady));
                    }
                }
                in = new FileInputStream(path);
                File file = new File(path);
                if (withSize) {
                    size = file.length();
                }
            } else {
                in = context.getContentResolver().openInputStream(mContentUri);
                if (withSize) {
                    InputStream tmp = context.getContentResolver().openInputStream(mContentUri);
                    size = Apg.getLengthOfStream(tmp);
                    tmp.close();
                }
            }
        } else if (mText != null || mData != null) {
            byte[] bytes = null;
            if (mData != null) {
                bytes = mData;
            } else {
                bytes = mText.getBytes();
            }
            in = new ByteArrayInputStream(bytes);
            if (withSize) {
                size = bytes.length;
            }
        }

        return new InputData(in, size);
    }

}