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