blob: dcdc949c551b5d08aac69ca6d69bc313476ede57 [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
19import org.apache.http.protocol.HTTP;
20
21import android.net.http.Headers;
22
23import java.io.ByteArrayInputStream;
24
25/**
26 * This class is a concrete implementation of StreamLoader that uses the
27 * content supplied as a URL as the source for the stream. The mimetype
28 * optionally provided in the URL is extracted and inserted into the HTTP
29 * response headers.
30 */
31class DataLoader extends StreamLoader {
32
33 private String mContentType; // Content mimetype, if supplied in URL
34
35 /**
36 * Constructor uses the dataURL as the source for an InputStream
37 * @param dataUrl data: URL string optionally containing a mimetype
38 * @param loadListener LoadListener to pass the content to
39 */
40 DataLoader(String dataUrl, LoadListener loadListener) {
41 super(loadListener);
42
43 String url = dataUrl.substring("data:".length());
44 String content;
45 int commaIndex = url.indexOf(',');
46 if (commaIndex != -1) {
47 mContentType = url.substring(0, commaIndex);
48 content = url.substring(commaIndex + 1);
49 } else {
50 content = url;
51 }
52 mDataStream = new ByteArrayInputStream(content.getBytes());
53 mContentLength = content.length();
54 }
55
56 @Override
57 protected boolean setupStreamAndSendStatus() {
58 mHandler.status(1, 1, 0, "OK");
59 return true;
60 }
61
62 @Override
63 protected void buildHeaders(Headers headers) {
64 if (mContentType != null) {
65 headers.setContentType(mContentType);
66 }
67 }
68
69 /**
70 * Construct a DataLoader and instruct it to start loading.
71 *
72 * @param url data: URL string optionally containing a mimetype
73 * @param loadListener LoadListener to pass the content to
74 */
75 public static void requestUrl(String url, LoadListener loadListener) {
76 DataLoader loader = new DataLoader(url, loadListener);
77 loader.load();
78 }
79
80}