jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java
author alexsch
Wed, 06 Mar 2013 19:42:26 +0400
changeset 15993 cdc681a6afd7
parent 11271 f10f98b24801
child 16705 1caaa379eded
permissions -rw-r--r--
6877495: JTextField and JTextArea does not support supplementary characters Reviewed-by: serb, alexp
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 {
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   174
        private static final SortedSet standardEncodings = load();
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   175
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   176
        private static SortedSet load() {
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);
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   179
            final SortedSet tempSet = new TreeSet(comparator);
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) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2639
diff changeset
   401
        if (dtLog.isLoggable(PlatformLogger.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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public static Iterator standardEncodings() {
9484
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
   527
        return StandardEncodingsHolder.standardEncodings.iterator();
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    private String translateBytesOrStreamToString(InputStream str,  byte[] bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                                                    long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                                                    Transferable localeTransferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        // A String holds all of its data in memory at one time, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        // we can't avoid reading the entire InputStream at this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            bytes = inputStreamToByteArray(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        str.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        Long lFormat = Long.valueOf(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        String charset = getBestCharsetForTextFormat(lFormat, localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        // Locate terminating NUL bytes. Note that if terminators is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        // the we never added an entry to nativeTerminators anyway, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        // we'll skip code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        // In other words: we are doing char alignment here basing on suggestion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        // that count of zero-'terminators' is a number of bytes in one symbol
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        // for selected charset (clipboard format). It is not complitly true for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        // multibyte coding like UTF-8, but helps understand the procedure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        // "abcde\0" -> "abcde"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        String eoln = (String)nativeEOLNs.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        Integer terminators = (Integer)nativeTerminators.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (terminators != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            int numTerminators = terminators.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
search:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            for (count = 0; count < (bytes.length - numTerminators + 1); count += numTerminators) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                for (int i = count; i < count + numTerminators; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    if (bytes[i] != 0x0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                        continue search;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                // found terminators
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                break search;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            count = bytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        // Decode text to chars. Don't include any terminators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        String converted = new String(bytes, 0, count, charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        // Search and replace EOLN. Note that if EOLN is "\n", then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        // never added an entry to nativeEOLNs anyway, so we'll skip this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        // code altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        // Count of NUL-terminators and EOLN coding are platform-specific and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        // loaded from flavormap.properties file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        // windows: "abc\r\nde" -> "abc\nde"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        if (eoln != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            /* Fix for 4463560: replace EOLNs symbol-by-symbol instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
             * of using buf.replace()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            char[] buf = converted.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            char[] eoln_arr = eoln.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            converted = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            for (int i = 0; i < buf.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                // Catch last few bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                if (i + eoln_arr.length > buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                    buf[j++] = buf[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                for (int k = 0, l = i; k < eoln_arr.length; k++, l++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                    if (eoln_arr[k] != buf[l]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                        match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                    buf[j++] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                    i += eoln_arr.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                    buf[j++] = buf[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            converted = new String(buf, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        return converted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Primary translation function for translating a Transferable into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * a byte array, given a source DataFlavor and target format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    public byte[] translateTransferable(Transferable contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                                        DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                                        long format) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        // Obtain the transfer data in the source DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        // Note that we special case DataFlavor.plainTextFlavor because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        // StringSelection supports this flavor incorrectly -- instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        // returning an InputStream as the DataFlavor representation class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        // states, it returns a Reader. Instead of using this broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        // functionality, we request the data in stringFlavor (the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        // DataFlavor which StringSelection supports) and use the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        // translator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        boolean stringSelectionHack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            obj = contents.getTransferData(flavor);
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
            if (flavor.equals(DataFlavor.plainTextFlavor) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                !(obj instanceof InputStream))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                obj = contents.getTransferData(DataFlavor.stringFlavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                stringSelectionHack = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                stringSelectionHack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        } catch (UnsupportedFlavorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        // Source data is a String. Search-and-replace EOLN. Encode into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        // target format. Append terminating NUL bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        if (stringSelectionHack ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            (String.class.equals(flavor.getRepresentationClass()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
             isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1210
            String str = removeSuspectedData(flavor, contents, (String)obj);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1211
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            return translateTransferableString(
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1213
                str,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        // Source data is a Reader. Convert to a String and recur. In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        // future, we may want to rewrite this so that we encode on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        } else if (flavor.isRepresentationClassReader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                    ("cannot transfer non-text data as Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            Reader r = (Reader)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            while ((c = r.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                buf.append((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                buf.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        // Source data is a CharBuffer. Convert to a String and recur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        } else if (flavor.isRepresentationClassCharBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                    ("cannot transfer non-text data as CharBuffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            CharBuffer buffer = (CharBuffer)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            int size = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            char[] chars = new char[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            buffer.get(chars, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                new String(chars),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        // Source data is a char array. Convert to a String and recur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        } else if (charArrayClass.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    ("cannot transfer non-text data as char array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                new String((char[])obj),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        // Source data is a ByteBuffer. For arbitrary flavors, simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        // the array. For text flavors, decode back to a String and recur to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        // reencode according to the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        } else if (flavor.isRepresentationClassByteBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            ByteBuffer buffer = (ByteBuffer)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            int size = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            byte[] bytes = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            buffer.get(bytes, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                String sourceEncoding = DataTransferer.getTextCharset(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    new String(bytes, sourceEncoding),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        // Source data is a byte array. For arbitrary flavors, simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        // the array. For text flavors, decode back to a String and recur to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        // reencode according to the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        } else if (byteArrayClass.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            byte[] bytes = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                String sourceEncoding = DataTransferer.getTextCharset(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                    new String(bytes, sourceEncoding),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        // Source data is Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        } else if (DataFlavor.imageFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            if (!isImageFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                throw new IOException("Data translation failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                                      "not an image format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            Image image = (Image)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            byte[] bytes = imageToPlatformBytes(image, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                throw new IOException("Data translation failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                    "cannot convert java image to native format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        // Target data is a file list. Source data must be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        // java.util.List which contains java.io.File or String instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        if (isFileFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            if (!DataFlavor.javaFileListFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1320
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            final List list = (List)obj;
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1322
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1323
            final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1324
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1325
            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
  1326
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1327
            bos = convertFileListToBytes(fileList);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1328
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
        // 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
  1331
        // 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
  1332
        } else if (isURIListFormat(format)) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1333
            if (!DataFlavor.javaFileListFlavor.equals(flavor)) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1334
                throw new IOException("data translation failed");
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1335
            }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1336
            String nat = getNativeForFormat(format);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1337
            String targetCharset = null;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1338
            if (nat != null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1339
                try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1340
                    targetCharset = new DataFlavor(nat).getParameter("charset");
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1341
                } catch (ClassNotFoundException cnfe) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1342
                    throw new IOException(cnfe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1345
            if (targetCharset == null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1346
                targetCharset = "UTF-8";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            }
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1348
            final List list = (List)obj;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1349
            final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1350
            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
  1351
            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
  1352
            for (String fileObject : fileList) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1353
                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
  1354
                // 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
  1355
                try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1356
                    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
  1357
                } catch (URISyntaxException uriSyntaxException) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1358
                    throw new IOException(uriSyntaxException);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1359
                  }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1360
              }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1361
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1362
            byte[] eoln = "\r\n".getBytes(targetCharset);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1363
            for (int i = 0; i < uriList.size(); i++) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1364
                byte[] bytes = uriList.get(i).getBytes(targetCharset);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1365
                bos.write(bytes, 0, bytes.length);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1366
                bos.write(eoln, 0, eoln.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        // Source data is an InputStream. For arbitrary flavors, just grab the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        // bytes and dump them into a byte array. For text flavors, decode back
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        // to a String and recur to reencode according to the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        } else if (flavor.isRepresentationClassInputStream()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            InputStream is = (InputStream)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            boolean eof = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            int avail = is.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            byte[] tmp = new byte[avail > 8192 ? avail : 8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                if (!(eof = (ret = is.read(tmp, 0, tmp.length)) == -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    bos.write(tmp, 0, ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            } while (!eof);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                byte[] bytes = bos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                bos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                String sourceEncoding = DataTransferer.getTextCharset(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                return translateTransferableString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    new String(bytes, sourceEncoding),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        // Source data is an RMI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        } else if (flavor.isRepresentationClassRemote()) {
4267
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  1396
            Object mo = RMI.newMarshalledObject(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            ObjectOutputStream oos = new ObjectOutputStream(bos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            oos.writeObject(mo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            oos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        // Source data is Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        } else if (flavor.isRepresentationClassSerializable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            ObjectOutputStream oos = new ObjectOutputStream(bos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            oos.writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            oos.close();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        byte[] ret = bos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        bos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1416
    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
  1417
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1418
    private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1419
            throws IOException
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1420
    {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1421
        if (null == System.getSecurityManager()
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1422
            || !flavor.isMimeTypeEqual("text/uri-list"))
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1423
        {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1424
            return str;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1425
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1426
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1427
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1428
        String ret_val = "";
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1429
        final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1430
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1431
        try {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1432
            ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1433
                    public Object run() {
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
                        StringBuffer allowedFiles = new StringBuffer(str.length());
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1436
                        String [] uriArray = str.split("(\\s)+");
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1437
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1438
                        for (String fileName : uriArray)
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
                            File file = new File(fileName);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1441
                            if (file.exists() &&
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1442
                                !(isFileInWebstartedCache(file) ||
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1443
                                isForbiddenToRead(file, userProtectionDomain)))
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1444
                            {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1445
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1446
                                if (0 != allowedFiles.length())
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1447
                                {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1448
                                    allowedFiles.append("\\r\\n");
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1449
                                }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1450
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1451
                                allowedFiles.append(fileName);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1452
                            }
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
                        return allowedFiles.toString();
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
                });
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1458
        } catch (PrivilegedActionException pae) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1459
            throw new IOException(pae.getMessage(), pae);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1460
        }
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
        return ret_val;
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
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1465
    private static ProtectionDomain getUserProtectionDomain(Transferable contents) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1466
        return contents.getClass().getProtectionDomain();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1467
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1468
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1469
    private boolean isForbiddenToRead (File file, ProtectionDomain protectionDomain)
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
        if (null == protectionDomain) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1472
            return false;
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
        try {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1475
            FilePermission filePermission =
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1476
                    new FilePermission(file.getCanonicalPath(), "read, delete");
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1477
            if (protectionDomain.implies(filePermission)) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1478
                return false;
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
        } catch (IOException e) {}
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1481
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1482
        return true;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1483
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1484
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1485
    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
  1486
                                          final ProtectionDomain userProtectionDomain) throws IOException
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1487
    {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1488
        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
  1489
        try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1490
            AccessController.doPrivileged(new PrivilegedExceptionAction() {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1491
                public Object run() throws IOException {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1492
                    for (Object fileObject : files)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1493
                    {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1494
                        File file = castToFile(fileObject);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1495
                        if (file != null &&
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1496
                            (null == System.getSecurityManager() ||
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1497
                            !(isFileInWebstartedCache(file) ||
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1498
                            isForbiddenToRead(file, userProtectionDomain))))
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1499
                        {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1500
                            fileList.add(file.getCanonicalPath());
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1501
                        }
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
                    return null;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1504
                }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1505
            });
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1506
        } catch (PrivilegedActionException pae) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1507
            throw new IOException(pae.getMessage());
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
        return fileList;
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
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1512
    // It is important do not use user's successors
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1513
    // of File class.
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1514
    private File castToFile(Object fileObject) throws IOException {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1515
        String filePath = null;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1516
        if (fileObject instanceof File) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1517
            filePath = ((File)fileObject).getCanonicalPath();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1518
        } else if (fileObject instanceof String) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1519
           filePath = (String) fileObject;
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1520
        } else {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1521
           return null;
2639
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1522
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1523
        return new File(filePath);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1524
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1525
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1526
    private final static String[] DEPLOYMENT_CACHE_PROPERTIES = {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1527
        "deployment.system.cachedir",
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1528
        "deployment.user.cachedir",
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1529
        "deployment.javaws.cachedir",
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1530
        "deployment.javapi.cachedir"
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
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1533
    private final static ArrayList <File> deploymentCacheDirectoryList =
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1534
            new ArrayList<File>();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1535
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1536
    private static boolean isFileInWebstartedCache(File f) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1537
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1538
        if (deploymentCacheDirectoryList.isEmpty()) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1539
            for (String cacheDirectoryProperty : DEPLOYMENT_CACHE_PROPERTIES) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1540
                String cacheDirectoryPath = System.getProperty(cacheDirectoryProperty);
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1541
                if (cacheDirectoryPath != null) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1542
                    try {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1543
                        File cacheDirectory = (new File(cacheDirectoryPath)).getCanonicalFile();
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1544
                        if (cacheDirectory != null) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1545
                            deploymentCacheDirectoryList.add(cacheDirectory);
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
                    } catch (IOException ioe) {}
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1548
                }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1549
            }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1550
        }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1551
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1552
        for (File deploymentCacheDirectory : deploymentCacheDirectoryList) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1553
            for (File dir = f; dir != null; dir = dir.getParentFile()) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1554
                if (dir.equals(deploymentCacheDirectory)) {
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1555
                    return true;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1556
                }
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
        return false;
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1561
    }
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1562
e7429e3a2529 6590857: Drag & Drop arbitrary file copy
denis
parents: 111
diff changeset
  1563
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    public Object translateBytes(byte[] bytes, DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                                 long format, Transferable localeTransferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        return translateBytesOrStream(null, bytes, flavor, format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                                      localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    public Object translateStream(InputStream str, DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                                  long format, Transferable localeTransferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        return translateBytesOrStream(str, null, flavor, format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                                      localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * Primary translation function for translating either a byte array or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * an InputStream into an Object, given a source format and a target
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * One of str/bytes is non-null; the other is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * The conversion from byte[] to InputStream is cheap, so do that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * immediately if necessary. The opposite conversion is expensive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * so avoid it if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    protected Object translateBytesOrStream(InputStream str, byte[] bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                                            DataFlavor flavor, long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                                            Transferable localeTransferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            str = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        // Source data is a file list. Use the dragQueryFile native function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        // do most of the decoding. Then wrap File objects around the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        // filenames and return a List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        if (isFileFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            if (!DataFlavor.javaFileListFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                bytes = inputStreamToByteArray(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            String[] filenames = dragQueryFile(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            if (filenames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                str.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            // Convert the strings to File objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            File[] files = new File[filenames.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            for (int i = 0; i < filenames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                files[i] = new File(filenames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            str.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            // Turn the list of Files into a List and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            return Arrays.asList(files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1627
        // Source data is a URI list. Convert to DataFlavor.javaFileListFlavor
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1628
        // where possible.
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1629
        } else if (isURIListFormat(format) && DataFlavor.javaFileListFlavor.equals(flavor)) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1630
            try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1631
                URI uris[] = dragQueryURIs(str, bytes, format, localeTransferable);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1632
                if (uris == null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1633
                    return null;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1634
                }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1635
                ArrayList files = new ArrayList();
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1636
                for (URI uri : uris) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1637
                    try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1638
                        files.add(new File(uri));
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1639
                    } catch (IllegalArgumentException illegalArg) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1640
                        // When converting from URIs to less generic files,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1641
                        // common practice (Wine, SWT) seems to be to
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1642
                        // silently drop the URIs that aren't local files.
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1643
                    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1644
                }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1645
                return files;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1646
            } finally {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1647
                str.close();
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1648
            }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  1649
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        // Target data is a String. Strip terminating NUL bytes. Decode bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        // into characters. Search-and-replace EOLN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        } else if (String.class.equals(flavor.getRepresentationClass()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                   isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            return translateBytesOrStreamToString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                str, bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                format, localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        // Special hack to maintain backwards-compatibility with the brokenness
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        // of StringSelection. Return a StringReader instead of an InputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        // Recur to obtain String and encapsulate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        } else if (DataFlavor.plainTextFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            return new StringReader(translateBytesOrStreamToString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                        str, bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                        format, localeTransferable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        // Target data is an InputStream. For arbitrary flavors, just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        // the raw bytes. For text flavors, decode to strip terminators and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        // search-and-replace EOLN, then reencode according to the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        // flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        } else if (flavor.isRepresentationClassInputStream()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            return translateBytesOrStreamToInputStream(str, flavor, format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                                                       localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        // Target data is a Reader. Obtain data in InputStream format, encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        // as "Unicode" (utf-16be). Then use an InputStreamReader to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        // back to chars on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        } else if (flavor.isRepresentationClassReader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                    ("cannot transfer non-text data as Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            InputStream is = (InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                translateBytesOrStreamToInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                    (str, DataFlavor.plainTextFlavor, format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                     localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            String unicode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                DataTransferer.getTextCharset(DataFlavor.plainTextFlavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            Reader reader = new InputStreamReader(is, unicode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            return constructFlavoredObject(reader, flavor, Reader.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        // Target data is a CharBuffer. Recur to obtain String and wrap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        } else if (flavor.isRepresentationClassCharBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                    ("cannot transfer non-text data as CharBuffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            CharBuffer buffer = CharBuffer.wrap(translateBytesOrStreamToString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                str, bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                format, localeTransferable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            return constructFlavoredObject(buffer, flavor, CharBuffer.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        // Target data is a char array. Recur to obtain String and convert to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        // char array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        } else if (charArrayClass.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                    ("cannot transfer non-text data as char array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            return translateBytesOrStreamToString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    str, bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                    format, localeTransferable).toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        // Target data is a ByteBuffer. For arbitrary flavors, just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        // the raw bytes. For text flavors, convert to a String to strip
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        // terminators and search-and-replace EOLN, then reencode according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        // the requested flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        } else if (flavor.isRepresentationClassByteBuffer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                bytes = translateBytesOrStreamToString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                    str, bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    format, localeTransferable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                ).getBytes(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    DataTransferer.getTextCharset(flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                    bytes = inputStreamToByteArray(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            ByteBuffer buffer = ByteBuffer.wrap(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            return constructFlavoredObject(buffer, flavor, ByteBuffer.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        // Target data is a byte array. For arbitrary flavors, just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        // the raw bytes. For text flavors, convert to a String to strip
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        // terminators and search-and-replace EOLN, then reencode according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        // the requested flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        } else if (byteArrayClass.equals(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                return translateBytesOrStreamToString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                    str, bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                    format, localeTransferable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                ).getBytes(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                    DataTransferer.getTextCharset(flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                return (bytes != null) ? bytes : inputStreamToByteArray(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        // Target data is an RMI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        } else if (flavor.isRepresentationClassRemote()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                byte[] ba = inputStreamToByteArray(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(ba));
4267
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  1761
                Object ret = RMI.getMarshalledObject(ois.readObject());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                ois.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                str.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        // Target data is Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        } else if (flavor.isRepresentationClassSerializable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                byte[] ba = inputStreamToByteArray(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(ba));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                Object ret = ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                ois.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                str.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        // Target data is Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        } else if (DataFlavor.imageFlavor.equals(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            if (!isImageFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            Image image = platformImageBytesOrStreamToImage(str, bytes, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
            str.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        throw new IOException("data translation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * For arbitrary flavors, just use the raw InputStream. For text flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * ReencodingInputStream will decode and reencode the InputStream on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * so that we can strip terminators and search-and-replace EOLN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
    private Object translateBytesOrStreamToInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        (InputStream str, DataFlavor flavor, long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
         Transferable localeTransferable) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            str = new ReencodingInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                (str, format, DataTransferer.getTextCharset(flavor),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                 localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        return constructFlavoredObject(str, flavor, InputStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * We support representations which are exactly of the specified Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * and also arbitrary Objects which have a constructor which takes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * instance of the Class as its sole parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    private Object constructFlavoredObject(Object arg, DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                                           Class clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        final Class dfrc = flavor.getRepresentationClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        if (clazz.equals(dfrc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            return arg; // simple case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            Constructor[] constructors = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                constructors = (Constructor[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                    AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                                return dfrc.getConstructors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                throw new IOException(se.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            Constructor constructor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            for (int j = 0; j < constructors.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                if (!Modifier.isPublic(constructors[j].getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                Class[] ptypes = constructors[j].getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                if (ptypes != null && ptypes.length == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                    clazz.equals(ptypes[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                    constructor = constructors[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            if (constructor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                throw new IOException("can't find <init>(L"+ clazz +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                                      ";)V for class: " + dfrc.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                return constructor.newInstance(new Object[] { arg } );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * Used for decoding and reencoding an InputStream on demand so that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * can strip NUL terminators and perform EOLN search-and-replace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
    public class ReencodingInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        protected BufferedReader wrapped;
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1876
        protected final char[] in = new char[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        protected byte[] out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        protected CharsetEncoder encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        protected CharBuffer inBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        protected ByteBuffer outBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        protected char[] eoln;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        protected int numTerminators;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        protected boolean eos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        protected int index, limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        public ReencodingInputStream(InputStream bytestream, long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                                     String targetEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                                     Transferable localeTransferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            Long lFormat = Long.valueOf(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            String sourceEncoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            if (isLocaleDependentTextFormat(format) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                localeTransferable != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                localeTransferable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                    isDataFlavorSupported(javaTextEncodingFlavor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                    sourceEncoding = new String((byte[])localeTransferable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                                       getTransferData(javaTextEncodingFlavor),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                                       "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                } catch (UnsupportedFlavorException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                sourceEncoding = getCharsetForTextFormat(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            if (sourceEncoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                // Only happens when we have a custom text type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                sourceEncoding = getDefaultTextCharset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            wrapped = new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                (new InputStreamReader(bytestream, sourceEncoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            if (targetEncoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                // Throw NullPointerException for compatibility with the former
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                // call to sun.io.CharToByteConverter.getConverter(null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                // (Charset.forName(null) throws unspecified IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                // now; see 6228568)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                throw new NullPointerException("null target encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                encoder = Charset.forName(targetEncoding).newEncoder();
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1929
                out = new byte[(int)(encoder.maxBytesPerChar() * 2 + 0.5)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                inBuf = CharBuffer.wrap(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                outBuf = ByteBuffer.wrap(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            } catch (IllegalCharsetNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                throw new IOException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            } catch (UnsupportedCharsetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                throw new IOException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            } catch (UnsupportedOperationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                throw new IOException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            String sEoln = (String)nativeEOLNs.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            if (sEoln != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                eoln = sEoln.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            // A hope and a prayer that this works generically. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            // definitely work on Win32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            Integer terminators = (Integer)nativeTerminators.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            if (terminators != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                numTerminators = terminators.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1953
        private int readChar() throws IOException {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1954
            int c = wrapped.read();
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1955
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1956
            if (c == -1) { // -1 is EOS
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1957
                eos = true;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1958
                return -1;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1959
            }
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1960
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1961
            // "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
  1962
            if (numTerminators > 0 && c == 0) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1963
                eos = true;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1964
                return -1;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1965
            } else if (eoln != null && matchCharArray(eoln, c)) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1966
                c = '\n' & 0xFFFF;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1967
            }
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1968
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1969
            return c;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1970
        }
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1971
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            if (eos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            if (index >= limit) {
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1978
                // deal with supplementary characters
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1979
                int c = readChar();
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1980
                if (c == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1984
                in[0] = (char) c;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1985
                in[1] = 0;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1986
                inBuf.limit(1);
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1987
                if (Character.isHighSurrogate((char) c)) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1988
                    c = readChar();
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1989
                    if (c != -1) {
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1990
                        in[1] = (char) c;
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1991
                        inBuf.limit(2);
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1992
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                inBuf.rewind();
15993
cdc681a6afd7 6877495: JTextField and JTextArea does not support supplementary characters
alexsch
parents: 11271
diff changeset
  1996
                outBuf.limit(out.length).rewind();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                encoder.encode(inBuf, outBuf, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                outBuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                limit = outBuf.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                return read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                return out[index++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            return ((eos) ? 0 : (limit - index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            wrapped.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
         * Checks to see if the next array.length characters in wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
         * match array. The first character is provided as c. Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
         * characters are read from wrapped itself. When this method returns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
         * the wrapped index may be different from what it was when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
         * method was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        private boolean matchCharArray(char[] array, int c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            wrapped.mark(array.length);  // BufferedReader supports mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            if ((char)c == array[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                for (count = 1; count < array.length; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                    c = wrapped.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                    if (c == -1 || ((char)c) != array[count]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            if (count == array.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                wrapped.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * Decodes a byte array into a set of String filenames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    protected abstract String[] dragQueryFile(byte[] bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    /**
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2054
     * 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
  2055
     */
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2056
    protected URI[] dragQueryURIs(InputStream stream,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2057
                                  byte[] bytes,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2058
                                  long format,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2059
                                  Transferable localeTransferable)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2060
      throws IOException
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2061
    {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2062
        throw new IOException(
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2063
            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
  2064
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2065
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 4362
diff changeset
  2066
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * Translates either a byte array or an input stream which contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * platform-specific image data in the given format into an Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    protected abstract Image platformImageBytesOrStreamToImage(InputStream str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                                                               byte[] bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                                                               long format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
      throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * Translates either a byte array or an input stream which contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * an image data in the given standard format into an Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * @param mimeType image MIME type, such as: image/png, image/jpeg, image/gif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
    protected Image standardImageBytesOrStreamToImage(InputStream inputStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                                                      byte[] bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                                                      String mimeType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        if (inputStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            inputStream = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        Iterator readerIterator = ImageIO.getImageReadersByMIMEType(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        if (!readerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
            throw new IOException("No registered service provider can decode " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                                  " an image from " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        IOException ioe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        while (readerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            ImageReader imageReader = (ImageReader)readerIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                ImageInputStream imageInputStream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                    ImageIO.createImageInputStream(inputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    ImageReadParam param = imageReader.getDefaultReadParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                    imageReader.setInput(imageInputStream, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                    BufferedImage bufferedImage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                        imageReader.read(imageReader.getMinIndex(), param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                    if (bufferedImage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                        return bufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                    imageInputStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                    imageReader.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                ioe = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        if (ioe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            ioe = new IOException("Registered service providers failed to decode"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                                  + " an image from " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * Translates a Java Image into a byte array which contains platform-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * specific image data in the given format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    protected abstract byte[] imageToPlatformBytes(Image image, long format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
      throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * Translates a Java Image into a byte array which contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * an image data in the given standard format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * @param mimeType image MIME type, such as: image/png, image/jpeg
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
    protected byte[] imageToStandardBytes(Image image, String mimeType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
        IOException originalIOE = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        if (!writerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            throw new IOException("No registered service provider can encode " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                                  " an image to " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        if (image instanceof RenderedImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            // Try to encode the original image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                return imageToStandardBytesImpl((RenderedImage)image, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                originalIOE = ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        // Retry with a BufferedImage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        if (image instanceof ToolkitImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            ir.reconstruct(ImageObserver.ALLBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            width = ir.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            height = ir.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            width = image.getWidth(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            height = image.getHeight(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        ColorModel model = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        WritableRaster raster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            model.createCompatibleWritableRaster(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        BufferedImage bufferedImage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            new BufferedImage(model, raster, model.isAlphaPremultiplied(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                              null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        Graphics g = bufferedImage.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
            g.drawImage(image, 0, 0, width, height, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
            return imageToStandardBytesImpl(bufferedImage, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            if (originalIOE != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                throw originalIOE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
    protected byte[] imageToStandardBytesImpl(RenderedImage renderedImage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                                              String mimeType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
        Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        ImageTypeSpecifier typeSpecifier =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            new ImageTypeSpecifier(renderedImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        IOException ioe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        while (writerIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            ImageWriter imageWriter = (ImageWriter)writerIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            if (!writerSpi.canEncodeImage(typeSpecifier)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                ImageOutputStream imageOutputStream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                    ImageIO.createImageOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                    imageWriter.setOutput(imageOutputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                    imageWriter.write(renderedImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                    imageOutputStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                    imageOutputStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                imageWriter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                ioe = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            imageWriter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            baos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
        baos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
        if (ioe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            ioe = new IOException("Registered service providers failed to encode "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                                  + renderedImage + " to " + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     * Concatenates the data represented by two objects. Objects can be either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * byte arrays or instances of <code>InputStream</code>. If both arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * are byte arrays byte array will be returned. Otherwise an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     * <code>InputStream</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     * Currently is only called from native code to prepend palette data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     * platform-specific image data during image transfer on Win32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     * @param obj1 the first object to be concatenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     * @param obj2 the second object to be concatenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     * @return a byte array or an <code>InputStream</code> which represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     *         a logical concatenation of the two arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     * @throws NullPointerException is either of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     *         <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     * @throws ClassCastException is either of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     *         neither byte array nor an instance of <code>InputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
    private Object concatData(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        InputStream str1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        InputStream str2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        if (obj1 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            byte[] arr1 = (byte[])obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
            if (obj2 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                byte[] arr2 = (byte[])obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                byte[] ret = new byte[arr1.length + arr2.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                System.arraycopy(arr1, 0, ret, 0, arr1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                System.arraycopy(arr2, 0, ret, arr1.length, arr2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                str1 = new ByteArrayInputStream(arr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                str2 = (InputStream)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            str1 = (InputStream)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            if (obj2 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                str2 = new ByteArrayInputStream((byte[])obj2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                str2 = (InputStream)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        return new SequenceInputStream(str1, str2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
    public byte[] convertData(final Object source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                              final Transferable contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                              final long format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                              final Map formatMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                              final boolean isToolkitThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        byte[] ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
         * If the current thread is the Toolkit thread we should post a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
         * Runnable to the event dispatch thread associated with source Object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
         * since translateTransferable() calls Transferable.getTransferData()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
         * that may contain client code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
        if (isToolkitThread) try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            final Stack stack = new Stack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            final Runnable dataConverter = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                // Guard against multiple executions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                private boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                    if (done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                    byte[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                        DataFlavor flavor = (DataFlavor)formatMap.get(Long.valueOf(format));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                        if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                            data = translateTransferable(contents, flavor, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                        data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                        getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                        stack.push(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                        getToolkitThreadBlockedHandler().exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                        getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
            final AppContext appContext = SunToolkit.targetToAppContext(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            if (appContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                appContext.put(DATA_CONVERTER_KEY, dataConverter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            SunToolkit.executeOnEventHandlerThread(source, dataConverter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
            while (stack.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                getToolkitThreadBlockedHandler().enter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            if (appContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                appContext.remove(DATA_CONVERTER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            ret = (byte[])stack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            DataFlavor flavor = (DataFlavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                formatMap.get(Long.valueOf(format));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
            if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                ret = translateTransferable(contents, flavor, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
    public void processDataConversionRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            AppContext appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                Runnable dataConverter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                    (Runnable)appContext.get(DATA_CONVERTER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                if (dataConverter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                    dataConverter.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                    appContext.remove(DATA_CONVERTER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
    public abstract ToolkitThreadBlockedHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        getToolkitThreadBlockedHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * Helper function to reduce a Map with Long keys to a long array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * The map keys are sorted according to the native formats preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    public static long[] keysToLongArray(SortedMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        Set keySet = map.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        long[] retval = new long[keySet.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
        for (Iterator iter = keySet.iterator(); iter.hasNext(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            retval[i] = ((Long)iter.next()).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * Helper function to reduce a Map with DataFlavor keys to a DataFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     * array. The array will be sorted according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * <code>DataFlavorComparator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
    public static DataFlavor[] keysToDataFlavorArray(Map map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        return setToSortedDataFlavorArray(map.keySet(), map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * Helper function to convert a Set of DataFlavors to a sorted array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     * The array will be sorted according to <code>DataFlavorComparator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
    public static DataFlavor[] setToSortedDataFlavorArray(Set flavorsSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        DataFlavor[] flavors = new DataFlavor[flavorsSet.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
        flavorsSet.toArray(flavors);
9484
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
  2429
        final Comparator comparator =
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
  2430
                new DataFlavorComparator(IndexedComparator.SELECT_WORST);
ef39ea9d781b 7016528: Deadlock during mutual initialization of DataTransferer and DataTransferer$DataFlavorComparator
serb
parents: 5506
diff changeset
  2431
        Arrays.sort(flavors, comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * Helper function to convert a Set of DataFlavors to a sorted array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * The array will be sorted according to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     * <code>DataFlavorComparator</code> created with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     * flavor-to-native map as an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    public static DataFlavor[] setToSortedDataFlavorArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        (Set flavorsSet, Map flavorToNativeMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        DataFlavor[] flavors = new DataFlavor[flavorsSet.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        flavorsSet.toArray(flavors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
        Comparator comparator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
            new DataFlavorComparator(flavorToNativeMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                                     IndexedComparator.SELECT_WORST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        Arrays.sort(flavors, comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * Helper function to convert an InputStream to a byte[] array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
    protected static byte[] inputStreamToByteArray(InputStream str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        byte[] buf = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
        while ((len = str.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
            baos.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     * Returns platform-specific mappings for the specified native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     * If there are no platform-specific mappings for this native, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     * returns an empty <code>List</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
    public List getPlatformMappingsForNative(String nat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
        return new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     * Returns platform-specific mappings for the specified flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     * If there are no platform-specific mappings for this flavor, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     * returns an empty <code>List</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
    public List getPlatformMappingsForFlavor(DataFlavor df) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
        return new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     * A Comparator which includes a helper function for comparing two Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     * which are likely to be keys in the specified Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
    public abstract static class IndexedComparator implements Comparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
         * The best Object (e.g., DataFlavor) will be the last in sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        public static final boolean SELECT_BEST = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
         * The best Object (e.g., DataFlavor) will be the first in sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
        public static final boolean SELECT_WORST = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
        protected final boolean order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
        public IndexedComparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
            this(SELECT_BEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
        public IndexedComparator(boolean order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
            this.order = order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
         * Helper method to compare two objects by their Integer indices in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
         * given map. If the map doesn't contain an entry for either of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
         * objects, the fallback index will be used for the object instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
         * @param indexMap the map which maps objects into Integer indexes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
         * @param obj1 the first object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
         * @param obj2 the second object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
         * @param fallbackIndex the Integer to be used as a fallback index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
         * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
         *             first object is mapped to a less, equal to, or greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
         *             index than the second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        protected static int compareIndices(Map indexMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                                            Object obj1, Object obj2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
                                            Integer fallbackIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
            Integer index1 = (Integer)indexMap.get(obj1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
            Integer index2 = (Integer)indexMap.get(obj2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
            if (index1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                index1 = fallbackIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
            if (index2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                index2 = fallbackIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
            return index1.compareTo(index2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
         * Helper method to compare two objects by their Long indices in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
         * given map. If the map doesn't contain an entry for either of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
         * objects, the fallback index will be used for the object instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
         * @param indexMap the map which maps objects into Long indexes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
         * @param obj1 the first object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
         * @param obj2 the second object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
         * @param fallbackIndex the Long to be used as a fallback index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
         * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
         *             first object is mapped to a less, equal to, or greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
         *             index than the second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        protected static int compareLongs(Map indexMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
                                          Object obj1, Object obj2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                                          Long fallbackIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            Long index1 = (Long)indexMap.get(obj1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            Long index2 = (Long)indexMap.get(obj2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            if (index1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                index1 = fallbackIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
            if (index2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                index2 = fallbackIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            return index1.compareTo(index2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
     * An IndexedComparator which compares two String charsets. The comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
     * follows the rules outlined in DataFlavor.selectBestTextFlavor. In order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
     * to ensure that non-Unicode, non-ASCII, non-default charsets are sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
     * in alphabetical order, charsets are not automatically converted to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
     * canonical forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
    public static class CharsetComparator extends IndexedComparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        private static final Map charsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        private static String defaultEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
        private static final Integer DEFAULT_CHARSET_INDEX = Integer.valueOf(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        private static final Integer OTHER_CHARSET_INDEX = Integer.valueOf(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
        private static final Integer WORST_CHARSET_INDEX = Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        private static final Integer UNSUPPORTED_CHARSET_INDEX =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
            Integer.valueOf(Integer.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
        private static final String UNSUPPORTED_CHARSET = "UNSUPPORTED";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
            HashMap charsetsMap = new HashMap(8, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
            // we prefer Unicode charsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
            charsetsMap.put(canonicalName("UTF-16LE"), Integer.valueOf(4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
            charsetsMap.put(canonicalName("UTF-16BE"), Integer.valueOf(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            charsetsMap.put(canonicalName("UTF-8"), Integer.valueOf(6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            charsetsMap.put(canonicalName("UTF-16"), Integer.valueOf(7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            // US-ASCII is the worst charset supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            charsetsMap.put(canonicalName("US-ASCII"), WORST_CHARSET_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
            String defEncoding = DataTransferer.canonicalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                (DataTransferer.getDefaultTextCharset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
            if (charsetsMap.get(defaultEncoding) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                charsetsMap.put(defaultEncoding, DEFAULT_CHARSET_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            charsetsMap.put(UNSUPPORTED_CHARSET, UNSUPPORTED_CHARSET_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
            charsets = Collections.unmodifiableMap(charsetsMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
        public CharsetComparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
            this(SELECT_BEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        public CharsetComparator(boolean order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
            super(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
         * Compares two String objects. Returns a negative integer, zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
         * or a positive integer as the first charset is worse than, equal to,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
         * or better than the second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
         * @param obj1 the first charset to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
         * @param obj2 the second charset to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
         * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
         *         first argument is worse, equal to, or better than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
         *         second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
         * @throws ClassCastException if either of the arguments is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
         *         instance of String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
         * @throws NullPointerException if either of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
         *         <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
        public int compare(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            String charset1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
            String charset2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            if (order == SELECT_BEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                charset1 = (String)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                charset2 = (String)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                charset1 = (String)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                charset2 = (String)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
            return compareCharsets(charset1, charset2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
         * Compares charsets. Returns a negative integer, zero, or a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
         * integer as the first charset is worse than, equal to, or better than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
         * the second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
         * Charsets are ordered according to the following rules:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
         * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
         * <li>All unsupported charsets are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
         * <li>Any unsupported charset is worse than any supported charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
         * <li>Unicode charsets, such as "UTF-16", "UTF-8", "UTF-16BE" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
         *     "UTF-16LE", are considered best.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
         * <li>After them, platform default charset is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
         * <li>"US-ASCII" is the worst of supported charsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
         * <li>For all other supported charsets, the lexicographically less
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
         *     one is considered the better.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
         * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
         * @param charset1 the first charset to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
         * @param charset2 the second charset to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
         * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
         *             first argument is worse, equal to, or better than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
         *             second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
        protected int compareCharsets(String charset1, String charset2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            charset1 = getEncoding(charset1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
            charset2 = getEncoding(charset2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            int comp = compareIndices(charsets, charset1, charset2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
                                      OTHER_CHARSET_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
            if (comp == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                return charset2.compareTo(charset1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
         * Returns encoding for the specified charset according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
         * following rules:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
         * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
         * <li>If the charset is <code>null</code>, then <code>null</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
         *     be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
         * <li>Iff the charset specifies an encoding unsupported by this JRE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
         *     <code>UNSUPPORTED_CHARSET</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
         * <li>If the charset specifies an alias name, the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
         *     canonical name will be returned iff the charset is a known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
         *     Unicode, ASCII, or default charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
         * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
         * @param charset the charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
         * @return an encoding for this charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
        protected static String getEncoding(String charset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
            if (charset == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
            } else if (!DataTransferer.isEncodingSupported(charset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
                return UNSUPPORTED_CHARSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
                // Only convert to canonical form if the charset is one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
                // of the charsets explicitly listed in the known charsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                // map. This will happen only for Unicode, ASCII, or default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                // charsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
                String canonicalName = DataTransferer.canonicalName(charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
                return (charsets.containsKey(canonicalName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                    ? canonicalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                    : charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
     * An IndexedComparator which compares two DataFlavors. For text flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
     * the comparison follows the rules outlined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
     * DataFlavor.selectBestTextFlavor. For non-text flavors, unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
     * application MIME types are preferred, followed by known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
     * application/x-java-* MIME types. Unknown application types are preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
     * because if the user provides his own data flavor, it will likely be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
     * most descriptive one. For flavors which are otherwise equal, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
     * flavors' native formats are compared, with greater long values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
     * taking precedence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
    public static class DataFlavorComparator extends IndexedComparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        protected final Map flavorToFormatMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        private final CharsetComparator charsetComparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        private static final Map exactTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
        private static final Map primaryTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
        private static final Map nonTextRepresentations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
        private static final Map textTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
        private static final Map decodedTextRepresentations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
        private static final Map encodedTextRepresentations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
        private static final Integer UNKNOWN_OBJECT_LOSES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
            Integer.valueOf(Integer.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        private static final Integer UNKNOWN_OBJECT_WINS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
            Integer.valueOf(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
        private static final Long UNKNOWN_OBJECT_LOSES_L =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            Long.valueOf(Long.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
        private static final Long UNKNOWN_OBJECT_WINS_L =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            Long.valueOf(Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                HashMap exactTypesMap = new HashMap(4, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                // application/x-java-* MIME types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                exactTypesMap.put("application/x-java-file-list",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
                                  Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
                exactTypesMap.put("application/x-java-serialized-object",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                                  Integer.valueOf(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                exactTypesMap.put("application/x-java-jvm-local-objectref",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                                  Integer.valueOf(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                exactTypesMap.put("application/x-java-remote-object",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                                  Integer.valueOf(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                exactTypes = Collections.unmodifiableMap(exactTypesMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
                HashMap primaryTypesMap = new HashMap(1, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                primaryTypesMap.put("application", Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
                primaryTypes = Collections.unmodifiableMap(primaryTypesMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
                HashMap nonTextRepresentationsMap = new HashMap(3, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                nonTextRepresentationsMap.put(java.io.InputStream.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                                              Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                nonTextRepresentationsMap.put(java.io.Serializable.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                                              Integer.valueOf(1));
4267
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2788
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2789
                Class<?> remoteClass = RMI.remoteClass();
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2790
                if (remoteClass != null) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2791
                    nonTextRepresentationsMap.put(remoteClass,
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2792
                                                  Integer.valueOf(2));
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  2793
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                nonTextRepresentations =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                    Collections.unmodifiableMap(nonTextRepresentationsMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                HashMap textTypesMap = new HashMap(16, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
                // plain text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
                textTypesMap.put("text/plain", Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                // stringFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                textTypesMap.put("application/x-java-serialized-object",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                                Integer.valueOf(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                // misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                textTypesMap.put("text/calendar", Integer.valueOf(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
                textTypesMap.put("text/css", Integer.valueOf(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                textTypesMap.put("text/directory", Integer.valueOf(4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                textTypesMap.put("text/parityfec", Integer.valueOf(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                textTypesMap.put("text/rfc822-headers", Integer.valueOf(6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                textTypesMap.put("text/t140", Integer.valueOf(7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                textTypesMap.put("text/tab-separated-values", Integer.valueOf(8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                textTypesMap.put("text/uri-list", Integer.valueOf(9));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                // enriched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                textTypesMap.put("text/richtext", Integer.valueOf(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                textTypesMap.put("text/enriched", Integer.valueOf(11));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                textTypesMap.put("text/rtf", Integer.valueOf(12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
                // markup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                textTypesMap.put("text/html", Integer.valueOf(13));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                textTypesMap.put("text/xml", Integer.valueOf(14));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                textTypesMap.put("text/sgml", Integer.valueOf(15));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                textTypes = Collections.unmodifiableMap(textTypesMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
                HashMap decodedTextRepresentationsMap = new HashMap(4, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                decodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
                    (DataTransferer.charArrayClass, Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                decodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                    (java.nio.CharBuffer.class, Integer.valueOf(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
                decodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                    (java.lang.String.class, Integer.valueOf(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
                decodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                    (java.io.Reader.class, Integer.valueOf(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
                decodedTextRepresentations =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
                    Collections.unmodifiableMap(decodedTextRepresentationsMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
                HashMap encodedTextRepresentationsMap = new HashMap(3, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
                encodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
                    (DataTransferer.byteArrayClass, Integer.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
                encodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
                    (java.nio.ByteBuffer.class, Integer.valueOf(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
                encodedTextRepresentationsMap.put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
                    (java.io.InputStream.class, Integer.valueOf(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
                encodedTextRepresentations =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
                    Collections.unmodifiableMap(encodedTextRepresentationsMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
        public DataFlavorComparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
            this(SELECT_BEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        public DataFlavorComparator(boolean order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
            super(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
            charsetComparator = new CharsetComparator(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            flavorToFormatMap = Collections.EMPTY_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        public DataFlavorComparator(Map map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            this(map, SELECT_BEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        public DataFlavorComparator(Map map, boolean order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
            super(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
            charsetComparator = new CharsetComparator(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
            HashMap hashMap = new HashMap(map.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
            hashMap.putAll(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            flavorToFormatMap = Collections.unmodifiableMap(hashMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
        public int compare(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
            DataFlavor flavor1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            DataFlavor flavor2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
            if (order == SELECT_BEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                flavor1 = (DataFlavor)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                flavor2 = (DataFlavor)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                flavor1 = (DataFlavor)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                flavor2 = (DataFlavor)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
            if (flavor1.equals(flavor2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
            int comp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
            String primaryType1 = flavor1.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
            String subType1 = flavor1.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
            String mimeType1 = primaryType1 + "/" + subType1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
            Class class1 = flavor1.getRepresentationClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
            String primaryType2 = flavor2.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
            String subType2 = flavor2.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
            String mimeType2 = primaryType2 + "/" + subType2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
            Class class2 = flavor2.getRepresentationClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
            if (flavor1.isFlavorTextType() && flavor2.isFlavorTextType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                // First, compare MIME types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                comp = compareIndices(textTypes, mimeType1, mimeType2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                                      UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                // Only need to test one flavor because they both have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                // same MIME type. Also don't need to worry about accidentally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                // passing stringFlavor because either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                //   1. Both flavors are stringFlavor, in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                //      equality test at the top of the function succeeded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                //   2. Only one flavor is stringFlavor, in which case the MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                //      type comparison returned a non-zero value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                if (doesSubtypeSupportCharset(flavor1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                    // Next, prefer the decoded text representations of Reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                    // String, CharBuffer, and [C, in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                    comp = compareIndices(decodedTextRepresentations, class1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                                          class2, UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                    if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                    // Next, compare charsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                    comp = charsetComparator.compareCharsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                        (DataTransferer.getTextCharset(flavor1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
                         DataTransferer.getTextCharset(flavor2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
                    if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                // Finally, prefer the encoded text representations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                // InputStream, ByteBuffer, and [B, in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
                comp = compareIndices(encodedTextRepresentations, class1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                                      class2, UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                // First, prefer application types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
                comp = compareIndices(primaryTypes, primaryType1, primaryType2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
                                      UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
                // Next, look for application/x-java-* types. Prefer unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
                // MIME types because if the user provides his own data flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
                // it will likely be the most descriptive one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
                comp = compareIndices(exactTypes, mimeType1, mimeType2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
                                      UNKNOWN_OBJECT_WINS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                // Finally, prefer the representation classes of Remote,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                // Serializable, and InputStream, in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                comp = compareIndices(nonTextRepresentations, class1, class2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                                      UNKNOWN_OBJECT_LOSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                if (comp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
            // As a last resort, take the DataFlavor with the greater integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
            // format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
            return compareLongs(flavorToFormatMap, flavor1, flavor2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                                UNKNOWN_OBJECT_LOSES_L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
     * Given the Map that maps objects to Integer indices and a boolean value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
     * this Comparator imposes a direct or reverse order on set of objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
     * If the specified boolean value is SELECT_BEST, the Comparator imposes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
     * direct index-based order: an object A is greater than an object B if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
     * only if the index of A is greater than the index of B. An object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
     * doesn't have an associated index is less or equal than any other object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
     * If the specified boolean value is SELECT_WORST, the Comparator imposes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     * reverse index-based order: an object A is greater than an object B if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * only if A is less than B with the direct index-based order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
    public static class IndexOrderComparator extends IndexedComparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
        private final Map indexMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
        private static final Integer FALLBACK_INDEX =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
            Integer.valueOf(Integer.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
        public IndexOrderComparator(Map indexMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
            super(SELECT_BEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
            this.indexMap = indexMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        public IndexOrderComparator(Map indexMap, boolean order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
            super(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
            this.indexMap = indexMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        public int compare(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
            if (order == SELECT_WORST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                return -compareIndices(indexMap, obj1, obj2, FALLBACK_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                return compareIndices(indexMap, obj1, obj2, FALLBACK_INDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
    }
4267
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3023
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3024
    /**
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3025
     * 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
  3026
     * without creating a static dependency.
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
    private static class RMI {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3029
        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
  3030
        private static final Class<?> marshallObjectClass =
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3031
            getClass("java.rmi.MarshalledObject");
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3032
        private static final Constructor<?> marshallCtor =
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3033
            getConstructor(marshallObjectClass, Object.class);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3034
        private static final Method marshallGet =
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3035
            getMethod(marshallObjectClass, "get");
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
        private static Class<?> getClass(String name) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3038
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3039
                return Class.forName(name, true, null);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3040
            } catch (ClassNotFoundException e) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3041
                return null;
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
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3045
        private static Constructor<?> getConstructor(Class<?> c, Class<?>... types) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3046
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3047
                return (c == null) ? null : c.getDeclaredConstructor(types);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3048
            } catch (NoSuchMethodException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3049
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3050
            }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3051
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3052
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3053
        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
  3054
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3055
                return (c == null) ? null : c.getMethod(name, types);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3056
            } catch (NoSuchMethodException e) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3057
                throw new AssertionError(e);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3058
            }
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
         * 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
  3063
         */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3064
        static boolean isRemote(Class<?> c) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3065
            return (remoteClass == null) ? null : remoteClass.isAssignableFrom(c);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3066
        }
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
        /**
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3069
         * 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
  3070
         */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3071
        static Class<?> remoteClass() {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3072
            return remoteClass;
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3073
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3074
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3075
        /**
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3076
         * Returns a new MarshalledObject containing the serialized representation
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3077
         * of the given object.
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3078
         */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3079
        static Object newMarshalledObject(Object obj) throws IOException {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3080
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3081
                return marshallCtor.newInstance(obj);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3082
            } catch (InstantiationException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3083
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3084
            } catch (IllegalAccessException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3085
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3086
            } catch (InvocationTargetException  x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3087
                Throwable cause = x.getCause();
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3088
                if (cause instanceof IOException)
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3089
                    throw (IOException)cause;
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3090
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3091
            }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3092
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3093
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3094
        /**
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3095
         * 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
  3096
         */
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3097
        static Object getMarshalledObject(Object obj)
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3098
            throws IOException, ClassNotFoundException
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3099
        {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3100
            try {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3101
                return marshallGet.invoke(obj);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3102
            } catch (IllegalAccessException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3103
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3104
            } catch (InvocationTargetException x) {
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3105
                Throwable cause = x.getCause();
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3106
                if (cause instanceof IOException)
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3107
                    throw (IOException)cause;
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3108
                if (cause instanceof ClassNotFoundException)
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3109
                    throw (ClassNotFoundException)cause;
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3110
                throw new AssertionError(x);
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3111
            }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3112
        }
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 3938
diff changeset
  3113
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
}