Skip to content

Package: JpegDataContentHandler

JpegDataContentHandler

nameinstructionbranchcomplexitylinemethod
JpegDataContentHandler()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getContent(DataSource)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getTransferData(ActivationDataFlavor, DataSource)
M: 30 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
getTransferDataFlavors()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
writeTo(Object, String, OutputStream)
M: 84 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.messaging.saaj.soap;
12:
13: import java.awt.*;
14: import java.awt.image.BufferedImage;
15: import java.io.*;
16:
17: import jakarta.activation.*;
18:
19: //import com.sun.image.codec.jpeg.*;
20: import javax.imageio.ImageIO;
21:
22: /**
23: * JAF data handler for Jpeg content
24: *
25: * @author Ana Lindstrom-Tamer
26: */
27:
28: public class JpegDataContentHandler
29: extends Component
30: implements DataContentHandler {
31: public static final String STR_SRC = "java.awt.Image";
32:
33: /**
34: * Return the DataFlavors for this <code>DataContentHandler</code>
35: * @return The DataFlavors.
36: */
37: @Override
38: public ActivationDataFlavor[] getTransferDataFlavors() { // throws Exception;
39: ActivationDataFlavor flavors[] = new ActivationDataFlavor[1];
40:
41: try {
42: flavors[0] =
43: new ActivationDataFlavor(
44: Class.forName(STR_SRC),
45: "image/jpeg",
46: "JPEG");
47: } catch (Exception e) {
48: System.out.println(e);
49: }
50:
51: return flavors;
52: }
53:
54: /**
55: * Return the Transfer Data of type DataFlavor from InputStream
56: * @param df The DataFlavor
57: * @param ds The DataSource
58: * @return The constructed Object.
59: */
60: @Override
61: public Object getTransferData(ActivationDataFlavor df, DataSource ds) {
62:
63: // this is sort of hacky, but will work for the
64: // sake of testing...
65:• if (df.getMimeType().startsWith("image/jpeg")) {
66:• if (df.getRepresentationClass().getName().equals(STR_SRC)) {
67: InputStream inputStream = null;
68: BufferedImage jpegLoadImage = null;
69:
70: try {
71: inputStream = ds.getInputStream();
72: jpegLoadImage = ImageIO.read(inputStream);
73:
74: } catch (Exception e) {
75: System.out.println(e);
76: }
77:
78: return jpegLoadImage;
79: }
80: }
81: return null;
82: }
83:
84: /**
85: *
86: */
87: @Override
88: public Object getContent(DataSource ds) { // throws Exception;
89: InputStream inputStream = null;
90: BufferedImage jpegLoadImage = null;
91:
92: try {
93: inputStream = ds.getInputStream();
94: jpegLoadImage = ImageIO.read(inputStream);
95:
96: } catch (Exception e) {
97: }
98:
99: return jpegLoadImage;
100: }
101:
102: /**
103: * Construct an object from a byte stream
104: * (similar semantically to previous method, we are deciding
105: * which one to support)
106: * @param obj object to write
107: * @param mimeType requested MIME type of the resulting byte stream
108: * @param os OutputStream
109: */
110: @Override
111: public void writeTo(Object obj, String mimeType, OutputStream os)
112: throws IOException {
113:• if (!mimeType.equals("image/jpeg"))
114: throw new IOException(
115: "Invalid content type \""
116: + mimeType
117: + "\" for ImageContentHandler");
118:
119:• if (obj == null) {
120: throw new IOException("Null object for ImageContentHandler");
121: }
122:
123: try {
124: BufferedImage bufImage = null;
125:• if (obj instanceof BufferedImage) {
126: bufImage = (BufferedImage) obj;
127:
128: } else {
129: Image img = (Image) obj;
130: MediaTracker tracker = new MediaTracker(this);
131: tracker.addImage(img, 0);
132: tracker.waitForAll();
133:• if (tracker.isErrorAny()) {
134:                         throw new IOException("Error while loading image");
135:                 }
136: bufImage =
137: new BufferedImage(
138: img.getWidth(null),
139: img.getHeight(null),
140: BufferedImage.TYPE_INT_RGB);
141:
142: Graphics g = bufImage.createGraphics();
143: g.drawImage(img, 0, 0, null);
144: }
145: ImageIO.write(bufImage, "jpeg", os);
146:
147: } catch (Exception ex) {
148: throw new IOException(
149: "Unable to run the JPEG Encoder on a stream "
150: + ex.getMessage());
151: }
152: }
153: }