📄 dataflavor.java
字号:
* charset (encoding). The supported representation classes are * <code>java.io.Reader</code>, <code>java.lang.String</code>, * <code>java.nio.CharBuffer</code>, <code>[C</code>, * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, * and <code>[B</code>. * <p> * Because text flavors which do not support the charset parameter are * encoded in a non-standard format, this method should not be called for * such flavors. However, in order to maintain backward-compatibility, * if this method is called for such a flavor, this method will treat the * flavor as though it supports the charset parameter and attempt to * decode it accordingly. See <code>selectBestTextFlavor</code> for a list * of text flavors which do not support the charset parameter. * * @param transferable the <code>Transferable</code> whose data will be * requested in this flavor * * @return a <code>Reader</code> to read the <code>Transferable</code>'s * data * * @exception IllegalArgumentException if the representation class * is not one of the seven listed above * @exception IllegalArgumentException if the <code>Transferable</code> * has <code>null</code> data * @exception NullPointerException if the <code>Transferable</code> is * <code>null</code> * @exception UnsupportedEncodingException if this flavor's representation * is <code>java.io.InputStream</code>, * <code>java.nio.ByteBuffer</code>, or <code>[B</code> and * this flavor's encoding is not supported by this * implementation of the Java platform * @exception UnsupportedFlavorException if the <code>Transferable</code> * does not support this flavor * @exception IOException if the data cannot be read because of an * I/O error * @see #selectBestTextFlavor * @since 1.3 */ public Reader getReaderForText(Transferable transferable) throws UnsupportedFlavorException, IOException { Object transferObject = transferable.getTransferData(this); if (transferObject == null) { throw new IllegalArgumentException ("getTransferData() returned null"); } if (transferObject instanceof Reader) { return (Reader)transferObject; } else if (transferObject instanceof String) { return new StringReader((String)transferObject); } else if (transferObject instanceof CharBuffer) { CharBuffer buffer = (CharBuffer)transferObject; int size = buffer.remaining(); char[] chars = new char[size]; buffer.get(chars, 0, size); return new CharArrayReader(chars); } else if (transferObject instanceof char[]) { return new CharArrayReader((char[])transferObject); } InputStream stream = null; if (transferObject instanceof InputStream) { stream = (InputStream)transferObject; } else if (transferObject instanceof ByteBuffer) { ByteBuffer buffer = (ByteBuffer)transferObject; int size = buffer.remaining(); byte[] bytes = new byte[size]; buffer.get(bytes, 0, size); stream = new ByteArrayInputStream(bytes); } else if (transferObject instanceof byte[]) { stream = new ByteArrayInputStream((byte[])transferObject); } if (stream == null) { throw new IllegalArgumentException("transfer data is not Reader, String, CharBuffer, char array, InputStream, ByteBuffer, or byte array"); } String encoding = getParameter("charset"); return (encoding == null) ? new InputStreamReader(stream) : new InputStreamReader(stream, encoding); } /** * Returns the MIME type string for this <code>DataFlavor</code>. * @return the MIME type string for this flavor */ public String getMimeType() { return (mimeType != null) ? mimeType.toString() : null; } /** * Returns the <code>Class</code> which objects supporting this * <code>DataFlavor</code> will return when this <code>DataFlavor</code> * is requested. * @return the <code>Class</code> which objects supporting this * <code>DataFlavor</code> will return when this <code>DataFlavor</code> * is requested */ public Class getRepresentationClass() { return representationClass; } /** * Returns the human presentable name for the data format that this * <code>DataFlavor</code> represents. This name would be localized * for different countries. * @return the human presentable name for the data format that this * <code>DataFlavor</code> represents */ public String getHumanPresentableName() { return humanPresentableName; } /** * Returns the primary MIME type for this <code>DataFlavor</code>. * @return the primary MIME type of this <code>DataFlavor</code> */ public String getPrimaryType() { return (mimeType != null) ? mimeType.getPrimaryType() : null; } /** * Returns the sub MIME type of this <code>DataFlavor</code>. * @return the Sub MIME type of this <code>DataFlavor</code> */ public String getSubType() { return (mimeType != null) ? mimeType.getSubType() : null; } /** * Returns the human presentable name for this <code>DataFlavor</code> * if <code>paramName</code> equals "humanPresentableName". Otherwise * returns the MIME type value associated with <code>paramName</code>. * * @param paramName the parameter name requested * @return the value of the name parameter, or <code>null</code> * if there is no associated value * @see MimeType#getParameter() */ public String getParameter(String paramName) { if (paramName.equals("humanPresentableName")) { return humanPresentableName; } else { return (mimeType != null) ? mimeType.getParameter(paramName) : null; } } /** * Sets the human presentable name for the data format that this * <code>DataFlavor</code> represents. This name would be localized * for different countries. * @param humanPresentableName the new human presentable name */ public void setHumanPresentableName(String humanPresentableName) { this.humanPresentableName = humanPresentableName; } /** * Tests an arbitrary <code>Object</code> to this <code>DataFlavor</code> * for equality. Two <code>DataFlavor</code>s are considered equal if and * only if their MIME primary type and subtype and representation class are * equal. Additionally, if the primary type is "text", the subtype denotes * a text flavor which supports the charset parameter, and the * representation class is not <code>java.io.Reader</code>, * <code>java.lang.String</code>, <code>java.nio.CharBuffer</code>, or * <code>[C</code>, the <code>charset</code> parameter must also be equal. * If a charset is not explicitly specified for one or both * <code>DataFlavor</code>s, the platform default encoding is assumed. See * <code>selectBestTextFlavor</code> for a list of text flavors which * support the charset parameter. * * @param o the <code>Object</code> to compare with <code>this</code> * @return <code>true</code> if <code>that</code> is equivalent to this * <code>DataFlavor</code>; <code>false</code> otherwise * @see #selectBestTextFlavor */ public boolean equals(Object o) { return ((o instanceof DataFlavor) && equals((DataFlavor)o)); } /** * Tests a <code>DataFlavor</code> to this <code>DataFlavor</code> for * equality. Two <code>DataFlavor</code>s are considered equal if and only * if their MIME primary type and subtype and representation class are * equal. Additionally, if the primary type is "text", the subtype denotes * a text flavor which supports the charset parameter, and the * representation class is not <code>java.io.Reader</code>, * <code>java.lang.String</code>, <code>java.nio.CharBuffer</code>, or * <code>[C</code>, the <code>charset</code> parameter must also be equal. * If a charset is not explicitly specified for one or both * <code>DataFlavor</code>s, the platform default encoding is assumed. See * <code>selectBestTextFlavor</code> for a list of text flavors which * support the charset parameter. * * @param that the <code>DataFlavor</code> to compare with * <code>this</code> * @return <code>true</code> if <code>that</code> is equivalent to this * <code>DataFlavor</code>; <code>false</code> otherwise * @see #selectBestTextFlavor */ public boolean equals(DataFlavor that) { if (that == null) { return false; } if (this == that) { return true; } if (representationClass == null) { if (that.getRepresentationClass() != null) { return false; } } else { if (!representationClass.equals(that.getRepresentationClass())) { return false; } } if (mimeType == null) { if (that.mimeType != null) { return false; } } else { if (!mimeType.match(that.mimeType)) { return false; } if ("text".equals(getPrimaryType()) && DataTransferer.doesSubtypeSupportCharset(this) && representationClass != null && !(isRepresentationClassReader() || String.class.equals(representationClass) || isRepresentationClassCharBuffer() || DataTransferer.charArrayClass.equals(representationClass))) { String thisCharset = DataTransferer.canonicalName(getParameter("charset")); String thatCharset = DataTransferer.canonicalName(that.getParameter("charset")); if (thisCharset == null) { if (thatCharset != null) { return false; } } else { if (!thisCharset.equals(thatCharset)) { return false; } } } } return true; } /** * Compares only the <code>mimeType</code> against the passed in * <code>String</code> and <code>representationClass</code> is * not considered in the comparison. * If <code>representationClass</code> needs to be compared, then * <code>equals(new DataFlavor(s))</code> may be used. * * @deprecated As inconsistent with <code>hashCode()</code> contract, * use <code>isMimeTypeEqual(String)</code> instead. * @return true if the String (MimeType) is equal */ public boolean equals(String s) { if (s == null || mimeType == null) return false; return isMimeTypeEqual(s); } /** * Returns hash code for this <code>DataFlavor</code>. * For two equal <code>DataFlavor</code>s, hash codes are equal. * For the <code>String</code> * that matches <code>DataFlavor.equals(String)</code>, it is not * guaranteed that <code>DataFlavor</code>'s hash code is equal * to the hash code of the <code>String</code>. * * @return a hash code for this <code>DataFlavor</code> */ public int hashCode() { int total = 0; if (representationClass != null) { total += representationClass.hashCode(); } if (mimeType != null) { String primaryType = mimeType.getPrimaryType(); if (primaryType != null) { total += primaryType.hashCode(); } // Do not add subType.hashCode() to the total. equals uses // MimeType.match which reports a match if one or both of the // subTypes is '*', regardless of the other subType. if ("text".equals(primaryType) && DataTransferer.doesSubtypeSupportCharset(this) && representationClass != null && !(isRepresentationClassReader() || String.class.equals(representationClass) || isRepresentationClassCharBuffer() || DataTransferer.charArrayClass.equals (representationClass))) { String charset = DataTransferer.canonicalName(getParameter("charset")); if (charset != null) { total += charset.hashCode(); } } } return total; } /** * Tests a <code>DataFlavor</code> to this <code>DataFlavor</code> for * equality. Two <code>DataFlavor</code>s are considered equal if and only * if their MIME primary type and subtype and representation class are * equal. Additionally, if the primary type is "text", the subtype denotes * a text flavor which supports the charset parameter, and the * representation class is not <code>java.io.Reader</code>,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -