/Users/lyon/j4p/src/futils/StreamSniffer.java

1    package futils; 
2     
3    // main class futils.StreamSniffer 
4     
5    import utils.ByteUtil; 
6     
7    import java.io.BufferedInputStream; 
8    import java.io.IOException; 
9    import java.io.*; 
10   import java.util.Hashtable; 
11    
12   public class StreamSniffer { 
13       private BufferedInputStream bis; 
14       private byte header[] = new byte[6]; 
15       private int numberActuallyRead = 0; 
16    
17       public StreamSniffer(InputStream is) { 
18           bis = new BufferedInputStream(is); 
19           init(); 
20           sniff(); 
21       } 
22    
23       public static void main(String args[]) { 
24           FileInputStream fis = 
25                   futils.Futil.getFileInputStream( 
26                           "select a new data file"); 
27           StreamSniffer ss = 
28                   new StreamSniffer(fis); 
29    
30           System.out.println("fee fi fo fum I smell:" + ss); 
31           futils.Futil.close(fis); 
32       } 
33    
34       public BufferedInputStream getStream() { 
35           return bis; 
36       } 
37    
38       // Sniff and cover your tracks! 
39       private void sniff() { 
40           if (!bis.markSupported()) { 
41               System.out.println( 
42                       "StreamSniffer needs" + 
43                       " a markable stream"); 
44               return; 
45           } 
46           bis.mark(header.length); 
47    
48           try { 
49               numberActuallyRead 
50                       = bis.read(header); 
51               bis.reset(); 
52           } catch (IOException e) { 
53               System.out.println(e); 
54               numberActuallyRead = -1; 
55           } 
56    
57       } 
58    
59       public void printHeader() { 
60           System.out.println("In hex..."); 
61           for (int i = 0; i < header.length; i++) 
62               ByteUtil.printToHex(header[i]); 
63           System.out.println("\nin base 8..."); 
64           for (int i = 0; i < header.length; i++) 
65               ByteUtil.printToOctal(header[i]); 
66           System.out.println("\n in ASCII"); 
67           System.out.println(new String(header)); 
68           System.out.println("if (match(" 
69                   + ByteUtil.toString(header[0]) + "," 
70                   + ByteUtil.toString(header[1]) + "," 
71                   + ByteUtil.toString(header[2]) + "," 
72                   + ByteUtil.toString(header[3]) + "))"); 
73    
74       } 
75    
76       public String getMimeType() { 
77           return getStringForId(classifyStream()); 
78       } 
79    
80       public String toString() { 
81           int id = classifyStream(); 
82           String s = getStringForId(id); 
83           if (id == TYPENOTFOUND) 
84               printHeader(); 
85           return s; 
86    
87       } 
88    
89       public boolean match( 
90               char c0, char c1) { 
91           byte b[] = header; 
92           return 
93                   (b[0] == c0) && 
94                   (b[1] == c1); 
95       } 
96    
97       public boolean match( 
98               char c0, char c1, 
99               char c2, char c3) { 
100          byte b[] = header; 
101          return 
102                  (b[0] == (c0 & 0xFF)) && 
103                  (b[1] == (c1 & 0xFF)) && 
104                  (b[2] == (c2 & 0xFF)) && 
105                  (b[3] == (c3 & 0xFF)); 
106      } 
107   
108      public boolean match( 
109              int c0, int c1, 
110              int c2, int c3) { 
111          byte b[] = header; 
112          return 
113                  ((b[0] & 0xFF) == (c0 & 0xFF)) && 
114                  ((b[1] & 0xFF) == (c1 & 0xFF)) && 
115                  ((b[2] & 0xFF) == (c2 & 0xFF)) && 
116                  ((b[3] & 0xFF) == (c3 & 0xFF)); 
117      } 
118   
119      public boolean match( 
120              int c0, int c1) { 
121          byte b[] = header; 
122          return 
123                  ((b[0] & 0xFF) == (c0 & 0xFF)) && 
124                  ((b[1] & 0xFF) == (c1 & 0xFF)); 
125      } 
126   
127   
128      public int classifyStream() { 
129          byte b[] = header; 
130          String s = new String(b); 
131          short leshort = 
132                  (short) ( 
133                  (b[0] << 8) | (b[1])); 
134          int belong = 
135                  (b[0] << 24) | 
136                  (b[1] << 16) | 
137                  (b[2] << 8) | 
138                  (b[3]); 
139   
140   
141          if (s.startsWith("begin")) 
142              return UUENCODED; 
143          if (s.startsWith("xbtoa")) 
144              return BTOAD; 
145          if (s.startsWith("P1")) 
146              return PBM; 
147          if (s.startsWith("P2")) 
148              return PGM; 
149          if (s.startsWith("P3")) 
150              return PPM; 
151          if (s.startsWith("P4")) 
152              return PBM_RAWBITS; 
153          if (s.startsWith("P5")) 
154              return PGM_RAWBITS; 
155          if (s.startsWith("P6")) 
156              return PPM_RAWBITS; 
157          if (s.startsWith("yz")) 
158              return MGR_BITMAP; 
159          if (s.startsWith("ILBM")) 
160              return IFF_ILBM; 
161          if ((b[0] == 131) && 
162                  (b[1] == 246) && 
163                  (b[2] == 152) && 
164                  (b[3] == 225)) 
165              return SUNRASTER; 
166          if ((b[0] == 1) && 
167                  (b[1] == 332)) 
168              return SGI_IMAGE; 
169          if ((b[0] == 361) && 
170                  (b[1] == 0) && 
171                  (b[2] == 100) && 
172                  (b[3] == 273)) 
173              return CMU_WINDOW_MANAGER_BITMAP; 
174          if ((b[0] == 131) && 
175                  (b[1] == 246) && 
176                  (b[2] == 152) && 
177                  (b[3] == 225)) 
178              return SUN; 
179          if ((b[0] == 115) && 
180                  (b[1] == 115)) 
181              return TIFF_BIG_ENDIAN; 
182          if ((b[0] == 111) && 
183                  (b[1] == 111)) 
184              return TIFF_LITTLE_ENDIAN; 
185          if (match(73, 73, 42, 0)) 
186              return TIFF_LITTLE_ENDIAN; 
187          if (s.startsWith("GIF87a")) 
188              return GIF87a; 
189          if (s.startsWith("GIF89a")) 
190              return GIF89a; 
191          if ((b[0] == 131) && 
192                  (b[1] == 246) && 
193                  (b[2] == 152) && 
194                  (b[3] == 225)) 
195              return SUNRASTER; 
196          if (leshort == 0xAF11) 
197              return FLI; 
198          if ((b[0] == 0) && 
199                  (b[1] == 0) && 
200                  (b[2] == 1) && 
201                  (b[3] == 263)) 
202              return MPEG; 
203          if (s.startsWith(".snd")) 
204              return SUN_NEXT_AUDIO; 
205          if (s.startsWith("MThd")) 
206              return STANDARD_MIDI; 
207          if (s.startsWith("RIFF")) 
208              return MICROSOFT_RIFF; 
209          if (s.startsWith("BZ")) 
210              return BZIP; 
211          if (s.startsWith("FORM")) 
212              return IFF_DATA; 
213          if (s.startsWith("IIN1")) 
214              return NIFF_IMAGE; 
215          if (s.startsWith("BM")) 
216              return PC_BITMAP; 
217          if (s.startsWith("%PDF-")) 
218              return PDF_DOCUMENT; 
219          if (s.startsWith("%!")) 
220              return POSTSCRIPT_DOCUMENT; 
221          if (s.startsWith("MOVI")) 
222              return SILICON_GRAPHICS_MOVIE; 
223          if (s.startsWith("moov")) 
224              return APPLE_QUICKTIME_MOVIE; 
225          if (s.startsWith("mdat")) 
226              return APPLE_QUICKTIME_MOVIE; 
227          if (match('P', 'K')) 
228              return ZIP_ARCHIVE; 
229          if (match(03, 0235)) 
230              return UNIX_COMPRESS; 
231          if (match(037, 0213)) 
232              return GZIP; 
233          if (match(037, 036)) 
234              return HUFFMAN; 
235          if (match((char) 0x89, 'P', 'N', 'G')) 
236              return PNG_IMAGE; 
237          if (match(0xFF, 0xD8, 0xFF, 0xE0)) 
238              return JPEG; 
239          if (match(0377, 0330, 0377, 0356)) 
240              return JPG; 
241          if (match('8', 'B', 'P', 'S')) 
242              return PSHOP8; 
243          if (match(172, 237, 0, 5)) 
244              return ZIP; 
245          if (match(60, 104, 116, 109)) 
246              return HTML; 
247          if (match(60, 63, 120, 109)) 
248              return XML; 
249          if (match(137, 80, 78, 71)) 
250              return PNG_IMAGE; 
251          return TYPENOTFOUND; 
252      } 
253   
254      private void init() { 
255          add(TYPENOTFOUND, "TYPENOTFOUND"); 
256          add(UUENCODED, "UUENCODED"); 
257          add(BTOAD, "BTOAD"); 
258          add(PBM, "PBM"); 
259          add(PGM, "PGM"); 
260          add(PPM, "PPM"); 
261          add(PBM_RAWBITS, "PBM_RAWBITS"); 
262          add(PGM_RAWBITS, "PGM_RAWBITS"); 
263          add(PPM_RAWBITS, "PPM_RAWBITS"); 
264          add(MGR_BITMAP, "MGR_BITMAP"); 
265          add(GIF87a, "image/gif");//gif 87a 
266          add(GIF89a, "GIF89a"); 
267          add(IFF_ILBM, "IFF_ILBM"); 
268          add(SUNRASTER, "SUNRASTER"); 
269          add(SGI_IMAGE, "SGI_IMAGE"); 
270          add(CMU_WINDOW_MANAGER_BITMAP, "CMU_WINDOW_MANAGER_BITMAP"); 
271          add(SUN, "SUN"); 
272          add(TIFF_BIG_ENDIAN, "TIFF_BIG_ENDIAN"); 
273          add(TIFF_LITTLE_ENDIAN, "TIFF_LITTLE_ENDIAN"); 
274          add(FLI, "FLI"); 
275          add(MPEG, "MPEG"); 
276          add(SUN_NEXT_AUDIO, "sound/au"); 
277          add(STANDARD_MIDI, "STANDARD_MIDI"); 
278          add(MICROSOFT_RIFF, "MICROSOFT_RIFF"); 
279          add(BZIP, "BZIP"); 
280          add(IFF_DATA, "IFF_DATA"); 
281          add(NIFF_IMAGE, "NIFF_IMAGE"); 
282          add(PC_BITMAP, "PC_BITMAP"); 
283          add(PDF_DOCUMENT, "PDF_DOCUMENT"); 
284          add(POSTSCRIPT_DOCUMENT, "POSTSCRIPT_DOCUMENT"); 
285          add(SILICON_GRAPHICS_MOVIE, "SILICON_GRAPHICS_MOVIE"); 
286          add(APPLE_QUICKTIME_MOVIE, "APPLE_QUICKTIME_MOVIE"); 
287          add(ZIP_ARCHIVE, "ZIP_ARCHIVE"); 
288          add(UNIX_COMPRESS, "UNIX_COMPRESS"); 
289          add(GZIP, "GZIP"); 
290          add(HUFFMAN, "HUFFMAN"); 
291          add(PNG_IMAGE, "PNG_IMAGE"); 
292          add(JPEG, "JPEG"); 
293          add(JPG, "JPG"); 
294          add(PSHOP8, "Photo Shop...8 bits per pel"); 
295          add(ZIP, "Zip file...Wavelet encoded image sequence?"); 
296          add(HTML, "text/html"); 
297          add(XML, "xml document"); 
298      } 
299   
300      protected void add(int i, String s) { 
301          h.put(new Integer(i), s); 
302      } 
303   
304      public String getStringForId(int id) { 
305          return (String) h.get(new Integer(id)); 
306      } 
307   
308   
309      public static final int TYPENOTFOUND = 0; 
310      public static final int UUENCODED = 1; 
311      public static final int BTOAD = 2; 
312      public static final int PBM = 3; 
313      public static final int PGM = 4; 
314      public static final int PPM = 5; 
315      public static final int PBM_RAWBITS = 6; 
316      public static final int PGM_RAWBITS = 7; 
317      public static final int PPM_RAWBITS = 8; 
318      public static final int MGR_BITMAP = 9; 
319      public static final int GIF87a = 10; 
320      public static final int GIF89a = 11; 
321      public static final int IFF_ILBM = 12; 
322      public static final int SUNRASTER = 13; 
323      public static final int SGI_IMAGE = 14; 
324      public static final int CMU_WINDOW_MANAGER_BITMAP = 15; 
325      public static final int SUN = 16; 
326      public static final int TIFF_BIG_ENDIAN = 17; 
327      public static final int TIFF_LITTLE_ENDIAN = 18; 
328      public static final int FLI = 19; 
329      public static final int MPEG = 20; 
330      public static final int SUN_NEXT_AUDIO = 21; 
331      public static final int STANDARD_MIDI = 22; 
332      public static final int MICROSOFT_RIFF = 23; 
333      public static final int BZIP = 24; 
334      public static final int IFF_DATA = 25; 
335      public static final int NIFF_IMAGE = 26; 
336      public static final int PC_BITMAP = 27; 
337      public static final int PDF_DOCUMENT = 28; 
338      public static final int POSTSCRIPT_DOCUMENT = 29; 
339      public static final int SILICON_GRAPHICS_MOVIE = 30; 
340      public static final int APPLE_QUICKTIME_MOVIE = 31; 
341      public static final int ZIP_ARCHIVE = 32; 
342      public static final int UNIX_COMPRESS = 33; 
343      public static final int GZIP = 34; 
344      public static final int HUFFMAN = 35; 
345      public static final int PNG_IMAGE = 38; 
346      public static final int JPEG = 39; 
347      public static final int JPG = 40; 
348      public static final int PSHOP8 = 41; 
349      public static final int ZIP = 42; 
350      public static final int HTML = 43; 
351      public static final int XML = 44; 
352   
353      protected Hashtable h = new Hashtable(); 
354  } 
355   
356   
357