1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
35 * DataContentHandler for image/gif.
36 *
37 * @author Ana Lindstrom-Tamer
38 */
39 public class GifDataContentHandler extends Component implements DataContentHandler {
40 private static ActivationDataFlavor myDF =
41 new ActivationDataFlavor(
42 java.awt.Image.class,
43 "image/gif",
44 "GIF Image");
45
46 protected ActivationDataFlavor getDF() {
47 return myDF;
48 }
49
50 /**
51 * Return the DataFlavors for this <code>DataContentHandler</code>.
52 *
53 * @return The DataFlavors
54 */
55 public DataFlavor[] getTransferDataFlavors() { // throws Exception;
56 return new DataFlavor[] { getDF()};
57 }
58
59 /**
60 * Return the Transfer Data of type DataFlavor from InputStream.
61 *
62 * @param df The DataFlavor
63 * @param ins The InputStream corresponding to the data
64 * @return String object
65 */
66 public Object getTransferData(DataFlavor df, DataSource ds)
67 throws IOException {
68 // use myDF.equals to be sure to get ActivationDataFlavor.equals,
69 // which properly ignores Content-Type parameters in comparison
70 if (getDF().equals(df))
71 return getContent(ds);
72 else
73 return null;
74 }
75
76 public Object getContent(DataSource ds) throws IOException {
77 InputStream is = ds.getInputStream();
78 int pos = 0;
79 int count;
80 byte buf[] = new byte[1024];
81
82 while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
83 pos += count;
84 if (pos >= buf.length) {
85 int size = buf.length;
86 if (size < 256*1024)
87 size += size;
88 else
89 size += 256*1024;
90 byte tbuf[] = new byte[size];
91 System.arraycopy(buf, 0, tbuf, 0, pos);
92 buf = tbuf;
93 }
94 }
95 Toolkit tk = Toolkit.getDefaultToolkit();
96 return tk.createImage(buf, 0, pos);
97 }
98
99 /**
100 * Write the object to the output stream, using the specified MIME type.
101 */
102 public void writeTo(Object obj, String type, OutputStream os)
103 throws IOException {
104 if (obj != null && !(obj instanceof Image))
105 throw new IOException("\"" + getDF().getMimeType() +
106 "\" DataContentHandler requires Image object, " +
107 "was given object of type " + obj.getClass().toString());
108
109 throw new IOException(getDF().getMimeType() + " encoding not supported");
110 }
111
112
113 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
35 * DataContentHandler for image/gif.
36 *
37 * @author Ana Lindstrom-Tamer
38 */
39 public class GifDataContentHandler extends Component implements DataContentHandler {
40 private static ActivationDataFlavor myDF =
41 new ActivationDataFlavor(
42 java.awt.Image.class,
43 "image/gif",
44 "GIF Image");
45
46 protected ActivationDataFlavor getDF() {
47 return myDF;
48 }
49
50 /**
51 * Return the DataFlavors for this <code>DataContentHandler</code>.
52 *
53 * @return The DataFlavors
54 */
55 @Override
56 public DataFlavor[] getTransferDataFlavors() { // throws Exception;
57 return new DataFlavor[] { getDF()};
58 }
59
60 /**
61 * Return the Transfer Data of type DataFlavor from InputStream.
62 *
63 * @param df The DataFlavor
64 * @param ds The DataSource
65 * @return String object
66 * @exception IOException in case of an I/O error
67 */
68 @Override
69 public Object getTransferData(DataFlavor df, DataSource ds)
70 throws IOException {
71 // use myDF.equals to be sure to get ActivationDataFlavor.equals,
72 // which properly ignores Content-Type parameters in comparison
73 if (getDF().equals(df))
74 return getContent(ds);
75 else
76 return null;
77 }
78
79 @Override
80 public Object getContent(DataSource ds) throws IOException {
81 InputStream is = ds.getInputStream();
82 int pos = 0;
83 int count;
84 byte buf[] = new byte[1024];
85
86 while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
87 pos += count;
88 if (pos >= buf.length) {
89 int size = buf.length;
90 if (size < 256*1024)
91 size += size;
92 else
93 size += 256*1024;
94 byte tbuf[] = new byte[size];
95 System.arraycopy(buf, 0, tbuf, 0, pos);
96 buf = tbuf;
97 }
98 }
99 Toolkit tk = Toolkit.getDefaultToolkit();
100 return tk.createImage(buf, 0, pos);
101 }
102
103 /**
104 * Write the object to the output stream, using the specified MIME type.
105 * @param obj object to write
106 * @param type requested MIME type of the resulting byte stream
107 * @param os OutputStream
108 */
109 @Override
110 public void writeTo(Object obj, String type, OutputStream os)
111 throws IOException {
112 if (obj != null && !(obj instanceof Image))
113 throw new IOException("\"" + getDF().getMimeType() +
114 "\" DataContentHandler requires Image object, " +
115 "was given object of type " + obj.getClass().toString());
116
117 throw new IOException(getDF().getMimeType() + " encoding not supported");
118 }
119
120
121 }
|