View | Details | Raw Unified | Return to bug 58193
Collapse All | Expand All

(-)a/src/java/org/apache/poi/util/IOUtils.java (+26 lines)
Lines 166-177 public final class IOUtils { Link Here
166
        }
166
        }
167
    }
167
    }
168
168
169
170
    /**
171
     * Calculate checksum on input data
172
     */
169
    public static long calculateChecksum(byte[] data) {
173
    public static long calculateChecksum(byte[] data) {
170
        Checksum sum = new CRC32();
174
        Checksum sum = new CRC32();
171
        sum.update(data, 0, data.length);
175
        sum.update(data, 0, data.length);
172
        return sum.getValue();
176
        return sum.getValue();
173
    }
177
    }
174
178
179
180
    /**
181
     * Calculate checksum on all the data read from input stream.
182
     *
183
     * This should be more efficient than the equivalent code
184
     * {@code IOUtils.calculateChecksum(IOUtils.toByteArray(stream))}
185
     */
186
    public static long calculateChecksum(InputStream stream) throws IOException
187
    {
188
        Checksum sum = new CRC32();
189
190
        byte[] buf = new byte[4096];
191
        int count;
192
        while ((count = stream.read(buf)) != -1) {
193
            if (count > 0) {
194
                sum.update(buf, 0, count);
195
            }
196
        }
197
        return sum.getValue();
198
    }
199
200
175
    /**
201
    /**
176
     * Quietly (no exceptions) close Closable resource. In case of error it will
202
     * Quietly (no exceptions) close Closable resource. In case of error it will
177
     * be printed to {@link IOUtils} class logger.
203
     * be printed to {@link IOUtils} class logger.
(-)a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java (-10 / +23 lines)
Lines 20-25 Link Here
20
package org.apache.poi.xslf.usermodel;
20
package org.apache.poi.xslf.usermodel;
21
21
22
import java.io.IOException;
22
import java.io.IOException;
23
import java.io.InputStream;
23
import java.io.OutputStream;
24
import java.io.OutputStream;
24
25
25
import org.apache.poi.POIXMLDocumentPart;
26
import org.apache.poi.POIXMLDocumentPart;
Lines 95-116 public final class XSLFPictureData extends POIXMLDocumentPart implements Picture Link Here
95
        super(part, rel);
96
        super(part, rel);
96
    }
97
    }
97
98
99
100
    /**
101
     * An InputStream to read the picture data directly
102
     * from the underlying package part
103
     *
104
     * @return InputStream
105
     */
106
    public InputStream getInputStream() throws IOException
107
    {
108
        return getPackagePart().getInputStream();
109
    }
110
98
    /**
111
    /**
99
     * Gets the picture data as a byte array.
112
     * Gets the picture data as a byte array.
100
     * <p>
113
     * <p>
101
     * Note, that this call might be expensive since all the picture data is copied into a temporary byte array.
114
     * Note, that this call might be expensive since all the picture data is copied into a temporary byte array.
102
     * You can grab the picture data directly from the underlying package part as follows:
115
     * You can grab the picture data directly from the underlying package part with the {@code getInputStream()} method
103
     * <br/>
104
     * <code>
105
     * InputStream is = getPackagePart().getInputStream();
106
     * </code>
107
     * </p>
116
     * </p>
108
     *
117
     *
109
     * @return the Picture data.
118
     * @return the Picture data.
110
     */
119
     */
111
    public byte[] getData() {
120
    public byte[] getData() {
112
        try {
121
        try {
113
            return IOUtils.toByteArray(getPackagePart().getInputStream());
122
            return IOUtils.toByteArray(getInputStream());
114
        } catch (IOException e) {
123
        } catch (IOException e) {
115
            throw new POIXMLException(e);
124
            throw new POIXMLException(e);
116
        }
125
        }
Lines 161-169 public final class XSLFPictureData extends POIXMLDocumentPart implements Picture Link Here
161
    }
170
    }
162
171
163
    long getChecksum(){
172
    long getChecksum(){
164
        if(checksum == null){
173
        if (checksum == null) {
165
            byte[] pictureData = getData();
174
            try {
166
            checksum = IOUtils.calculateChecksum(pictureData);
175
                checksum = IOUtils.calculateChecksum(getInputStream());
176
            } catch (IOException e) {
177
                throw new POIXMLException(e);
178
            }
167
        }
179
        }
168
        return checksum;
180
        return checksum;
169
    }
181
    }
Lines 204-210 public final class XSLFPictureData extends POIXMLDocumentPart implements Picture Link Here
204
        os.close();
216
        os.close();
205
217
206
        // checksum is invalid
218
        // checksum is invalid
207
        checksum = null;
219
        // - recalculate now since we already have the data bytes available anyhow
220
        checksum = IOUtils.calculateChecksum(data);
208
    }
221
    }
209
    
222
    
210
    
223
    
(-)a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java (-4 / +1 lines)
Lines 91-100 public class XSLFPictureShape extends XSLFSimpleShape implements PictureShape { Link Here
91
     * for other types sets the default size of 200x200 pixels.
91
     * for other types sets the default size of 200x200 pixels.
92
     */
92
     */
93
    public void resize() {
93
    public void resize() {
94
        XSLFPictureData pict = getPictureData();
95
96
        try {
94
        try {
97
            BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
95
            BufferedImage img = ImageIO.read(getPictureData().getInputStream());
98
            setAnchor(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
96
            setAnchor(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
99
        }
97
        }
100
        catch (Exception e) {
98
        catch (Exception e) {
101
- 

Return to bug 58193