01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /* $Id$ */
19:
20: package org.apache.fop.util;
21:
22: import java.io.IOException;
23: import java.io.InputStream;
24: import java.io.StringWriter;
25: import java.io.Writer;
26:
27: import org.apache.commons.io.IOUtils;
28: import org.apache.xmlgraphics.util.io.Base64EncodeStream;
29:
30: /**
31: * Utility classes for generating RFC 2397 data URLs.
32: */
33: public class DataURLUtil {
34:
35: /**
36: * Creates a new data URL and returns it as a String.
37: * @param in the InputStream to read the data from
38: * @param mediatype the MIME type of the content, or null
39: * @return the newly created data URL
40: * @throws IOException if an I/O error occurs
41: */
42: public static String createDataURL(InputStream in, String mediatype)
43: throws IOException {
44: StringWriter writer = new StringWriter();
45: writeDataURL(in, mediatype, writer);
46: return writer.toString();
47: }
48:
49: /**
50: * Generates a data URL and writes it to a Writer.
51: * @param in the InputStream to read the data from
52: * @param mediatype the MIME type of the content, or null
53: * @param writer the Writer to write to
54: * @throws IOException if an I/O error occurs
55: */
56: public static void writeDataURL(InputStream in, String mediatype,
57: Writer writer) throws IOException {
58: writer.write("data:");
59: if (mediatype != null) {
60: writer.write(mediatype);
61: }
62: writer.write(";base64,");
63: Base64EncodeStream out = new Base64EncodeStream(
64: new WriterOutputStream(writer, "US-ASCII"));
65: IOUtils.copy(in, out);
66: out.flush();
67: }
68: }
|