KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > util > MimeMap


1 /* jcifs smb client library in Java
2  * Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package jcifs.util;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 public class MimeMap {
25
26     private static final int IN_SIZE = 7000;
27
28     private static final int ST_START = 1;
29     private static final int ST_COMM = 2;
30     private static final int ST_TYPE = 3;
31     private static final int ST_GAP = 4;
32     private static final int ST_EXT = 5;
33
34     private byte[] in;
35     private int inLen;
36
37     public MimeMap() throws IOException JavaDoc {
38         int n;
39
40         in = new byte[IN_SIZE];
41         InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream( "jcifs/util/mime.map" );
42
43         inLen = 0;
44         while(( n = is.read( in, inLen, IN_SIZE - inLen )) != -1 ) {
45             inLen += n;
46         }
47         if( inLen < 100 || inLen == IN_SIZE ) {
48             throw new IOException JavaDoc( "Error reading jcifs/util/mime.map resource" );
49         }
50         is.close();
51     }
52
53     public String JavaDoc getMimeType( String JavaDoc extension ) throws IOException JavaDoc {
54         return getMimeType( extension, "application/octet-stream" );
55     }
56     public String JavaDoc getMimeType( String JavaDoc extension, String JavaDoc def ) throws IOException JavaDoc {
57         int state, t, x, i, off;
58         byte ch;
59         byte[] type = new byte[128];
60         byte[] buf = new byte[16];
61         byte[] ext = extension.toLowerCase().getBytes( "ASCII" );
62
63         state = ST_START;
64         t = x = i = 0;
65         for( off = 0; off < inLen; off++ ) {
66             ch = in[off];
67             switch( state ) {
68                 case ST_START:
69                     if( ch == ' ' || ch == '\t' ) {
70                         break;
71                     } else if( ch == '#' ) {
72                         state = ST_COMM;
73                         break;
74                     }
75                     state = ST_TYPE;
76                 case ST_TYPE:
77                     if( ch == ' ' || ch == '\t' ) {
78                         state = ST_GAP;
79                     } else {
80                         type[t++] = ch;
81                     }
82                     break;
83                 case ST_COMM:
84                     if( ch == '\n' ) {
85                         t = x = i = 0;
86                         state = ST_START;
87                     }
88                     break;
89                 case ST_GAP:
90                     if( ch == ' ' || ch == '\t' ) {
91                         break;
92                     }
93                     state = ST_EXT;
94                 case ST_EXT:
95                     switch( ch ) {
96                         case ' ':
97                         case '\t':
98                         case '\n':
99                         case '#':
100                             for( i = 0; i < x && x == ext.length && buf[i] == ext[i]; i++ ) {
101                                 ;
102                             }
103                             if( i == ext.length ) {
104                                 return new String JavaDoc( type, 0, t, "ASCII" );
105                             }
106                             if( ch == '#' ) {
107                                 state = ST_COMM;
108                             } else if( ch == '\n' ) {
109                                 t = x = i = 0;
110                                 state = ST_START;
111                             }
112                             x = 0;
113                             break;
114                         default:
115                             buf[x++] = ch;
116                     }
117                     break;
118             }
119         }
120         return def;
121     }
122 }
123
124
Popular Tags