jdk/src/java.desktop/share/classes/sun/awt/datatransfer/DataTransferer.java
author prr
Sat, 19 Sep 2015 15:45:59 -0700
changeset 32865 f9cb6e427f9e
parent 32861 e87c3d1cf3cf
child 35667 ed476aba94de
permissions -rw-r--r--
8136783: Run blessed-modifier-order script on java.desktop Reviewed-by: martin, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32861
e87c3d1cf3cf 8014725: closed/java/awt/Clipboard/HTMLTransferTest/HTMLTransferTest.html failed intermittently
ssadetsky
parents: 28231
diff changeset
     2
 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4363
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4363
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4363
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4363
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4363
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.datatransfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.EventQueue;
23260
d68cbf54738f 8029893: Remove reflection from DataTransferer.getInstance
pchelko
parents: 23238
diff changeset
    29
import java.awt.Graphics;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Image;
23260
d68cbf54738f 8029893: Remove reflection from DataTransferer.getInstance
pchelko
parents: 23238
diff changeset
    31
import java.awt.Toolkit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.datatransfer.DataFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.datatransfer.FlavorMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.datatransfer.FlavorTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.datatransfer.Transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.datatransfer.UnsupportedFlavorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.io.Reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.io.SequenceInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.io.StringReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
    52
import java.net.URI;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
    53
import java.net.URISyntaxException;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
    54
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.nio.charset.IllegalCharsetNameException;
24530
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
    60
import java.nio.charset.StandardCharsets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.nio.charset.UnsupportedCharsetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import java.security.PrivilegedExceptionAction;
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
    70
import java.security.ProtectionDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23279
diff changeset
    72
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
    74
import sun.datatransfer.DataFlavorUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import sun.awt.AppContext;
26011
0fa4f1bd2843 8051588: DataTransferer.getInstance throws ClassCastException in headless mode
pchelko
parents: 26010
diff changeset
    77
