jdk/src/java.desktop/unix/classes/sun/awt/X11/XDataTransferer.java
changeset 26037 508779ce6619
parent 26010 9a3cf8ee0776
parent 25859 3317bb8137f4
equal deleted inserted replaced
25992:e9b05e933ddd 26037:508779ce6619
    27 
    27 
    28 import java.awt.Image;
    28 import java.awt.Image;
    29 
    29 
    30 import java.awt.datatransfer.DataFlavor;
    30 import java.awt.datatransfer.DataFlavor;
    31 import java.awt.datatransfer.Transferable;
    31 import java.awt.datatransfer.Transferable;
    32 import java.awt.datatransfer.UnsupportedFlavorException;
       
    33 
    32 
    34 import java.awt.image.BufferedImage;
    33 import java.awt.image.BufferedImage;
    35 import java.awt.image.ColorModel;
    34 import java.awt.image.ColorModel;
    36 import java.awt.image.WritableRaster;
    35 import java.awt.image.WritableRaster;
    37 
    36 
    44 import java.net.URISyntaxException;
    43 import java.net.URISyntaxException;
    45 
    44 
    46 import java.util.ArrayList;
    45 import java.util.ArrayList;
    47 import java.util.Iterator;
    46 import java.util.Iterator;
    48 import java.util.LinkedHashSet;
    47 import java.util.LinkedHashSet;
    49 import java.util.List;
       
    50 
    48 
    51 import javax.imageio.ImageIO;
    49 import javax.imageio.ImageIO;
    52 import javax.imageio.ImageReader;
    50 import javax.imageio.ImageReader;
    53 import javax.imageio.ImageTypeSpecifier;
    51 import javax.imageio.ImageTypeSpecifier;
    54 import javax.imageio.ImageWriter;
    52 import javax.imageio.ImageWriter;
    55 import javax.imageio.spi.ImageWriterSpi;
    53 import javax.imageio.spi.ImageWriterSpi;
    56 
    54 
       
    55 import sun.datatransfer.DataFlavorUtil;
    57 import sun.awt.datatransfer.DataTransferer;
    56 import sun.awt.datatransfer.DataTransferer;
    58 import sun.awt.datatransfer.ToolkitThreadBlockedHandler;
    57 import sun.awt.datatransfer.ToolkitThreadBlockedHandler;
    59 
    58 
    60 import java.io.ByteArrayOutputStream;
    59 import java.io.ByteArrayOutputStream;
    61 import java.util.stream.Stream;
       
    62 
    60 
    63 /**
    61 /**
    64  * Platform-specific support for the data transfer subsystem.
    62  * Platform-specific support for the data transfer subsystem.
    65  */
    63  */
    66 public class XDataTransferer extends DataTransferer {
    64 public class XDataTransferer extends DataTransferer {
    85             transferer = new XDataTransferer();
    83             transferer = new XDataTransferer();
    86         }
    84         }
    87         return transferer;
    85         return transferer;
    88     }
    86     }
    89 
    87 
       
    88     @Override
    90     public String getDefaultUnicodeEncoding() {
    89     public String getDefaultUnicodeEncoding() {
    91         return "iso-10646-ucs-2";
    90         return "iso-10646-ucs-2";
    92     }
    91     }
    93 
    92 
       
    93     @Override
    94     public boolean isLocaleDependentTextFormat(long format) {
    94     public boolean isLocaleDependentTextFormat(long format) {
    95         return false;
    95         return false;
    96     }
    96     }
    97 
    97 
       
    98     @Override
    98     public boolean isTextFormat(long format) {
    99     public boolean isTextFormat(long format) {
    99         return super.isTextFormat(format)
   100         return super.isTextFormat(format)
   100             || isMimeFormat(format, "text");
   101             || isMimeFormat(format, "text");
   101     }
   102     }
   102 
   103 
       
   104     @Override
   103     protected String getCharsetForTextFormat(Long lFormat) {
   105     protected String getCharsetForTextFormat(Long lFormat) {
   104         long format = lFormat.longValue();
   106         if (isMimeFormat(lFormat, "text")) {
   105         if (isMimeFormat(format, "text")) {
   107             String nat = getNativeForFormat(lFormat);
   106             String nat = getNativeForFormat(format);
       
   107             DataFlavor df = new DataFlavor(nat, null);
   108             DataFlavor df = new DataFlavor(nat, null);
   108             // Ignore the charset parameter of the MIME type if the subtype
   109             // Ignore the charset parameter of the MIME type if the subtype
   109             // doesn't support charset.
   110             // doesn't support charset.
   110             if (!DataTransferer.doesSubtypeSupportCharset(df)) {
   111             if (!DataFlavorUtil.doesSubtypeSupportCharset(df)) {
   111                 return null;
   112                 return null;
   112             }
   113             }
   113             String charset = df.getParameter("charset");
   114             String charset = df.getParameter("charset");
   114             if (charset != null) {
   115             if (charset != null) {
   115                 return charset;
   116                 return charset;
   116             }
   117             }
   117         }
   118         }
   118         return super.getCharsetForTextFormat(lFormat);
   119         return super.getCharsetForTextFormat(lFormat);
   119     }
   120     }
   120 
   121 
       
   122     @Override
   121     protected boolean isURIListFormat(long format) {
   123     protected boolean isURIListFormat(long format) {
   122         String nat = getNativeForFormat(format);
   124         String nat = getNativeForFormat(format);
   123         if (nat == null) {
   125         if (nat == null) {
   124             return false;
   126             return false;
   125         }
   127         }
   132             // Not a MIME format.
   134             // Not a MIME format.
   133         }
   135         }
   134         return false;
   136         return false;
   135     }
   137     }
   136 
   138 
       
   139     @Override
   137     public boolean isFileFormat(long format) {
   140     public boolean isFileFormat(long format) {
   138         return format == FILE_NAME_ATOM.getAtom() ||
   141         return format == FILE_NAME_ATOM.getAtom() ||
   139             format == DT_NET_FILE_ATOM.getAtom();
   142             format == DT_NET_FILE_ATOM.getAtom();
   140     }
   143     }
   141 
   144 
       
   145     @Override
   142     public boolean isImageFormat(long format) {
   146     public boolean isImageFormat(long format) {
   143         return format == PNG_ATOM.getAtom() ||
   147         return format == PNG_ATOM.getAtom() ||
   144             format == JFIF_ATOM.getAtom() ||
   148             format == JFIF_ATOM.getAtom() ||
   145             isMimeFormat(format, "image");
   149             isMimeFormat(format, "image");
   146     }
   150     }
   147 
   151 
       
   152     @Override
   148     protected Long getFormatForNativeAsLong(String str) {
   153     protected Long getFormatForNativeAsLong(String str) {
   149         // Just get the atom. If it has already been retrived
   154         // Just get the atom. If it has already been retrived
   150         // once, we'll get a copy so this should be very fast.
   155         // once, we'll get a copy so this should be very fast.
   151         long atom = XAtom.get(str).getAtom();
   156         return XAtom.get(str).getAtom();
   152         return Long.valueOf(atom);
   157     }
   153     }
   158 
   154 
   159     @Override
   155     protected String getNativeForFormat(long format) {
   160     protected String getNativeForFormat(long format) {
   156         return getTargetNameForAtom(format);
   161         return getTargetNameForAtom(format);
   157     }
   162     }
   158 
   163 
   159     public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() {
   164     public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() {
   165      */
   170      */
   166     private String getTargetNameForAtom(long atom) {
   171     private String getTargetNameForAtom(long atom) {
   167         return XAtom.get(atom).getName();
   172         return XAtom.get(atom).getName();
   168     }
   173     }
   169 
   174 
       
   175     @Override
   170     protected byte[] imageToPlatformBytes(Image image, long format)
   176     protected byte[] imageToPlatformBytes(Image image, long format)
   171       throws IOException {
   177       throws IOException {
   172         String mimeType = null;
   178         String mimeType = null;
   173         if (format == PNG_ATOM.getAtom()) {
   179         if (format == PNG_ATOM.getAtom()) {
   174             mimeType = "image/png";
   180             mimeType = "image/png";
   194             throw new IOException("Translation to " + nativeFormat +
   200             throw new IOException("Translation to " + nativeFormat +
   195                                   " is not supported.");
   201                                   " is not supported.");
   196         }
   202         }
   197     }
   203     }
   198 
   204 
       
   205     @Override
   199     protected ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList)
   206     protected ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList)
   200         throws IOException
   207         throws IOException
   201     {
   208     {
   202         ByteArrayOutputStream bos = new ByteArrayOutputStream();
   209         ByteArrayOutputStream bos = new ByteArrayOutputStream();
   203         for (int i = 0; i < fileList.size(); i++)
   210         for (int i = 0; i < fileList.size(); i++)
   211 
   218 
   212     /**
   219     /**
   213      * Translates either a byte array or an input stream which contain
   220      * Translates either a byte array or an input stream which contain
   214      * platform-specific image data in the given format into an Image.
   221      * platform-specific image data in the given format into an Image.
   215      */
   222      */
       
   223     @Override
   216     protected Image platformImageBytesToImage(
   224     protected Image platformImageBytesToImage(
   217         byte[] bytes, long format) throws IOException
   225         byte[] bytes, long format) throws IOException
   218     {
   226     {
   219         String mimeType = null;
   227         String mimeType = null;
   220         if (format == PNG_ATOM.getAtom()) {
   228         if (format == PNG_ATOM.getAtom()) {
   315 
   323 
   316         if (nat == null) {
   324         if (nat == null) {
   317             return flavors;
   325             return flavors;
   318         }
   326         }
   319 
   327 
   320         DataFlavor df = null;
   328         DataFlavor df;
   321 
       
   322         try {
   329         try {
   323             df = new DataFlavor(nat);
   330             df = new DataFlavor(nat);
   324         } catch (Exception e) {
   331         } catch (Exception e) {
   325             // The string doesn't constitute a valid MIME type.
   332             // The string doesn't constitute a valid MIME type.
   326             return flavors;
   333             return flavors;
   381 
   388 
   382         String charset = df.getParameter("charset");
   389         String charset = df.getParameter("charset");
   383         String baseType = df.getPrimaryType() + "/" + df.getSubType();
   390         String baseType = df.getPrimaryType() + "/" + df.getSubType();
   384         String mimeType = baseType;
   391         String mimeType = baseType;
   385 
   392 
   386         if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
   393         if (charset != null && DataFlavorUtil.isFlavorCharsetTextType(df)) {
   387             mimeType += ";charset=" + charset;
   394             mimeType += ";charset=" + charset;
   388         }
   395         }
   389 
   396 
   390         // Add a mapping to the MIME native whenever the representation class
   397         // Add a mapping to the MIME native whenever the representation class
   391         // doesn't require translation.
   398         // doesn't require translation.
   411                             break;
   418                             break;
   412                         }
   419                         }
   413                     }
   420                     }
   414                 }
   421                 }
   415             }
   422             }
   416         } else if (DataTransferer.isFlavorCharsetTextType(df)) {
   423         } else if (DataFlavorUtil.isFlavorCharsetTextType(df)) {
   417             // stringFlavor is semantically equivalent to the standard
   424             // stringFlavor is semantically equivalent to the standard
   418             // "text/plain" MIME type.
   425             // "text/plain" MIME type.
   419             if (DataFlavor.stringFlavor.equals(df)) {
   426             if (DataFlavor.stringFlavor.equals(df)) {
   420                 baseType = "text/plain";
   427                 baseType = "text/plain";
   421             }
   428             }
   422 
   429 
   423             for (String encoding : DataTransferer.standardEncodings()) {
   430             for (String encoding : DataFlavorUtil.standardEncodings()) {
   424                 if (!encoding.equals(charset)) {
   431                 if (!encoding.equals(charset)) {
   425                     natives.add(baseType + ";charset=" + encoding);
   432                     natives.add(baseType + ";charset=" + encoding);
   426                 }
   433                 }
   427             }
   434             }
   428 
   435