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