import sun.awt.ComponentFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import java.awt.image.ImageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import java.awt.image.RenderedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
import javax.imageio.ImageReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
import javax.imageio.ImageReadParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
import javax.imageio.spi.ImageWriterSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
import javax.imageio.stream.ImageInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
import javax.imageio.stream.ImageOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
import sun.awt.image.ImageRepresentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
import sun.awt.image.ToolkitImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   100
import java.io.FilePermission;
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   101
import java.util.stream.Stream;
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   102
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Provides a set of functions to be shared among the DataFlavor class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * platform-specific data transfer implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * The concept of "flavors" and "natives" is extended to include "formats",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * which are the numeric values Win32 and X11 use to express particular data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * types. Like FlavorMap, which provides getNativesForFlavors(DataFlavor[]) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * getFlavorsForNatives(String[]) functions, DataTransferer provides a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * of getFormatsFor(Transferable|Flavor|Flavors) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * getFlavorsFor(Format|Formats) functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * Also provided are functions for translating a Transferable into a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * array, given a source DataFlavor and a target format, and for translating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * a byte array or InputStream into an Object, given a source format and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * a target DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @author Danila Sinopalnikov
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @since 1.3.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
public abstract class DataTransferer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The <code>DataFlavor</code> representing a Java text encoding String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * encoded in UTF-8, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *     representationClass = [B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *     mimeType            = "application/x-java-text-encoding"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static final DataFlavor javaTextEncodingFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
9484
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   136
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * A collection of all natives listed in flavormap.properties with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * a primary MIME type of "text".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   140
    private static final Set<Long> textNatives =
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   141
            Collections.synchronizedSet(new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * The native encodings/charsets for the Set of textNatives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   146
    private static final Map<Long, String> nativeCharsets =
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   147
            Collections.synchronizedMap(new HashMap<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * The end-of-line markers for the Set of textNatives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   152
    private static final Map<Long, String> nativeEOLNs =
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   153
            Collections.synchronizedMap(new HashMap<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * The number of terminating NUL bytes for the Set of textNatives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   158
    private static final Map<Long, Integer> nativeTerminators =
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   159
            Collections.synchronizedMap(new HashMap<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * The key used to store pending data conversion requests for an AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static final String DATA_CONVERTER_KEY = "DATA_CONVERTER_KEY";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        DataFlavor tJavaTextEncodingFlavor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        try {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   169
            tJavaTextEncodingFlavor = new DataFlavor("application/x-java-text-encoding;class=\"[B\"");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } catch (ClassNotFoundException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        javaTextEncodingFlavor = tJavaTextEncodingFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * The accessor method for the singleton DataTransferer instance. Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * that in a headless environment, there may be no DataTransferer instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * instead, null will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
23260
d68cbf54738f 8029893: Remove reflection from DataTransferer.getInstance
pchelko
parents: 23238
diff changeset
   180
    public static synchronized DataTransferer getInstance() {
26011
0fa4f1bd2843 8051588: DataTransferer.getInstance throws ClassCastException in headless mode
pchelko
parents: 26010
diff changeset
   181
        return ((ComponentFactory) Toolkit.getDefaultToolkit()).getDataTransferer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Converts a FlavorMap to a FlavorTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public static FlavorTable adaptFlavorMap(final FlavorMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (map instanceof FlavorTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return (FlavorTable)map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return new FlavorTable() {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   193
            @Override
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   194
            public Map<DataFlavor, String> getNativesForFlavors(DataFlavor[] flavors) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   195
                return map.getNativesForFlavors(flavors);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   196
            }
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   197
            @Override
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   198
            public Map<String, DataFlavor> getFlavorsForNatives(String[] natives) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   199
                return map.getFlavorsForNatives(natives);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   200
            }
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   201
            @Override
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   202
            public List<String> getNativesForFlavor(DataFlavor flav) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   203
                Map<DataFlavor, String> natives = getNativesForFlavors(new DataFlavor[]{flav});
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   204
                String nat = natives.get(flav);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   205
                if (nat != null) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   206
                    return Collections.singletonList(nat);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   207
                } else {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   208
                    return Collections.emptyList();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                }
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   210
            }
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   211
            @Override
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   212
            public List<DataFlavor> getFlavorsForNative(String nat) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   213
                Map<String, DataFlavor> flavors = getFlavorsForNatives(new String[]{nat});
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   214
                DataFlavor flavor = flavors.get(nat);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   215
                if (flavor != null) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   216
                    return Collections.singletonList(flavor);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   217
                } else {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   218
                    return Collections.emptyList();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   220
            }
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   221
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Returns the default Unicode encoding for the platform. The encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * need not be canonical. This method is only used by the archaic function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * DataFlavor.getTextPlainUnicodeFlavor().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public abstract String getDefaultUnicodeEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * This method is called for text flavor mappings established while parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * the flavormap.properties file. It stores the "eoln" and "terminators"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * parameters which are not officially part of the MIME type. They are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * MIME parameters specific to the flavormap.properties file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public void registerTextFlavorProperties(String nat, String charset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                             String eoln, String terminators) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Long format = getFormatForNativeAsLong(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        textNatives.add(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        nativeCharsets.put(format, (charset != null && charset.length() != 0)
24530
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
   243
                ? charset : Charset.defaultCharset().name());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (eoln != null && eoln.length() != 0 && !eoln.equals("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            nativeEOLNs.put(format, eoln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (terminators != null && terminators.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            Integer iTerminators = Integer.valueOf(terminators);
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   249
            if (iTerminators > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                nativeTerminators.put(format, iTerminators);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Determines whether the native corresponding to the specified long format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * was listed in the flavormap.properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    protected boolean isTextFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return textNatives.contains(Long.valueOf(format));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    protected String getCharsetForTextFormat(Long lFormat) {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   264
        return nativeCharsets.get(lFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Specifies whether text imported from the native system in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * format is locale-dependent. If so, when decoding such text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * 'nativeCharsets' should be ignored, and instead, the Transferable should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * be queried for its javaTextEncodingFlavor data for the correct encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public abstract boolean isLocaleDependentTextFormat(long format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Determines whether the DataFlavor corresponding to the specified long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * format is DataFlavor.javaFileListFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public abstract boolean isFileFormat(long format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Determines whether the DataFlavor corresponding to the specified long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * format is DataFlavor.imageFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public abstract boolean isImageFormat(long format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   288
     * Determines whether the format is a URI list we can convert to
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   289
     * a DataFlavor.javaFileListFlavor.
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   290
     */
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   291
    protected boolean isURIListFormat(long format) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   292
        return false;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   293
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   294
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   295
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Returns a Map whose keys are all of the possible formats into which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Transferable's transfer data flavors can be translated. The value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * each key is the DataFlavor in which the Transferable's data should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * requested when converting to the format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * The map keys are sorted according to the native formats preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   304
    public SortedMap<Long,DataFlavor> getFormatsForTransferable(Transferable contents,
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   305
                                                                FlavorTable map)
11271
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   306
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        DataFlavor[] flavors = contents.getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (flavors == null) {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   309
            return Collections.emptySortedMap();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return getFormatsForFlavors(flavors, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Returns a Map whose keys are all of the possible formats into which data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * in the specified DataFlavors can be translated. The value of each key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * is the DataFlavor in which the Transferable's data should be requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * when converting to the format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * The map keys are sorted according to the native formats preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param flavors the data flavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param map the FlavorTable which contains mappings between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *            DataFlavors and data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @throws NullPointerException if flavors or map is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   328
    public SortedMap<Long, DataFlavor> getFormatsForFlavors(DataFlavor[] flavors,
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   329
                                                            FlavorTable map)
11271
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   330
    {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   331
        Map<Long,DataFlavor> formatMap = new HashMap<>(flavors.length);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   332
        Map<Long,DataFlavor> textPlainMap = new HashMap<>(flavors.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // Maps formats to indices that will be used to sort the formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // according to the preference order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // Larger index value corresponds to the more preferable format.
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   336
        Map<Long, Integer> indexMap = new HashMap<>(flavors.length);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   337
        Map<Long, Integer> textPlainIndexMap = new HashMap<>(flavors.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        int currentIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        // Iterate backwards so that preferred DataFlavors are used over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // other DataFlavors. (See javadoc for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // Transferable.getTransferDataFlavors.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        for (int i = flavors.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            DataFlavor flavor = flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (flavor == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // Don't explicitly test for String, since it is just a special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            // case of Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (flavor.isFlavorTextType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                flavor.isFlavorJavaFileListType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                DataFlavor.imageFlavor.equals(flavor) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                flavor.isRepresentationClassSerializable() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                flavor.isRepresentationClassInputStream() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                flavor.isRepresentationClassRemote())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   357
                List<String> natives = map.getNativesForFlavor(flavor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                currentIndex += natives.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   361
                for (String aNative : natives) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   362
                    Long lFormat = getFormatForNativeAsLong(aNative);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   363
                    Integer index = currentIndex--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    formatMap.put(lFormat, flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    indexMap.put(lFormat, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    // SystemFlavorMap.getNativesForFlavor will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    // text/plain natives for all text/*. While this is good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    // for a single text/* flavor, we would prefer that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    // text/plain native data come from a text/plain flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    if (("text".equals(flavor.getPrimaryType()) &&
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   373
                            "plain".equals(flavor.getSubType())) ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   374
                            flavor.equals(DataFlavor.stringFlavor)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        textPlainMap.put(lFormat, flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        textPlainIndexMap.put(lFormat, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                currentIndex += natives.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        formatMap.putAll(textPlainMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        indexMap.putAll(textPlainIndexMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // Sort the map keys according to the formats preference order.
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   388
        Comparator<Long> comparator = DataFlavorUtil.getIndexOrderComparator(indexMap).reversed();
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   389
        SortedMap<Long, DataFlavor> sortedMap = new TreeMap<>(comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        sortedMap.putAll(formatMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return sortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Reduces the Map output for the root function to an array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Map's keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public long[] getFormatsForTransferableAsArray(Transferable contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                                   FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return keysToLongArray(getFormatsForTransferable(contents, map));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Returns a Map whose keys are all of the possible DataFlavors into which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * data in the specified formats can be translated. The value of each key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * is the format in which the Clipboard or dropped data should be requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * when converting to the DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   410
    public Map<DataFlavor, Long> getFlavorsForFormats(long[] formats, FlavorTable map) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   411
        Map<DataFlavor, Long> flavorMap = new HashMap<>(formats.length);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   412
        Set<AbstractMap.SimpleEntry<Long, DataFlavor>> mappingSet = new HashSet<>(formats.length);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   413
        Set<DataFlavor> flavorSet = new HashSet<>(formats.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // First step: build flavorSet, mappingSet and initial flavorMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        // flavorSet  - the set of all the DataFlavors into which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        //              data in the specified formats can be translated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // mappingSet - the set of all the mappings from the specified formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        //              into any DataFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        // flavorMap  - after this step, this map maps each of the DataFlavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        //              from flavorSet to any of the specified formats.
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   422
        for (long format : formats) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            String nat = getNativeForFormat(format);
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   424
            List<DataFlavor> flavors = map.getFlavorsForNative(nat);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   425
            for (DataFlavor flavor : flavors) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                // Don't explicitly test for String, since it is just a special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                // case of Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                if (flavor.isFlavorTextType() ||
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   429
                        flavor.isFlavorJavaFileListType() ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   430
                        DataFlavor.imageFlavor.equals(flavor) ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   431
                        flavor.isRepresentationClassSerializable() ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   432
                        flavor.isRepresentationClassInputStream() ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   433
                        flavor.isRepresentationClassRemote()) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   434
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   435
                    AbstractMap.SimpleEntry<Long, DataFlavor> mapping =
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   436
                            new AbstractMap.SimpleEntry<>(format, flavor);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   437
                    flavorMap.put(flavor, format);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    mappingSet.add(mapping);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    flavorSet.add(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // Second step: for each DataFlavor try to figure out which of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // specified formats is the best to translate to this flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        // Then map each flavor to the best format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        // For the given flavor, FlavorTable indicates which native will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // best reflect data in the specified flavor to the underlying native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // platform. We assume that this native is the best to translate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // to this flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        // Note: FlavorTable allows one-way mappings, so we can occasionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // map a flavor to the format for which the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // format-to-flavor mapping doesn't exist. For this reason we have built
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // a mappingSet of all format-to-flavor mappings for the specified formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // and check if the format-to-flavor mapping exists for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // (flavor,format) pair being added.
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   457
        for (DataFlavor flavor : flavorSet) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   458
            List<String> natives = map.getNativesForFlavor(flavor);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   459
            for (String aNative : natives) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   460
                Long lFormat = getFormatForNativeAsLong(aNative);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   461
                if (mappingSet.contains(new AbstractMap.SimpleEntry<>(lFormat, flavor))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    flavorMap.put(flavor, lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return flavorMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Returns a Set of all DataFlavors for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * 1) a mapping from at least one of the specified formats exists in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * specified map and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * 2) the data translation for this mapping can be performed by the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param formats the data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param map the FlavorTable which contains mappings between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *            DataFlavors and data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @throws NullPointerException if formats or map is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
   483
    public Set<DataFlavor> getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   484
        Set<DataFlavor> flavorSet = new HashSet<>(formats.length);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   485
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   486
        for (long format : formats) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   487
            List<DataFlavor> flavors = map.getFlavorsForNative(getNativeForFormat(format));
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   488
            for (DataFlavor flavor : flavors) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                // Don't explicitly test for String, since it is just a special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                // case of Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                if (flavor.isFlavorTextType() ||
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   492
                        flavor.isFlavorJavaFileListType() ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   493
                        DataFlavor.imageFlavor.equals(flavor) ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   494
                        flavor.isRepresentationClassSerializable() ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   495
                        flavor.isRepresentationClassInputStream() ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   496
                        flavor.isRepresentationClassRemote()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    flavorSet.add(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return flavorSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Returns an array of all DataFlavors for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * 1) a mapping from at least one of the specified formats exists in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * specified map and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * 2) the data translation for this mapping can be performed by the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * The array will be sorted according to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * <code>DataFlavorComparator</code> created with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * map as an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @param formats the data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param map the FlavorTable which contains mappings between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *            DataFlavors and data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @throws NullPointerException if formats or map is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public DataFlavor[] getFlavorsForFormatsAsArray(long[] formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                                                    FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // getFlavorsForFormatsAsSet() is less expensive than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        // getFlavorsForFormats().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return setToSortedDataFlavorArray(getFlavorsForFormatsAsSet(formats, map));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Looks-up or registers the String native with the native data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * system and returns a long format corresponding to that native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    protected abstract Long getFormatForNativeAsLong(String str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Looks-up the String native corresponding to the specified long format in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * the native data transfer system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    protected abstract String getNativeForFormat(long format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /* Contains common code for finding the best charset for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * clipboard string encoding/decoding, basing on clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * format and localeTransferable(on decoding, if available)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
24530
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
   543
    protected String getBestCharsetForTextFormat(Long lFormat,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        Transferable localeTransferable) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        String charset = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (localeTransferable != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            isLocaleDependentTextFormat(lFormat) &&
24530
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
   549
            localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            try {
24530
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
   551
                byte[] charsetNameBytes = (byte[])localeTransferable
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
   552
                        .getTransferData(javaTextEncodingFlavor);
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
   553
                charset = new String(charsetNameBytes, StandardCharsets.UTF_8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            } catch (UnsupportedFlavorException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            charset = getCharsetForTextFormat(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (charset == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            // Only happens when we have a custom text type.
24530
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
   561
            charset = Charset.defaultCharset().name();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *  Translation function for converting string into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *  a byte array. Search-and-replace EOLN. Encode into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *  target format. Append terminating NUL bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *  Java to Native string conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    private byte[] translateTransferableString(String str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                               long format) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   576
        Long lFormat = format;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        String charset = getBestCharsetForTextFormat(lFormat, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // Search and replace EOLN. Note that if EOLN is "\n", then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // never added an entry to nativeEOLNs anyway, so we'll skip this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // windows: "abc\nde"->"abc\r\nde"
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   582
        String eoln = nativeEOLNs.get(lFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (eoln != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            int length = str.length();
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   585
            StringBuilder buffer = new StringBuilder(length * 2); // 2 is a heuristic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                // Fix for 4914613 - skip native EOLN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                if (str.startsWith(eoln, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    buffer.append(eoln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    i += eoln.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                char c = str.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    buffer.append(eoln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            str = buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        // Encode text in target format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        byte[] bytes = str.getBytes(charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        // Append terminating NUL bytes. Note that if terminators is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        // the we never added an entry to nativeTerminators anyway, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // we'll skip code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        // "abcde" -> "abcde\0"
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   610
        Integer terminators = nativeTerminators.get(lFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (terminators != null) {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   612
            int numTerminators = terminators;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            byte[] terminatedBytes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                new byte[bytes.length + numTerminators];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            System.arraycopy(bytes, 0, terminatedBytes, 0, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            for (int i = bytes.length; i < terminatedBytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                terminatedBytes[i] = 0x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            bytes = terminatedBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Translating either a byte array or an InputStream into an String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * Strip terminators and search-and-replace EOLN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Native to Java string conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   630
    private String translateBytesToString(byte[] bytes, long format,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   631
                                          Transferable localeTransferable)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   635
        Long lFormat = format;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        String charset = getBestCharsetForTextFormat(lFormat, localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        // Locate terminating NUL bytes. Note that if terminators is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        // the we never added an entry to nativeTerminators anyway, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        // we'll skip code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        // In other words: we are doing char alignment here basing on suggestion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // that count of zero-'terminators' is a number of bytes in one symbol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        // for selected charset (clipboard format). It is not complitly true for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        // multibyte coding like UTF-8, but helps understand the procedure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        // "abcde\0" -> "abcde"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   648
        String eoln = nativeEOLNs.get(lFormat);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   649
        Integer terminators = nativeTerminators.get(lFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (terminators != null) {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   652
            int numTerminators = terminators;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
search:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            for (count = 0; count < (bytes.length - numTerminators + 1); count += numTerminators) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                for (int i = count; i < count + numTerminators; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    if (bytes[i] != 0x0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                        continue search;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                // found terminators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                break search;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            count = bytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        // Decode text to chars. Don't include any terminators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        String converted = new String(bytes, 0, count, charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        // Search and replace EOLN. Note that if EOLN is "\n", then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        // never added an entry to nativeEOLNs anyway, so we'll skip this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        // code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        // Count of NUL-terminators and EOLN coding are platform-specific and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        // loaded from flavormap.properties file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        // windows: "abc\r\nde" -> "abc\nde"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        if (eoln != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            /* Fix for 4463560: replace EOLNs symbol-by-symbol instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
             * of using buf.replace()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            char[] buf = converted.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            char[] eoln_arr = eoln.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            for (int i = 0; i < buf.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                // Catch last few bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                if (i + eoln_arr.length > buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    buf[j++] = buf[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                for (int k = 0, l = i; k < eoln_arr.length; k++, l++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    if (eoln_arr[k] != buf[l]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    buf[j++] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    i += eoln_arr.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    buf[j++] = buf[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            converted = new String(buf, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        return converted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Primary translation function for translating a Transferable into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * a byte array, given a source DataFlavor and target format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 28073
diff changeset
   720
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    public byte[] translateTransferable(Transferable contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                        DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                                        long format) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        // Obtain the transfer data in the source DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        // Note that we special case DataFlavor.plainTextFlavor because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        // StringSelection supports this flavor incorrectly -- instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        // returning an InputStream as the DataFlavor representation class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        // states, it returns a Reader. Instead of using this broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        // functionality, we request the data in stringFlavor (the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        // DataFlavor which StringSelection supports) and use the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        // translator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        boolean stringSelectionHack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            obj = contents.getTransferData(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            if (flavor.equals(DataFlavor.plainTextFlavor) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                !(obj instanceof InputStream))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                obj = contents.getTransferData(DataFlavor.stringFlavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                stringSelectionHack = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                stringSelectionHack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        } catch (UnsupportedFlavorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        // Source data is a String. Search-and-replace EOLN. Encode into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        // target format. Append terminating NUL bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        if (stringSelectionHack ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            (String.class.equals(flavor.getRepresentationClass()) &&
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   760
             DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   762
            String str = removeSuspectedData(flavor, contents, (String)obj);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   763
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return translateTransferableString(
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   765
                str,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        // Source data is a Reader. Convert to a String and recur. In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        // future, we may want to rewrite this so that we encode on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        } else if (flavor.isRepresentationClassReader()) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   771
            if (!(DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                    ("cannot transfer non-text data as Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   776
            StringBuilder buf = new StringBuilder();
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   777
            try (Reader r = (Reader)obj) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   778
                int c;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   779
                while ((c = r.read()) != -1) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   780
                    buf.append((char)c);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   781
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                buf.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        // Source data is a CharBuffer. Convert to a String and recur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        } else if (flavor.isRepresentationClassCharBuffer()) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   790
            if (!(DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    ("cannot transfer non-text data as CharBuffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            CharBuffer buffer = (CharBuffer)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            int size = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            char[] chars = new char[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            buffer.get(chars, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                new String(chars),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // Source data is a char array. Convert to a String and recur.
23260
d68cbf54738f 8029893: Remove reflection from DataTransferer.getInstance
pchelko
parents: 23238
diff changeset
   805
        } else if (char[].class.equals(flavor.getRepresentationClass())) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   806
            if (!(DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    ("cannot transfer non-text data as char array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                new String((char[])obj),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        // Source data is a ByteBuffer. For arbitrary flavors, simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // the array. For text flavors, decode back to a String and recur to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        // reencode according to the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        } else if (flavor.isRepresentationClassByteBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            ByteBuffer buffer = (ByteBuffer)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            int size = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            byte[] bytes = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            buffer.get(bytes, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   824
            if (DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   825
                String sourceEncoding = DataFlavorUtil.getTextCharset(flavor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    new String(bytes, sourceEncoding),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // Source data is a byte array. For arbitrary flavors, simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        // the array. For text flavors, decode back to a String and recur to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        // reencode according to the requested format.
23260
d68cbf54738f 8029893: Remove reflection from DataTransferer.getInstance
pchelko
parents: 23238
diff changeset
   836
        } else if (byte[].class.equals(flavor.getRepresentationClass())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            byte[] bytes = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   839
            if (DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   840
                String sourceEncoding = DataFlavorUtil.getTextCharset(flavor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    new String(bytes, sourceEncoding),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        // Source data is Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        } else if (DataFlavor.imageFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (!isImageFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                throw new IOException("Data translation failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                      "not an image format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            Image image = (Image)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            byte[] bytes = imageToPlatformBytes(image, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                throw new IOException("Data translation failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    "cannot convert java image to native format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   864
        byte[] theByteArray = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        // Target data is a file list. Source data must be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        // java.util.List which contains java.io.File or String instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        if (isFileFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            if (!DataFlavor.javaFileListFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   872
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
   873
            final List<?> list = (List<?>)obj;
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   874
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   875
            final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   876
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   877
            final ArrayList<String> fileList = castToFiles(list, userProtectionDomain);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   878
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   879
            try (ByteArrayOutputStream bos = convertFileListToBytes(fileList)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   880
                theByteArray = bos.toByteArray();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   881
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   882
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   883
        // Target data is a URI list. Source data must be a
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   884
        // java.util.List which contains java.io.File or String instances.
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   885
        } else if (isURIListFormat(format)) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   886
            if (!DataFlavor.javaFileListFlavor.equals(flavor)) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   887
                throw new IOException("data translation failed");
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   888
            }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   889
            String nat = getNativeForFormat(format);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   890
            String targetCharset = null;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   891
            if (nat != null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   892
                try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   893
                    targetCharset = new DataFlavor(nat).getParameter("charset");
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   894
                } catch (ClassNotFoundException cnfe) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   895
                    throw new IOException(cnfe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   898
            if (targetCharset == null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   899
                targetCharset = "UTF-8";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            }
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
   901
            final List<?> list = (List<?>)obj;
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   902
            final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   903
            final ArrayList<String> fileList = castToFiles(list, userProtectionDomain);
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   904
            final ArrayList<String> uriList = new ArrayList<>(fileList.size());
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   905
            for (String fileObject : fileList) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   906
                final URI uri = new File(fileObject).toURI();
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   907
                // Some implementations are fussy about the number of slashes (file:///path/to/file is best)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   908
                try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   909
                    uriList.add(new URI(uri.getScheme(), "", uri.getPath(), uri.getFragment()).toString());
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   910
                } catch (URISyntaxException uriSyntaxException) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   911
                    throw new IOException(uriSyntaxException);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   912
                  }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   913
              }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   914
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   915
            byte[] eoln = "\r\n".getBytes(targetCharset);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   916
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   917
            try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   918
                for (String uri : uriList) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   919
                    byte[] bytes = uri.getBytes(targetCharset);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   920
                    bos.write(bytes, 0, bytes.length);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   921
                    bos.write(eoln, 0, eoln.length);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   922
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   923
                theByteArray = bos.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        // Source data is an InputStream. For arbitrary flavors, just grab the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        // bytes and dump them into a byte array. For text flavors, decode back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        // to a String and recur to reencode according to the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        } else if (flavor.isRepresentationClassInputStream()) {
28073
b8f12f02bd93 8065098: JColorChooser no longer supports drag and drop between two JVM instances
alexsch
parents: 26037
diff changeset
   930
b8f12f02bd93 8065098: JColorChooser no longer supports drag and drop between two JVM instances
alexsch
parents: 26037
diff changeset
   931
            // Workaround to JDK-8024061: Exception thrown when drag and drop
b8f12f02bd93 8065098: JColorChooser no longer supports drag and drop between two JVM instances
alexsch
parents: 26037
diff changeset
   932
            //      between two components is executed quickly.
b8f12f02bd93 8065098: JColorChooser no longer supports drag and drop between two JVM instances
alexsch
parents: 26037
diff changeset
   933
            // and JDK-8065098:  JColorChooser no longer supports drag and drop
b8f12f02bd93 8065098: JColorChooser no longer supports drag and drop between two JVM instances
alexsch
parents: 26037
diff changeset
   934
            //      between two JVM instances
b8f12f02bd93 8065098: JColorChooser no longer supports drag and drop between two JVM instances
alexsch
parents: 26037
diff changeset
   935
            if (!(obj instanceof InputStream)) {
b8f12f02bd93 8065098: JColorChooser no longer supports drag and drop between two JVM instances
alexsch
parents: 26037
diff changeset
   936
                return new byte[0];
b8f12f02bd93 8065098: JColorChooser no longer supports drag and drop between two JVM instances
alexsch
parents: 26037
diff changeset
   937
            }
b8f12f02bd93 8065098: JColorChooser no longer supports drag and drop between two JVM instances
alexsch
parents: 26037
diff changeset
   938
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   939
            try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   940
                try (InputStream is = (InputStream)obj) {
32861
e87c3d1cf3cf 8014725: closed/java/awt/Clipboard/HTMLTransferTest/HTMLTransferTest.html failed intermittently
ssadetsky
parents: 28231
diff changeset
   941
                    is.mark(Integer.MAX_VALUE);
e87c3d1cf3cf 8014725: closed/java/awt/Clipboard/HTMLTransferTest/HTMLTransferTest.html failed intermittently
ssadetsky
parents: 28231
diff changeset
   942
                    is.transferTo(bos);
e87c3d1cf3cf 8014725: closed/java/awt/Clipboard/HTMLTransferTest/HTMLTransferTest.html failed intermittently
ssadetsky
parents: 28231
diff changeset
   943
                    is.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   945
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   946
                if (DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   947
                    byte[] bytes = bos.toByteArray();
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   948
                    String sourceEncoding = DataFlavorUtil.getTextCharset(flavor);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   949
                    return translateTransferableString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   950
                               new String(bytes, sourceEncoding),
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   951
                               format);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   952
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   953
                theByteArray = bos.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   956
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        // Source data is an RMI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        } else if (flavor.isRepresentationClassRemote()) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   959
            theByteArray = convertObjectToBytes(DataFlavorUtil.RMI.newMarshalledObject(obj));
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   960
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
   961
        // Source data is Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        } else if (flavor.isRepresentationClassSerializable()) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   963
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   964
            theByteArray = convertObjectToBytes(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   970
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   971
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   972
        return theByteArray;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   973
    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   974
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   975
    private static byte[] convertObjectToBytes(Object object) throws IOException {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   976
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   977
             ObjectOutputStream oos = new ObjectOutputStream(bos))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   978
        {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   979
            oos.writeObject(object);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   980
            return bos.toByteArray();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   981
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   984
    protected abstract ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList) throws IOException;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   985
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   986
    private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   987
            throws IOException
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   988
    {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   989
        if (null == System.getSecurityManager()
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   990
            || !flavor.isMimeTypeEqual("text/uri-list"))
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   991
        {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   992
            return str;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   993
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   994
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   995
        final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   996
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   997
        try {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   998
            return AccessController.doPrivileged((PrivilegedExceptionAction<String>) () -> {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
   999
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1000
                StringBuilder allowedFiles = new StringBuilder(str.length());
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1001
                String [] uriArray = str.split("(\\s)+");
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1002
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1003
                for (String fileName : uriArray)
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1004
                {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1005
                    File file = new File(fileName);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1006
                    if (file.exists() &&
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1007
                        !(isFileInWebstartedCache(file) ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1008
                        isForbiddenToRead(file, userProtectionDomain)))
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1009
                    {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1010
                        if (0 != allowedFiles.length())
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1011
                        {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1012
                            allowedFiles.append("\\r\\n");
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1013
                        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1014
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1015
                        allowedFiles.append(fileName);
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1016
                    }
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1017
                }
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1018
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1019
                return allowedFiles.toString();
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1020
            });
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1021
        } catch (PrivilegedActionException pae) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1022
            throw new IOException(pae.getMessage(), pae);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1023
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1024
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1025
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1026
    private static ProtectionDomain getUserProtectionDomain(Transferable contents) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1027
        return contents.getClass().getProtectionDomain();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1028
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1029
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1030
    private boolean isForbiddenToRead (File file, ProtectionDomain protectionDomain)
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1031
    {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1032
        if (null == protectionDomain) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1033
            return false;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1034
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1035
        try {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1036
            FilePermission filePermission =
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1037
                    new FilePermission(file.getCanonicalPath(), "read, delete");
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1038
            if (protectionDomain.implies(filePermission)) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1039
                return false;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1040
            }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1041
        } catch (IOException e) {}
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1042
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1043
        return true;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1044
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1045
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1046
    private ArrayList<String> castToFiles(final List<?> files,
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1047
                                          final ProtectionDomain userProtectionDomain) throws IOException {
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1048
        try {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1049
            return AccessController.doPrivileged((PrivilegedExceptionAction<ArrayList<String>>) () -> {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1050
                ArrayList<String> fileList = new ArrayList<>();
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1051
                for (Object fileObject : files)
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1052
                {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1053
                    File file = castToFile(fileObject);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1054
                    if (file != null &&
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1055
                        (null == System.getSecurityManager() ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1056
                        !(isFileInWebstartedCache(file) ||
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1057
                        isForbiddenToRead(file, userProtectionDomain))))
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1058
                    {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1059
                        fileList.add(file.getCanonicalPath());
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1060
                    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1061
                }
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1062
                return fileList;
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1063
            });
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1064
        } catch (PrivilegedActionException pae) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1065
            throw new IOException(pae.getMessage());
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1066
        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1067
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1068
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1069
    // It is important do not use user's successors
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1070
    // of File class.
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1071
    private File castToFile(Object fileObject) throws IOException {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1072
        String filePath = null;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1073
        if (fileObject instanceof File) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1074
            filePath = ((File)fileObject).getCanonicalPath();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1075
        } else if (fileObject instanceof String) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1076
           filePath = (String) fileObject;
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1077
        } else {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1078
           return null;
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1079
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1080
        return new File(filePath);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1081
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1082
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 32861
diff changeset
  1083
    private static final String[] DEPLOYMENT_CACHE_PROPERTIES = {
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1084
        "deployment.system.cachedir",
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1085
        "deployment.user.cachedir",
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1086
        "deployment.javaws.cachedir",
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1087
        "deployment.javapi.cachedir"
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1088
    };
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1089
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 32861
diff changeset
  1090
    private static final ArrayList <File> deploymentCacheDirectoryList = new ArrayList<>();
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1091
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1092
    private static boolean isFileInWebstartedCache(File f) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1093
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1094
        if (deploymentCacheDirectoryList.isEmpty()) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1095
            for (String cacheDirectoryProperty : DEPLOYMENT_CACHE_PROPERTIES) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1096
                String cacheDirectoryPath = System.getProperty(cacheDirectoryProperty);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1097
                if (cacheDirectoryPath != null) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1098
                    try {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1099
                        File cacheDirectory = (new File(cacheDirectoryPath)).getCanonicalFile();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1100
                        if (cacheDirectory != null) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1101
                            deploymentCacheDirectoryList.add(cacheDirectory);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1102
                        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1103
                    } catch (IOException ioe) {}
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1104
                }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1105
            }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1106
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1107
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1108
        for (File deploymentCacheDirectory : deploymentCacheDirectoryList) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1109
            for (File dir = f; dir != null; dir = dir.getParentFile()) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1110
                if (dir.equals(deploymentCacheDirectory)) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1111
                    return true;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1112
                }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1113
            }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1114
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1115
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1116
        return false;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1117
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1118
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1119
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    public Object translateBytes(byte[] bytes, DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                                 long format, Transferable localeTransferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1124
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1125
        Object theObject = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        // Source data is a file list. Use the dragQueryFile native function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        // do most of the decoding. Then wrap File objects around the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        // filenames and return a List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        if (isFileFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            if (!DataFlavor.javaFileListFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            String[] filenames = dragQueryFile(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            if (filenames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            // Convert the strings to File objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            File[] files = new File[filenames.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            for (int i = 0; i < filenames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                files[i] = new File(filenames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            // Turn the list of Files into a List and return
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1146
            theObject = Arrays.asList(files);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1147
23238
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1148
            // Source data is a URI list. Convert to DataFlavor.javaFileListFlavor
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1149
            // where possible.
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1150
        } else if (isURIListFormat(format)
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1151
                    && DataFlavor.javaFileListFlavor.equals(flavor)) {
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1152
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1153
            try (ByteArrayInputStream str = new ByteArrayInputStream(bytes))  {
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1154
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1155
                URI uris[] = dragQueryURIs(str, format, localeTransferable);
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1156
                if (uris == null) {
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1157
                    return null;
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1158
                }
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1159
                List<File> files = new ArrayList<>();
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1160
                for (URI uri : uris) {
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1161
                    try {
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1162
                        files.add(new File(uri));
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1163
                    } catch (IllegalArgumentException illegalArg) {
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1164
                        // When converting from URIs to less generic files,
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1165
                        // common practice (Wine, SWT) seems to be to
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1166
                        // silently drop the URIs that aren't local files.
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1167
                    }
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1168
                }
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1169
                theObject = files;
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1170
            }
57997d148fc0 8029565: java.awt.dnd.InvalidDnDOperationException: data translation failed on file drop
pchelko
parents: 21280
diff changeset
  1171
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1172
            // Target data is a String. Strip terminating NUL bytes. Decode bytes
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1173
            // into characters. Search-and-replace EOLN.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        } else if (String.class.equals(flavor.getRepresentationClass()) &&
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1175
                   DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1176
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1177
            theObject = translateBytesToString(bytes, format, localeTransferable);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1178
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1179
            // Target data is a Reader. Obtain data in InputStream format, encoded
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1180
            // as "Unicode" (utf-16be). Then use an InputStreamReader to decode
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1181
            // back to chars on demand.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        } else if (flavor.isRepresentationClassReader()) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1183
            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1184
                theObject = translateStream(bais,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1185
                        flavor, format, localeTransferable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1187
            // Target data is a CharBuffer. Recur to obtain String and wrap.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        } else if (flavor.isRepresentationClassCharBuffer()) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1189
            if (!(DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1190
                throw new IOException("cannot transfer non-text data as CharBuffer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1193
            CharBuffer buffer = CharBuffer.wrap(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1194
                translateBytesToString(bytes,format, localeTransferable));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1195
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1196
            theObject = constructFlavoredObject(buffer, flavor, CharBuffer.class);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1197
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1198
            // Target data is a char array. Recur to obtain String and convert to
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1199
            // char array.
23260
d68cbf54738f 8029893: Remove reflection from DataTransferer.getInstance
pchelko
parents: 23238
diff changeset
  1200
        } else if (char[].class.equals(flavor.getRepresentationClass())) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1201
            if (!(DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                throw new IOException
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1203
                          ("cannot transfer non-text data as char array");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1206
            theObject = translateBytesToString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1207
                bytes, format, localeTransferable).toCharArray();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1208
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1209
            // Target data is a ByteBuffer. For arbitrary flavors, just return
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1210
            // the raw bytes. For text flavors, convert to a String to strip
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1211
            // terminators and search-and-replace EOLN, then reencode according to
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1212
            // the requested flavor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        } else if (flavor.isRepresentationClassByteBuffer()) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1214
            if (DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1215
                bytes = translateBytesToString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1216
                    bytes, format, localeTransferable).getBytes(
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1217
                        DataFlavorUtil.getTextCharset(flavor)
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1218
                    );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            ByteBuffer buffer = ByteBuffer.wrap(bytes);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1222
            theObject = constructFlavoredObject(buffer, flavor, ByteBuffer.class);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1223
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1224
            // Target data is a byte array. For arbitrary flavors, just return
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1225
            // the raw bytes. For text flavors, convert to a String to strip
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1226
            // terminators and search-and-replace EOLN, then reencode according to
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1227
            // the requested flavor.
23260
d68cbf54738f 8029893: Remove reflection from DataTransferer.getInstance
pchelko
parents: 23238
diff changeset
  1228
        } else if (byte[].class.equals(flavor.getRepresentationClass())) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1229
            if (DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1230
                theObject = translateBytesToString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1231
                    bytes, format, localeTransferable
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1232
                ).getBytes(DataFlavorUtil.getTextCharset(flavor));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            } else {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1234
                theObject = bytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1237
            // Target data is an InputStream. For arbitrary flavors, just return
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1238
            // the raw bytes. For text flavors, decode to strip terminators and
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1239
            // search-and-replace EOLN, then reencode according to the requested
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1240
            // flavor.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1241
        } else if (flavor.isRepresentationClassInputStream()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1242
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1243
            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1244
                theObject = translateStream(bais, flavor, format, localeTransferable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
21271
62d0e22ef687 8027030: AWT Multiple JVM DnD Test Failing on Linux (OEL and Ubuntu) and Solaris (Sparc and x64)
pchelko
parents: 20431
diff changeset
  1247
        } else if (flavor.isRepresentationClassRemote()) {
62d0e22ef687 8027030: AWT Multiple JVM DnD Test Failing on Linux (OEL and Ubuntu) and Solaris (Sparc and x64)
pchelko
parents: 20431
diff changeset
  1248
            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1249
                 ObjectInputStream ois = new ObjectInputStream(bais)) {
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1250
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1251
                theObject = DataFlavorUtil.RMI.getMarshalledObject(ois.readObject());
21271
62d0e22ef687 8027030: AWT Multiple JVM DnD Test Failing on Linux (OEL and Ubuntu) and Solaris (Sparc and x64)
pchelko
parents: 20431
diff changeset
  1252
            } catch (Exception e) {
62d0e22ef687 8027030: AWT Multiple JVM DnD Test Failing on Linux (OEL and Ubuntu) and Solaris (Sparc and x64)
pchelko
parents: 20431
diff changeset
  1253
                throw new IOException(e.getMessage());
62d0e22ef687 8027030: AWT Multiple JVM DnD Test Failing on Linux (OEL and Ubuntu) and Solaris (Sparc and x64)
pchelko
parents: 20431
diff changeset
  1254
            }
62d0e22ef687 8027030: AWT Multiple JVM DnD Test Failing on Linux (OEL and Ubuntu) and Solaris (Sparc and x64)
pchelko
parents: 20431
diff changeset
  1255
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1256
            // Target data is Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        } else if (flavor.isRepresentationClassSerializable()) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1258
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1259
            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1260
                theObject = translateStream(bais, flavor, format, localeTransferable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1263
            // Target data is Image
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        } else if (DataFlavor.imageFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            if (!isImageFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1269
            theObject = platformImageBytesToImage(bytes, format);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1270
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1271
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1272
        if (theObject == null) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1273
            throw new IOException("data translation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1276
        return theObject;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1277
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1278
    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1279
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1280
    /**
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1281
     * Primary translation function for translating
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1282
     * an InputStream into an Object, given a source format and a target
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1283
     * DataFlavor.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1284
     */
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 28073
diff changeset
  1285
    @SuppressWarnings("deprecation")
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1286
    public Object translateStream(InputStream str, DataFlavor flavor,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1287
                                  long format, Transferable localeTransferable)
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1288
        throws IOException
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1289
    {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1290
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1291
        Object theObject = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1292
        // Source data is a URI list. Convert to DataFlavor.javaFileListFlavor
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1293
        // where possible.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1294
        if (isURIListFormat(format)
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1295
                && DataFlavor.javaFileListFlavor.equals(flavor))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1296
        {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1297
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1298
            URI uris[] = dragQueryURIs(str, format, localeTransferable);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1299
            if (uris == null) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1300
                return null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1301
            }
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1302
            List<File> files = new ArrayList<>();
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1303
            for (URI uri : uris) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1304
                try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1305
                    files.add(new File(uri));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1306
                } catch (IllegalArgumentException illegalArg) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1307
                    // When converting from URIs to less generic files,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1308
                    // common practice (Wine, SWT) seems to be to
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1309
                    // silently drop the URIs that aren't local files.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1310
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1311
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1312
            theObject = files;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1313
21280
68b145d54e2a 8027151: AWT_DnD/Basic_DnD/Automated/DnDMerlinQL/MultipleJVM failing on windows machine
bagiras
parents: 21271
diff changeset
  1314
        // Target data is a String. Strip terminating NUL bytes. Decode bytes
68b145d54e2a 8027151: AWT_DnD/Basic_DnD/Automated/DnDMerlinQL/MultipleJVM failing on windows machine
bagiras
parents: 21271
diff changeset
  1315
        // into characters. Search-and-replace EOLN.
68b145d54e2a 8027151: AWT_DnD/Basic_DnD/Automated/DnDMerlinQL/MultipleJVM failing on windows machine
bagiras
parents: 21271
diff changeset
  1316
        } else if (String.class.equals(flavor.getRepresentationClass()) &&
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1317
                   DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
21280
68b145d54e2a 8027151: AWT_DnD/Basic_DnD/Automated/DnDMerlinQL/MultipleJVM failing on windows machine
bagiras
parents: 21271
diff changeset
  1318
68b145d54e2a 8027151: AWT_DnD/Basic_DnD/Automated/DnDMerlinQL/MultipleJVM failing on windows machine
bagiras
parents: 21271
diff changeset
  1319
            return translateBytesToString(inputStreamToByteArray(str),
68b145d54e2a 8027151: AWT_DnD/Basic_DnD/Automated/DnDMerlinQL/MultipleJVM failing on windows machine
bagiras
parents: 21271
diff changeset
  1320
                format, localeTransferable);
68b145d54e2a 8027151: AWT_DnD/Basic_DnD/Automated/DnDMerlinQL/MultipleJVM failing on windows machine
bagiras
parents: 21271
diff changeset
  1321
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1322
            // Special hack to maintain backwards-compatibility with the brokenness
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1323
            // of StringSelection. Return a StringReader instead of an InputStream.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1324
            // Recur to obtain String and encapsulate.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1325
        } else if (DataFlavor.plainTextFlavor.equals(flavor)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1326
            theObject = new StringReader(translateBytesToString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1327
                inputStreamToByteArray(str),
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1328
                format, localeTransferable));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1329
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1330
            // Target data is an InputStream. For arbitrary flavors, just return
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1331
            // the raw bytes. For text flavors, decode to strip terminators and
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1332
            // search-and-replace EOLN, then reencode according to the requested
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1333
            // flavor.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1334
        } else if (flavor.isRepresentationClassInputStream()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1335
            theObject = translateStreamToInputStream(str, flavor, format,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1336
                                                               localeTransferable);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1337
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1338
            // Target data is a Reader. Obtain data in InputStream format, encoded
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1339
            // as "Unicode" (utf-16be). Then use an InputStreamReader to decode
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1340
            // back to chars on demand.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1341
        } else if (flavor.isRepresentationClassReader()) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1342
            if (!(DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1343
                throw new IOException
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1344
                          ("cannot transfer non-text data as Reader");
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1345
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1346
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1347
            InputStream is = (InputStream)translateStreamToInputStream(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1348
                    str, DataFlavor.plainTextFlavor,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1349
                    format, localeTransferable);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1350
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1351
            String unicode = DataFlavorUtil.getTextCharset(DataFlavor.plainTextFlavor);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1352
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1353
            Reader reader = new InputStreamReader(is, unicode);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1354
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1355
            theObject = constructFlavoredObject(reader, flavor, Reader.class);
20128
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1356
            // Target data is a byte array
23260
d68cbf54738f 8029893: Remove reflection from DataTransferer.getInstance
pchelko
parents: 23238
diff changeset
  1357
        } else if (byte[].class.equals(flavor.getRepresentationClass())) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1358
            if (DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
20128
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1359
                theObject = translateBytesToString(inputStreamToByteArray(str), format, localeTransferable)
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1360
                        .getBytes(DataFlavorUtil.getTextCharset(flavor));
20128
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1361
            } else {
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1362
                theObject = inputStreamToByteArray(str);
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1363
            }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1364
            // Target data is an RMI object
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1365
        } else if (flavor.isRepresentationClassRemote()) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1366
            try (ObjectInputStream ois = new ObjectInputStream(str)) {
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1367
                theObject = DataFlavorUtil.RMI.getMarshalledObject(ois.readObject());
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1368
            } catch (Exception e) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1369
                throw new IOException(e.getMessage());
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1370
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1371
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1372
            // Target data is Serializable
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1373
        } else if (flavor.isRepresentationClassSerializable()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1374
            try (ObjectInputStream ois =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1375
                     new ObjectInputStream(str))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1376
            {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1377
                theObject = ois.readObject();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1378
            } catch (Exception e) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1379
                throw new IOException(e.getMessage());
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1380
            }
20431
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1381
            // Target data is Image
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1382
        } else if (DataFlavor.imageFlavor.equals(flavor)) {
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1383
            if (!isImageFormat(format)) {
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1384
                throw new IOException("data translation failed");
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1385
            }
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1386
            theObject = platformImageBytesToImage(inputStreamToByteArray(str), format);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1387
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1388
20431
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1389
        if (theObject == null) {
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1390
            throw new IOException("data translation failed");
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1391
        }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1392
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1393
        return theObject;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1394
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * For arbitrary flavors, just use the raw InputStream. For text flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * ReencodingInputStream will decode and reencode the InputStream on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * so that we can strip terminators and search-and-replace EOLN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1402
    private Object translateStreamToInputStream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        (InputStream str, DataFlavor flavor, long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
         Transferable localeTransferable) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1406
        if (DataFlavorUtil.isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            str = new ReencodingInputStream
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1408
                (str, format, DataFlavorUtil.getTextCharset(flavor),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                 localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        return constructFlavoredObject(str, flavor, InputStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * We support representations which are exactly of the specified Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * and also arbitrary Objects which have a constructor which takes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * instance of the Class as its sole parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    private Object constructFlavoredObject(Object arg, DataFlavor flavor,
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1421
                                           Class<?> clazz)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1424
        final Class<?> dfrc = flavor.getRepresentationClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        if (clazz.equals(dfrc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            return arg; // simple case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        } else {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1429
            Constructor<?>[] constructors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            try {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1432
                constructors = AccessController.doPrivileged(
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1433
                        (PrivilegedAction<Constructor<?>[]>) dfrc::getConstructors);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                throw new IOException(se.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1438
            Constructor<?> constructor = Stream.of(constructors)
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1439
                    .filter(c -> Modifier.isPublic(c.getModifiers()))
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1440
                    .filter(c -> {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1441
                        Class<?>[] ptypes = c.getParameterTypes();
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1442
                        return ptypes != null
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1443
                                && ptypes.length == 1
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1444
                                && clazz.equals(ptypes[0]);
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1445
                    })
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1446
                    .findFirst()
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1447
                    .orElseThrow(() ->
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1448
                            new IOException("can't find <init>(L"+ clazz + ";)V for class: " + dfrc.getName()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            try {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1451
                return constructor.newInstance(arg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * Used for decoding and reencoding an InputStream on demand so that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * can strip NUL terminators and perform EOLN search-and-replace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    public class ReencodingInputStream extends InputStream {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1463
        BufferedReader wrapped;
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1464
        final char[] in = new char[2];
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1465
        byte[] out;
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1466
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1467
        CharsetEncoder encoder;
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1468
        CharBuffer inBuf;
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1469
        ByteBuffer outBuf;
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1470
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1471
        char[] eoln;
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1472
        int numTerminators;
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1473
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1474
        boolean eos;
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1475
        int index, limit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        public ReencodingInputStream(InputStream bytestream, long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                                     String targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                                     Transferable localeTransferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1482
            Long lFormat = format;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
24530
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
  1484
            String sourceEncoding = getBestCharsetForTextFormat(format, localeTransferable);
479661529a54 8039567: Duplicated code in DataTransferer
pchelko
parents: 24529
diff changeset
  1485
            wrapped = new BufferedReader(new InputStreamReader(bytestream, sourceEncoding));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            if (targetEncoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                // Throw NullPointerException for compatibility with the former
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                // call to sun.io.CharToByteConverter.getConverter(null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                // (Charset.forName(null) throws unspecified IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                // now; see 6228568)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                throw new NullPointerException("null target encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                encoder = Charset.forName(targetEncoding).newEncoder();
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1497
                out = new byte[(int)(encoder.maxBytesPerChar() * 2 + 0.5)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                inBuf = CharBuffer.wrap(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                outBuf = ByteBuffer.wrap(out);
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1500
            } catch (IllegalCharsetNameException
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1501
                    | UnsupportedCharsetException
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1502
                    | UnsupportedOperationException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                throw new IOException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1506
            String sEoln = nativeEOLNs.get(lFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            if (sEoln != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                eoln = sEoln.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            // A hope and a prayer that this works generically. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            // definitely work on Win32.
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1513
            Integer terminators = nativeTerminators.get(lFormat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            if (terminators != null) {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1515
                numTerminators = terminators;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1519
        private int readChar() throws IOException {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1520
            int c = wrapped.read();
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1521
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1522
            if (c == -1) { // -1 is EOS
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1523
                eos = true;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1524
                return -1;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1525
            }
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1526
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1527
            // "c == 0" is not quite correct, but good enough on Windows.
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1528
            if (numTerminators > 0 && c == 0) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1529
                eos = true;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1530
                return -1;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1531
            } else if (eoln != null && matchCharArray(eoln, c)) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1532
                c = '\n' & 0xFFFF;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1533
            }
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1534
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1535
            return c;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1536
        }
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1537
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            if (eos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            if (index >= limit) {
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1544
                // deal with supplementary characters
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1545
                int c = readChar();
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1546
                if (c == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1550
                in[0] = (char) c;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1551
                in[1] = 0;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1552
                inBuf.limit(1);
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1553
                if (Character.isHighSurrogate((char) c)) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1554
                    c = readChar();
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1555
                    if (c != -1) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1556
                        in[1] = (char) c;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1557
                        inBuf.limit(2);
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1558
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                inBuf.rewind();
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1562
                outBuf.limit(out.length).rewind();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                encoder.encode(inBuf, outBuf, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                outBuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                limit = outBuf.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                return read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                return out[index++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            return ((eos) ? 0 : (limit - index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            wrapped.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
         * Checks to see if the next array.length characters in wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
         * match array. The first character is provided as c. Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
         * characters are read from wrapped itself. When this method returns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
         * the wrapped index may be different from what it was when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
         * method was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        private boolean matchCharArray(char[] array, int c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            wrapped.mark(array.length);  // BufferedReader supports mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            if ((char)c == array[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                for (count = 1; count < array.length; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                    c = wrapped.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                    if (c == -1 || ((char)c) != array[count]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            if (count == array.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                wrapped.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * Decodes a byte array into a set of String filenames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    protected abstract String[] dragQueryFile(byte[] bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    /**
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1620
     * Decodes URIs from either a byte array or a stream.
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1621
     */
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1622
    protected URI[] dragQueryURIs(InputStream stream,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1623
                                  long format,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1624
                                  Transferable localeTransferable)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1625
      throws IOException
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1626
    {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1627
        throw new IOException(
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1628
            new UnsupportedOperationException("not implemented on this platform"));
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1629
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1630
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1631
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * Translates either a byte array or an input stream which contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * platform-specific image data in the given format into an Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1635
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1636
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1637
    protected abstract Image platformImageBytesToImage(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1638
        byte[] bytes,long format) throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * Translates either a byte array or an input stream which contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * an image data in the given standard format into an Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * @param mimeType image MIME type, such as: image/png, image/jpeg, image/gif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1646
    protected Image standardImageBytesToImage(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1647
        byte[] bytes, String mimeType) throws IOException
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1648
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1650
        Iterator<ImageReader> readerIterator = ImageIO.getImageReadersByMIMEType(mimeType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        if (!readerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            throw new IOException("No registered service provider can decode " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                                  " an image from " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        IOException ioe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        while (readerIterator.hasNext()) {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1660
            ImageReader imageReader = readerIterator.next();
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1661
            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1662
                try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(bais)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                    ImageReadParam param = imageReader.getDefaultReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                    imageReader.setInput(imageInputStream, true, true);
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1665
                    BufferedImage bufferedImage = imageReader.read(imageReader.getMinIndex(), param);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                    if (bufferedImage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                        return bufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                    imageReader.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                ioe = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        if (ioe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            ioe = new IOException("Registered service providers failed to decode"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                                  + " an image from " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * Translates a Java Image into a byte array which contains platform-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * specific image data in the given format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    protected abstract byte[] imageToPlatformBytes(Image image, long format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
      throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * Translates a Java Image into a byte array which contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * an image data in the given standard format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * @param mimeType image MIME type, such as: image/png, image/jpeg
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    protected byte[] imageToStandardBytes(Image image, String mimeType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        IOException originalIOE = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1703
        Iterator<ImageWriter> writerIterator = ImageIO.getImageWritersByMIMEType(mimeType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        if (!writerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            throw new IOException("No registered service provider can encode " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                                  " an image to " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        if (image instanceof RenderedImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            // Try to encode the original image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                return imageToStandardBytesImpl((RenderedImage)image, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                originalIOE = ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        // Retry with a BufferedImage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        if (image instanceof ToolkitImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            ir.reconstruct(ImageObserver.ALLBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            width = ir.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            height = ir.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            width = image.getWidth(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            height = image.getHeight(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        ColorModel model = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        WritableRaster raster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            model.createCompatibleWritableRaster(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        BufferedImage bufferedImage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            new BufferedImage(model, raster, model.isAlphaPremultiplied(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                              null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        Graphics g = bufferedImage.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            g.drawImage(image, 0, 0, width, height, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            return imageToStandardBytesImpl(bufferedImage, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            if (originalIOE != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                throw originalIOE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1758
    byte[] imageToStandardBytesImpl(RenderedImage renderedImage,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                                              String mimeType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1762
        Iterator<ImageWriter> writerIterator = ImageIO.getImageWritersByMIMEType(mimeType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        ImageTypeSpecifier typeSpecifier =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            new ImageTypeSpecifier(renderedImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        IOException ioe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        while (writerIterator.hasNext()) {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1771
            ImageWriter imageWriter = writerIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            if (!writerSpi.canEncodeImage(typeSpecifier)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            try {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1779
                try (ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(baos)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                    imageWriter.setOutput(imageOutputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                    imageWriter.write(renderedImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                    imageOutputStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                imageWriter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                ioe = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            imageWriter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            baos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        baos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        if (ioe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            ioe = new IOException("Registered service providers failed to encode "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                                  + renderedImage + " to " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * Concatenates the data represented by two objects. Objects can be either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * byte arrays or instances of <code>InputStream</code>. If both arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * are byte arrays byte array will be returned. Otherwise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * <code>InputStream</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * Currently is only called from native code to prepend palette data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * platform-specific image data during image transfer on Win32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * @param obj1 the first object to be concatenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * @param obj2 the second object to be concatenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * @return a byte array or an <code>InputStream</code> which represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     *         a logical concatenation of the two arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * @throws NullPointerException is either of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     *         <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * @throws ClassCastException is either of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     *         neither byte array nor an instance of <code>InputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
    private Object concatData(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        InputStream str1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        InputStream str2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        if (obj1 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            byte[] arr1 = (byte[])obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            if (obj2 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                byte[] arr2 = (byte[])obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                byte[] ret = new byte[arr1.length + arr2.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                System.arraycopy(arr1, 0, ret, 0, arr1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                System.arraycopy(arr2, 0, ret, arr1.length, arr2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                str1 = new ByteArrayInputStream(arr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                str2 = (InputStream)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            str1 = (InputStream)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            if (obj2 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                str2 = new ByteArrayInputStream((byte[])obj2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                str2 = (InputStream)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        return new SequenceInputStream(str1, str2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    public byte[] convertData(final Object source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                              final Transferable contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                              final long format,
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1855
                              final Map<Long, DataFlavor> formatMap,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                              final boolean isToolkitThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        byte[] ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
         * If the current thread is the Toolkit thread we should post a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
         * Runnable to the event dispatch thread associated with source Object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
         * since translateTransferable() calls Transferable.getTransferData()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
         * that may contain client code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        if (isToolkitThread) try {
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1868
            final Stack<byte[]> stack = new Stack<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            final Runnable dataConverter = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                // Guard against multiple executions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                private boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                    if (done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                    byte[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                    try {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1878
                        DataFlavor flavor = formatMap.get(format);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                        if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                            data = translateTransferable(contents, flavor, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                        data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                        getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                        stack.push(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                        getToolkitThreadBlockedHandler().exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                        getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            final AppContext appContext = SunToolkit.targetToAppContext(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            if (appContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                appContext.put(DATA_CONVERTER_KEY, dataConverter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            SunToolkit.executeOnEventHandlerThread(source, dataConverter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            while (stack.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                getToolkitThreadBlockedHandler().enter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            if (appContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                appContext.remove(DATA_CONVERTER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1915
            ret = stack.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        } else {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1919
            DataFlavor flavor = formatMap.get(format);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                ret = translateTransferable(contents, flavor, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    public void processDataConversionRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            AppContext appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                Runnable dataConverter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                    (Runnable)appContext.get(DATA_CONVERTER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                if (dataConverter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                    dataConverter.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                    appContext.remove(DATA_CONVERTER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1945
    public abstract ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * Helper function to reduce a Map with Long keys to a long array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * The map keys are sorted according to the native formats preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     */
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1953
    public static long[] keysToLongArray(SortedMap<Long, ?> map) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1954
        Set<Long> keySet = map.keySet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        long[] retval = new long[keySet.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        int i = 0;
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1957
        for (Iterator<Long> iter = keySet.iterator(); iter.hasNext(); i++) {
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 23260
diff changeset
  1958
            retval[i] = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * Helper function to convert a Set of DataFlavors to a sorted array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * The array will be sorted according to <code>DataFlavorComparator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     */
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 24530
diff changeset
  1967
    public static DataFlavor[] setToSortedDataFlavorArray(Set<DataFlavor> flavorsSet) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        DataFlavor[] flavors = new DataFlavor[flavorsSet.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        flavorsSet.toArray(flavors);
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24538
diff changeset
  1970
        final Comparator<DataFlavor> comparator = DataFlavorUtil.getDataFlavorComparator().reversed();
9484
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
  1971
        Arrays.sort(flavors, comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * Helper function to convert an InputStream to a byte[] array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    protected static byte[] inputStreamToByteArray(InputStream str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1981
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1982
            int len = 0;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1983
            byte[] buf = new byte[8192];
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1984
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1985
            while ((len = str.read(buf)) != -1) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1986
                baos.write(buf, 0, len);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1987
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1988
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1989
            return baos.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     * Returns platform-specific mappings for the specified native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * If there are no platform-specific mappings for this native, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * returns an empty <code>List</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23279
diff changeset
  1998
    public LinkedHashSet<DataFlavor> getPlatformMappingsForNative(String nat) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23279
diff changeset
  1999
        return new LinkedHashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * Returns platform-specific mappings for the specified flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * If there are no platform-specific mappings for this flavor, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * returns an empty <code>List</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23279
diff changeset
  2007
    public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23279
diff changeset
  2008
        return new LinkedHashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
}