Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.datatransfer/share/classes/java/awt/datatransfer/DataFlavor.java
41159 views
1
/*
2
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.awt.datatransfer;
27
28
import java.io.ByteArrayInputStream;
29
import java.io.CharArrayReader;
30
import java.io.Externalizable;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.InputStreamReader;
34
import java.io.ObjectInput;
35
import java.io.ObjectOutput;
36
import java.io.OptionalDataException;
37
import java.io.Reader;
38
import java.io.Serial;
39
import java.io.StringReader;
40
import java.io.UnsupportedEncodingException;
41
import java.nio.ByteBuffer;
42
import java.nio.CharBuffer;
43
import java.util.Arrays;
44
import java.util.Collections;
45
import java.util.Objects;
46
47
import sun.datatransfer.DataFlavorUtil;
48
import sun.reflect.misc.ReflectUtil;
49
50
/**
51
* A {@code DataFlavor} provides meta information about data. {@code DataFlavor}
52
* is typically used to access data on the clipboard, or during a drag and drop
53
* operation.
54
* <p>
55
* An instance of {@code DataFlavor} encapsulates a content type as defined in
56
* <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> and
57
* <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a>. A content type is
58
* typically referred to as a MIME type.
59
* <p>
60
* A content type consists of a media type (referred to as the primary type), a
61
* subtype, and optional parameters. See
62
* <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> for details on the
63
* syntax of a MIME type.
64
* <p>
65
* The JRE data transfer implementation interprets the parameter
66
* &quot;class&quot; of a MIME type as <B>a representation class</b>. The
67
* representation class reflects the class of the object being transferred. In
68
* other words, the representation class is the type of object returned by
69
* {@link Transferable#getTransferData}. For example, the MIME type of
70
* {@link #imageFlavor} is {@code "image/x-java-image;class=java.awt.Image"},
71
* the primary type is {@code image}, the subtype is {@code x-java-image}, and
72
* the representation class is {@code java.awt.Image}. When
73
* {@code getTransferData} is invoked with a {@code DataFlavor} of
74
* {@code imageFlavor}, an instance of {@code java.awt.Image} is returned. It's
75
* important to note that {@code DataFlavor} does no error checking against the
76
* representation class. It is up to consumers of {@code DataFlavor}, such as
77
* {@code Transferable}, to honor the representation class.
78
* <br>
79
* Note, if you do not specify a representation class when creating a
80
* {@code DataFlavor}, the default representation class is used. See appropriate
81
* documentation for {@code DataFlavor}'s constructors.
82
* <p>
83
* Also, {@code DataFlavor} instances with the &quot;text&quot; primary MIME
84
* type may have a &quot;charset&quot; parameter. Refer to
85
* <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a> and
86
* {@link #selectBestTextFlavor} for details on &quot;text&quot; MIME types and
87
* the &quot;charset&quot; parameter.
88
* <p>
89
* Equality of {@code DataFlavors} is determined by the primary type, subtype,
90
* and representation class. Refer to {@link #equals(DataFlavor)} for details.
91
* When determining equality, any optional parameters are ignored. For example,
92
* the following produces two {@code DataFlavors} that are considered identical:
93
* <pre>
94
* DataFlavor flavor1 = new DataFlavor(Object.class, &quot;X-test/test; class=&lt;java.lang.Object&gt;; foo=bar&quot;);
95
* DataFlavor flavor2 = new DataFlavor(Object.class, &quot;X-test/test; class=&lt;java.lang.Object&gt;; x=y&quot;);
96
* // The following returns true.
97
* flavor1.equals(flavor2);
98
* </pre>
99
* As mentioned, {@code flavor1} and {@code flavor2} are considered identical.
100
* As such, asking a {@code Transferable} for either {@code DataFlavor} returns
101
* the same results.
102
* <p>
103
* For more information on using data transfer with Swing see the
104
* <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html">How
105
* to Use Drag and Drop and Data Transfer</a>, section in
106
* <em>The Java Tutorial</em>.
107
*
108
* @author Blake Sullivan
109
* @author Laurence P. G. Cable
110
* @author Jeff Dunn
111
* @since 1.1
112
*/
113
public class DataFlavor implements Externalizable, Cloneable {
114
115
/**
116
* Use serialVersionUID from JDK 1.2 for interoperability.
117
*/
118
@Serial
119
private static final long serialVersionUID = 8367026044764648243L;
120
121
/**
122
* Tries to load a class from: the bootstrap loader, the system loader, the
123
* context loader (if one is present) and finally the loader specified.
124
*
125
* @param className the name of the class to be loaded
126
* @param fallback the fallback loader
127
* @return the class loaded
128
* @throws ClassNotFoundException if class is not found
129
*/
130
protected static final Class<?> tryToLoadClass(String className,
131
ClassLoader fallback)
132
throws ClassNotFoundException
133
{
134
ReflectUtil.checkPackageAccess(className);
135
try {
136
@SuppressWarnings("removal")
137
SecurityManager sm = System.getSecurityManager();
138
if (sm != null) {
139
sm.checkPermission(new RuntimePermission("getClassLoader"));
140
}
141
ClassLoader loader = ClassLoader.getSystemClassLoader();
142
try {
143
// bootstrap class loader and system class loader if present
144
return Class.forName(className, true, loader);
145
}
146
catch (ClassNotFoundException exception) {
147
// thread context class loader if and only if present
148
loader = Thread.currentThread().getContextClassLoader();
149
if (loader != null) {
150
try {
151
return Class.forName(className, true, loader);
152
}
153
catch (ClassNotFoundException e) {
154
// fallback to user's class loader
155
}
156
}
157
}
158
} catch (SecurityException exception) {
159
// ignore secured class loaders
160
}
161
return Class.forName(className, true, fallback);
162
}
163
164
/*
165
* private initializer
166
*/
167
private static DataFlavor createConstant(Class<?> rc, String prn) {
168
try {
169
return new DataFlavor(rc, prn);
170
} catch (Exception e) {
171
return null;
172
}
173
}
174
175
/*
176
* private initializer
177
*/
178
private static DataFlavor createConstant(String mt, String prn) {
179
try {
180
return new DataFlavor(mt, prn);
181
} catch (Exception e) {
182
return null;
183
}
184
}
185
186
/*
187
* private initializer
188
*/
189
private static DataFlavor initHtml(String htmlFlavorType) {
190
try {
191
return new DataFlavor("text/html; class=java.lang.String;document="
192
+ htmlFlavorType + ";charset=Unicode");
193
} catch (Exception e) {
194
return null;
195
}
196
}
197
198
/**
199
* The {@code DataFlavor} representing a Java Unicode String class, where:
200
* <pre>
201
* representationClass = java.lang.String
202
* mimeType = "application/x-java-serialized-object"
203
* </pre>
204
*/
205
public static final DataFlavor stringFlavor = createConstant(java.lang.String.class, "Unicode String");
206
207
/**
208
* The {@code DataFlavor} representing a Java Image class, where:
209
* <pre>
210
* representationClass = java.awt.Image
211
* mimeType = "image/x-java-image"
212
* </pre>
213
* Will be {@code null} if {@code java.awt.Image} is not visible, the
214
* {@code java.desktop} module is not loaded, or the {@code java.desktop}
215
* module is not in the run-time image.
216
*/
217
public static final DataFlavor imageFlavor = createConstant("image/x-java-image; class=java.awt.Image", "Image");
218
219
/**
220
* The {@code DataFlavor} representing plain text with Unicode encoding,
221
* where:
222
* <pre>
223
* representationClass = InputStream
224
* mimeType = "text/plain; charset=unicode"
225
* </pre>
226
* This {@code DataFlavor} has been <b>deprecated</b> because:
227
* <ul>
228
* <li>Its representation is an InputStream, an 8-bit based representation,
229
* while Unicode is a 16-bit character set</li>
230
* <li>The charset "unicode" is not well-defined. "unicode" implies a
231
* particular platform's implementation of Unicode, not a cross-platform
232
* implementation</li>
233
* </ul>
234
*
235
* @deprecated as of 1.3. Use {@link #getReaderForText} instead of
236
* {@code Transferable.getTransferData(DataFlavor.plainTextFlavor)}.
237
*/
238
@Deprecated
239
public static final DataFlavor plainTextFlavor = createConstant("text/plain; charset=unicode; class=java.io.InputStream", "Plain Text");
240
241
/**
242
* A MIME Content-Type of application/x-java-serialized-object represents a
243
* graph of Java object(s) that have been made persistent.
244
* <p>
245
* The representation class associated with this {@code DataFlavor}
246
* identifies the Java type of an object returned as a reference from an
247
* invocation {@code java.awt.datatransfer.getTransferData}.
248
*/
249
public static final String javaSerializedObjectMimeType = "application/x-java-serialized-object";
250
251
/**
252
* To transfer a list of files to/from Java (and the underlying platform) a
253
* {@code DataFlavor} of this type/subtype and representation class of
254
* {@code java.util.List} is used. Each element of the list is
255
* required/guaranteed to be of type {@code java.io.File}.
256
*/
257
public static final DataFlavor javaFileListFlavor = createConstant("application/x-java-file-list;class=java.util.List", null);
258
259
/**
260
* To transfer a reference to an arbitrary Java object reference that has no
261
* associated MIME Content-type, across a {@code Transferable} interface
262
* WITHIN THE SAME JVM, a {@code DataFlavor} with this type/subtype is used,
263
* with a {@code representationClass} equal to the type of the
264
* class/interface being passed across the {@code Transferable}.
265
* <p>
266
* The object reference returned from {@code Transferable.getTransferData}
267
* for a {@code DataFlavor} with this MIME Content-Type is required to be an
268
* instance of the representation Class of the {@code DataFlavor}.
269
*/
270
public static final String javaJVMLocalObjectMimeType = "application/x-java-jvm-local-objectref";
271
272
/**
273
* In order to pass a live link to a Remote object via a Drag and Drop
274
* {@code ACTION_LINK} operation a Mime Content Type of
275
* application/x-java-remote-object should be used, where the representation
276
* class of the {@code DataFlavor} represents the type of the {@code Remote}
277
* interface to be transferred.
278
*/
279
public static final String javaRemoteObjectMimeType = "application/x-java-remote-object";
280
281
/**
282
* Represents a piece of an HTML markup. The markup consists of the part
283
* selected on the source side. Therefore some tags in the markup may be
284
* unpaired. If the flavor is used to represent the data in a
285
* {@link Transferable} instance, no additional changes will be made. This
286
* DataFlavor instance represents the same HTML markup as DataFlavor
287
* instances which content MIME type does not contain document parameter
288
* and representation class is the String class.
289
* <pre>
290
* representationClass = String
291
* mimeType = "text/html"
292
* </pre>
293
*
294
* @since 1.8
295
*/
296
public static final DataFlavor selectionHtmlFlavor = initHtml("selection");
297
298
/**
299
* Represents a piece of an HTML markup. If possible, the markup received
300
* from a native system is supplemented with pair tags to be a well-formed
301
* HTML markup. If the flavor is used to represent the data in a
302
* {@link Transferable} instance, no additional changes will be made.
303
* <pre>
304
* representationClass = String
305
* mimeType = "text/html"
306
* </pre>
307
*
308
* @since 1.8
309
*/
310
public static final DataFlavor fragmentHtmlFlavor = initHtml("fragment");
311
312
/**
313
* Represents a piece of an HTML markup. If possible, the markup received
314
* from a native system is supplemented with additional tags to make up a
315
* well-formed HTML document. If the flavor is used to represent the data in
316
* a {@link Transferable} instance, no additional changes will be made.
317
* <pre>
318
* representationClass = String
319
* mimeType = "text/html"
320
* </pre>
321
*
322
* @since 1.8
323
*/
324
public static final DataFlavor allHtmlFlavor = initHtml("all");
325
326
/**
327
* Constructs a new {@code DataFlavor}. This constructor is provided only
328
* for the purpose of supporting the {@code Externalizable} interface. It is
329
* not intended for public (client) use.
330
*
331
* @since 1.2
332
*/
333
public DataFlavor() {
334
super();
335
}
336
337
/**
338
* Constructs a fully specified {@code DataFlavor}.
339
*
340
* @throws NullPointerException if either {@code primaryType},
341
* {@code subType} or {@code representationClass} is {@code null}
342
*/
343
private DataFlavor(String primaryType, String subType, MimeTypeParameterList params, Class<?> representationClass, String humanPresentableName) {
344
super();
345
if (primaryType == null) {
346
throw new NullPointerException("primaryType");
347
}
348
if (subType == null) {
349
throw new NullPointerException("subType");
350
}
351
if (representationClass == null) {
352
throw new NullPointerException("representationClass");
353
}
354
355
if (params == null) params = new MimeTypeParameterList();
356
357
params.set("class", representationClass.getName());
358
359
if (humanPresentableName == null) {
360
humanPresentableName = params.get("humanPresentableName");
361
362
if (humanPresentableName == null)
363
humanPresentableName = primaryType + "/" + subType;
364
}
365
366
try {
367
mimeType = new MimeType(primaryType, subType, params);
368
} catch (MimeTypeParseException mtpe) {
369
throw new IllegalArgumentException("MimeType Parse Exception: " + mtpe.getMessage());
370
}
371
372
this.representationClass = representationClass;
373
this.humanPresentableName = humanPresentableName;
374
375
mimeType.removeParameter("humanPresentableName");
376
}
377
378
/**
379
* Constructs a {@code DataFlavor} that represents a Java class.
380
* <p>
381
* The returned {@code DataFlavor} will have the following characteristics:
382
* <pre>
383
* representationClass = representationClass
384
* mimeType = application/x-java-serialized-object
385
* </pre>
386
*
387
* @param representationClass the class used to transfer data in this
388
* flavor
389
* @param humanPresentableName the human-readable string used to identify
390
* this flavor; if this parameter is {@code null} then the value of
391
* the MIME Content Type is used
392
* @throws NullPointerException if {@code representationClass} is
393
* {@code null}
394
*/
395
public DataFlavor(Class<?> representationClass, String humanPresentableName) {
396
this("application", "x-java-serialized-object", null, representationClass, humanPresentableName);
397
if (representationClass == null) {
398
throw new NullPointerException("representationClass");
399
}
400
}
401
402
/**
403
* Constructs a {@code DataFlavor} that represents a {@code MimeType}.
404
* <p>
405
* The returned {@code DataFlavor} will have the following characteristics:
406
* <p>
407
* If the {@code mimeType} is
408
* "application/x-java-serialized-object; class=&lt;representation class&gt;",
409
* the result is the same as calling
410
* {@code new DataFlavor(Class.forName(<representation class>)}.
411
* <p>
412
* Otherwise:
413
* <pre>
414
* representationClass = InputStream
415
* mimeType = mimeType
416
* </pre>
417
*
418
* @param mimeType the string used to identify the MIME type for this
419
* flavor; if the {@code mimeType} does not specify a "class="
420
* parameter, or if the class is not successfully loaded, then an
421
* {@code IllegalArgumentException} is thrown
422
* @param humanPresentableName the human-readable string used to identify
423
* this flavor; if this parameter is {@code null} then the value of
424
* the MIME Content Type is used
425
* @throws IllegalArgumentException if {@code mimeType} is invalid or if the
426
* class is not successfully loaded
427
* @throws NullPointerException if {@code mimeType} is {@code null}
428
*/
429
public DataFlavor(String mimeType, String humanPresentableName) {
430
super();
431
if (mimeType == null) {
432
throw new NullPointerException("mimeType");
433
}
434
try {
435
initialize(mimeType, humanPresentableName, this.getClass().getClassLoader());
436
} catch (MimeTypeParseException mtpe) {
437
throw new IllegalArgumentException("failed to parse:" + mimeType);
438
} catch (ClassNotFoundException cnfe) {
439
throw new IllegalArgumentException("can't find specified class: " + cnfe.getMessage());
440
}
441
}
442
443
/**
444
* Constructs a {@code DataFlavor} that represents a {@code MimeType}.
445
* <p>
446
* The returned {@code DataFlavor} will have the following characteristics:
447
* <p>
448
* If the mimeType is
449
* "application/x-java-serialized-object; class=&lt;representation class&gt;",
450
* the result is the same as calling
451
* {@code new DataFlavor(Class.forName(<representation class>)}.
452
* <p>
453
* Otherwise:
454
* <pre>
455
* representationClass = InputStream
456
* mimeType = mimeType
457
* </pre>
458
*
459
* @param mimeType the string used to identify the MIME type for this
460
* flavor
461
* @param humanPresentableName the human-readable string used to identify
462
* this flavor
463
* @param classLoader the class loader to use
464
* @throws ClassNotFoundException if the class is not loaded
465
* @throws IllegalArgumentException if {@code mimeType} is invalid
466
* @throws NullPointerException if {@code mimeType} is {@code null}
467
*/
468
public DataFlavor(String mimeType, String humanPresentableName, ClassLoader classLoader) throws ClassNotFoundException {
469
super();
470
if (mimeType == null) {
471
throw new NullPointerException("mimeType");
472
}
473
try {
474
initialize(mimeType, humanPresentableName, classLoader);
475
} catch (MimeTypeParseException mtpe) {
476
throw new IllegalArgumentException("failed to parse:" + mimeType);
477
}
478
}
479
480
/**
481
* Constructs a {@code DataFlavor} from a {@code mimeType} string. The
482
* string can specify a "class=&lt;fully specified Java class name&gt;"
483
* parameter to create a {@code DataFlavor} with the desired representation
484
* class. If the string does not contain "class=" parameter,
485
* {@code java.io.InputStream} is used as default.
486
*
487
* @param mimeType the string used to identify the MIME type for this
488
* flavor; if the class specified by "class=" parameter is not
489
* successfully loaded, then a {@code ClassNotFoundException} is
490
* thrown
491
* @throws ClassNotFoundException if the class is not loaded
492
* @throws IllegalArgumentException if {@code mimeType} is invalid
493
* @throws NullPointerException if {@code mimeType} is {@code null}
494
*/
495
public DataFlavor(String mimeType) throws ClassNotFoundException {
496
super();
497
if (mimeType == null) {
498
throw new NullPointerException("mimeType");
499
}
500
try {
501
initialize(mimeType, null, this.getClass().getClassLoader());
502
} catch (MimeTypeParseException mtpe) {
503
throw new IllegalArgumentException("failed to parse:" + mimeType);
504
}
505
}
506
507
/**
508
* Common initialization code called from various constructors.
509
*
510
* @param mimeType the MIME Content Type (must have a class= param)
511
* @param humanPresentableName the human Presentable Name or {@code null}
512
* @param classLoader the fallback class loader to resolve against
513
* @throws MimeTypeParseException
514
* @throws ClassNotFoundException
515
* @throws NullPointerException if {@code mimeType} is {@code null}
516
* @see #tryToLoadClass
517
*/
518
private void initialize(String mimeType, String humanPresentableName, ClassLoader classLoader) throws MimeTypeParseException, ClassNotFoundException {
519
if (mimeType == null) {
520
throw new NullPointerException("mimeType");
521
}
522
523
this.mimeType = new MimeType(mimeType); // throws
524
525
String rcn = getParameter("class");
526
527
if (rcn == null) {
528
if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType()))
529
530
throw new IllegalArgumentException("no representation class specified for:" + mimeType);
531
else
532
representationClass = java.io.InputStream.class; // default
533
} else { // got a class name
534
representationClass = DataFlavor.tryToLoadClass(rcn, classLoader);
535
}
536
537
this.mimeType.setParameter("class", representationClass.getName());
538
539
if (humanPresentableName == null) {
540
humanPresentableName = this.mimeType.getParameter("humanPresentableName");
541
if (humanPresentableName == null)
542
humanPresentableName = this.mimeType.getPrimaryType() + "/" + this.mimeType.getSubType();
543
}
544
545
this.humanPresentableName = humanPresentableName; // set it.
546
547
this.mimeType.removeParameter("humanPresentableName"); // just in case
548
}
549
550
/**
551
* String representation of this {@code DataFlavor} and its parameters. The
552
* resulting {@code String} contains the name of the {@code DataFlavor}
553
* class, this flavor's MIME type, and its representation class. If this
554
* flavor has a primary MIME type of "text", supports the charset parameter,
555
* and has an encoded representation, the flavor's charset is also included.
556
* See {@code selectBestTextFlavor} for a list of text flavors which support
557
* the charset parameter.
558
*
559
* @return string representation of this {@code DataFlavor}
560
* @see #selectBestTextFlavor
561
*/
562
public String toString() {
563
String string = getClass().getName();
564
string += "["+paramString()+"]";
565
return string;
566
}
567
568
private String paramString() {
569
String params = "";
570
params += "mimetype=";
571
if (mimeType == null) {
572
params += "null";
573
} else {
574
params += mimeType.getBaseType();
575
}
576
params += ";representationclass=";
577
if (representationClass == null) {
578
params += "null";
579
} else {
580
params += representationClass.getName();
581
}
582
if (DataFlavorUtil.isFlavorCharsetTextType(this) &&
583
(isRepresentationClassInputStream() ||
584
isRepresentationClassByteBuffer() ||
585
byte[].class.equals(representationClass)))
586
{
587
params += ";charset=" + DataFlavorUtil.getTextCharset(this);
588
}
589
return params;
590
}
591
592
/**
593
* Returns a {@code DataFlavor} representing plain text with Unicode
594
* encoding, where:
595
* <pre>
596
* representationClass = java.io.InputStream
597
* mimeType = "text/plain;
598
* charset=&lt;platform default Unicode encoding&gt;"
599
* </pre>
600
* @implNote Oracle's implementation for Microsoft Windows and macOS uses
601
* the encoding {@code utf-16le}. Oracle's implementation for Solaris and
602
* Linux uses the encoding {@code iso-10646-ucs-2}.
603
*
604
* @return a {@code DataFlavor} representing plain text with Unicode
605
* encoding
606
* @since 1.3
607
*/
608
public static final DataFlavor getTextPlainUnicodeFlavor() {
609
return new DataFlavor(
610
"text/plain;charset=" + DataFlavorUtil.getDesktopService().getDefaultUnicodeEncoding()
611
+";class=java.io.InputStream", "Plain Text");
612
}
613
614
/**
615
* Selects the best text {@code DataFlavor} from an array of
616
* {@code DataFlavor}s. Only {@code DataFlavor.stringFlavor}, and equivalent
617
* flavors, and flavors that have a primary MIME type of "text", are
618
* considered for selection.
619
* <p>
620
* Flavors are first sorted by their MIME types in the following order:
621
* <ul>
622
* <li>"text/sgml"
623
* <li>"text/xml"
624
* <li>"text/html"
625
* <li>"text/rtf"
626
* <li>"text/enriched"
627
* <li>"text/richtext"
628
* <li>"text/uri-list"
629
* <li>"text/tab-separated-values"
630
* <li>"text/t140"
631
* <li>"text/rfc822-headers"
632
* <li>"text/parityfec"
633
* <li>"text/directory"
634
* <li>"text/css"
635
* <li>"text/calendar"
636
* <li>"application/x-java-serialized-object"
637
* <li>"text/plain"
638
* <li>"text/&lt;other&gt;"
639
* </ul>
640
* <p>
641
* For example, "text/sgml" will be selected over "text/html", and
642
* {@code DataFlavor.stringFlavor} will be chosen over
643
* {@code DataFlavor.plainTextFlavor}.
644
* <p>
645
* If two or more flavors share the best MIME type in the array, then that
646
* MIME type will be checked to see if it supports the charset parameter.
647
* <p>
648
* The following MIME types support, or are treated as though they support,
649
* the charset parameter:
650
* <ul>
651
* <li>"text/sgml"
652
* <li>"text/xml"
653
* <li>"text/html"
654
* <li>"text/enriched"
655
* <li>"text/richtext"
656
* <li>"text/uri-list"
657
* <li>"text/directory"
658
* <li>"text/css"
659
* <li>"text/calendar"
660
* <li>"application/x-java-serialized-object"
661
* <li>"text/plain"
662
* </ul>
663
* The following MIME types do not support, or are treated as though they do
664
* not support, the charset parameter:
665
* <ul>
666
* <li>"text/rtf"
667
* <li>"text/tab-separated-values"
668
* <li>"text/t140"
669
* <li>"text/rfc822-headers"
670
* <li>"text/parityfec"
671
* </ul>
672
* For "text/&lt;other&gt;" MIME types, the first time the JRE needs to
673
* determine whether the MIME type supports the charset parameter, it will
674
* check whether the parameter is explicitly listed in an arbitrarily chosen
675
* {@code DataFlavor} which uses that MIME type. If so, the JRE will assume
676
* from that point on that the MIME type supports the charset parameter and
677
* will not check again. If the parameter is not explicitly listed, the JRE
678
* will assume from that point on that the MIME type does not support the
679
* charset parameter and will not check again. Because this check is
680
* performed on an arbitrarily chosen {@code DataFlavor}, developers must
681
* ensure that all {@code DataFlavor}s with a "text/&lt;other&gt;" MIME type
682
* specify the charset parameter if it is supported by that MIME type.
683
* Developers should never rely on the JRE to substitute the platform's
684
* default charset for a "text/&lt;other&gt;" DataFlavor. Failure to adhere
685
* to this restriction will lead to undefined behavior.
686
* <p>
687
* If the best MIME type in the array does not support the charset
688
* parameter, the flavors which share that MIME type will then be sorted by
689
* their representation classes in the following order:
690
* {@code java.io.InputStream}, {@code java.nio.ByteBuffer}, {@code [B},
691
* &lt;all others&gt;.
692
* <p>
693
* If two or more flavors share the best representation class, or if no
694
* flavor has one of the three specified representations, then one of those
695
* flavors will be chosen non-deterministically.
696
* <p>
697
* If the best MIME type in the array does support the charset parameter,
698
* the flavors which share that MIME type will then be sorted by their
699
* representation classes in the following order: {@code java.io.Reader},
700
* {@code java.lang.String}, {@code java.nio.CharBuffer}, {@code [C},
701
* &lt;all others&gt;.
702
* <p>
703
* If two or more flavors share the best representation class, and that
704
* representation is one of the four explicitly listed, then one of those
705
* flavors will be chosen non-deterministically. If, however, no flavor has
706
* one of the four specified representations, the flavors will then be
707
* sorted by their charsets. Unicode charsets, such as "UTF-16", "UTF-8",
708
* "UTF-16BE", "UTF-16LE", and their aliases, are considered best. After
709
* them, the platform default charset and its aliases are selected.
710
* "US-ASCII" and its aliases are worst. All other charsets are chosen in
711
* alphabetical order, but only charsets supported by this implementation of
712
* the Java platform will be considered.
713
* <p>
714
* If two or more flavors share the best charset, the flavors will then
715
* again be sorted by their representation classes in the following order:
716
* {@code java.io.InputStream}, {@code java.nio.ByteBuffer}, {@code [B},
717
* &lt;all others&gt;.
718
* <p>
719
* If two or more flavors share the best representation class, or if no
720
* flavor has one of the three specified representations, then one of those
721
* flavors will be chosen non-deterministically.
722
*
723
* @param availableFlavors an array of available {@code DataFlavor}s
724
* @return the best (highest fidelity) flavor according to the rules
725
* specified above, or {@code null}, if {@code availableFlavors} is
726
* {@code null}, has zero length, or contains no text flavors
727
* @since 1.3
728
*/
729
public static final DataFlavor selectBestTextFlavor(
730
DataFlavor[] availableFlavors) {
731
if (availableFlavors == null || availableFlavors.length == 0) {
732
return null;
733
}
734
735
DataFlavor bestFlavor = Collections.max(Arrays.asList(availableFlavors),
736
DataFlavorUtil.getTextFlavorComparator());
737
738
if (!bestFlavor.isFlavorTextType()) {
739
return null;
740
}
741
742
return bestFlavor;
743
}
744
745
/**
746
* Gets a Reader for a text flavor, decoded, if necessary, for the expected
747
* charset (encoding). The supported representation classes are
748
* {@code java.io.Reader}, {@code java.lang.String},
749
* {@code java.nio.CharBuffer}, {@code [C}, {@code java.io.InputStream},
750
* {@code java.nio.ByteBuffer}, and {@code [B}.
751
* <p>
752
* Because text flavors which do not support the charset parameter are
753
* encoded in a non-standard format, this method should not be called for
754
* such flavors. However, in order to maintain backward-compatibility, if
755
* this method is called for such a flavor, this method will treat the
756
* flavor as though it supports the charset parameter and attempt to decode
757
* it accordingly. See {@code selectBestTextFlavor} for a list of text
758
* flavors which do not support the charset parameter.
759
*
760
* @param transferable the {@code Transferable} whose data will be
761
* requested in this flavor
762
* @return a {@code Reader} to read the {@code Transferable}'s data
763
* @throws IllegalArgumentException if the representation class is not one
764
* of the seven listed above
765
* @throws IllegalArgumentException if the {@code Transferable} has
766
* {@code null} data
767
* @throws NullPointerException if the {@code Transferable} is {@code null}
768
* @throws UnsupportedEncodingException if this flavor's representation is
769
* {@code java.io.InputStream}, {@code java.nio.ByteBuffer}, or
770
* {@code [B} and this flavor's encoding is not supported by this
771
* implementation of the Java platform
772
* @throws UnsupportedFlavorException if the {@code Transferable} does not
773
* support this flavor
774
* @throws IOException if the data cannot be read because of an I/O error
775
* @see #selectBestTextFlavor
776
* @since 1.3
777
*/
778
public Reader getReaderForText(Transferable transferable)
779
throws UnsupportedFlavorException, IOException
780
{
781
Object transferObject = transferable.getTransferData(this);
782
if (transferObject == null) {
783
throw new IllegalArgumentException
784
("getTransferData() returned null");
785
}
786
787
if (transferObject instanceof Reader) {
788
return (Reader)transferObject;
789
} else if (transferObject instanceof String) {
790
return new StringReader((String)transferObject);
791
} else if (transferObject instanceof CharBuffer) {
792
CharBuffer buffer = (CharBuffer)transferObject;
793
int size = buffer.remaining();
794
char[] chars = new char[size];
795
buffer.get(chars, 0, size);
796
return new CharArrayReader(chars);
797
} else if (transferObject instanceof char[]) {
798
return new CharArrayReader((char[])transferObject);
799
}
800
801
InputStream stream = null;
802
803
if (transferObject instanceof InputStream) {
804
stream = (InputStream)transferObject;
805
} else if (transferObject instanceof ByteBuffer) {
806
ByteBuffer buffer = (ByteBuffer)transferObject;
807
int size = buffer.remaining();
808
byte[] bytes = new byte[size];
809
buffer.get(bytes, 0, size);
810
stream = new ByteArrayInputStream(bytes);
811
} else if (transferObject instanceof byte[]) {
812
stream = new ByteArrayInputStream((byte[])transferObject);
813
}
814
815
if (stream == null) {
816
throw new IllegalArgumentException("transfer data is not Reader, String, CharBuffer, char array, InputStream, ByteBuffer, or byte array");
817
}
818
819
String encoding = getParameter("charset");
820
return (encoding == null)
821
? new InputStreamReader(stream)
822
: new InputStreamReader(stream, encoding);
823
}
824
825
/**
826
* Returns the MIME type string for this {@code DataFlavor}.
827
*
828
* @return the MIME type string for this flavor
829
*/
830
public String getMimeType() {
831
return (mimeType != null) ? mimeType.toString() : null;
832
}
833
834
/**
835
* Returns the {@code Class} which objects supporting this
836
* {@code DataFlavor} will return when this {@code DataFlavor} is requested.
837
*
838
* @return the {@code Class} which objects supporting this
839
* {@code DataFlavor} will return when this {@code DataFlavor} is
840
* requested
841
*/
842
public Class<?> getRepresentationClass() {
843
return representationClass;
844
}
845
846
/**
847
* Returns the human presentable name for the data format that this
848
* {@code DataFlavor} represents. This name would be localized for different
849
* countries.
850
*
851
* @return the human presentable name for the data format that this
852
* {@code DataFlavor} represents
853
*/
854
public String getHumanPresentableName() {
855
return humanPresentableName;
856
}
857
858
/**
859
* Returns the primary MIME type for this {@code DataFlavor}.
860
*
861
* @return the primary MIME type of this {@code DataFlavor}
862
*/
863
public String getPrimaryType() {
864
return (mimeType != null) ? mimeType.getPrimaryType() : null;
865
}
866
867
/**
868
* Returns the sub MIME type of this {@code DataFlavor}.
869
*
870
* @return the Sub MIME type of this {@code DataFlavor}
871
*/
872
public String getSubType() {
873
return (mimeType != null) ? mimeType.getSubType() : null;
874
}
875
876
/**
877
* Returns the human presentable name for this {@code DataFlavor} if
878
* {@code paramName} equals "humanPresentableName". Otherwise returns the
879
* MIME type value associated with {@code paramName}.
880
*
881
* @param paramName the parameter name requested
882
* @return the value of the name parameter, or {@code null} if there is no
883
* associated value
884
*/
885
public String getParameter(String paramName) {
886
if (paramName.equals("humanPresentableName")) {
887
return humanPresentableName;
888
} else {
889
return (mimeType != null)
890
? mimeType.getParameter(paramName) : null;
891
}
892
}
893
894
/**
895
* Sets the human presentable name for the data format that this
896
* {@code DataFlavor} represents. This name would be localized for different
897
* countries.
898
*
899
* @param humanPresentableName the new human presentable name
900
*/
901
public void setHumanPresentableName(String humanPresentableName) {
902
this.humanPresentableName = humanPresentableName;
903
}
904
905
/**
906
* {@inheritDoc}
907
* <p>
908
* The equals comparison for the {@code DataFlavor} class is implemented as
909
* follows: Two {@code DataFlavor}s are considered equal if and only if
910
* their MIME primary type and subtype and representation class are equal.
911
* Additionally, if the primary type is "text", the subtype denotes a text
912
* flavor which supports the charset parameter, and the representation class
913
* is not {@code java.io.Reader}, {@code java.lang.String},
914
* {@code java.nio.CharBuffer}, or {@code [C}, the {@code charset} parameter
915
* must also be equal. If a charset is not explicitly specified for one or
916
* both {@code DataFlavor}s, the platform default encoding is assumed. See
917
* {@code selectBestTextFlavor} for a list of text flavors which support the
918
* charset parameter.
919
*
920
* @param o the {@code Object} to compare with {@code this}
921
* @return {@code true} if {@code that} is equivalent to this
922
* {@code DataFlavor}; {@code false} otherwise
923
* @see #selectBestTextFlavor
924
*/
925
public boolean equals(Object o) {
926
return ((o instanceof DataFlavor) && equals((DataFlavor)o));
927
}
928
929
/**
930
* This method has the same behavior as {@link #equals(Object)}. The only
931
* difference being that it takes a {@code DataFlavor} instance as a
932
* parameter.
933
*
934
* @param that the {@code DataFlavor} to compare with {@code this}
935
* @return {@code true} if {@code that} is equivalent to this
936
* {@code DataFlavor}; {@code false} otherwise
937
* @see #selectBestTextFlavor
938
*/
939
public boolean equals(DataFlavor that) {
940
if (that == null) {
941
return false;
942
}
943
if (this == that) {
944
return true;
945
}
946
947
if (!Objects.equals(this.getRepresentationClass(), that.getRepresentationClass())) {
948
return false;
949
}
950
951
if (mimeType == null) {
952
if (that.mimeType != null) {
953
return false;
954
}
955
} else {
956
if (!mimeType.match(that.mimeType)) {
957
return false;
958
}
959
960
if ("text".equals(getPrimaryType())) {
961
if (DataFlavorUtil.doesSubtypeSupportCharset(this)
962
&& representationClass != null
963
&& !isStandardTextRepresentationClass()) {
964
String thisCharset =
965
DataFlavorUtil.canonicalName(this.getParameter("charset"));
966
String thatCharset =
967
DataFlavorUtil.canonicalName(that.getParameter("charset"));
968
if (!Objects.equals(thisCharset, thatCharset)) {
969
return false;
970
}
971
}
972
973
if ("html".equals(getSubType())) {
974
String thisDocument = this.getParameter("document");
975
String thatDocument = that.getParameter("document");
976
if (!Objects.equals(thisDocument, thatDocument)) {
977
return false;
978
}
979
}
980
}
981
}
982
983
return true;
984
}
985
986
/**
987
* Compares only the {@code mimeType} against the passed in {@code String}
988
* and {@code representationClass} is not considered in the comparison. If
989
* {@code representationClass} needs to be compared, then
990
* {@code equals(new DataFlavor(s))} may be used.
991
*
992
* @param s the {@code mimeType} to compare
993
* @return {@code true} if the String (MimeType) is equal; {@code false}
994
* otherwise or if {@code s} is {@code null}
995
* @deprecated As inconsistent with {@code hashCode()} contract, use
996
* {@link #isMimeTypeEqual(String)} instead.
997
*/
998
@Deprecated
999
public boolean equals(String s) {
1000
if (s == null || mimeType == null)
1001
return false;
1002
return isMimeTypeEqual(s);
1003
}
1004
1005
/**
1006
* Returns hash code for this {@code DataFlavor}. For two equal
1007
* {@code DataFlavor}s, hash codes are equal. For the {@code String} that
1008
* matches {@code DataFlavor.equals(String)}, it is not guaranteed that
1009
* {@code DataFlavor}'s hash code is equal to the hash code of the
1010
* {@code String}.
1011
*
1012
* @return a hash code for this {@code DataFlavor}
1013
*/
1014
public int hashCode() {
1015
int total = 0;
1016
1017
if (representationClass != null) {
1018
total += representationClass.hashCode();
1019
}
1020
1021
if (mimeType != null) {
1022
String primaryType = mimeType.getPrimaryType();
1023
if (primaryType != null) {
1024
total += primaryType.hashCode();
1025
}
1026
1027
// Do not add subType.hashCode() to the total. equals uses
1028
// MimeType.match which reports a match if one or both of the
1029
// subTypes is '*', regardless of the other subType.
1030
1031
if ("text".equals(primaryType)) {
1032
if (DataFlavorUtil.doesSubtypeSupportCharset(this)
1033
&& representationClass != null
1034
&& !isStandardTextRepresentationClass()) {
1035
String charset = DataFlavorUtil.canonicalName(getParameter("charset"));
1036
if (charset != null) {
1037
total += charset.hashCode();
1038
}
1039
}
1040
1041
if ("html".equals(getSubType())) {
1042
String document = this.getParameter("document");
1043
if (document != null) {
1044
total += document.hashCode();
1045
}
1046
}
1047
}
1048
}
1049
1050
return total;
1051
}
1052
1053
/**
1054
* Identical to {@link #equals(DataFlavor)}.
1055
*
1056
* @param that the {@code DataFlavor} to compare with {@code this}
1057
* @return {@code true} if {@code that} is equivalent to this
1058
* {@code DataFlavor}; {@code false} otherwise
1059
* @see #selectBestTextFlavor
1060
* @since 1.3
1061
*/
1062
public boolean match(DataFlavor that) {
1063
return equals(that);
1064
}
1065
1066
/**
1067
* Returns whether the string representation of the MIME type passed in is
1068
* equivalent to the MIME type of this {@code DataFlavor}. Parameters are
1069
* not included in the comparison.
1070
*
1071
* @param mimeType the string representation of the MIME type
1072
* @return {@code true} if the string representation of the MIME type passed
1073
* in is equivalent to the MIME type of this {@code DataFlavor};
1074
* {@code false} otherwise
1075
* @throws NullPointerException if mimeType is {@code null}
1076
*/
1077
public boolean isMimeTypeEqual(String mimeType) {
1078
// JCK Test DataFlavor0117: if 'mimeType' is null, throw NPE
1079
if (mimeType == null) {
1080
throw new NullPointerException("mimeType");
1081
}
1082
if (this.mimeType == null) {
1083
return false;
1084
}
1085
try {
1086
return this.mimeType.match(new MimeType(mimeType));
1087
} catch (MimeTypeParseException mtpe) {
1088
return false;
1089
}
1090
}
1091
1092
/**
1093
* Compares the {@code mimeType} of two {@code DataFlavor} objects. No
1094
* parameters are considered.
1095
*
1096
* @param dataFlavor the {@code DataFlavor} to be compared
1097
* @return {@code true} if the {@code MimeType}s are equal, otherwise
1098
* {@code false}
1099
*/
1100
public final boolean isMimeTypeEqual(DataFlavor dataFlavor) {
1101
return isMimeTypeEqual(dataFlavor.mimeType);
1102
}
1103
1104
/**
1105
* Compares the {@code mimeType} of two {@code DataFlavor} objects. No
1106
* parameters are considered.
1107
*
1108
* @return {@code true} if the {@code MimeType}s are equal, otherwise
1109
* {@code false}
1110
*/
1111
private boolean isMimeTypeEqual(MimeType mtype) {
1112
if (this.mimeType == null) {
1113
return (mtype == null);
1114
}
1115
return mimeType.match(mtype);
1116
}
1117
1118
/**
1119
* Checks if the representation class is one of the standard text
1120
* representation classes.
1121
*
1122
* @return {@code true} if the representation class is one of the standard
1123
* text representation classes, otherwise {@code false}
1124
*/
1125
private boolean isStandardTextRepresentationClass() {
1126
return isRepresentationClassReader()
1127
|| String.class.equals(representationClass)
1128
|| isRepresentationClassCharBuffer()
1129
|| char[].class.equals(representationClass);
1130
}
1131
1132
/**
1133
* Does the {@code DataFlavor} represent a serialized object?
1134
*
1135
* @return whether or not a serialized object is represented
1136
*/
1137
public boolean isMimeTypeSerializedObject() {
1138
return isMimeTypeEqual(javaSerializedObjectMimeType);
1139
}
1140
1141
/**
1142
* Returns the default representation class.
1143
*
1144
* @return the default representation class
1145
*/
1146
public final Class<?> getDefaultRepresentationClass() {
1147
return java.io.InputStream.class;
1148
}
1149
1150
/**
1151
* Returns the name of the default representation class.
1152
*
1153
* @return the name of the default representation class
1154
*/
1155
public final String getDefaultRepresentationClassAsString() {
1156
return getDefaultRepresentationClass().getName();
1157
}
1158
1159
/**
1160
* Does the {@code DataFlavor} represent a {@code java.io.InputStream}?
1161
*
1162
* @return whether or not this {@code DataFlavor} represent a
1163
* {@code java.io.InputStream}
1164
*/
1165
public boolean isRepresentationClassInputStream() {
1166
return java.io.InputStream.class.isAssignableFrom(representationClass);
1167
}
1168
1169
/**
1170
* Returns whether the representation class for this {@code DataFlavor} is
1171
* {@code java.io.Reader} or a subclass thereof.
1172
*
1173
* @return whether or not the representation class for this
1174
* {@code DataFlavor} is {@code java.io.Reader} or a subclass
1175
* thereof
1176
* @since 1.4
1177
*/
1178
public boolean isRepresentationClassReader() {
1179
return java.io.Reader.class.isAssignableFrom(representationClass);
1180
}
1181
1182
/**
1183
* Returns whether the representation class for this {@code DataFlavor} is
1184
* {@code java.nio.CharBuffer} or a subclass thereof.
1185
*
1186
* @return whether or not the representation class for this
1187
* {@code DataFlavor} is {@code java.nio.CharBuffer} or a subclass
1188
* thereof
1189
* @since 1.4
1190
*/
1191
public boolean isRepresentationClassCharBuffer() {
1192
return java.nio.CharBuffer.class.isAssignableFrom(representationClass);
1193
}
1194
1195
/**
1196
* Returns whether the representation class for this {@code DataFlavor} is
1197
* {@code java.nio.ByteBuffer} or a subclass thereof.
1198
*
1199
* @return whether or not the representation class for this
1200
* {@code DataFlavor} is {@code java.nio.ByteBuffer} or a subclass
1201
* thereof
1202
* @since 1.4
1203
*/
1204
public boolean isRepresentationClassByteBuffer() {
1205
return java.nio.ByteBuffer.class.isAssignableFrom(representationClass);
1206
}
1207
1208
/**
1209
* Returns {@code true} if the representation class can be serialized.
1210
*
1211
* @return {@code true} if the representation class can be serialized
1212
*/
1213
public boolean isRepresentationClassSerializable() {
1214
return java.io.Serializable.class.isAssignableFrom(representationClass);
1215
}
1216
1217
/**
1218
* Returns {@code true} if the representation class is {@code Remote}.
1219
*
1220
* @return {@code true} if the representation class is {@code Remote}
1221
*/
1222
public boolean isRepresentationClassRemote() {
1223
return DataFlavorUtil.RMI.isRemote(representationClass);
1224
}
1225
1226
/**
1227
* Returns {@code true} if the {@code DataFlavor} specified represents a
1228
* serialized object.
1229
*
1230
* @return {@code true} if the {@code DataFlavor} specified represents a
1231
* Serialized Object
1232
*/
1233
public boolean isFlavorSerializedObjectType() {
1234
return isRepresentationClassSerializable() && isMimeTypeEqual(javaSerializedObjectMimeType);
1235
}
1236
1237
/**
1238
* Returns {@code true} if the {@code DataFlavor} specified represents a
1239
* remote object.
1240
*
1241
* @return {@code true} if the {@code DataFlavor} specified represents a
1242
* Remote Object
1243
*/
1244
public boolean isFlavorRemoteObjectType() {
1245
return isRepresentationClassRemote()
1246
&& isRepresentationClassSerializable()
1247
&& isMimeTypeEqual(javaRemoteObjectMimeType);
1248
}
1249
1250
/**
1251
* Returns {@code true} if the {@code DataFlavor} specified represents a
1252
* list of file objects.
1253
*
1254
* @return {@code true} if the {@code DataFlavor} specified represents a
1255
* {@code java.util.List} of {@code java.io.File} objects
1256
*/
1257
public boolean isFlavorJavaFileListType() {
1258
if (mimeType == null || representationClass == null)
1259
return false;
1260
return java.util.List.class.isAssignableFrom(representationClass) &&
1261
mimeType.match(javaFileListFlavor.mimeType);
1262
1263
}
1264
1265
/**
1266
* Returns whether this {@code DataFlavor} is a valid text flavor for this
1267
* implementation of the Java platform. Only flavors equivalent to
1268
* {@code DataFlavor.stringFlavor} and {@code DataFlavor}s with a primary
1269
* MIME type of "text" can be valid text flavors.
1270
* <p>
1271
* If this flavor supports the charset parameter, it must be equivalent to
1272
* {@code DataFlavor.stringFlavor}, or its representation must be
1273
* {@code java.io.Reader}, {@code java.lang.String},
1274
* {@code java.nio.CharBuffer}, {@code [C}, {@code java.io.InputStream},
1275
* {@code java.nio.ByteBuffer}, or {@code [B}. If the representation is
1276
* {@code java.io.InputStream}, {@code java.nio.ByteBuffer}, or {@code [B},
1277
* then this flavor's {@code charset} parameter must be supported by this
1278
* implementation of the Java platform. If a charset is not specified, then
1279
* the platform default charset, which is always supported, is assumed.
1280
* <p>
1281
* If this flavor does not support the charset parameter, its representation
1282
* must be {@code java.io.InputStream}, {@code java.nio.ByteBuffer}, or
1283
* {@code [B}.
1284
* <p>
1285
* See {@code selectBestTextFlavor} for a list of text flavors which support
1286
* the charset parameter.
1287
*
1288
* @return {@code true} if this {@code DataFlavor} is a valid text flavor as
1289
* described above; {@code false} otherwise
1290
* @see #selectBestTextFlavor
1291
* @since 1.4
1292
*/
1293
public boolean isFlavorTextType() {
1294
return (DataFlavorUtil.isFlavorCharsetTextType(this) ||
1295
DataFlavorUtil.isFlavorNoncharsetTextType(this));
1296
}
1297
1298
/**
1299
* Serializes this {@code DataFlavor}.
1300
*/
1301
public synchronized void writeExternal(ObjectOutput os) throws IOException {
1302
if (mimeType != null) {
1303
mimeType.setParameter("humanPresentableName", humanPresentableName);
1304
os.writeObject(mimeType);
1305
mimeType.removeParameter("humanPresentableName");
1306
} else {
1307
os.writeObject(null);
1308
}
1309
1310
os.writeObject(representationClass);
1311
}
1312
1313
/**
1314
* Restores this {@code DataFlavor} from a Serialized state.
1315
*/
1316
public synchronized void readExternal(ObjectInput is) throws IOException , ClassNotFoundException {
1317
String rcn = null;
1318
mimeType = (MimeType)is.readObject();
1319
1320
if (mimeType != null) {
1321
humanPresentableName =
1322
mimeType.getParameter("humanPresentableName");
1323
mimeType.removeParameter("humanPresentableName");
1324
rcn = mimeType.getParameter("class");
1325
if (rcn == null) {
1326
throw new IOException("no class parameter specified in: " +
1327
mimeType);
1328
}
1329
}
1330
1331
try {
1332
representationClass = (Class)is.readObject();
1333
} catch (OptionalDataException ode) {
1334
if (!ode.eof || ode.length != 0) {
1335
throw ode;
1336
}
1337
// Ensure backward compatibility.
1338
// Old versions didn't write the representation class to the stream.
1339
if (rcn != null) {
1340
representationClass =
1341
DataFlavor.tryToLoadClass(rcn, getClass().getClassLoader());
1342
}
1343
}
1344
}
1345
1346
/**
1347
* Returns a clone of this {@code DataFlavor}.
1348
*
1349
* @return a clone of this {@code DataFlavor}
1350
*/
1351
public Object clone() throws CloneNotSupportedException {
1352
Object newObj = super.clone();
1353
if (mimeType != null) {
1354
((DataFlavor)newObj).mimeType = (MimeType)mimeType.clone();
1355
}
1356
return newObj;
1357
} // clone()
1358
1359
/**
1360
* Called on {@code DataFlavor} for every MIME Type parameter to allow
1361
* {@code DataFlavor} subclasses to handle special parameters like the
1362
* text/plain {@code charset} parameters, whose values are case insensitive.
1363
* (MIME type parameter values are supposed to be case sensitive.
1364
* <p>
1365
* This method is called for each parameter name/value pair and should
1366
* return the normalized representation of the {@code parameterValue}.
1367
*
1368
* @param parameterName the parameter name
1369
* @param parameterValue the parameter value
1370
* @return the parameter value
1371
* @deprecated This method is never invoked by this implementation from 1.1
1372
* onwards
1373
*/
1374
@Deprecated
1375
protected String normalizeMimeTypeParameter(String parameterName, String parameterValue) {
1376
return parameterValue;
1377
}
1378
1379
/**
1380
* Called for each MIME type string to give {@code DataFlavor} subtypes the
1381
* opportunity to change how the normalization of MIME types is
1382
* accomplished. One possible use would be to add default parameter/value
1383
* pairs in cases where none are present in the MIME type string passed in.
1384
*
1385
* @param mimeType the mime type
1386
* @return the mime type
1387
* @deprecated This method is never invoked by this implementation from 1.1
1388
* onwards
1389
*/
1390
@Deprecated
1391
protected String normalizeMimeType(String mimeType) {
1392
return mimeType;
1393
}
1394
1395
/*
1396
* fields
1397
*/
1398
1399
/* placeholder for caching any platform-specific data for flavor */
1400
1401
transient int atom;
1402
1403
/* Mime Type of DataFlavor */
1404
1405
MimeType mimeType;
1406
1407
private String humanPresentableName;
1408
1409
/**
1410
* Java class of objects this DataFlavor represents.
1411
**/
1412
private Class<?> representationClass;
1413
1414
} // class DataFlavor
1415
1416