~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to src/java/org/apache/xmlgraphics/image/loader/impl/ImageRawStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-02-11 14:15:14 UTC
  • mfrom: (8.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110211141514-h67achft6x31gju1
Tags: 1.4.dfsg-3
Uploading to unstable, hoping we won't break too many things ;-)...

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
6
 * (the "License"); you may not use this file except in compliance with
7
7
 * the License.  You may obtain a copy of the License at
8
 
 * 
 
8
 *
9
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 
 * 
 
10
 *
11
11
 * Unless required by applicable law or agreed to in writing, software
12
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
 * limitations under the License.
16
16
 */
17
17
 
18
 
/* $Id: ImageRawStream.java 606580 2007-12-23 17:45:02Z jeremias $ */
 
18
/* $Id: ImageRawStream.java 734655 2009-01-15 10:14:39Z jeremias $ */
19
19
 
20
20
package org.apache.xmlgraphics.image.loader.impl;
21
21
 
 
22
import java.io.ByteArrayInputStream;
22
23
import java.io.File;
23
24
import java.io.IOException;
24
25
import java.io.InputStream;
28
29
 
29
30
import org.apache.xmlgraphics.image.loader.ImageFlavor;
30
31
import org.apache.xmlgraphics.image.loader.ImageInfo;
 
32
import org.apache.xmlgraphics.image.loader.MimeEnabledImageFlavor;
31
33
 
32
34
/**
33
35
 * This class is an implementation of the Image interface exposing an InputStream for loading the
37
39
 
38
40
    private ImageFlavor flavor;
39
41
    private InputStreamFactory streamFactory;
40
 
    
 
42
 
41
43
    /**
42
44
     * Main constructor.
43
45
     * @param info the image info object
49
51
        this.flavor = flavor;
50
52
        setInputStreamFactory(streamFactory);
51
53
    }
52
 
    
 
54
 
53
55
    /**
54
56
     * Constructor for a simple InputStream as parameter.
55
57
     * @param info the image info object
59
61
    public ImageRawStream(ImageInfo info, ImageFlavor flavor, InputStream in) {
60
62
        this(info, flavor, new SingleStreamFactory(in));
61
63
    }
62
 
    
 
64
 
63
65
    /** {@inheritDoc} */
64
66
    public ImageFlavor getFlavor() {
65
67
        return this.flavor;
66
68
    }
67
69
 
 
70
    /**
 
71
     * Returns the MIME type of the stream data.
 
72
     * @return the MIME type
 
73
     */
 
74
    public String getMimeType() {
 
75
        if (getFlavor() instanceof MimeEnabledImageFlavor) {
 
76
            return getFlavor().getMimeType();
 
77
        } else {
 
78
            //Undetermined
 
79
            return "application/octet-stream";
 
80
        }
 
81
    }
 
82
 
68
83
    /** {@inheritDoc} */
69
84
    public boolean isCacheable() {
70
85
        return !this.streamFactory.isUsedOnceOnly();
71
86
    }
72
 
    
 
87
 
73
88
    /**
74
89
     * Sets the InputStreamFactory to be used by this image. This method allows to replace the
75
90
     * original factory.
81
96
        }
82
97
        this.streamFactory = factory;
83
98
    }
84
 
    
 
99
 
85
100
    /**
86
101
     * Returns a new InputStream to access the raw image.
87
102
     * @return the InputStream
89
104
    public InputStream createInputStream() {
90
105
        return this.streamFactory.createInputStream();
91
106
    }
92
 
    
 
107
 
93
108
    /**
94
109
     * Writes the content of the image to an OutputStream. The OutputStream in NOT closed at the
95
110
     * end.
104
119
            IOUtils.closeQuietly(in);
105
120
        }
106
121
    }
107
 
    
 
122
 
108
123
    /**
109
124
     * Writes the content of the image to a File.
110
125
     * @param target the file to be written
118
133
            IOUtils.closeQuietly(out);
119
134
        }
120
135
    }
121
 
    
 
136
 
122
137
    /**
123
138
     * Represents a factory for InputStream objects. Make sure the class is thread-safe!
124
139
     */
125
140
    public interface InputStreamFactory {
126
 
        
 
141
 
127
142
        /**
128
143
         * Indicates whether this factory is only usable once or many times.
129
144
         * @return true if the factory can only be used once
130
145
         */
131
146
        boolean isUsedOnceOnly();
132
 
        
 
147
 
133
148
        /**
134
149
         * Creates and returns a new InputStream.
135
150
         * @return the new InputStream
136
151
         */
137
152
        InputStream createInputStream();
138
 
        
 
153
 
139
154
        /**
140
155
         * Closes the factory and releases any resources held open during the lifetime of this
141
156
         * object.
142
157
         */
143
158
        void close();
144
 
        
 
159
 
145
160
    }
146
 
    
 
161
 
 
162
    /**
 
163
     * InputStream factory that can return a pre-constructed InputStream exactly once.
 
164
     */
147
165
    private static class SingleStreamFactory implements InputStreamFactory {
148
 
        
 
166
 
149
167
        private InputStream in;
150
 
        
 
168
 
151
169
        public SingleStreamFactory(InputStream in) {
152
170
            this.in = in;
153
171
        }
154
 
        
 
172
 
155
173
        public synchronized InputStream createInputStream() {
156
174
            if (this.in != null) {
157
175
                InputStream tempin = this.in;
175
193
        protected void finalize() {
176
194
            close();
177
195
        }
178
 
        
179
 
    }
180
 
    
 
196
 
 
197
    }
 
198
 
 
199
    /**
 
200
     * InputStream factory that wraps a byte array.
 
201
     */
 
202
    public static class ByteArrayStreamFactory implements InputStreamFactory {
 
203
 
 
204
        private byte[] data;
 
205
 
 
206
        /**
 
207
         * Main constructor.
 
208
         * @param data the byte array
 
209
         */
 
210
        public ByteArrayStreamFactory(byte[] data) {
 
211
            this.data = data;
 
212
        }
 
213
 
 
214
        /** {@inheritDoc} */
 
215
        public InputStream createInputStream() {
 
216
            return new ByteArrayInputStream(data);
 
217
        }
 
218
 
 
219
        /** {@inheritDoc} */
 
220
        public void close() {
 
221
            //nop
 
222
        }
 
223
 
 
224
        /** {@inheritDoc} */
 
225
        public boolean isUsedOnceOnly() {
 
226
            return false;
 
227
        }
 
228
 
 
229
    }
 
230
 
181
231
}