KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > spi > MagicNumberRegistryEntry


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image.spi;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.StreamCorruptedException JavaDoc;
23
24 /**
25  * This Image tag registry entry is built around the notion of magic
26  * numbers. These are strings of bytes that are at a well known
27  * location in the input stream (often the start).
28  *
29  * This base class can handle the compatiblity check based on a list
30  * of Magic Numbers that correspond to your format (Some formats have
31  * multiple magic numbers associated with them).
32  */

33 public abstract class MagicNumberRegistryEntry
34     extends AbstractRegistryEntry
35     implements StreamRegistryEntry {
36
37     public static final float PRIORITY = 1000;
38
39     /**
40      * Inner class that represents one magic number. Simply consists
41      * of an offset in bytes from the start of the file, and a byte
42      * array that must match.
43      */

44     public static class MagicNumber {
45         int offset;
46         byte [] magicNumber;
47         byte [] buffer;
48         
49         /**
50          * Constructor.
51          * @param offset the location of the magic number in file.
52          * @param magicNumber the sequence of bytes that must match.
53          */

54         public MagicNumber(int offset, byte[]magicNumber) {
55             this.offset = offset;
56             this.magicNumber = (byte[])magicNumber.clone();
57             buffer = new byte[magicNumber.length];
58         }
59
60         /**
61          * Returns the maximum number of bytes that will be read for
62          * this magic number compairison.
63          */

64         int getReadlimit() {
65             return offset+magicNumber.length;
66         }
67
68         /**
69          * Performs the check of is.
70          */

71         boolean isMatch(InputStream JavaDoc is)
72             throws StreamCorruptedException JavaDoc {
73             int idx = 0;
74             is.mark(getReadlimit());
75             try {
76                 // Skip to the offset location.
77
while (idx < offset) {
78                     int rn = (int)is.skip(offset-idx);
79                     if (rn == -1) return false;
80                     idx += rn;
81                 }
82         
83                 idx = 0;
84                 while (idx < buffer.length) {
85                     int rn = is.read(buffer, idx, buffer.length-idx);
86                     if (rn == -1) return false;
87                     idx += rn;
88                 }
89         
90                 for (int i=0; i<magicNumber.length; i++) {
91                     if (magicNumber[i] != buffer[i])
92                         return false;
93                 }
94             } catch (IOException JavaDoc ioe) {
95                 return false;
96             } finally {
97                 try {
98                     // Make sure we always put back what we have read.
99
// If this throws an IOException then the current
100
// stream should be closed an reopend by the registry.
101
is.reset();
102                 } catch (IOException JavaDoc ioe) {
103                     throw new StreamCorruptedException JavaDoc(ioe.getMessage());
104                 }
105             }
106             return true;
107         }
108     }
109
110     /** The list of magic numbers associated with this entry */
111     MagicNumber [] magicNumbers;
112
113     /**
114      * Constructor, simplifies construction of entry when only
115      * one extension and one magic number is required.
116      * @param name Format Name
117      * @param ext Standard extension
118      * @param offset Offset of magic number
119      * @param magicNumber byte array to match.
120      */

121     public MagicNumberRegistryEntry(String JavaDoc name,
122                                     String JavaDoc ext,
123                                     String JavaDoc mimeType,
124                                     int offset, byte[]magicNumber) {
125         super(name, PRIORITY, ext, mimeType);
126         magicNumbers = new MagicNumber[1];
127         magicNumbers[0] = new MagicNumber(offset, magicNumber);
128     }
129     
130     /**
131      * Constructor, simplifies construction of entry when only
132      * one extension is required.
133      * @param name Format Name
134      * @param ext Standard extension
135      * @param magicNumbers Array of magic numbers any of which can match.
136      */

137     public MagicNumberRegistryEntry(String JavaDoc name,
138                                     String JavaDoc ext,
139                                     String JavaDoc mimeType,
140                                     MagicNumber [] magicNumbers) {
141         super(name, PRIORITY, ext, mimeType);
142         this.magicNumbers = magicNumbers;
143     }
144
145     /**
146      * Constructor, simplifies construction of entry when only
147      * one magic number is required.
148      * @param name Format Name
149      * @param exts Standard set of extensions
150      * @param offset Offset of magic number
151      * @param magicNumber byte array to match.
152      */

153     public MagicNumberRegistryEntry(String JavaDoc name,
154                                     String JavaDoc [] exts,
155                                     String JavaDoc [] mimeTypes,
156                                     int offset, byte[]magicNumber) {
157         super(name, PRIORITY, exts, mimeTypes);
158         magicNumbers = new MagicNumber[1];
159         magicNumbers[0] = new MagicNumber(offset, magicNumber);
160     }
161     
162     /**
163      * Constructor
164      * @param name Format Name
165      * @param exts Standard set of extensions
166      * @param magicNumbers array of magic numbers any of which can match.
167      */

168     public MagicNumberRegistryEntry(String JavaDoc name,
169                                     String JavaDoc [] exts,
170                                     String JavaDoc [] mimeTypes,
171                                     MagicNumber [] magicNumbers) {
172         super(name, PRIORITY, exts, mimeTypes);
173         this.magicNumbers = magicNumbers;
174     }
175     
176     /**
177      * Constructor, allows for overriding the default priority of
178      * magic number entries. This should be needed very rarely since
179      * magic number checks are fairly relyable and hence aren't usually
180      * sensative to order issues.
181      * @param name Format Name
182      * @param exts Standard set of extensions
183      * @param magicNumbers array of magic numbers any of which can match.
184      * @param priority The priority of this entry (1000 is baseline)
185      */

186     public MagicNumberRegistryEntry(String JavaDoc name,
187                                     String JavaDoc [] exts,
188                                     String JavaDoc [] mimeTypes,
189                                     MagicNumber [] magicNumbers,
190                                     float priority) {
191         super(name, priority, exts, mimeTypes);
192         this.magicNumbers = magicNumbers;
193     }
194
195     /**
196      * Returns the maximume read ahead needed for all magic numbers.
197      */

198     public int getReadlimit() {
199         int maxbuf = 0;
200         for (int i=0; i<magicNumbers.length; i++) {
201             int req = magicNumbers[i].getReadlimit();
202             if (req > maxbuf) maxbuf = req;
203         }
204         return maxbuf;
205     }
206     
207     /**
208      * Check if the stream contains an image that can be
209      * handled by this format handler
210      */

211     public boolean isCompatibleStream(InputStream JavaDoc is)
212         throws StreamCorruptedException JavaDoc {
213         for (int i=0; i<magicNumbers.length; i++) {
214             if (magicNumbers[i].isMatch(is))
215                 return true;
216         }
217
218         return false;
219     }
220 }
221
Popular Tags