jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java
author pchelko
Fri, 27 Sep 2013 18:35:00 +0400
changeset 20431 3845c63e933d
parent 20128 25a1c15eea88
child 21271 62d0e22ef687
permissions -rw-r--r--
8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails Reviewed-by: anthony, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4363
diff changeset
     2
 * Copyright (c) 2000, 2008, 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.AWTError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.EventQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Graphics;
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.nio.charset.UnsupportedCharsetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import java.security.PrivilegedExceptionAction;
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
    71
import java.security.ProtectionDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
import java.util.SortedSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
import java.util.TreeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
import java.util.TreeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2639
diff changeset
    89
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
import java.awt.image.ImageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
import java.awt.image.RenderedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
import javax.imageio.ImageReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
import javax.imageio.ImageReadParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
import javax.imageio.spi.ImageWriterSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
import javax.imageio.stream.ImageInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
import javax.imageio.stream.ImageOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
import sun.awt.image.ImageRepresentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
import sun.awt.image.ToolkitImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   114
import java.io.FilePermission;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
   115
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * Provides a set of functions to be shared among the DataFlavor class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * platform-specific data transfer implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * The concept of "flavors" and "natives" is extended to include "formats",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * which are the numeric values Win32 and X11 use to express particular data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * types. Like FlavorMap, which provides getNativesForFlavors(DataFlavor[]) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * getFlavorsForNatives(String[]) functions, DataTransferer provides a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * of getFormatsFor(Transferable|Flavor|Flavors) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * getFlavorsFor(Format|Formats) functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Also provided are functions for translating a Transferable into a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * array, given a source DataFlavor and a target format, and for translating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * a byte array or InputStream into an Object, given a source format and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * a target DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @author Danila Sinopalnikov
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * @since 1.3.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
public abstract class DataTransferer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Cached value of Class.forName("[C");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static final Class charArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Cached value of Class.forName("[B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static final Class byteArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * The <code>DataFlavor</code> representing plain text with Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * encoding, where:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *     representationClass = java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *     mimeType            = "text/plain; charset=Unicode"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public static final DataFlavor plainTextStringFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * The <code>DataFlavor</code> representing a Java text encoding String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * encoded in UTF-8, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *     representationClass = [B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *     mimeType            = "application/x-java-text-encoding"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public static final DataFlavor javaTextEncodingFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
9484
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   170
    /**
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   171
     * Lazy initialization of Standard Encodings.
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   172
     */
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   173
    private static class StandardEncodingsHolder {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   174
        private static final SortedSet<String> standardEncodings = load();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   175
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   176
        private static SortedSet<String> load() {
9484
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   177
            final Comparator comparator =
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   178
                    new CharsetComparator(IndexedComparator.SELECT_WORST);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   179
            final SortedSet<String> tempSet = new TreeSet<String>(comparator);
9484
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   180
            tempSet.add("US-ASCII");
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   181
            tempSet.add("ISO-8859-1");
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   182
            tempSet.add("UTF-8");
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   183
            tempSet.add("UTF-16BE");
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   184
            tempSet.add("UTF-16LE");
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   185
            tempSet.add("UTF-16");
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   186
            tempSet.add(getDefaultTextCharset());
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   187
            return Collections.unmodifiableSortedSet(tempSet);
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   188
        }
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   189
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Tracks whether a particular text/* MIME type supports the charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * parameter. The Map is initialized with all of the standard MIME types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * listed in the DataFlavor.selectBestTextFlavor method comment. Additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * entries may be added during the life of the JRE for text/<other> types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private static final Map textMIMESubtypeCharsetSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Cache of the platform default encoding as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * "file.encoding" system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private static String defaultEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * A collection of all natives listed in flavormap.properties with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * a primary MIME type of "text".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private static final Set textNatives =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Collections.synchronizedSet(new HashSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * The native encodings/charsets for the Set of textNatives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private static final Map nativeCharsets =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        Collections.synchronizedMap(new HashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * The end-of-line markers for the Set of textNatives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static final Map nativeEOLNs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Collections.synchronizedMap(new HashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * The number of terminating NUL bytes for the Set of textNatives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private static final Map nativeTerminators =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        Collections.synchronizedMap(new HashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * The key used to store pending data conversion requests for an AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private static final String DATA_CONVERTER_KEY = "DATA_CONVERTER_KEY";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * The singleton DataTransferer instance. It is created during MToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * or WToolkit initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static DataTransferer transferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2639
diff changeset
   241
    private static final PlatformLogger dtLog = PlatformLogger.getLogger("sun.awt.datatransfer.DataTransfer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        Class tCharArrayClass = null, tByteArrayClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            tCharArrayClass = Class.forName("[C");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            tByteArrayClass = Class.forName("[B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } catch (ClassNotFoundException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        charArrayClass = tCharArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        byteArrayClass = tByteArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        DataFlavor tPlainTextStringFlavor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            tPlainTextStringFlavor = new DataFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                ("text/plain;charset=Unicode;class=java.lang.String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } catch (ClassNotFoundException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        plainTextStringFlavor = tPlainTextStringFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        DataFlavor tJavaTextEncodingFlavor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            tJavaTextEncodingFlavor = new DataFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                ("application/x-java-text-encoding;class=\"[B\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } catch (ClassNotFoundException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        javaTextEncodingFlavor = tJavaTextEncodingFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        Map tempMap = new HashMap(17);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        tempMap.put("sgml", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        tempMap.put("xml", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        tempMap.put("html", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        tempMap.put("enriched", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        tempMap.put("richtext", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        tempMap.put("uri-list", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        tempMap.put("directory", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        tempMap.put("css", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        tempMap.put("calendar", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        tempMap.put("plain", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        tempMap.put("rtf", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        tempMap.put("tab-separated-values", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        tempMap.put("t140", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        tempMap.put("rfc822-headers", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        tempMap.put("parityfec", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        textMIMESubtypeCharsetSupport = Collections.synchronizedMap(tempMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * The accessor method for the singleton DataTransferer instance. Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * that in a headless environment, there may be no DataTransferer instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * instead, null will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public static DataTransferer getInstance() {
111
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   294
        synchronized (DataTransferer.class) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   295
            if (transferer == null) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   296
                final String name = SunToolkit.getDataTransfererClassName();
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   297
                if (name != null) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   298
                    PrivilegedAction<DataTransferer> action = new PrivilegedAction<DataTransferer>()
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   299
                    {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   300
                      public DataTransferer run() {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   301
                          Class cls = null;
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   302
                          Method method = null;
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   303
                          DataTransferer ret = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
111
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   305
                          try {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   306
                              cls = Class.forName(name);
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   307
                          } catch (ClassNotFoundException e) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   308
                              ClassLoader cl = ClassLoader.
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   309
                                  getSystemClassLoader();
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   310
                              if (cl != null) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   311
                                  try {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   312
                                      cls = cl.loadClass(name);
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   313
                                  } catch (ClassNotFoundException ee) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   314
                                      ee.printStackTrace();
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   315
                                      throw new AWTError("DataTransferer not found: " + name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                              }
111
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   318
                          }
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   319
                          if (cls != null) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   320
                              try {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   321
                                  method = cls.getDeclaredMethod("getInstanceImpl");
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   322
                                  method.setAccessible(true);
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   323
                              } catch (NoSuchMethodException e) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   324
                                  e.printStackTrace();
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   325
                                  throw new AWTError("Cannot instantiate DataTransferer: " + name);
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   326
                              } catch (SecurityException e) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   327
                                  e.printStackTrace();
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   328
                                  throw new AWTError("Access is denied for DataTransferer: " + name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                              }
111
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   330
                          }
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   331
                          if (method != null) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   332
                              try {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   333
                                  ret = (DataTransferer) method.invoke(null);
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   334
                              } catch (InvocationTargetException e) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   335
                                  e.printStackTrace();
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   336
                                  throw new AWTError("Cannot instantiate DataTransferer: " + name);
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   337
                              } catch (IllegalAccessException e) {
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   338
                                  e.printStackTrace();
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   339
                                  throw new AWTError("Cannot access DataTransferer: " + name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                          }
111
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   342
                          return ret;
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   343
                      }
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   344
                    };
adfeae7879c2 6636369: sun.awt.datatransfer.DataTransferer contains double-check idiom
son
parents: 2
diff changeset
   345
                    transferer = AccessController.doPrivileged(action);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return transferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Converts an arbitrary text encoding to its canonical name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public static String canonicalName(String encoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (encoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return Charset.forName(encoding).name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } catch (IllegalCharsetNameException icne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } catch (UnsupportedCharsetException uce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * If the specified flavor is a text flavor which supports the "charset"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * parameter, then this method returns that parameter, or the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * charset if no such parameter was specified at construction. For non-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * text DataFlavors, and for non-charset text flavors, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public static String getTextCharset(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (!isFlavorCharsetTextType(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        String encoding = flavor.getParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return (encoding != null) ? encoding : getDefaultTextCharset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Returns the platform's default character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public static String getDefaultTextCharset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (defaultEncoding != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            return defaultEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return defaultEncoding = Charset.defaultCharset().name();
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
     * Tests only whether the flavor's MIME type supports the charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * parameter. Must only be called for flavors with a primary type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * "text".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public static boolean doesSubtypeSupportCharset(DataFlavor flavor) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16705
diff changeset
   401
        if (dtLog.isLoggable(PlatformLogger.Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (!"text".equals(flavor.getPrimaryType())) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2639
diff changeset
   403
                dtLog.fine("Assertion (\"text\".equals(flavor.getPrimaryType())) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        String subType = flavor.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (subType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        Object support = textMIMESubtypeCharsetSupport.get(subType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (support != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            return (support == Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        boolean ret_val = (flavor.getParameter("charset") != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        textMIMESubtypeCharsetSupport.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            (subType, (ret_val) ? Boolean.TRUE : Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return ret_val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public static boolean doesSubtypeSupportCharset(String subType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                                    String charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        Object support = textMIMESubtypeCharsetSupport.get(subType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (support != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return (support == Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        boolean ret_val = (charset != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        textMIMESubtypeCharsetSupport.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            (subType, (ret_val) ? Boolean.TRUE : Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return ret_val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Returns whether this flavor is a text type which supports the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * 'charset' parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public static boolean isFlavorCharsetTextType(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        // Although stringFlavor doesn't actually support the charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // parameter (because its primary MIME type is not "text"), it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // be treated as though it does. stringFlavor is semantically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        // equivalent to "text/plain" data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (DataFlavor.stringFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (!"text".equals(flavor.getPrimaryType()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            !doesSubtypeSupportCharset(flavor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        Class rep_class = flavor.getRepresentationClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (flavor.isRepresentationClassReader() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            String.class.equals(rep_class) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            flavor.isRepresentationClassCharBuffer() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            DataTransferer.charArrayClass.equals(rep_class))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (!(flavor.isRepresentationClassInputStream() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
              flavor.isRepresentationClassByteBuffer() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
              DataTransferer.byteArrayClass.equals(rep_class))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        String charset = flavor.getParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return (charset != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            ? DataTransferer.isEncodingSupported(charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            : true; // null equals default encoding which is always supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Returns whether this flavor is a text type which does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * 'charset' parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public static boolean isFlavorNoncharsetTextType(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (!"text".equals(flavor.getPrimaryType()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            doesSubtypeSupportCharset(flavor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return (flavor.isRepresentationClassInputStream() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                flavor.isRepresentationClassByteBuffer() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                DataTransferer.byteArrayClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    equals(flavor.getRepresentationClass()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Determines whether this JRE can both encode and decode text in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * specified encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public static boolean isEncodingSupported(String encoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (encoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            return Charset.isSupported(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        } catch (IllegalCharsetNameException icne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
4267
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
   513
     * Returns {@code true} if the given type is a java.rmi.Remote.
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
   514
     */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
   515
    public static boolean isRemote(Class<?> type) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
   516
        return RMI.isRemote(type);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
   517
    }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
   518
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
   519
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Returns an Iterator which traverses a SortedSet of Strings which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * a total order of the standard character sets supported by the JRE. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * ordering follows the same principles as DataFlavor.selectBestTextFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * So as to avoid loading all available character converters, optional,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * non-standard, character sets are not included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   526
    public static Set <String> standardEncodings() {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
   527
        return StandardEncodingsHolder.standardEncodings;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Converts a FlavorMap to a FlavorTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public static FlavorTable adaptFlavorMap(final FlavorMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (map instanceof FlavorTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            return (FlavorTable)map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return new FlavorTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                public Map getNativesForFlavors(DataFlavor[] flavors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    return map.getNativesForFlavors(flavors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                public Map getFlavorsForNatives(String[] natives) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    return map.getFlavorsForNatives(natives);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                public List getNativesForFlavor(DataFlavor flav) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    Map natives =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        getNativesForFlavors(new DataFlavor[] { flav } );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    String nat = (String)natives.get(flav);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    if (nat != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        List list = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        list.add(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        return Collections.EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                public List getFlavorsForNative(String nat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    Map flavors =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        getFlavorsForNatives(new String[] { nat } );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    DataFlavor flavor = (DataFlavor)flavors.get(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        List list = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        list.add(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                        return Collections.EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Returns the default Unicode encoding for the platform. The encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * need not be canonical. This method is only used by the archaic function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * DataFlavor.getTextPlainUnicodeFlavor().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public abstract String getDefaultUnicodeEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * This method is called for text flavor mappings established while parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * the flavormap.properties file. It stores the "eoln" and "terminators"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * parameters which are not officially part of the MIME type. They are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * MIME parameters specific to the flavormap.properties file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public void registerTextFlavorProperties(String nat, String charset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                             String eoln, String terminators) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        Long format = getFormatForNativeAsLong(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        textNatives.add(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        nativeCharsets.put(format, (charset != null && charset.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            ? charset : getDefaultTextCharset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (eoln != null && eoln.length() != 0 && !eoln.equals("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            nativeEOLNs.put(format, eoln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (terminators != null && terminators.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            Integer iTerminators = Integer.valueOf(terminators);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if (iTerminators.intValue() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                nativeTerminators.put(format, iTerminators);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Determines whether the native corresponding to the specified long format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * was listed in the flavormap.properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    protected boolean isTextFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return textNatives.contains(Long.valueOf(format));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    protected String getCharsetForTextFormat(Long lFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        return (String)nativeCharsets.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * Specifies whether text imported from the native system in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * format is locale-dependent. If so, when decoding such text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * 'nativeCharsets' should be ignored, and instead, the Transferable should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * be queried for its javaTextEncodingFlavor data for the correct encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    public abstract boolean isLocaleDependentTextFormat(long format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Determines whether the DataFlavor corresponding to the specified long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * format is DataFlavor.javaFileListFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public abstract boolean isFileFormat(long format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Determines whether the DataFlavor corresponding to the specified long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * format is DataFlavor.imageFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public abstract boolean isImageFormat(long format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   636
     * 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
   637
     * a DataFlavor.javaFileListFlavor.
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   638
     */
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   639
    protected boolean isURIListFormat(long format) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   640
        return false;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   641
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   642
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
   643
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Returns a Map whose keys are all of the possible formats into which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Transferable's transfer data flavors can be translated. The value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * each key is the DataFlavor in which the Transferable's data should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * requested when converting to the format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * The map keys are sorted according to the native formats preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
11271
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   652
    public SortedMap<Long,DataFlavor> getFormatsForTransferable(
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   653
                               Transferable contents, FlavorTable map)
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   654
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        DataFlavor[] flavors = contents.getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (flavors == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            return new TreeMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        return getFormatsForFlavors(flavors, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Returns a Map whose keys are all of the possible formats into which data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * in the specified DataFlavor can be translated. The value of each key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * is the DataFlavor in which a Transferable's data should be requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * when converting to the format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * The map keys are sorted according to the native formats preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public SortedMap getFormatsForFlavor(DataFlavor flavor, FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        return getFormatsForFlavors(new DataFlavor[] { flavor },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                    map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Returns a Map whose keys are all of the possible formats into which data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * in the specified DataFlavors can be translated. The value of each key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * is the DataFlavor in which the Transferable's data should be requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * when converting to the format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * The map keys are sorted according to the native formats preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @param flavors the data flavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @param map the FlavorTable which contains mappings between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *            DataFlavors and data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @throws NullPointerException if flavors or map is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
11271
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   690
    public SortedMap <Long, DataFlavor> getFormatsForFlavors(
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   691
        DataFlavor[] flavors, FlavorTable map)
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   692
    {
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   693
        Map <Long,DataFlavor> formatMap =
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   694
            new HashMap <> (flavors.length);
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   695
        Map <Long,DataFlavor> textPlainMap =
f10f98b24801 7117011: Reduce number of warnings in sun/awt/windows and sun/awt/datatransfer
denis
parents: 9484
diff changeset
   696
            new HashMap <> (flavors.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        // Maps formats to indices that will be used to sort the formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        // according to the preference order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        // Larger index value corresponds to the more preferable format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        Map indexMap = new HashMap(flavors.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        Map textPlainIndexMap = new HashMap(flavors.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        int currentIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        // Iterate backwards so that preferred DataFlavors are used over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        // other DataFlavors. (See javadoc for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // Transferable.getTransferDataFlavors.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        for (int i = flavors.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            DataFlavor flavor = flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if (flavor == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            // Don't explicitly test for String, since it is just a special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            // case of Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (flavor.isFlavorTextType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                flavor.isFlavorJavaFileListType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                DataFlavor.imageFlavor.equals(flavor) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                flavor.isRepresentationClassSerializable() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                flavor.isRepresentationClassInputStream() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                flavor.isRepresentationClassRemote())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                List natives = map.getNativesForFlavor(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                currentIndex += natives.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                for (Iterator iter = natives.iterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    Long lFormat =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        getFormatForNativeAsLong((String)iter.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    Integer index = Integer.valueOf(currentIndex--);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    formatMap.put(lFormat, flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    indexMap.put(lFormat, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    // SystemFlavorMap.getNativesForFlavor will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    // text/plain natives for all text/*. While this is good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    // for a single text/* flavor, we would prefer that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    // text/plain native data come from a text/plain flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    if (("text".equals(flavor.getPrimaryType()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                         "plain".equals(flavor.getSubType())) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                        flavor.equals(DataFlavor.stringFlavor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        textPlainMap.put(lFormat, flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        textPlainIndexMap.put(lFormat, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                currentIndex += natives.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        formatMap.putAll(textPlainMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        indexMap.putAll(textPlainIndexMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // Sort the map keys according to the formats preference order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        Comparator comparator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            new IndexOrderComparator(indexMap, IndexedComparator.SELECT_WORST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        SortedMap sortedMap = new TreeMap(comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        sortedMap.putAll(formatMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        return sortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * Reduces the Map output for the root function to an array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Map's keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    public long[] getFormatsForTransferableAsArray(Transferable contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                                   FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        return keysToLongArray(getFormatsForTransferable(contents, map));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    public long[] getFormatsForFlavorAsArray(DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                             FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return keysToLongArray(getFormatsForFlavor(flavor, map));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    public long[] getFormatsForFlavorsAsArray(DataFlavor[] flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                                              FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        return keysToLongArray(getFormatsForFlavors(flavors, map));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * Returns a Map whose keys are all of the possible DataFlavors into which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * data in the specified format can be translated. The value of each key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * is the format in which the Clipboard or dropped data should be requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * when converting to the DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    public Map getFlavorsForFormat(long format, FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        return getFlavorsForFormats(new long[] { format }, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * Returns a Map whose keys are all of the possible DataFlavors into which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * data in the specified formats can be translated. The value of each key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * is the format in which the Clipboard or dropped data should be requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * when converting to the DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    public Map getFlavorsForFormats(long[] formats, FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        Map flavorMap = new HashMap(formats.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        Set mappingSet = new HashSet(formats.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        Set flavorSet = new HashSet(formats.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        // First step: build flavorSet, mappingSet and initial flavorMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        // flavorSet  - the set of all the DataFlavors into which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        //              data in the specified formats can be translated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        // mappingSet - the set of all the mappings from the specified formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        //              into any DataFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        // flavorMap  - after this step, this map maps each of the DataFlavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        //              from flavorSet to any of the specified formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        for (int i = 0; i < formats.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            long format = formats[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            String nat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            List flavors = map.getFlavorsForNative(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            for (Iterator iter = flavors.iterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                DataFlavor flavor = (DataFlavor)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                // Don't explicitly test for String, since it is just a special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                // case of Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                if (flavor.isFlavorTextType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    flavor.isFlavorJavaFileListType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    DataFlavor.imageFlavor.equals(flavor) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    flavor.isRepresentationClassSerializable() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    flavor.isRepresentationClassInputStream() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    flavor.isRepresentationClassRemote())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    Long lFormat = Long.valueOf(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    Object mapping =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                        DataTransferer.createMapping(lFormat, flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    flavorMap.put(flavor, lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    mappingSet.add(mapping);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    flavorSet.add(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        // Second step: for each DataFlavor try to figure out which of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        // specified formats is the best to translate to this flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        // Then map each flavor to the best format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        // For the given flavor, FlavorTable indicates which native will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        // best reflect data in the specified flavor to the underlying native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        // platform. We assume that this native is the best to translate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        // to this flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        // Note: FlavorTable allows one-way mappings, so we can occasionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // map a flavor to the format for which the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        // format-to-flavor mapping doesn't exist. For this reason we have built
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        // a mappingSet of all format-to-flavor mappings for the specified formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        // and check if the format-to-flavor mapping exists for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        // (flavor,format) pair being added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        for (Iterator flavorIter = flavorSet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
             flavorIter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            DataFlavor flavor = (DataFlavor)flavorIter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            List natives = map.getNativesForFlavor(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            for (Iterator nativeIter = natives.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                 nativeIter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                Long lFormat =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    getFormatForNativeAsLong((String)nativeIter.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                Object mapping = DataTransferer.createMapping(lFormat, flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                if (mappingSet.contains(mapping)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    flavorMap.put(flavor, lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        return flavorMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * Returns a Set of all DataFlavors for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * 1) a mapping from at least one of the specified formats exists in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * specified map and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * 2) the data translation for this mapping can be performed by the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @param formats the data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @param map the FlavorTable which contains mappings between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *            DataFlavors and data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * @throws NullPointerException if formats or map is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    public Set getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        Set flavorSet = new HashSet(formats.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        for (int i = 0; i < formats.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            String nat = getNativeForFormat(formats[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            List flavors = map.getFlavorsForNative(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            for (Iterator iter = flavors.iterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                DataFlavor flavor = (DataFlavor)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                // Don't explicitly test for String, since it is just a special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                // case of Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                if (flavor.isFlavorTextType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    flavor.isFlavorJavaFileListType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    DataFlavor.imageFlavor.equals(flavor) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    flavor.isRepresentationClassSerializable() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    flavor.isRepresentationClassInputStream() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    flavor.isRepresentationClassRemote())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                    flavorSet.add(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return flavorSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Returns an array of all DataFlavors for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * 1) a mapping from the specified format exists in the specified map and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * 2) the data translation for this mapping can be performed by the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * The array will be sorted according to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * <code>DataFlavorComparator</code> created with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * map as an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @param format the data format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @param map the FlavorTable which contains mappings between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *            DataFlavors and data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @throws NullPointerException if map is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    public DataFlavor[] getFlavorsForFormatAsArray(long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                                                   FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        return getFlavorsForFormatsAsArray(new long[] { format }, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Returns an array of all DataFlavors for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * 1) a mapping from at least one of the specified formats exists in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * specified map and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * 2) the data translation for this mapping can be performed by the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * The array will be sorted according to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * <code>DataFlavorComparator</code> created with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * map as an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * @param formats the data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @param map the FlavorTable which contains mappings between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *            DataFlavors and data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @throws NullPointerException if formats or map is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    public DataFlavor[] getFlavorsForFormatsAsArray(long[] formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                                                    FlavorTable map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        // getFlavorsForFormatsAsSet() is less expensive than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        // getFlavorsForFormats().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        return setToSortedDataFlavorArray(getFlavorsForFormatsAsSet(formats, map));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * Returns an object that represents a mapping between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * key and value. <tt>null</tt> values and the <tt>null</tt> keys are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * permitted. The internal representation of the mapping object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * irrelevant. The only requrement is that the two mapping objects are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * if and only if their keys are equal and their values are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * More formally, the two mapping objects are equal if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * <tt>(value1 == null ? value2 == null : value1.equals(value2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * && (key1 == null ? key2 == null : key1.equals(key2))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    private static Object createMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        // NOTE: Should be updated to use AbstractMap.SimpleEntry as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        // soon as it is made public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        return Arrays.asList(new Object[] { key, value });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Looks-up or registers the String native with the native data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * system and returns a long format corresponding to that native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    protected abstract Long getFormatForNativeAsLong(String str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * Looks-up the String native corresponding to the specified long format in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * the native data transfer system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    protected abstract String getNativeForFormat(long format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /* Contains common code for finding the best charset for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * clipboard string encoding/decoding, basing on clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * format and localeTransferable(on decoding, if available)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    private String getBestCharsetForTextFormat(Long lFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        Transferable localeTransferable) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        String charset = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        if (localeTransferable != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            isLocaleDependentTextFormat(lFormat) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                charset = new String(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    (byte[])localeTransferable.getTransferData(javaTextEncodingFlavor),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    "UTF-8"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            } catch (UnsupportedFlavorException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            charset = getCharsetForTextFormat(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        if (charset == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            // Only happens when we have a custom text type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            charset = getDefaultTextCharset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        return charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *  Translation function for converting string into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *  a byte array. Search-and-replace EOLN. Encode into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *  target format. Append terminating NUL bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *  Java to Native string conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    private byte[] translateTransferableString(String str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                                               long format) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        Long lFormat = Long.valueOf(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        String charset = getBestCharsetForTextFormat(lFormat, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        // Search and replace EOLN. Note that if EOLN is "\n", then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        // never added an entry to nativeEOLNs anyway, so we'll skip this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        // code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        // windows: "abc\nde"->"abc\r\nde"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        String eoln = (String)nativeEOLNs.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (eoln != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            int length = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            StringBuffer buffer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                new StringBuffer(length * 2); // 2 is a heuristic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                // Fix for 4914613 - skip native EOLN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                if (str.startsWith(eoln, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                    buffer.append(eoln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    i += eoln.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                char c = str.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    buffer.append(eoln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                    buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            str = buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        // Encode text in target format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        byte[] bytes = str.getBytes(charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        // Append terminating NUL bytes. Note that if terminators is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        // the we never added an entry to nativeTerminators anyway, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        // we'll skip code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        // "abcde" -> "abcde\0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        Integer terminators = (Integer)nativeTerminators.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        if (terminators != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            int numTerminators = terminators.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            byte[] terminatedBytes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                new byte[bytes.length + numTerminators];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            System.arraycopy(bytes, 0, terminatedBytes, 0, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            for (int i = bytes.length; i < terminatedBytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                terminatedBytes[i] = 0x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            bytes = terminatedBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * Translating either a byte array or an InputStream into an String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Strip terminators and search-and-replace EOLN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * Native to Java string conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1071
    private String translateBytesToString(byte[] bytes, long format,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1072
                                          Transferable localeTransferable)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        Long lFormat = Long.valueOf(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        String charset = getBestCharsetForTextFormat(lFormat, localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        // Locate terminating NUL bytes. Note that if terminators is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        // the we never added an entry to nativeTerminators anyway, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        // we'll skip code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        // In other words: we are doing char alignment here basing on suggestion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        // that count of zero-'terminators' is a number of bytes in one symbol
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        // for selected charset (clipboard format). It is not complitly true for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        // multibyte coding like UTF-8, but helps understand the procedure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        // "abcde\0" -> "abcde"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        String eoln = (String)nativeEOLNs.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        Integer terminators = (Integer)nativeTerminators.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        if (terminators != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            int numTerminators = terminators.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
search:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            for (count = 0; count < (bytes.length - numTerminators + 1); count += numTerminators) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                for (int i = count; i < count + numTerminators; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                    if (bytes[i] != 0x0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                        continue search;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                // found terminators
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                break search;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            count = bytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        // Decode text to chars. Don't include any terminators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        String converted = new String(bytes, 0, count, charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        // Search and replace EOLN. Note that if EOLN is "\n", then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        // never added an entry to nativeEOLNs anyway, so we'll skip this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        // code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        // Count of NUL-terminators and EOLN coding are platform-specific and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        // loaded from flavormap.properties file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        // windows: "abc\r\nde" -> "abc\nde"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        if (eoln != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            /* Fix for 4463560: replace EOLNs symbol-by-symbol instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
             * of using buf.replace()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            char[] buf = converted.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            char[] eoln_arr = eoln.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            converted = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            for (int i = 0; i < buf.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                // Catch last few bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                if (i + eoln_arr.length > buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    buf[j++] = buf[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                for (int k = 0, l = i; k < eoln_arr.length; k++, l++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    if (eoln_arr[k] != buf[l]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                        match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    buf[j++] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                    i += eoln_arr.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                    buf[j++] = buf[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            converted = new String(buf, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        return converted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Primary translation function for translating a Transferable into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * a byte array, given a source DataFlavor and target format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    public byte[] translateTransferable(Transferable contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                                        DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                                        long format) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        // Obtain the transfer data in the source DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        // Note that we special case DataFlavor.plainTextFlavor because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        // StringSelection supports this flavor incorrectly -- instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        // returning an InputStream as the DataFlavor representation class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        // states, it returns a Reader. Instead of using this broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        // functionality, we request the data in stringFlavor (the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        // DataFlavor which StringSelection supports) and use the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        // translator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        boolean stringSelectionHack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            obj = contents.getTransferData(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            if (flavor.equals(DataFlavor.plainTextFlavor) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                !(obj instanceof InputStream))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                obj = contents.getTransferData(DataFlavor.stringFlavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                stringSelectionHack = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                stringSelectionHack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        } catch (UnsupportedFlavorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        // Source data is a String. Search-and-replace EOLN. Encode into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        // target format. Append terminating NUL bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        if (stringSelectionHack ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            (String.class.equals(flavor.getRepresentationClass()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
             isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1203
            String str = removeSuspectedData(flavor, contents, (String)obj);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1204
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            return translateTransferableString(
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1206
                str,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        // Source data is a Reader. Convert to a String and recur. In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        // future, we may want to rewrite this so that we encode on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        } else if (flavor.isRepresentationClassReader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    ("cannot transfer non-text data as Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            StringBuffer buf = new StringBuffer();
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1218
            try (Reader r = (Reader)obj) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1219
                int c;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1220
                while ((c = r.read()) != -1) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1221
                    buf.append((char)c);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1222
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                buf.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        // Source data is a CharBuffer. Convert to a String and recur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        } else if (flavor.isRepresentationClassCharBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    ("cannot transfer non-text data as CharBuffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            CharBuffer buffer = (CharBuffer)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            int size = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            char[] chars = new char[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            buffer.get(chars, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                new String(chars),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        // Source data is a char array. Convert to a String and recur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        } else if (charArrayClass.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                    ("cannot transfer non-text data as char array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                new String((char[])obj),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        // Source data is a ByteBuffer. For arbitrary flavors, simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        // the array. For text flavors, decode back to a String and recur to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        // reencode according to the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        } else if (flavor.isRepresentationClassByteBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            ByteBuffer buffer = (ByteBuffer)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            int size = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            byte[] bytes = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            buffer.get(bytes, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                String sourceEncoding = DataTransferer.getTextCharset(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    new String(bytes, sourceEncoding),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                    format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        // Source data is a byte array. For arbitrary flavors, simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        // the array. For text flavors, decode back to a String and recur to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        // reencode according to the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        } else if (byteArrayClass.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            byte[] bytes = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                String sourceEncoding = DataTransferer.getTextCharset(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    new String(bytes, sourceEncoding),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        // Source data is Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        } else if (DataFlavor.imageFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            if (!isImageFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                throw new IOException("Data translation failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                                      "not an image format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            Image image = (Image)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            byte[] bytes = imageToPlatformBytes(image, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                throw new IOException("Data translation failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                    "cannot convert java image to native format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1305
        byte[] theByteArray = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        // Target data is a file list. Source data must be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        // java.util.List which contains java.io.File or String instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        if (isFileFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            if (!DataFlavor.javaFileListFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1313
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            final List list = (List)obj;
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1315
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1316
            final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1317
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1318
            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
  1319
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1320
            try (ByteArrayOutputStream bos = convertFileListToBytes(fileList)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1321
                theByteArray = bos.toByteArray();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1322
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1323
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1324
        // 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
  1325
        // 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
  1326
        } else if (isURIListFormat(format)) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1327
            if (!DataFlavor.javaFileListFlavor.equals(flavor)) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1328
                throw new IOException("data translation failed");
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1329
            }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1330
            String nat = getNativeForFormat(format);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1331
            String targetCharset = null;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1332
            if (nat != null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1333
                try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1334
                    targetCharset = new DataFlavor(nat).getParameter("charset");
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1335
                } catch (ClassNotFoundException cnfe) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1336
                    throw new IOException(cnfe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1339
            if (targetCharset == null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1340
                targetCharset = "UTF-8";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1342
            final List list = (List)obj;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1343
            final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1344
            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
  1345
            final ArrayList<String> uriList = new ArrayList<String>(fileList.size());
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1346
            for (String fileObject : fileList) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1347
                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
  1348
                // 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
  1349
                try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1350
                    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
  1351
                } catch (URISyntaxException uriSyntaxException) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1352
                    throw new IOException(uriSyntaxException);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1353
                  }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1354
              }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1355
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1356
            byte[] eoln = "\r\n".getBytes(targetCharset);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1357
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1358
            try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1359
                for (int i = 0; i < uriList.size(); i++) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1360
                    byte[] bytes = uriList.get(i).getBytes(targetCharset);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1361
                    bos.write(bytes, 0, bytes.length);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1362
                    bos.write(eoln, 0, eoln.length);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1363
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1364
                theByteArray = bos.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        // Source data is an InputStream. For arbitrary flavors, just grab the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        // bytes and dump them into a byte array. For text flavors, decode back
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        // to a String and recur to reencode according to the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        } else if (flavor.isRepresentationClassInputStream()) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1371
            try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1372
                try (InputStream is = (InputStream)obj) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1373
                    boolean eof = false;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1374
                    int avail = is.available();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1375
                    byte[] tmp = new byte[avail > 8192 ? avail : 8192];
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1376
                    do {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1377
                        int aValue;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1378
                        if (!(eof = (aValue = is.read(tmp, 0, tmp.length)) == -1)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1379
                            bos.write(tmp, 0, aValue);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1380
                        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1381
                    } while (!eof);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1383
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1384
                if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1385
                    byte[] bytes = bos.toByteArray();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1386
                    String sourceEncoding = DataTransferer.getTextCharset(flavor);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1387
                    return translateTransferableString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1388
                               new String(bytes, sourceEncoding),
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1389
                               format);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1390
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1391
                theByteArray = bos.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1394
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1395
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        // Source data is an RMI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        } else if (flavor.isRepresentationClassRemote()) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1398
4267
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  1399
            Object mo = RMI.newMarshalledObject(obj);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1400
            theByteArray = convertObjectToBytes(mo);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1401
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1402
            // Source data is Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        } else if (flavor.isRepresentationClassSerializable()) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1404
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1405
            theByteArray = convertObjectToBytes(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1411
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1412
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1413
        return theByteArray;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1414
    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1415
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1416
    private static byte[] convertObjectToBytes(Object object) throws IOException {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1417
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1418
             ObjectOutputStream oos = new ObjectOutputStream(bos))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1419
        {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1420
            oos.writeObject(object);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1421
            return bos.toByteArray();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1422
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1425
    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
  1426
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1427
    private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1428
            throws IOException
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1429
    {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1430
        if (null == System.getSecurityManager()
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1431
            || !flavor.isMimeTypeEqual("text/uri-list"))
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1432
        {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1433
            return str;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1434
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1435
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1436
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1437
        String ret_val = "";
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1438
        final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1439
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1440
        try {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1441
            ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1442
                    public Object run() {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1443
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1444
                        StringBuffer allowedFiles = new StringBuffer(str.length());
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1445
                        String [] uriArray = str.split("(\\s)+");
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1446
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1447
                        for (String fileName : uriArray)
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1448
                        {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1449
                            File file = new File(fileName);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1450
                            if (file.exists() &&
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1451
                                !(isFileInWebstartedCache(file) ||
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1452
                                isForbiddenToRead(file, userProtectionDomain)))
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1453
                            {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1454
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1455
                                if (0 != allowedFiles.length())
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1456
                                {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1457
                                    allowedFiles.append("\\r\\n");
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1458
                                }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1459
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1460
                                allowedFiles.append(fileName);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1461
                            }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1462
                        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1463
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1464
                        return allowedFiles.toString();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1465
                    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1466
                });
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1467
        } catch (PrivilegedActionException pae) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1468
            throw new IOException(pae.getMessage(), pae);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1469
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1470
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1471
        return ret_val;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1472
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1473
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1474
    private static ProtectionDomain getUserProtectionDomain(Transferable contents) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1475
        return contents.getClass().getProtectionDomain();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1476
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1477
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1478
    private boolean isForbiddenToRead (File file, ProtectionDomain protectionDomain)
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1479
    {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1480
        if (null == protectionDomain) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1481
            return false;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1482
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1483
        try {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1484
            FilePermission filePermission =
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1485
                    new FilePermission(file.getCanonicalPath(), "read, delete");
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1486
            if (protectionDomain.implies(filePermission)) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1487
                return false;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1488
            }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1489
        } catch (IOException e) {}
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1490
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1491
        return true;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1492
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1493
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1494
    private ArrayList<String> castToFiles(final List files,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1495
                                          final ProtectionDomain userProtectionDomain) throws IOException
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1496
    {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1497
        final ArrayList<String> fileList = new ArrayList<String>();
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1498
        try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1499
            AccessController.doPrivileged(new PrivilegedExceptionAction() {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1500
                public Object run() throws IOException {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1501
                    for (Object fileObject : files)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1502
                    {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1503
                        File file = castToFile(fileObject);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1504
                        if (file != null &&
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1505
                            (null == System.getSecurityManager() ||
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1506
                            !(isFileInWebstartedCache(file) ||
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1507
                            isForbiddenToRead(file, userProtectionDomain))))
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1508
                        {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1509
                            fileList.add(file.getCanonicalPath());
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1510
                        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1511
                    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1512
                    return null;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1513
                }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1514
            });
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1515
        } catch (PrivilegedActionException pae) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1516
            throw new IOException(pae.getMessage());
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1517
        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1518
        return fileList;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1519
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1520
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1521
    // It is important do not use user's successors
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1522
    // of File class.
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1523
    private File castToFile(Object fileObject) throws IOException {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1524
        String filePath = null;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1525
        if (fileObject instanceof File) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1526
            filePath = ((File)fileObject).getCanonicalPath();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1527
        } else if (fileObject instanceof String) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1528
           filePath = (String) fileObject;
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1529
        } else {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1530
           return null;
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1531
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1532
        return new File(filePath);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1533
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1534
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1535
    private final static String[] DEPLOYMENT_CACHE_PROPERTIES = {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1536
        "deployment.system.cachedir",
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1537
        "deployment.user.cachedir",
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1538
        "deployment.javaws.cachedir",
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1539
        "deployment.javapi.cachedir"
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1540
    };
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1541
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1542
    private final static ArrayList <File> deploymentCacheDirectoryList =
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1543
            new ArrayList<File>();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1544
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1545
    private static boolean isFileInWebstartedCache(File f) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1546
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1547
        if (deploymentCacheDirectoryList.isEmpty()) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1548
            for (String cacheDirectoryProperty : DEPLOYMENT_CACHE_PROPERTIES) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1549
                String cacheDirectoryPath = System.getProperty(cacheDirectoryProperty);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1550
                if (cacheDirectoryPath != null) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1551
                    try {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1552
                        File cacheDirectory = (new File(cacheDirectoryPath)).getCanonicalFile();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1553
                        if (cacheDirectory != null) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1554
                            deploymentCacheDirectoryList.add(cacheDirectory);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1555
                        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1556
                    } catch (IOException ioe) {}
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1557
                }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1558
            }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1559
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1560
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1561
        for (File deploymentCacheDirectory : deploymentCacheDirectoryList) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1562
            for (File dir = f; dir != null; dir = dir.getParentFile()) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1563
                if (dir.equals(deploymentCacheDirectory)) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1564
                    return true;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1565
                }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1566
            }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1567
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1568
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1569
        return false;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1570
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1571
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1572
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
    public Object translateBytes(byte[] bytes, DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                                 long format, Transferable localeTransferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1577
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1578
        Object theObject = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        // Source data is a file list. Use the dragQueryFile native function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        // do most of the decoding. Then wrap File objects around the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        // filenames and return a List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        if (isFileFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            if (!DataFlavor.javaFileListFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            String[] filenames = dragQueryFile(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            if (filenames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            // Convert the strings to File objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            File[] files = new File[filenames.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            for (int i = 0; i < filenames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                files[i] = new File(filenames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            // 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
  1599
            theObject = Arrays.asList(files);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1600
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1601
            // 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
  1602
            // into characters. Search-and-replace EOLN.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        } else if (String.class.equals(flavor.getRepresentationClass()) &&
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1604
                       isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1605
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1606
            theObject = translateBytesToString(bytes, format, localeTransferable);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1607
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1608
            // 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
  1609
            // 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
  1610
            // back to chars on demand.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        } else if (flavor.isRepresentationClassReader()) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1612
            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1613
                theObject = translateStream(bais,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1614
                        flavor, format, localeTransferable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1616
            // Target data is a CharBuffer. Recur to obtain String and wrap.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        } else if (flavor.isRepresentationClassCharBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                throw new IOException
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1620
                          ("cannot transfer non-text data as CharBuffer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1623
            CharBuffer buffer = CharBuffer.wrap(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1624
                translateBytesToString(bytes,format, localeTransferable));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1625
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1626
            theObject = constructFlavoredObject(buffer, flavor, CharBuffer.class);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1627
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1628
            // 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
  1629
            // char array.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        } else if (charArrayClass.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                throw new IOException
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1633
                          ("cannot transfer non-text data as char array");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1636
            theObject = translateBytesToString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1637
                bytes, format, localeTransferable).toCharArray();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1638
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1639
            // 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
  1640
            // 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
  1641
            // 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
  1642
            // the requested flavor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        } else if (flavor.isRepresentationClassByteBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1645
                bytes = translateBytesToString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1646
                    bytes, format, localeTransferable).getBytes(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1647
                        DataTransferer.getTextCharset(flavor)
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            ByteBuffer buffer = ByteBuffer.wrap(bytes);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1652
            theObject = constructFlavoredObject(buffer, flavor, ByteBuffer.class);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1653
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1654
            // 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
  1655
            // 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
  1656
            // 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
  1657
            // the requested flavor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        } else if (byteArrayClass.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1660
                theObject = translateBytesToString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1661
                    bytes, format, localeTransferable
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1662
                ).getBytes(DataTransferer.getTextCharset(flavor));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            } else {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1664
                theObject = bytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1667
            // 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
  1668
            // 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
  1669
            // 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
  1670
            // flavor.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1671
        } else if (flavor.isRepresentationClassInputStream()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1672
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1673
            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1674
                theObject = translateStream(bais, flavor, format, localeTransferable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1677
            // Target data is Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        } else if (flavor.isRepresentationClassSerializable()) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1679
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1680
            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1681
                theObject = translateStream(bais, flavor, format, localeTransferable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1684
            // Target data is Image
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        } else if (DataFlavor.imageFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            if (!isImageFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1690
            theObject = platformImageBytesToImage(bytes, format);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1691
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1692
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1693
        if (theObject == null) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1694
            throw new IOException("data translation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1697
        return theObject;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1698
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1699
    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1700
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1701
    /**
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1702
     * Primary translation function for translating
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1703
     * 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
  1704
     * DataFlavor.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1705
     */
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1706
    public Object translateStream(InputStream str, DataFlavor flavor,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1707
                                  long format, Transferable localeTransferable)
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1708
        throws IOException
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1709
    {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1710
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1711
        Object theObject = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1712
        // 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
  1713
        // where possible.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1714
        if (isURIListFormat(format)
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1715
                && DataFlavor.javaFileListFlavor.equals(flavor))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1716
        {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1717
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1718
            URI uris[] = dragQueryURIs(str, format, localeTransferable);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1719
            if (uris == null) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1720
                return null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1721
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1722
            ArrayList files = new ArrayList();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1723
            for (URI uri : uris) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1724
                try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1725
                    files.add(new File(uri));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1726
                } catch (IllegalArgumentException illegalArg) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1727
                    // When converting from URIs to less generic files,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1728
                    // common practice (Wine, SWT) seems to be to
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1729
                    // 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
  1730
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1731
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1732
            theObject = files;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1733
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1734
            // 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
  1735
            // 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
  1736
            // Recur to obtain String and encapsulate.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1737
        } else if (DataFlavor.plainTextFlavor.equals(flavor)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1738
            theObject = new StringReader(translateBytesToString(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1739
                inputStreamToByteArray(str),
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1740
                format, localeTransferable));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1741
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1742
            // 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
  1743
            // 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
  1744
            // 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
  1745
            // flavor.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1746
        } else if (flavor.isRepresentationClassInputStream()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1747
            theObject = translateStreamToInputStream(str, flavor, format,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1748
                                                               localeTransferable);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1749
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1750
            // 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
  1751
            // 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
  1752
            // back to chars on demand.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1753
        } else if (flavor.isRepresentationClassReader()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1754
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1755
                throw new IOException
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1756
                          ("cannot transfer non-text data as Reader");
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1757
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1758
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1759
            InputStream is = (InputStream)translateStreamToInputStream(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1760
                    str, DataFlavor.plainTextFlavor,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1761
                    format, localeTransferable);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1762
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1763
            String unicode = DataTransferer.getTextCharset(DataFlavor.plainTextFlavor);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1764
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1765
            Reader reader = new InputStreamReader(is, unicode);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1766
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1767
            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
  1768
            // Target data is a byte array
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1769
        } else if (byteArrayClass.equals(flavor.getRepresentationClass())) {
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1770
            if(isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1771
                theObject = translateBytesToString(inputStreamToByteArray(str), format, localeTransferable)
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1772
                        .getBytes(DataTransferer.getTextCharset(flavor));
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1773
            } else {
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1774
                theObject = inputStreamToByteArray(str);
25a1c15eea88 8015453: java/awt/DataFlavor/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug.java failed on windows with jdk8 since b86
pchelko
parents: 19364
diff changeset
  1775
            }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1776
            // Target data is an RMI object
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1777
        } else if (flavor.isRepresentationClassRemote()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1778
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1779
            try (ObjectInputStream ois =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1780
                     new ObjectInputStream(str))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1781
            {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1782
                theObject = RMI.getMarshalledObject(ois.readObject());
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1783
            }catch (Exception e) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1784
                throw new IOException(e.getMessage());
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1785
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1786
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1787
            // Target data is Serializable
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1788
        } else if (flavor.isRepresentationClassSerializable()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1789
            try (ObjectInputStream ois =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1790
                     new ObjectInputStream(str))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1791
            {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1792
                theObject = ois.readObject();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1793
            } catch (Exception e) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1794
                throw new IOException(e.getMessage());
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1795
            }
20431
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1796
            // Target data is Image
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1797
        } else if (DataFlavor.imageFlavor.equals(flavor)) {
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1798
            if (!isImageFormat(format)) {
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1799
                throw new IOException("data translation failed");
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1800
            }
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1801
            theObject = platformImageBytesToImage(inputStreamToByteArray(str), format);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1802
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1803
20431
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1804
        if (theObject == null) {
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1805
            throw new IOException("data translation failed");
3845c63e933d 8016563: Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 20128
diff changeset
  1806
        }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1807
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1808
        return theObject;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1809
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * For arbitrary flavors, just use the raw InputStream. For text flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * ReencodingInputStream will decode and reencode the InputStream on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * so that we can strip terminators and search-and-replace EOLN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  1817
    private Object translateStreamToInputStream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        (InputStream str, DataFlavor flavor, long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
         Transferable localeTransferable) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            str = new ReencodingInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                (str, format, DataTransferer.getTextCharset(flavor),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                 localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        return constructFlavoredObject(str, flavor, InputStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * We support representations which are exactly of the specified Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * and also arbitrary Objects which have a constructor which takes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * instance of the Class as its sole parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
    private Object constructFlavoredObject(Object arg, DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                                           Class clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        final Class dfrc = flavor.getRepresentationClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        if (clazz.equals(dfrc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            return arg; // simple case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            Constructor[] constructors = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                constructors = (Constructor[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                    AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                                return dfrc.getConstructors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                throw new IOException(se.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            Constructor constructor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            for (int j = 0; j < constructors.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                if (!Modifier.isPublic(constructors[j].getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                Class[] ptypes = constructors[j].getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                if (ptypes != null && ptypes.length == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                    clazz.equals(ptypes[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                    constructor = constructors[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            if (constructor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                throw new IOException("can't find <init>(L"+ clazz +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                                      ";)V for class: " + dfrc.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                return constructor.newInstance(new Object[] { arg } );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * Used for decoding and reencoding an InputStream on demand so that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * can strip NUL terminators and perform EOLN search-and-replace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    public class ReencodingInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        protected BufferedReader wrapped;
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1892
        protected final char[] in = new char[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        protected byte[] out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        protected CharsetEncoder encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        protected CharBuffer inBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        protected ByteBuffer outBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        protected char[] eoln;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        protected int numTerminators;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        protected boolean eos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        protected int index, limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        public ReencodingInputStream(InputStream bytestream, long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                                     String targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                                     Transferable localeTransferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            Long lFormat = Long.valueOf(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            String sourceEncoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            if (isLocaleDependentTextFormat(format) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                localeTransferable != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                localeTransferable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                    isDataFlavorSupported(javaTextEncodingFlavor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                    sourceEncoding = new String((byte[])localeTransferable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                                       getTransferData(javaTextEncodingFlavor),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                                       "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                } catch (UnsupportedFlavorException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                sourceEncoding = getCharsetForTextFormat(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            if (sourceEncoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                // Only happens when we have a custom text type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                sourceEncoding = getDefaultTextCharset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            wrapped = new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                (new InputStreamReader(bytestream, sourceEncoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            if (targetEncoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                // Throw NullPointerException for compatibility with the former
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                // call to sun.io.CharToByteConverter.getConverter(null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                // (Charset.forName(null) throws unspecified IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                // now; see 6228568)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                throw new NullPointerException("null target encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                encoder = Charset.forName(targetEncoding).newEncoder();
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1945
                out = new byte[(int)(encoder.maxBytesPerChar() * 2 + 0.5)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                inBuf = CharBuffer.wrap(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                outBuf = ByteBuffer.wrap(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            } catch (IllegalCharsetNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                throw new IOException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            } catch (UnsupportedCharsetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                throw new IOException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            } catch (UnsupportedOperationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                throw new IOException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            String sEoln = (String)nativeEOLNs.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            if (sEoln != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                eoln = sEoln.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            // A hope and a prayer that this works generically. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            // definitely work on Win32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            Integer terminators = (Integer)nativeTerminators.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            if (terminators != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                numTerminators = terminators.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1969
        private int readChar() throws IOException {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1970
            int c = wrapped.read();
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1971
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1972
            if (c == -1) { // -1 is EOS
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1973
                eos = true;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1974
                return -1;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1975
            }
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1976
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1977
            // "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
  1978
            if (numTerminators > 0 && c == 0) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1979
                eos = true;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1980
                return -1;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1981
            } else if (eoln != null && matchCharArray(eoln, c)) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1982
                c = '\n' & 0xFFFF;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1983
            }
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1984
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1985
            return c;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1986
        }
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1987
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            if (eos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            if (index >= limit) {
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1994
                // deal with supplementary characters
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1995
                int c = readChar();
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1996
                if (c == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2000
                in[0] = (char) c;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2001
                in[1] = 0;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2002
                inBuf.limit(1);
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2003
                if (Character.isHighSurrogate((char) c)) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2004
                    c = readChar();
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2005
                    if (c != -1) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2006
                        in[1] = (char) c;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2007
                        inBuf.limit(2);
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2008
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                inBuf.rewind();
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  2012
                outBuf.limit(out.length).rewind();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                encoder.encode(inBuf, outBuf, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                outBuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                limit = outBuf.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                return read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                return out[index++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            return ((eos) ? 0 : (limit - index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            wrapped.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
         * Checks to see if the next array.length characters in wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
         * match array. The first character is provided as c. Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
         * characters are read from wrapped itself. When this method returns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
         * the wrapped index may be different from what it was when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
         * method was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        private boolean matchCharArray(char[] array, int c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            wrapped.mark(array.length);  // BufferedReader supports mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            if ((char)c == array[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                for (count = 1; count < array.length; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                    c = wrapped.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                    if (c == -1 || ((char)c) != array[count]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
            if (count == array.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                wrapped.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * Decodes a byte array into a set of String filenames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    protected abstract String[] dragQueryFile(byte[] bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    /**
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2070
     * 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
  2071
     */
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2072
    protected URI[] dragQueryURIs(InputStream stream,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2073
                                  long format,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2074
                                  Transferable localeTransferable)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2075
      throws IOException
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2076
    {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2077
        throw new IOException(
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2078
            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
  2079
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2080
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2081
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * Translates either a byte array or an input stream which contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * platform-specific image data in the given format into an Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2085
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2086
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2087
    protected abstract Image platformImageBytesToImage(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2088
        byte[] bytes,long format) throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * Translates either a byte array or an input stream which contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * an image data in the given standard format into an Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * @param mimeType image MIME type, such as: image/png, image/jpeg, image/gif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2096
    protected Image standardImageBytesToImage(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2097
        byte[] bytes, String mimeType) throws IOException
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2098
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        Iterator readerIterator = ImageIO.getImageReadersByMIMEType(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        if (!readerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            throw new IOException("No registered service provider can decode " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                                  " an image from " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        IOException ioe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        while (readerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            ImageReader imageReader = (ImageReader)readerIterator.next();
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2111
            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                ImageInputStream imageInputStream =
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2113
                    ImageIO.createImageInputStream(bais);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                    ImageReadParam param = imageReader.getDefaultReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                    imageReader.setInput(imageInputStream, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                    BufferedImage bufferedImage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                        imageReader.read(imageReader.getMinIndex(), param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                    if (bufferedImage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                        return bufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                    imageInputStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                    imageReader.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                ioe = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        if (ioe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            ioe = new IOException("Registered service providers failed to decode"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                                  + " an image from " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * Translates a Java Image into a byte array which contains platform-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * specific image data in the given format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
    protected abstract byte[] imageToPlatformBytes(Image image, long format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
      throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * Translates a Java Image into a byte array which contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * an image data in the given standard format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * @param mimeType image MIME type, such as: image/png, image/jpeg
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
    protected byte[] imageToStandardBytes(Image image, String mimeType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        IOException originalIOE = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        if (!writerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            throw new IOException("No registered service provider can encode " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                                  " an image to " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        if (image instanceof RenderedImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            // Try to encode the original image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                return imageToStandardBytesImpl((RenderedImage)image, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                originalIOE = ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        // Retry with a BufferedImage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        if (image instanceof ToolkitImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
            ir.reconstruct(ImageObserver.ALLBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
            width = ir.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            height = ir.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
            width = image.getWidth(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
            height = image.getHeight(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        ColorModel model = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        WritableRaster raster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            model.createCompatibleWritableRaster(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        BufferedImage bufferedImage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
            new BufferedImage(model, raster, model.isAlphaPremultiplied(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                              null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        Graphics g = bufferedImage.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
            g.drawImage(image, 0, 0, width, height, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            return imageToStandardBytesImpl(bufferedImage, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            if (originalIOE != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                throw originalIOE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    protected byte[] imageToStandardBytesImpl(RenderedImage renderedImage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                                              String mimeType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        ImageTypeSpecifier typeSpecifier =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            new ImageTypeSpecifier(renderedImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        IOException ioe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        while (writerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            ImageWriter imageWriter = (ImageWriter)writerIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            if (!writerSpi.canEncodeImage(typeSpecifier)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                ImageOutputStream imageOutputStream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                    ImageIO.createImageOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                    imageWriter.setOutput(imageOutputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                    imageWriter.write(renderedImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                    imageOutputStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                    imageOutputStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                imageWriter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                ioe = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            imageWriter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            baos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        baos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        if (ioe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            ioe = new IOException("Registered service providers failed to encode "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                                  + renderedImage + " to " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * Concatenates the data represented by two objects. Objects can be either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     * byte arrays or instances of <code>InputStream</code>. If both arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     * are byte arrays byte array will be returned. Otherwise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     * <code>InputStream</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     * Currently is only called from native code to prepend palette data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
     * platform-specific image data during image transfer on Win32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     * @param obj1 the first object to be concatenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     * @param obj2 the second object to be concatenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * @return a byte array or an <code>InputStream</code> which represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     *         a logical concatenation of the two arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     * @throws NullPointerException is either of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     *         <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * @throws ClassCastException is either of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     *         neither byte array nor an instance of <code>InputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
    private Object concatData(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        InputStream str1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        InputStream str2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        if (obj1 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            byte[] arr1 = (byte[])obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            if (obj2 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                byte[] arr2 = (byte[])obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                byte[] ret = new byte[arr1.length + arr2.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                System.arraycopy(arr1, 0, ret, 0, arr1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                System.arraycopy(arr2, 0, ret, arr1.length, arr2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                str1 = new ByteArrayInputStream(arr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                str2 = (InputStream)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            str1 = (InputStream)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            if (obj2 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                str2 = new ByteArrayInputStream((byte[])obj2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                str2 = (InputStream)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        return new SequenceInputStream(str1, str2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
    public byte[] convertData(final Object source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                              final Transferable contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                              final long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                              final Map formatMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                              final boolean isToolkitThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
        byte[] ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
         * If the current thread is the Toolkit thread we should post a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
         * Runnable to the event dispatch thread associated with source Object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
         * since translateTransferable() calls Transferable.getTransferData()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
         * that may contain client code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        if (isToolkitThread) try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            final Stack stack = new Stack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            final Runnable dataConverter = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                // Guard against multiple executions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                private boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                    if (done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                    byte[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                        DataFlavor flavor = (DataFlavor)formatMap.get(Long.valueOf(format));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                        if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                            data = translateTransferable(contents, flavor, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                        data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                        getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                        stack.push(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                        getToolkitThreadBlockedHandler().exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                        getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            final AppContext appContext = SunToolkit.targetToAppContext(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
            getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            if (appContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                appContext.put(DATA_CONVERTER_KEY, dataConverter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
            SunToolkit.executeOnEventHandlerThread(source, dataConverter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
            while (stack.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                getToolkitThreadBlockedHandler().enter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            if (appContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                appContext.remove(DATA_CONVERTER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            ret = (byte[])stack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
            DataFlavor flavor = (DataFlavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                formatMap.get(Long.valueOf(format));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                ret = translateTransferable(contents, flavor, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
    public void processDataConversionRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            AppContext appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
            getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                Runnable dataConverter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                    (Runnable)appContext.get(DATA_CONVERTER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                if (dataConverter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                    dataConverter.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                    appContext.remove(DATA_CONVERTER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
                getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
    public abstract ToolkitThreadBlockedHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        getToolkitThreadBlockedHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * Helper function to reduce a Map with Long keys to a long array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * The map keys are sorted according to the native formats preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
    public static long[] keysToLongArray(SortedMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        Set keySet = map.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        long[] retval = new long[keySet.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        for (Iterator iter = keySet.iterator(); iter.hasNext(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            retval[i] = ((Long)iter.next()).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * Helper function to convert a Set of DataFlavors to a sorted array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * The array will be sorted according to <code>DataFlavorComparator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
    public static DataFlavor[] setToSortedDataFlavorArray(Set flavorsSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        DataFlavor[] flavors = new DataFlavor[flavorsSet.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        flavorsSet.toArray(flavors);
9484
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
  2431
        final Comparator comparator =
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
  2432
                new DataFlavorComparator(IndexedComparator.SELECT_WORST);
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
  2433
        Arrays.sort(flavors, comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     * Helper function to convert an InputStream to a byte[] array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
    protected static byte[] inputStreamToByteArray(InputStream str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
    {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2443
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2444
            int len = 0;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2445
            byte[] buf = new byte[8192];
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2446
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2447
            while ((len = str.read(buf)) != -1) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2448
                baos.write(buf, 0, len);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2449
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2450
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15993
diff changeset
  2451
            return baos.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * Returns platform-specific mappings for the specified native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     * If there are no platform-specific mappings for this native, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * returns an empty <code>List</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
    public List getPlatformMappingsForNative(String nat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        return new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     * Returns platform-specific mappings for the specified flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     * If there are no platform-specific mappings for this flavor, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * returns an empty <code>List</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
    public List getPlatformMappingsForFlavor(DataFlavor df) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        return new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     * A Comparator which includes a helper function for comparing two Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     * which are likely to be keys in the specified Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
    public abstract static class IndexedComparator implements Comparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
         * The best Object (e.g., DataFlavor) will be the last in sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
        public static final boolean SELECT_BEST = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
         * The best Object (e.g., DataFlavor) will be the first in sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        public static final boolean SELECT_WORST = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
        protected final boolean order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        public IndexedComparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            this(SELECT_BEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        public IndexedComparator(boolean order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            this.order = order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
         * Helper method to compare two objects by their Integer indices in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
         * given map. If the map doesn't contain an entry for either of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
         * objects, the fallback index will be used for the object instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
         * @param indexMap the map which maps objects into Integer indexes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
         * @param obj1 the first object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
         * @param obj2 the second object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
         * @param fallbackIndex the Integer to be used as a fallback index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
         * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
         *             first object is mapped to a less, equal to, or greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
         *             index than the second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
        protected static int compareIndices(Map indexMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                                            Object obj1, Object obj2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                                            Integer fallbackIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
            Integer index1 = (Integer)indexMap.get(obj1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            Integer index2 = (Integer)indexMap.get(obj2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
            if (index1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                index1 = fallbackIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            if (index2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                index2 = fallbackIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
            return index1.compareTo(index2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
         * Helper method to compare two objects by their Long indices in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
         * given map. If the map doesn't contain an entry for either of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
         * objects, the fallback index will be used for the object instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
         * @param indexMap the map which maps objects into Long indexes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
         * @param obj1 the first object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
         * @param obj2 the second object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
         * @param fallbackIndex the Long to be used as a fallback index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
         * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
         *             first object is mapped to a less, equal to, or greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
         *             index than the second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        protected static int compareLongs(Map indexMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                                          Object obj1, Object obj2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                                          Long fallbackIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            Long index1 = (Long)indexMap.get(obj1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
            Long index2 = (Long)indexMap.get(obj2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            if (index1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                index1 = fallbackIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            if (index2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                index2 = fallbackIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
            return index1.compareTo(index2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
     * An IndexedComparator which compares two String charsets. The comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     * follows the rules outlined in DataFlavor.selectBestTextFlavor. In order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
     * to ensure that non-Unicode, non-ASCII, non-default charsets are sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     * in alphabetical order, charsets are not automatically converted to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     * canonical forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
    public static class CharsetComparator extends IndexedComparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        private static final Map charsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
        private static String defaultEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
        private static final Integer DEFAULT_CHARSET_INDEX = Integer.valueOf(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
        private static final Integer OTHER_CHARSET_INDEX = Integer.valueOf(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        private static final Integer WORST_CHARSET_INDEX = Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        private static final Integer UNSUPPORTED_CHARSET_INDEX =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            Integer.valueOf(Integer.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        private static final String UNSUPPORTED_CHARSET = "UNSUPPORTED";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
            HashMap charsetsMap = new HashMap(8, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
            // we prefer Unicode charsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
            charsetsMap.put(canonicalName("UTF-16LE"), Integer.valueOf(4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            charsetsMap.put(canonicalName("UTF-16BE"), Integer.valueOf(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
            charsetsMap.put(canonicalName("UTF-8"), Integer.valueOf(6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
            charsetsMap.put(canonicalName("UTF-16"), Integer.valueOf(7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
            // US-ASCII is the worst charset supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            charsetsMap.put(canonicalName("US-ASCII"), WORST_CHARSET_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            String defEncoding = DataTransferer.canonicalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                (DataTransferer.getDefaultTextCharset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            if (charsetsMap.get(defaultEncoding) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                charsetsMap.put(defaultEncoding, DEFAULT_CHARSET_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
            charsetsMap.put(UNSUPPORTED_CHARSET, UNSUPPORTED_CHARSET_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
            charsets = Collections.unmodifiableMap(charsetsMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
        public CharsetComparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            this(SELECT_BEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        public CharsetComparator(boolean order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            super(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
         * Compares two String objects. Returns a negative integer, zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
         * or a positive integer as the first charset is worse than, equal to,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
         * or better than the second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
         * @param obj1 the first charset to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
         * @param obj2 the second charset to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
         * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
         *         first argument is worse, equal to, or better than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
         *         second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
         * @throws ClassCastException if either of the arguments is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
         *         instance of String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
         * @throws NullPointerException if either of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
         *         <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        public int compare(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            String charset1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
            String charset2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
            if (order == SELECT_BEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                charset1 = (String)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                charset2 = (String)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
                charset1 = (String)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
                charset2 = (String)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            return compareCharsets(charset1, charset2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
         * Compares charsets. Returns a negative integer, zero, or a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
         * integer as the first charset is worse than, equal to, or better than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
         * the second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
         * Charsets are ordered according to the following rules:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
         * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
         * <li>All unsupported charsets are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
         * <li>Any unsupported charset is worse than any supported charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
         * <li>Unicode charsets, such as "UTF-16", "UTF-8", "UTF-16BE" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
         *     "UTF-16LE", are considered best.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
         * <li>After them, platform default charset is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
         * <li>"US-ASCII" is the worst of supported charsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
         * <li>For all other supported charsets, the lexicographically less
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
         *     one is considered the better.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
         * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
         * @param charset1 the first charset to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
         * @param charset2 the second charset to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
         * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
         *             first argument is worse, equal to, or better than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
         *             second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
        protected int compareCharsets(String charset1, String charset2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
            charset1 = getEncoding(charset1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
            charset2 = getEncoding(charset2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
            int comp = compareIndices(charsets, charset1, charset2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                                      OTHER_CHARSET_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
            if (comp == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                return charset2.compareTo(charset1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
         * Returns encoding for the specified charset according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
         * following rules:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
         * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
         * <li>If the charset is <code>null</code>, then <code>null</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
         *     be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
         * <li>Iff the charset specifies an encoding unsupported by this JRE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
         *     <code>UNSUPPORTED_CHARSET</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
         * <li>If the charset specifies an alias name, the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
         *     canonical name will be returned iff the charset is a known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
         *     Unicode, ASCII, or default charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
         * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
         * @param charset the charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
         * @return an encoding for this charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
        protected static String getEncoding(String charset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
            if (charset == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
            } else if (!DataTransferer.isEncodingSupported(charset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                return UNSUPPORTED_CHARSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                // Only convert to canonical form if the charset is one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                // of the charsets explicitly listed in the known charsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                // map. This will happen only for Unicode, ASCII, or default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                // charsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                String canonicalName = DataTransferer.canonicalName(charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                return (charsets.containsKey(canonicalName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
                    ? canonicalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                    : charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
     * An IndexedComparator which compares two DataFlavors. For text flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
     * the comparison follows the rules outlined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
     * DataFlavor.selectBestTextFlavor. For non-text flavors, unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
     * application MIME types are preferred, followed by known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     * application/x-java-* MIME types. Unknown application types are preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     * because if the user provides his own data flavor, it will likely be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
     * most descriptive one. For flavors which are otherwise equal, the
19364
f6324eb18ece 7173464: Clipboard.getAvailableDataFlavors: Comparison method violates contract
pchelko
parents: 18178
diff changeset
  2716
     * flavors' string representation are compared in the alphabetical order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
    public static class DataFlavorComparator extends IndexedComparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        private final CharsetComparator charsetComparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
        private static final Map exactTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
        private static final Map primaryTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
        private static final Map nonTextRepresentations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
        private static final Map textTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
        private static final Map decodedTextRepresentations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
        private static final Map encodedTextRepresentations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
        private static final Integer UNKNOWN_OBJECT_LOSES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
            Integer.valueOf(Integer.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
        private static final Integer UNKNOWN_OBJECT_WINS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
            Integer.valueOf(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
        private static final Long UNKNOWN_OBJECT_LOSES_L =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
            Long.valueOf(Long.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
        private static final Long UNKNOWN_OBJECT_WINS_L =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
            Long.valueOf(Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                HashMap exactTypesMap = new HashMap(4, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                // application/x-java-* MIME types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
                exactTypesMap.put("application/x-java-file-list",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
                                  Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
                exactTypesMap.put("application/x-java-serialized-object",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                                  Integer.valueOf(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                exactTypesMap.put("application/x-java-jvm-local-objectref",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                                  Integer.valueOf(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                exactTypesMap.put("application/x-java-remote-object",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                                  Integer.valueOf(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                exactTypes = Collections.unmodifiableMap(exactTypesMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                HashMap primaryTypesMap = new HashMap(1, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                primaryTypesMap.put("application", Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                primaryTypes = Collections.unmodifiableMap(primaryTypesMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                HashMap nonTextRepresentationsMap = new HashMap(3, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                nonTextRepresentationsMap.put(java.io.InputStream.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                                              Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                nonTextRepresentationsMap.put(java.io.Serializable.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                                              Integer.valueOf(1));
4267
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2771
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2772
                Class<?> remoteClass = RMI.remoteClass();
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2773
                if (remoteClass != null) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2774
                    nonTextRepresentationsMap.put(remoteClass,
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2775
                                                  Integer.valueOf(2));
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2776
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
                nonTextRepresentations =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                    Collections.unmodifiableMap(nonTextRepresentationsMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                HashMap textTypesMap = new HashMap(16, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                // plain text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                textTypesMap.put("text/plain", Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                // stringFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                textTypesMap.put("application/x-java-serialized-object",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
                                Integer.valueOf(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                // misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                textTypesMap.put("text/calendar", Integer.valueOf(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                textTypesMap.put("text/css", Integer.valueOf(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                textTypesMap.put("text/directory", Integer.valueOf(4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                textTypesMap.put("text/parityfec", Integer.valueOf(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                textTypesMap.put("text/rfc822-headers", Integer.valueOf(6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                textTypesMap.put("text/t140", Integer.valueOf(7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                textTypesMap.put("text/tab-separated-values", Integer.valueOf(8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                textTypesMap.put("text/uri-list", Integer.valueOf(9));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
                // enriched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
                textTypesMap.put("text/richtext", Integer.valueOf(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                textTypesMap.put("text/enriched", Integer.valueOf(11));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                textTypesMap.put("text/rtf", Integer.valueOf(12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                // markup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
                textTypesMap.put("text/html", Integer.valueOf(13));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                textTypesMap.put("text/xml", Integer.valueOf(14));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                textTypesMap.put("text/sgml", Integer.valueOf(15));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                textTypes = Collections.unmodifiableMap(textTypesMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                HashMap decodedTextRepresentationsMap = new HashMap(4, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                decodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                    (DataTransferer.charArrayClass, Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                decodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                    (java.nio.CharBuffer.class, Integer.valueOf(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                decodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
                    (java.lang.String.class, Integer.valueOf(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
                decodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                    (java.io.Reader.class, Integer.valueOf(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                decodedTextRepresentations =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
                    Collections.unmodifiableMap(decodedTextRepresentationsMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
                HashMap encodedTextRepresentationsMap = new HashMap(3, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
                encodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                    (DataTransferer.byteArrayClass, Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
                encodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                    (java.nio.ByteBuffer.class, Integer.valueOf(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                encodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
                    (java.io.InputStream.class, Integer.valueOf(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
                encodedTextRepresentations =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                    Collections.unmodifiableMap(encodedTextRepresentationsMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
        public DataFlavorComparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
            this(SELECT_BEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
        public DataFlavorComparator(boolean order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
            super(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
            charsetComparator = new CharsetComparator(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
        public int compare(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
            DataFlavor flavor1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
            DataFlavor flavor2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
            if (order == SELECT_BEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                flavor1 = (DataFlavor)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                flavor2 = (DataFlavor)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                flavor1 = (DataFlavor)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                flavor2 = (DataFlavor)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
            if (flavor1.equals(flavor2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            int comp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
            String primaryType1 = flavor1.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
            String subType1 = flavor1.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            String mimeType1 = primaryType1 + "/" + subType1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
            Class class1 = flavor1.getRepresentationClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
            String primaryType2 = flavor2.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
            String subType2 = flavor2.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
            String mimeType2 = primaryType2 + "/" + subType2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
            Class class2 = flavor2.getRepresentationClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
            if (flavor1.isFlavorTextType() && flavor2.isFlavorTextType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
                // First, compare MIME types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
                comp = compareIndices(textTypes, mimeType1, mimeType2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                                      UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                // Only need to test one flavor because they both have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                // same MIME type. Also don't need to worry about accidentally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                // passing stringFlavor because either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                //   1. Both flavors are stringFlavor, in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                //      equality test at the top of the function succeeded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                //   2. Only one flavor is stringFlavor, in which case the MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                //      type comparison returned a non-zero value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                if (doesSubtypeSupportCharset(flavor1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                    // Next, prefer the decoded text representations of Reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                    // String, CharBuffer, and [C, in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                    comp = compareIndices(decodedTextRepresentations, class1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
                                          class2, UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
                    if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
                    // Next, compare charsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
                    comp = charsetComparator.compareCharsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
                        (DataTransferer.getTextCharset(flavor1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
                         DataTransferer.getTextCharset(flavor2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
                    if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
                        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                // Finally, prefer the encoded text representations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                // InputStream, ByteBuffer, and [B, in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                comp = compareIndices(encodedTextRepresentations, class1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                                      class2, UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                // First, prefer application types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                comp = compareIndices(primaryTypes, primaryType1, primaryType2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                                      UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                // Next, look for application/x-java-* types. Prefer unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                // MIME types because if the user provides his own data flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                // it will likely be the most descriptive one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                comp = compareIndices(exactTypes, mimeType1, mimeType2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                                      UNKNOWN_OBJECT_WINS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                // Finally, prefer the representation classes of Remote,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
                // Serializable, and InputStream, in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
                comp = compareIndices(nonTextRepresentations, class1, class2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                                      UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
19364
f6324eb18ece 7173464: Clipboard.getAvailableDataFlavors: Comparison method violates contract
pchelko
parents: 18178
diff changeset
  2949
            // The flavours are not equal but still not distinguishable.
f6324eb18ece 7173464: Clipboard.getAvailableDataFlavors: Comparison method violates contract
pchelko
parents: 18178
diff changeset
  2950
            // Compare String representations in alphabetical order
f6324eb18ece 7173464: Clipboard.getAvailableDataFlavors: Comparison method violates contract
pchelko
parents: 18178
diff changeset
  2951
            return flavor1.getMimeType().compareTo(flavor2.getMimeType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
     * Given the Map that maps objects to Integer indices and a boolean value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
     * this Comparator imposes a direct or reverse order on set of objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
     * If the specified boolean value is SELECT_BEST, the Comparator imposes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
     * direct index-based order: an object A is greater than an object B if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
     * only if the index of A is greater than the index of B. An object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
     * doesn't have an associated index is less or equal than any other object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
     * If the specified boolean value is SELECT_WORST, the Comparator imposes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
     * reverse index-based order: an object A is greater than an object B if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
     * only if A is less than B with the direct index-based order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
    public static class IndexOrderComparator extends IndexedComparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
        private final Map indexMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
        private static final Integer FALLBACK_INDEX =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
            Integer.valueOf(Integer.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
        public IndexOrderComparator(Map indexMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
            super(SELECT_BEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
            this.indexMap = indexMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
        public IndexOrderComparator(Map indexMap, boolean order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            super(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
            this.indexMap = indexMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
        public int compare(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
            if (order == SELECT_WORST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                return -compareIndices(indexMap, obj1, obj2, FALLBACK_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                return compareIndices(indexMap, obj1, obj2, FALLBACK_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
    }
4267
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2991
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2992
    /**
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2993
     * A class that provides access to java.rmi.Remote and java.rmi.MarshalledObject
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2994
     * without creating a static dependency.
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2995
     */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2996
    private static class RMI {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2997
        private static final Class<?> remoteClass = getClass("java.rmi.Remote");
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2998
        private static final Class<?> marshallObjectClass =
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2999
            getClass("java.rmi.MarshalledObject");
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3000
        private static final Constructor<?> marshallCtor =
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3001
            getConstructor(marshallObjectClass, Object.class);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3002
        private static final Method marshallGet =
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3003
            getMethod(marshallObjectClass, "get");
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3004
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3005
        private static Class<?> getClass(String name) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3006
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3007
                return Class.forName(name, true, null);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3008
            } catch (ClassNotFoundException e) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3009
                return null;
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3010
            }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3011
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3012
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3013
        private static Constructor<?> getConstructor(Class<?> c, Class<?>... types) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3014
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3015
                return (c == null) ? null : c.getDeclaredConstructor(types);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3016
            } catch (NoSuchMethodException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3017
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3018
            }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3019
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3020
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3021
        private static Method getMethod(Class<?> c, String name, Class<?>... types) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3022
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3023
                return (c == null) ? null : c.getMethod(name, types);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3024
            } catch (NoSuchMethodException e) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3025
                throw new AssertionError(e);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3026
            }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3027
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3028
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3029
        /**
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3030
         * Returns {@code true} if the given class is java.rmi.Remote.
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3031
         */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3032
        static boolean isRemote(Class<?> c) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3033
            return (remoteClass == null) ? null : remoteClass.isAssignableFrom(c);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3034
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3035
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3036
        /**
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3037
         * Returns java.rmi.Remote.class if RMI is present; otherwise {@code null}.
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3038
         */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3039
        static Class<?> remoteClass() {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3040
            return remoteClass;
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3041
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3042
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3043
        /**
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3044
         * Returns a new MarshalledObject containing the serialized representation
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3045
         * of the given object.
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3046
         */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3047
        static Object newMarshalledObject(Object obj) throws IOException {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3048
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3049
                return marshallCtor.newInstance(obj);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3050
            } catch (InstantiationException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3051
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3052
            } catch (IllegalAccessException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3053
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3054
            } catch (InvocationTargetException  x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3055
                Throwable cause = x.getCause();
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3056
                if (cause instanceof IOException)
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3057
                    throw (IOException)cause;
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3058
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3059
            }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3060
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3061
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3062
        /**
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3063
         * Returns a new copy of the contained marshalled object.
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3064
         */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3065
        static Object getMarshalledObject(Object obj)
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3066
            throws IOException, ClassNotFoundException
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3067
        {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3068
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3069
                return marshallGet.invoke(obj);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3070
            } catch (IllegalAccessException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3071
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3072
            } catch (InvocationTargetException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3073
                Throwable cause = x.getCause();
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3074
                if (cause instanceof IOException)
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3075
                    throw (IOException)cause;
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3076
                if (cause instanceof ClassNotFoundException)
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3077
                    throw (ClassNotFoundException)cause;
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3078
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3079
            }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3080
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3081
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
}