001: /* jcifs smb client library in Java
002: * Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package jcifs.util;
020:
021: import java.io.InputStream;
022: import java.io.IOException;
023:
024: public class MimeMap {
025:
026: private static final int IN_SIZE = 7000;
027:
028: private static final int ST_START = 1;
029: private static final int ST_COMM = 2;
030: private static final int ST_TYPE = 3;
031: private static final int ST_GAP = 4;
032: private static final int ST_EXT = 5;
033:
034: private byte[] in;
035: private int inLen;
036:
037: public MimeMap() throws IOException {
038: int n;
039:
040: in = new byte[IN_SIZE];
041: InputStream is = getClass().getClassLoader()
042: .getResourceAsStream("jcifs/util/mime.map");
043:
044: inLen = 0;
045: while ((n = is.read(in, inLen, IN_SIZE - inLen)) != -1) {
046: inLen += n;
047: }
048: if (inLen < 100 || inLen == IN_SIZE) {
049: throw new IOException(
050: "Error reading jcifs/util/mime.map resource");
051: }
052: is.close();
053: }
054:
055: public String getMimeType(String extension) throws IOException {
056: return getMimeType(extension, "application/octet-stream");
057: }
058:
059: public String getMimeType(String extension, String def)
060: throws IOException {
061: int state, t, x, i, off;
062: byte ch;
063: byte[] type = new byte[128];
064: byte[] buf = new byte[16];
065: byte[] ext = extension.toLowerCase().getBytes("ASCII");
066:
067: state = ST_START;
068: t = x = i = 0;
069: for (off = 0; off < inLen; off++) {
070: ch = in[off];
071: switch (state) {
072: case ST_START:
073: if (ch == ' ' || ch == '\t') {
074: break;
075: } else if (ch == '#') {
076: state = ST_COMM;
077: break;
078: }
079: state = ST_TYPE;
080: case ST_TYPE:
081: if (ch == ' ' || ch == '\t') {
082: state = ST_GAP;
083: } else {
084: type[t++] = ch;
085: }
086: break;
087: case ST_COMM:
088: if (ch == '\n') {
089: t = x = i = 0;
090: state = ST_START;
091: }
092: break;
093: case ST_GAP:
094: if (ch == ' ' || ch == '\t') {
095: break;
096: }
097: state = ST_EXT;
098: case ST_EXT:
099: switch (ch) {
100: case ' ':
101: case '\t':
102: case '\n':
103: case '#':
104: for (i = 0; i < x && x == ext.length
105: && buf[i] == ext[i]; i++) {
106: ;
107: }
108: if (i == ext.length) {
109: return new String(type, 0, t, "ASCII");
110: }
111: if (ch == '#') {
112: state = ST_COMM;
113: } else if (ch == '\n') {
114: t = x = i = 0;
115: state = ST_START;
116: }
117: x = 0;
118: break;
119: default:
120: buf[x++] = ch;
121: }
122: break;
123: }
124: }
125: return def;
126: }
127: }
|