1387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount/*
2387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * Copyright (C) 2010 The Android Open Source Project
3387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount *
4387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * Licensed under the Apache License, Version 2.0 (the "License");
5387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * you may not use this file except in compliance with the License.
6387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * You may obtain a copy of the License at
7387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount *
8387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount *      http://www.apache.org/licenses/LICENSE-2.0
9387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount *
10387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * Unless required by applicable law or agreed to in writing, software
11387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * distributed under the License is distributed on an "AS IS" BASIS,
12387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * See the License for the specific language governing permissions and
14387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * limitations under the License.
15387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount */
16387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mountpackage com.android.browser;
17387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount
18387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mountimport java.net.MalformedURLException;
19387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount
20387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mountimport libcore.io.Base64;
21387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount
22387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount/**
23387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * Class extracts the mime type and data from a data uri.
24387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * A data URI is of the form:
25387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * <pre>
26387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * data:[&lt;MIME-type&gt;][;charset=&lt;encoding&gt;][;base64],&lt;data&gt;
27387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount * </pre>
28387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount */
29387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mountpublic class DataUri {
30387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    private static final String DATA_URI_PREFIX = "data:";
31387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    private static final String BASE_64_ENCODING = ";base64";
32387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount
33387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    private String mMimeType;
34387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    private byte[] mData;
35387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount
36387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    public DataUri(String uri) throws MalformedURLException {
37387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        if (!isDataUri(uri)) {
38387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount            throw new MalformedURLException("Not a data URI");
39387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        }
40387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount
41387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        int commaIndex = uri.indexOf(',', DATA_URI_PREFIX.length());
42387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        if (commaIndex < 0) {
43387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount            throw new MalformedURLException("Comma expected in data URI");
44387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        }
45387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        String contentType = uri.substring(DATA_URI_PREFIX.length(),
46387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount                commaIndex);
47387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        mData = uri.substring(commaIndex + 1).getBytes();
48387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        if (contentType.contains(BASE_64_ENCODING)) {
49387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount            mData = Base64.decode(mData);
50387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        }
51387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        int semiIndex = contentType.indexOf(';');
52387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        if (semiIndex > 0) {
53387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount            mMimeType = contentType.substring(0, semiIndex);
54387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        } else {
55387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount            mMimeType = contentType;
56387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        }
57387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    }
58387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount
59387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    /**
60387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount     * Returns true if the text passed in appears to be a data URI.
61387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount     */
62387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    public static boolean isDataUri(String text)
63387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    {
64387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        return text.startsWith(DATA_URI_PREFIX);
65387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    }
66387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount
67387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    public String getMimeType() {
68387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        return mMimeType;
69387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    }
70387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount
71387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    public byte[] getData() {
72387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount        return mData;
73387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount    }
74387d45d2284c7fd7f12cbadc96161f946ae29cadGeorge Mount}
75