📄 dataflavor.java
字号:
throw new NullPointerException("mimeType"); } try { initialize(mimeType, humanPresentableName, classLoader); } catch (MimeTypeParseException mtpe) { throw new IllegalArgumentException("failed to parse:" + mimeType); } } /** * Constructs a <code>DataFlavor</code> from a <code>mimeType</code> string. * The string can specify a "class=<fully specified Java class name>" * parameter to create a <code>DataFlavor</code> with the desired * representation class. If the string does not contain "class=" parameter, * <code>java.io.InputStream</code> is used as default. * * @param mimeType the string used to identify the MIME type for this flavor; * if the class specified by "class=" parameter is not * successfully loaded, then an * <code>ClassNotFoundException</code> is thrown * @exception ClassNotFoundException if the class is not loaded * @exception IllegalArgumentException if <code>mimeType</code> is * invalid * @exception NullPointerException if <code>mimeType</code> is null */ public DataFlavor(String mimeType) throws ClassNotFoundException { super(); if (mimeType == null) { throw new NullPointerException("mimeType"); } try { initialize(mimeType, null, this.getClass().getClassLoader()); } catch (MimeTypeParseException mtpe) { throw new IllegalArgumentException("failed to parse:" + mimeType); } } /** * Common initialization code called from various constructors. * * @param mimeType the MIME Content Type (must have a class= param) * @param humanPresentableName the human Presentable Name or * <code>null</code> * @param classLoader the fallback class loader to resolve against * * @throws MimeTypeParseException * @throws ClassNotFoundException * @throws NullPointerException if <code>mimeType</code> is null * * @see tryToLoadClass */ private void initialize(String mimeType, String humanPresentableName, ClassLoader classLoader) throws MimeTypeParseException, ClassNotFoundException { if (mimeType == null) { throw new NullPointerException("mimeType"); } this.mimeType = new MimeType(mimeType); // throws String rcn = getParameter("class"); if (rcn == null) { if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType())) throw new IllegalArgumentException("no representation class specified for:" + mimeType); else representationClass = java.io.InputStream.class; // default } else { // got a class name representationClass = DataFlavor.tryToLoadClass(rcn, classLoader); } this.mimeType.setParameter("class", representationClass.getName()); if (humanPresentableName == null) { humanPresentableName = this.mimeType.getParameter("humanPresentableName"); if (humanPresentableName == null) humanPresentableName = this.mimeType.getPrimaryType() + "/" + this.mimeType.getSubType(); } this.humanPresentableName = humanPresentableName; // set it. this.mimeType.removeParameter("humanPresentableName"); // just in case } /** * String representation of this <code>DataFlavor</code> and its * parameters. The resulting <code>String</code> contains the name of * the <code>DataFlavor</code> class, this flavor's MIME type, and its * representation class. If this flavor has a primary MIME type of "text", * supports the charset parameter, and has an encoded representation, the * flavor's charset is also included. See <code>selectBestTextFlavor</code> * for a list of text flavors which support the charset parameter. * * @return string representation of this <code>DataFlavor</code> * @see #selectBestTextFlavor */ public String toString() { String string = getClass().getName(); string += "["+paramString()+"]"; return string; } private String paramString() { String params = ""; params += "mimetype="; if (mimeType == null) { params += "null"; } else { params += mimeType.getBaseType(); } params += ";representationclass="; if (representationClass == null) { params += "null"; } else { params += representationClass.getName(); } if (DataTransferer.isFlavorCharsetTextType(this) && (isRepresentationClassInputStream() || isRepresentationClassByteBuffer() || DataTransferer.byteArrayClass.equals(representationClass))) { params += ";charset=" + DataTransferer.getTextCharset(this); } return params; } /** * Returns a <code>DataFlavor</code> representing plain text with Unicode * encoding, where: * <pre> * representationClass = java.io.InputStream * mimeType = "text/plain; * charset=<platform default Unicode encoding>" * </pre> * Sun's implementation for Microsoft Windows use the encoding <code>utf-16le</code>. * Sun's implementation for Solaris and Linux use the encoding * <code>iso-10646-ucs-2</code>. * * @return a <code>DataFlavor</code> representing plain text * with Unicode encoding */ public static final DataFlavor getTextPlainUnicodeFlavor() { String encoding = null; DataTransferer transferer = DataTransferer.getInstance(); if (transferer != null) { encoding = transferer.getDefaultUnicodeEncoding(); } return new DataFlavor( "text/plain;charset="+encoding +";class=java.io.InputStream", "Plain Text"); } /** * Selects the best text <code>DataFlavor</code> from an array of <code> * DataFlavor</code>s. Only <code>DataFlavor.stringFlavor</code>, and * equivalent flavors, and flavors that have a primary MIME type of "text", * are considered for selection. * <p> * Flavors are first sorted by their MIME types in the following order: * <ul> * <li>"text/sgml" * <li>"text/xml" * <li>"text/html" * <li>"text/rtf" * <li>"text/enriched" * <li>"text/richtext" * <li>"text/uri-list" * <li>"text/tab-separated-values" * <li>"text/t140" * <li>"text/rfc822-headers" * <li>"text/parityfec" * <li>"text/directory" * <li>"text/css" * <li>"text/calendar" * <li>"application/x-java-serialized-object" * <li>"text/plain" * <li>"text/<other>" * </ul> * <p>For example, "text/sgml" will be selected over * "text/html", and <code>DataFlavor.stringFlavor</code> will be chosen * over <code>DataFlavor.plainTextFlavor</code>. * <p> * If two or more flavors share the best MIME type in the array, then that * MIME type will be checked to see if it supports the charset parameter. * <p> * The following MIME types support, or are treated as though they support, * the charset parameter: * <ul> * <li>"text/sgml" * <li>"text/xml" * <li>"text/html" * <li>"text/enriched" * <li>"text/richtext" * <li>"text/uri-list" * <li>"text/directory" * <li>"text/css" * <li>"text/calendar" * <li>"application/x-java-serialized-object" * <li>"text/plain" * </ul> * The following MIME types do not support, or are treated as though they * do not support, the charset parameter: * <ul> * <li>"text/rtf" * <li>"text/tab-separated-values" * <li>"text/t140" * <li>"text/rfc822-headers" * <li>"text/parityfec" * </ul> * For "text/<other>" MIME types, the first time the JRE needs to * determine whether the MIME type supports the charset parameter, it will * check whether the parameter is explicitly listed in an arbitrarily * chosen <code>DataFlavor</code> which uses that MIME type. If so, the JRE * will assume from that point on that the MIME type supports the charset * parameter and will not check again. If the parameter is not explicitly * listed, the JRE will assume from that point on that the MIME type does * not support the charset parameter and will not check again. Because * this check is performed on an arbitrarily chosen * <code>DataFlavor</code>, developers must ensure that all * <code>DataFlavor</code>s with a "text/<other>" MIME type specify * the charset parameter if it is supported by that MIME type. Developers * should never rely on the JRE to substitute the platform's default * charset for a "text/<other>" DataFlavor. Failure to adhere to this * restriction will lead to undefined behavior. * <p> * If the best MIME type in the array does not support the charset * parameter, the flavors which share that MIME type will then be sorted by * their representation classes in the following order: * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, * <code>[B</code>, <all others>. * <p> * If two or more flavors share the best representation class, or if no * flavor has one of the three specified representations, then one of those * flavors will be chosen non-deterministically. * <p> * If the best MIME type in the array does support the charset parameter, * the flavors which share that MIME type will then be sorted by their * representation classes in the following order: * <code>java.io.Reader</code>, <code>java.lang.String</code>, * <code>java.nio.CharBuffer</code>, <code>[C</code>, <all others>. * <p> * If two or more flavors share the best representation class, and that * representation is one of the four explicitly listed, then one of those * flavors will be chosen non-deterministically. If, however, no flavor has * one of the four specified representations, the flavors will then be * sorted by their charsets. Unicode charsets, such as "UTF-16", "UTF-8", * "UTF-16BE", "UTF-16LE", and their aliases, are considered best. After * them, the platform default charset and its aliases are selected. * "US-ASCII" and its aliases are worst. All other charsets are chosen in * alphabetical order, but only charsets supported by this implementation * of the Java platform will be considered. * <p> * If two or more flavors share the best charset, the flavors will then * again be sorted by their representation classes in the following order: * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, * <code>[B</code>, <all others>. * <p> * If two or more flavors share the best representation class, or if no * flavor has one of the three specified representations, then one of those * flavors will be chosen non-deterministically. * * @param availableFlavors an array of available <code>DataFlavor</code>s * @return the best (highest fidelity) flavor according to the rules * specified above, or <code>null</code>, * if <code>availableFlavors</code> is <code>null</code>, * has zero length, or contains no text flavors * @since 1.3 */ public static final DataFlavor selectBestTextFlavor( DataFlavor[] availableFlavors) { if (availableFlavors == null || availableFlavors.length == 0) { return null; } if (textFlavorComparator == null) { textFlavorComparator = new TextFlavorComparator(); } DataFlavor bestFlavor = (DataFlavor)Collections.max(Arrays.asList(availableFlavors), textFlavorComparator); if (!bestFlavor.isFlavorTextType()) { return null; } return bestFlavor; } private static Comparator textFlavorComparator; static class TextFlavorComparator extends DataTransferer.DataFlavorComparator { /** * Compares two <code>DataFlavor</code> objects. Returns a negative * integer, zero, or a positive integer as the first * <code>DataFlavor</code> is worse than, equal to, or better than the * second. * <p> * <code>DataFlavor</code>s are ordered according to the rules outlined * for <code>selectBestTextFlavor</code>. * * @param obj1 the first <code>DataFlavor</code> to be compared * @param obj2 the second <code>DataFlavor</code> to be compared * @return a negative integer, zero, or a positive integer as the first * argument is worse, equal to, or better than the second * @throws ClassCastException if either of the arguments is not an * instance of <code>DataFlavor</code> * @throws NullPointerException if either of the arguments is * <code>null</code> * * @see #selectBestTextFlavor */ public int compare(Object obj1, Object obj2) { DataFlavor flavor1 = (DataFlavor)obj1; DataFlavor flavor2 = (DataFlavor)obj2; if (flavor1.isFlavorTextType()) { if (flavor2.isFlavorTextType()) { return super.compare(obj1, obj2); } else { return 1; } } else if (flavor2.isFlavorTextType()) { return -1; } else { return 0; } } } /** * Gets a Reader for a text flavor, decoded, if necessary, for the expected
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -