< prev index next >

src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/soap/ImageDataContentHandler.java

Print this page


   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


 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 i = ImageIO.getImageWritersByMIMEType(type);
 135             if (i.hasNext()) {
 136                 writer = (ImageWriter)i.next();
 137             }
 138             if (writer != null) {
 139                 ImageOutputStream stream = null;
 140                 stream = ImageIO.createImageOutputStream(os);
 141                 writer.setOutput(stream);
 142                 writer.write(bufImage);
 143                 writer.dispose();
 144                 stream.close();
 145             } else {
 146                 log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type",
 147                     new String[] { type });
 148                 throw new IOException("Unsupported mime type:"+ type);
 149             }
 150         } catch (Exception e) {
 151             log.severe("SAAJ0525.soap.cannot.encode.img");
 152             throw new IOException("Unable to encode the image to a stream "
 153                 + e.getMessage());
 154         }
 155     }
 156 
   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


 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);
 135             if (i.hasNext()) {
 136                 writer = i.next();
 137             }
 138             if (writer != null) {
 139                 ImageOutputStream stream = null;
 140                 stream = ImageIO.createImageOutputStream(os);
 141                 writer.setOutput(stream);
 142                 writer.write(bufImage);
 143                 writer.dispose();
 144                 stream.close();
 145             } else {
 146                 log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type",
 147                     new String[] { type });
 148                 throw new IOException("Unsupported mime type:"+ type);
 149             }
 150         } catch (Exception e) {
 151             log.severe("SAAJ0525.soap.cannot.encode.img");
 152             throw new IOException("Unable to encode the image to a stream "
 153                 + e.getMessage());
 154         }
 155     }
 156 
< prev index next >