1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 6877495
  26    @summary JTextField and JTextArea does not support supplementary characters
  27    @author Alexander Scherbatiy
  28    @run main SuplementaryCharactersTransferTest
  29 */
  30 
  31 
  32 import java.io.*;
  33 import java.util.*;
  34 import java.awt.*;
  35 import java.awt.datatransfer.*;
  36 import sun.awt.datatransfer.*;
  37 import sun.awt.datatransfer.DataTransferer.ReencodingInputStream;
  38 import sun.datatransfer.DataFlavorUtil;
  39 
  40 public class SuplementaryCharactersTransferTest {
  41 
  42     public static final long TEXT_FORMAT = 13;
  43 
  44     public static void main(String[] args) throws Exception {
  45 
  46         DataTransferer dataTransferer = new TestDataTransferer();
  47         dataTransferer.registerTextFlavorProperties("UNICODE TEXT", "utf-16le", "\r\n", "2");
  48         ByteTransferable transferable = new ByteTransferable();
  49         ReencodingInputStream is = dataTransferer.new ReencodingInputStream(transferable.getByteInputStream(), TEXT_FORMAT,
  50                 DataFlavorUtil.getTextCharset(transferable.getDataFlavor()), transferable);
  51 
  52         byte[] bytes = transferable.getBytes();
  53         byte[] result = new byte[bytes.length];
  54 
  55         is.read(result);
  56 
  57         for (int i = 0; i < bytes.length; i++) {
  58             if (bytes[i] != result[i]) {
  59                 throw new RuntimeException("Characters are not equal!");
  60             }
  61         }
  62 
  63     }
  64 
  65     static class ByteTransferable implements Transferable, ClipboardOwner {
  66 
  67         private final DataFlavor dataFlavor;
  68 
  69         public ByteTransferable() throws Exception {
  70             dataFlavor = DataFlavor.getTextPlainUnicodeFlavor();
  71         }
  72 
  73         public DataFlavor getDataFlavor() {
  74             return dataFlavor;
  75         }
  76 
  77         public DataFlavor[] getTransferDataFlavors() {
  78             return new DataFlavor[]{dataFlavor};
  79         }
  80 
  81         public boolean isDataFlavorSupported(DataFlavor flavor) {
  82             return flavor.equals(dataFlavor);
  83         }
  84 
  85         public byte[] getBytes() {
  86             return new byte[]{97, 0, 64, -40, 32, -36, 98, 0};
  87         }
  88 
  89         public InputStream getByteInputStream() {
  90             return new ByteArrayInputStream(getBytes());
  91         }
  92 
  93         public Object getTransferData(DataFlavor flavor)
  94                 throws UnsupportedFlavorException, IOException {
  95             if (flavor.equals(dataFlavor)) {
  96                 return getByteInputStream();
  97             } else {
  98                 throw new UnsupportedFlavorException(flavor);
  99             }
 100         }
 101 
 102         public void lostOwnership(Clipboard clipboard, Transferable contents) {
 103         }
 104     }
 105 
 106     static class TestDataTransferer extends DataTransferer {
 107 
 108         @Override
 109         public String getDefaultUnicodeEncoding() {
 110             throw new UnsupportedOperationException("Not supported yet.");
 111         }
 112 
 113         @Override
 114         public boolean isLocaleDependentTextFormat(long format) {
 115             return false;
 116         }
 117 
 118         @Override
 119         public boolean isFileFormat(long format) {
 120             throw new UnsupportedOperationException("Not supported yet.");
 121         }
 122 
 123         @Override
 124         public boolean isImageFormat(long format) {
 125             throw new UnsupportedOperationException("Not supported yet.");
 126         }
 127 
 128         @Override
 129         protected Long getFormatForNativeAsLong(String str) {
 130             return TEXT_FORMAT;
 131         }
 132 
 133         @Override
 134         protected String getNativeForFormat(long format) {
 135             throw new UnsupportedOperationException("Not supported yet.");
 136         }
 137 
 138         @Override
 139         protected ByteArrayOutputStream convertFileListToBytes(
 140                 ArrayList<String> fileList) throws IOException {
 141             throw new UnsupportedOperationException("Not supported yet.");
 142         }
 143 
 144         @Override
 145         protected String[] dragQueryFile(byte[] bytes) {
 146             throw new UnsupportedOperationException("Not supported yet.");
 147         }
 148 
 149         @Override
 150         protected byte[] imageToPlatformBytes(Image image, long format)
 151                 throws IOException {
 152             throw new UnsupportedOperationException("Not supported yet.");
 153         }
 154 
 155         @Override
 156         public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() {
 157             throw new UnsupportedOperationException("Not supported yet.");
 158         }
 159 
 160         @Override
 161         protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException {
 162             throw new UnsupportedOperationException("Not supported yet.");
 163         }
 164     }
 165 }
 166