blob: c1d1280b7787e03a2f40dd7bf72c14ba716e2c8c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import java.io.ByteArrayInputStream;
20
Patrick Scotte82dc422009-04-29 10:21:57 -040021import org.apache.harmony.luni.util.Base64;
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023/**
24 * This class is a concrete implementation of StreamLoader that uses the
25 * content supplied as a URL as the source for the stream. The mimetype
26 * optionally provided in the URL is extracted and inserted into the HTTP
27 * response headers.
28 */
29class DataLoader extends StreamLoader {
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 /**
32 * Constructor uses the dataURL as the source for an InputStream
33 * @param dataUrl data: URL string optionally containing a mimetype
34 * @param loadListener LoadListener to pass the content to
35 */
36 DataLoader(String dataUrl, LoadListener loadListener) {
37 super(loadListener);
38
39 String url = dataUrl.substring("data:".length());
Patrick Scotte82dc422009-04-29 10:21:57 -040040 byte[] data = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 int commaIndex = url.indexOf(',');
42 if (commaIndex != -1) {
Patrick Scotte82dc422009-04-29 10:21:57 -040043 String contentType = url.substring(0, commaIndex);
44 data = url.substring(commaIndex + 1).getBytes();
45 loadListener.parseContentTypeHeader(contentType);
46 if (loadListener.transferEncoding().equals("base64")) {
47 data = Base64.decode(data);
48 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 } else {
Patrick Scotte82dc422009-04-29 10:21:57 -040050 data = url.getBytes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 }
Patrick Scotte82dc422009-04-29 10:21:57 -040052 mDataStream = new ByteArrayInputStream(data);
53 mContentLength = data.length;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 }
55
56 @Override
57 protected boolean setupStreamAndSendStatus() {
58 mHandler.status(1, 1, 0, "OK");
59 return true;
60 }
61
62 @Override
Patrick Scotte82dc422009-04-29 10:21:57 -040063 protected void buildHeaders(android.net.http.Headers h) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 }
65
66 /**
67 * Construct a DataLoader and instruct it to start loading.
68 *
69 * @param url data: URL string optionally containing a mimetype
70 * @param loadListener LoadListener to pass the content to
71 */
72 public static void requestUrl(String url, LoadListener loadListener) {
73 DataLoader loader = new DataLoader(url, loadListener);
74 loader.load();
75 }
76
77}