1 /*
2 * Copyright (c) 1997, 2012, 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
50
51 private DataFlavor[] flavor;
52
53 public ImageDataContentHandler() {
54 String[] mimeTypes = ImageIO.getReaderMIMETypes();
55 flavor = new DataFlavor[mimeTypes.length];
56 for(int i=0; i < mimeTypes.length; i++) {
57 flavor[i] = new ActivationDataFlavor(
58 java.awt.Image.class, mimeTypes[i], "Image");
59 }
60 }
61
62 /**
63 * Returns an array of DataFlavor objects indicating the flavors the
64 * data can be provided in. The array should be ordered according to
65 * preference for providing the data (from most richly descriptive to
66 * least descriptive).
67 *
68 * @return The DataFlavors.
69 */
70 public DataFlavor[] getTransferDataFlavors() {
71 return (DataFlavor[]) Arrays.copyOf(flavor, flavor.length);
72 }
73
74 /**
75 * Returns an object which represents the data to be transferred.
76 * The class of the object returned is defined by the representation class
77 * of the flavor.
78 *
79 * @param df The DataFlavor representing the requested type.
80 * @param ds The DataSource representing the data to be converted.
81 * @return The constructed Object.
82 */
83 public Object getTransferData(DataFlavor df, DataSource ds)
84 throws IOException {
85 for (int i=0; i < flavor.length; i++) {
86 if (flavor[i].equals(df)) {
87 return getContent(ds);
88 }
89 }
90 return null;
91 }
92
93 /**
94 * Return an object representing the data in its most preferred form.
95 * Generally this will be the form described by the first DataFlavor
96 * returned by the <code>getTransferDataFlavors</code> method.
97 *
98 * @param ds The DataSource representing the data to be converted.
99 * @return The constructed Object.
100 */
101 public Object getContent(DataSource ds) throws IOException {
102 return ImageIO.read(new BufferedInputStream(ds.getInputStream()));
103 }
104
105 /**
106 * Convert the object to a byte stream of the specified MIME type
107 * and write it to the output stream.
108 *
109 * @param obj The object to be converted.
110 * @param mimeType The requested MIME type of the resulting byte stream.
111 * @param os The output stream into which to write the converted
112 * byte stream.
113 */
114
115 public void writeTo(Object obj, String type, OutputStream os)
116 throws IOException {
117
118 try {
119 BufferedImage bufImage = null;
120 if (obj instanceof BufferedImage) {
121 bufImage = (BufferedImage)obj;
122 } else if (obj instanceof Image) {
123 bufImage = render((Image)obj);
124 } else {
125 log.log(Level.SEVERE,
126 "SAAJ0520.soap.invalid.obj.type",
127 new String[] { obj.getClass().toString() });
128 throw new IOException(
129 "ImageDataContentHandler requires Image object, "
130 + "was given object of type "
131 + obj.getClass().toString());
132 }
133 ImageWriter writer = null;
134 Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(type);
|
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
50
51 private DataFlavor[] flavor;
52
53 public ImageDataContentHandler() {
54 String[] mimeTypes = ImageIO.getReaderMIMETypes();
55 flavor = new DataFlavor[mimeTypes.length];
56 for(int i=0; i < mimeTypes.length; i++) {
57 flavor[i] = new ActivationDataFlavor(
58 java.awt.Image.class, mimeTypes[i], "Image");
59 }
60 }
61
62 /**
63 * Returns an array of DataFlavor objects indicating the flavors the
64 * data can be provided in. The array should be ordered according to
65 * preference for providing the data (from most richly descriptive to
66 * least descriptive).
67 *
68 * @return The DataFlavors.
69 */
70 @Override
71 public DataFlavor[] getTransferDataFlavors() {
72 return Arrays.copyOf(flavor, flavor.length);
73 }
74
75 /**
76 * Returns an object which represents the data to be transferred.
77 * The class of the object returned is defined by the representation class
78 * of the flavor.
79 *
80 * @param df The DataFlavor representing the requested type.
81 * @param ds The DataSource representing the data to be converted.
82 * @return The constructed Object.
83 */
84 @Override
85 public Object getTransferData(DataFlavor df, DataSource ds)
86 throws IOException {
87 for (int i=0; i < flavor.length; i++) {
88 if (flavor[i].equals(df)) {
89 return getContent(ds);
90 }
91 }
92 return null;
93 }
94
95 /**
96 * Return an object representing the data in its most preferred form.
97 * Generally this will be the form described by the first DataFlavor
98 * returned by the <code>getTransferDataFlavors</code> method.
99 *
100 * @param ds The DataSource representing the data to be converted.
101 * @return The constructed Object.
102 */
103 @Override
104 public Object getContent(DataSource ds) throws IOException {
105 return ImageIO.read(new BufferedInputStream(ds.getInputStream()));
106 }
107
108 /**
109 * Convert the object to a byte stream of the specified MIME type
110 * and write it to the output stream.
111 *
112 * @param obj The object to be converted.
113 * @param type The requested MIME type of the resulting byte stream.
114 * @param os The output stream into which to write the converted
115 * byte stream.
116 */
117 @Override
118 public void writeTo(Object obj, String type, OutputStream os)
119 throws IOException {
120
121 try {
122 BufferedImage bufImage = null;
123 if (obj instanceof BufferedImage) {
124 bufImage = (BufferedImage)obj;
125 } else if (obj instanceof Image) {
126 bufImage = render((Image)obj);
127 } else {
128 log.log(Level.SEVERE,
129 "SAAJ0520.soap.invalid.obj.type",
130 new String[] { obj.getClass().toString() });
131 throw new IOException(
132 "ImageDataContentHandler requires Image object, "
133 + "was given object of type "
134 + obj.getClass().toString());
135 }
136 ImageWriter writer = null;
137 Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(type);
|