1 18 package org.apache.batik.ext.awt.image.spi; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.StreamCorruptedException ; 23 24 33 public abstract class MagicNumberRegistryEntry 34 extends AbstractRegistryEntry 35 implements StreamRegistryEntry { 36 37 public static final float PRIORITY = 1000; 38 39 44 public static class MagicNumber { 45 int offset; 46 byte [] magicNumber; 47 byte [] buffer; 48 49 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 64 int getReadlimit() { 65 return offset+magicNumber.length; 66 } 67 68 71 boolean isMatch(InputStream is) 72 throws StreamCorruptedException { 73 int idx = 0; 74 is.mark(getReadlimit()); 75 try { 76 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 ioe) { 95 return false; 96 } finally { 97 try { 98 is.reset(); 102 } catch (IOException ioe) { 103 throw new StreamCorruptedException (ioe.getMessage()); 104 } 105 } 106 return true; 107 } 108 } 109 110 111 MagicNumber [] magicNumbers; 112 113 121 public MagicNumberRegistryEntry(String name, 122 String ext, 123 String 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 137 public MagicNumberRegistryEntry(String name, 138 String ext, 139 String mimeType, 140 MagicNumber [] magicNumbers) { 141 super(name, PRIORITY, ext, mimeType); 142 this.magicNumbers = magicNumbers; 143 } 144 145 153 public MagicNumberRegistryEntry(String name, 154 String [] exts, 155 String [] 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 168 public MagicNumberRegistryEntry(String name, 169 String [] exts, 170 String [] mimeTypes, 171 MagicNumber [] magicNumbers) { 172 super(name, PRIORITY, exts, mimeTypes); 173 this.magicNumbers = magicNumbers; 174 } 175 176 186 public MagicNumberRegistryEntry(String name, 187 String [] exts, 188 String [] mimeTypes, 189 MagicNumber [] magicNumbers, 190 float priority) { 191 super(name, priority, exts, mimeTypes); 192 this.magicNumbers = magicNumbers; 193 } 194 195 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 211 public boolean isCompatibleStream(InputStream is) 212 throws StreamCorruptedException { 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 |