forked from pranjalbajaj/java7-sourcecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMimeType.java
More file actions
393 lines (353 loc) · 12.3 KB
/
Copy pathMimeType.java
File metadata and controls
393 lines (353 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (c) 1997, 2002, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.awt.datatransfer;
import java.io.Externalizable;
import java.io.ObjectOutput;
import java.io.ObjectInput;
import java.io.IOException;
import java.util.Enumeration;
/**
* A Multipurpose Internet Mail Extension (MIME) type, as defined
* in RFC 2045 and 2046.
*
* THIS IS *NOT* - REPEAT *NOT* - A PUBLIC CLASS! DataFlavor IS
* THE PUBLIC INTERFACE, AND THIS IS PROVIDED AS A ***PRIVATE***
* (THAT IS AS IN *NOT* PUBLIC) HELPER CLASS!
*/
class MimeType implements Externalizable, Cloneable {
/*
* serialization support
*/
static final long serialVersionUID = -6568722458793895906L;
/**
* Constructor for externalization; this constructor should not be
* called directly by an application, since the result will be an
* uninitialized, immutable <code>MimeType</code> object.
*/
public MimeType() {
}
/**
* Builds a <code>MimeType</code> from a <code>String</code>.
*
* @param rawdata text used to initialize the <code>MimeType</code>
* @throws NullPointerException if <code>rawdata</code> is null
*/
public MimeType(String rawdata) throws MimeTypeParseException {
parse(rawdata);
}
/**
* Builds a <code>MimeType</code> with the given primary and sub
* type but has an empty parameter list.
*
* @param primary the primary type of this <code>MimeType</code>
* @param sub the subtype of this <code>MimeType</code>
* @throws NullPointerException if either <code>primary</code> or
* <code>sub</code> is null
*/
public MimeType(String primary, String sub) throws MimeTypeParseException {
this(primary, sub, new MimeTypeParameterList());
}
/**
* Builds a <code>MimeType</code> with a pre-defined
* and valid (or empty) parameter list.
*
* @param primary the primary type of this <code>MimeType</code>
* @param sub the subtype of this <code>MimeType</code>
* @param mtpl the requested parameter list
* @throws NullPointerException if either <code>primary</code>,
* <code>sub</code> or <code>mtpl</code> is null
*/
public MimeType(String primary, String sub, MimeTypeParameterList mtpl) throws
MimeTypeParseException {
// check to see if primary is valid
if(isValidToken(primary)) {
primaryType = primary.toLowerCase();
} else {
throw new MimeTypeParseException("Primary type is invalid.");
}
// check to see if sub is valid
if(isValidToken(sub)) {
subType = sub.toLowerCase();
} else {
throw new MimeTypeParseException("Sub type is invalid.");
}
parameters = (MimeTypeParameterList)mtpl.clone();
}
public int hashCode() {
// We sum up the hash codes for all of the strings. This
// way, the order of the strings is irrelevant
int code = 0;
code += primaryType.hashCode();
code += subType.hashCode();
code += parameters.hashCode();
return code;
} // hashCode()
/**
* <code>MimeType</code>s are equal if their primary types,
* subtypes, and parameters are all equal. No default values
* are taken into account.
* @param thatObject the object to be evaluated as a
* <code>MimeType</code>
* @return <code>true</code> if <code>thatObject</code> is
* a <code>MimeType</code>; otherwise returns <code>false</code>
*/
public boolean equals(Object thatObject) {
if (!(thatObject instanceof MimeType)) {
return false;
}
MimeType that = (MimeType)thatObject;
boolean isIt =
((this.primaryType.equals(that.primaryType)) &&
(this.subType.equals(that.subType)) &&
(this.parameters.equals(that.parameters)));
return isIt;
} // equals()
/**
* A routine for parsing the MIME type out of a String.
*
* @throws NullPointerException if <code>rawdata</code> is null
*/
private void parse(String rawdata) throws MimeTypeParseException {
int slashIndex = rawdata.indexOf('/');
int semIndex = rawdata.indexOf(';');
if((slashIndex < 0) && (semIndex < 0)) {
// neither character is present, so treat it
// as an error
throw new MimeTypeParseException("Unable to find a sub type.");
} else if((slashIndex < 0) && (semIndex >= 0)) {
// we have a ';' (and therefore a parameter list),
// but no '/' indicating a sub type is present
throw new MimeTypeParseException("Unable to find a sub type.");
} else if((slashIndex >= 0) && (semIndex < 0)) {
// we have a primary and sub type but no parameter list
primaryType = rawdata.substring(0,
slashIndex).trim().toLowerCase();
subType = rawdata.substring(slashIndex +
1).trim().toLowerCase();
parameters = new MimeTypeParameterList();
} else if (slashIndex < semIndex) {
// we have all three items in the proper sequence
primaryType = rawdata.substring(0,
slashIndex).trim().toLowerCase();
subType = rawdata.substring(slashIndex + 1,
semIndex).trim().toLowerCase();
parameters = new
MimeTypeParameterList(rawdata.substring(semIndex));
} else {
// we have a ';' lexically before a '/' which means we have a primary type
// & a parameter list but no sub type
throw new MimeTypeParseException("Unable to find a sub type.");
}
// now validate the primary and sub types
// check to see if primary is valid
if(!isValidToken(primaryType)) {
throw new MimeTypeParseException("Primary type is invalid.");
}
// check to see if sub is valid
if(!isValidToken(subType)) {
throw new MimeTypeParseException("Sub type is invalid.");
}
}
/**
* Retrieve the primary type of this object.
*/
public String getPrimaryType() {
return primaryType;
}
/**
* Retrieve the sub type of this object.
*/
public String getSubType() {
return subType;
}
/**
* Retrieve a copy of this object's parameter list.
*/
public MimeTypeParameterList getParameters() {
return (MimeTypeParameterList)parameters.clone();
}
/**
* Retrieve the value associated with the given name, or null if there
* is no current association.
*/
public String getParameter(String name) {
return parameters.get(name);
}
/**
* Set the value to be associated with the given name, replacing
* any previous association.
*
* @throw IllegalArgumentException if parameter or value is illegal
*/
public void setParameter(String name, String value) {
parameters.set(name, value);
}
/**
* Remove any value associated with the given name.
*
* @throw IllegalArgumentExcpetion if parameter may not be deleted
*/
public void removeParameter(String name) {
parameters.remove(name);
}
/**
* Return the String representation of this object.
*/
public String toString() {
return getBaseType() + parameters.toString();
}
/**
* Return a String representation of this object
* without the parameter list.
*/
public String getBaseType() {
return primaryType + "/" + subType;
}
/**
* Returns <code>true</code> if the primary type and the
* subtype of this object are the same as the specified
* <code>type</code>; otherwise returns <code>false</code>.
*
* @param type the type to compare to <code>this</code>'s type
* @return <code>true</code> if the primary type and the
* subtype of this object are the same as the
* specified <code>type</code>; otherwise returns
* <code>false</code>
*/
public boolean match(MimeType type) {
if (type == null)
return false;
return primaryType.equals(type.getPrimaryType())
&& (subType.equals("*")
|| type.getSubType().equals("*")
|| (subType.equals(type.getSubType())));
}
/**
* Returns <code>true</code> if the primary type and the
* subtype of this object are the same as the content type
* described in <code>rawdata</code>; otherwise returns
* <code>false</code>.
*
* @param rawdata the raw data to be examined
* @return <code>true</code> if the primary type and the
* subtype of this object are the same as the content type
* described in <code>rawdata</code>; otherwise returns
* <code>false</code>; if <code>rawdata</code> is
* <code>null</code>, returns <code>false</code>
*/
public boolean match(String rawdata) throws MimeTypeParseException {
if (rawdata == null)
return false;
return match(new MimeType(rawdata));
}
/**
* The object implements the writeExternal method to save its contents
* by calling the methods of DataOutput for its primitive values or
* calling the writeObject method of ObjectOutput for objects, strings
* and arrays.
* @exception IOException Includes any I/O exceptions that may occur
*/
public void writeExternal(ObjectOutput out) throws IOException {
String s = toString(); // contains ASCII chars only
// one-to-one correspondence between ASCII char and byte in UTF string
if (s.length() <= 65535) { // 65535 is max length of UTF string
out.writeUTF(s);
} else {
out.writeByte(0);
out.writeByte(0);
out.writeInt(s.length());
out.write(s.getBytes());
}
}
/**
* The object implements the readExternal method to restore its
* contents by calling the methods of DataInput for primitive
* types and readObject for objects, strings and arrays. The
* readExternal method must read the values in the same sequence
* and with the same types as were written by writeExternal.
* @exception ClassNotFoundException If the class for an object being
* restored cannot be found.
*/
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
String s = in.readUTF();
if (s == null || s.length() == 0) { // long mime type
byte[] ba = new byte[in.readInt()];
in.readFully(ba);
s = new String(ba);
}
try {
parse(s);
} catch(MimeTypeParseException e) {
throw new IOException(e.toString());
}
}
/**
* Returns a clone of this object.
* @return a clone of this object
*/
public Object clone() {
MimeType newObj = null;
try {
newObj = (MimeType)super.clone();
} catch (CloneNotSupportedException cannotHappen) {
}
newObj.parameters = (MimeTypeParameterList)parameters.clone();
return newObj;
}
private String primaryType;
private String subType;
private MimeTypeParameterList parameters;
// below here be scary parsing related things
/**
* Determines whether or not a given character belongs to a legal token.
*/
private static boolean isTokenChar(char c) {
return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
}
/**
* Determines whether or not a given string is a legal token.
*
* @throws NullPointerException if <code>s</code> is null
*/
private boolean isValidToken(String s) {
int len = s.length();
if(len > 0) {
for (int i = 0; i < len; ++i) {
char c = s.charAt(i);
if (!isTokenChar(c)) {
return false;
}
}
return true;
} else {
return false;
}
}
/**
* A string that holds all the special chars.
*/
private static final String TSPECIALS = "()<>@,;:\\\"/[]?=";
} // class MimeType