1 18 19 package jcifs.util; 20 21 import java.io.InputStream ; 22 import java.io.IOException ; 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 { 38 int n; 39 40 in = new byte[IN_SIZE]; 41 InputStream 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 ( "Error reading jcifs/util/mime.map resource" ); 49 } 50 is.close(); 51 } 52 53 public String getMimeType( String extension ) throws IOException { 54 return getMimeType( extension, "application/octet-stream" ); 55 } 56 public String getMimeType( String extension, String def ) throws IOException { 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 ( 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 |