The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.webkit; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import java.io.ByteArrayInputStream; |
| 20 | |
Patrick Scott | e82dc42 | 2009-04-29 10:21:57 -0400 | [diff] [blame] | 21 | import org.apache.harmony.luni.util.Base64; |
| 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | /** |
| 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 | */ |
| 29 | class DataLoader extends StreamLoader { |
| 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | /** |
| 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 Scott | e82dc42 | 2009-04-29 10:21:57 -0400 | [diff] [blame] | 40 | byte[] data = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | int commaIndex = url.indexOf(','); |
| 42 | if (commaIndex != -1) { |
Patrick Scott | e82dc42 | 2009-04-29 10:21:57 -0400 | [diff] [blame] | 43 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | } else { |
Patrick Scott | e82dc42 | 2009-04-29 10:21:57 -0400 | [diff] [blame] | 50 | data = url.getBytes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | } |
Patrick Scott | e82dc42 | 2009-04-29 10:21:57 -0400 | [diff] [blame] | 52 | mDataStream = new ByteArrayInputStream(data); |
| 53 | mContentLength = data.length; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected boolean setupStreamAndSendStatus() { |
| 58 | mHandler.status(1, 1, 0, "OK"); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | @Override |
Patrick Scott | e82dc42 | 2009-04-29 10:21:57 -0400 | [diff] [blame] | 63 | protected void buildHeaders(android.net.http.Headers h) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
| 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 | } |