1 /*
2  * Copyright (c) 2013, 2015, 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    @modules java.datatransfer/sun.datatransfer
29             java.desktop/sun.awt.datatransfer
30    @run main SuplementaryCharactersTransferTest
31 */
32 
33 
34 import java.io.*;
35 import java.util.*;
36 import java.awt.*;
37 import java.awt.datatransfer.*;
38 import sun.awt.datatransfer.*;
39 import sun.awt.datatransfer.DataTransferer.ReencodingInputStream;
40 import sun.datatransfer.DataFlavorUtil;
41 
42 public class SuplementaryCharactersTransferTest {
43 
44     public static final long TEXT_FORMAT = 13;
45 
main(String[] args)46     public static void main(String[] args) throws Exception {
47 
48         DataTransferer dataTransferer = new TestDataTransferer();
49         dataTransferer.registerTextFlavorProperties("UNICODE TEXT", "utf-16le", "\r\n", "2");
50         ByteTransferable transferable = new ByteTransferable();
51         ReencodingInputStream is = dataTransferer.new ReencodingInputStream(transferable.getByteInputStream(), TEXT_FORMAT,
52                 DataFlavorUtil.getTextCharset(transferable.getDataFlavor()), transferable);
53 
54         byte[] bytes = transferable.getBytes();
55         byte[] result = new byte[bytes.length];
56 
57         is.read(result);
58 
59         for (int i = 0; i < bytes.length; i++) {
60             if (bytes[i] != result[i]) {
61                 throw new RuntimeException("Characters are not equal!");
62             }
63         }
64 
65     }
66 
67     static class ByteTransferable implements Transferable, ClipboardOwner {
68 
69         private final DataFlavor dataFlavor;
70 
ByteTransferable()71         public ByteTransferable() throws Exception {
72             dataFlavor = DataFlavor.getTextPlainUnicodeFlavor();
73         }
74 
getDataFlavor()75         public DataFlavor getDataFlavor() {
76             return dataFlavor;
77         }
78 
getTransferDataFlavors()79         public DataFlavor[] getTransferDataFlavors() {
80             return new DataFlavor[]{dataFlavor};
81         }
82 
isDataFlavorSupported(DataFlavor flavor)83         public boolean isDataFlavorSupported(DataFlavor flavor) {
84             return flavor.equals(dataFlavor);
85         }
86 
getBytes()87         public byte[] getBytes() {
88             return new byte[]{97, 0, 64, -40, 32, -36, 98, 0};
89         }
90 
getByteInputStream()91         public InputStream getByteInputStream() {
92             return new ByteArrayInputStream(getBytes());
93         }
94 
getTransferData(DataFlavor flavor)95         public Object getTransferData(DataFlavor flavor)
96                 throws UnsupportedFlavorException, IOException {
97             if (flavor.equals(dataFlavor)) {
98                 return getByteInputStream();
99             } else {
100                 throw new UnsupportedFlavorException(flavor);
101             }
102         }
103 
lostOwnership(Clipboard clipboard, Transferable contents)104         public void lostOwnership(Clipboard clipboard, Transferable contents) {
105         }
106     }
107 
108     static class TestDataTransferer extends DataTransferer {
109 
110         @Override
getDefaultUnicodeEncoding()111         public String getDefaultUnicodeEncoding() {
112             throw new UnsupportedOperationException("Not supported yet.");
113         }
114 
115         @Override
isLocaleDependentTextFormat(long format)116         public boolean isLocaleDependentTextFormat(long format) {
117             return false;
118         }
119 
120         @Override
isFileFormat(long format)121         public boolean isFileFormat(long format) {
122             throw new UnsupportedOperationException("Not supported yet.");
123         }
124 
125         @Override
isImageFormat(long format)126         public boolean isImageFormat(long format) {
127             throw new UnsupportedOperationException("Not supported yet.");
128         }
129 
130         @Override
getFormatForNativeAsLong(String str)131         protected Long getFormatForNativeAsLong(String str) {
132             return TEXT_FORMAT;
133         }
134 
135         @Override
getNativeForFormat(long format)136         protected String getNativeForFormat(long format) {
137             throw new UnsupportedOperationException("Not supported yet.");
138         }
139 
140         @Override
convertFileListToBytes( ArrayList<String> fileList)141         protected ByteArrayOutputStream convertFileListToBytes(
142                 ArrayList<String> fileList) throws IOException {
143             throw new UnsupportedOperationException("Not supported yet.");
144         }
145 
146         @Override
dragQueryFile(byte[] bytes)147         protected String[] dragQueryFile(byte[] bytes) {
148             throw new UnsupportedOperationException("Not supported yet.");
149         }
150 
151         @Override
imageToPlatformBytes(Image image, long format)152         protected byte[] imageToPlatformBytes(Image image, long format)
153                 throws IOException {
154             throw new UnsupportedOperationException("Not supported yet.");
155         }
156 
157         @Override
getToolkitThreadBlockedHandler()158         public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() {
159             throw new UnsupportedOperationException("Not supported yet.");
160         }
161 
162         @Override
platformImageBytesToImage(byte[] bytes, long format)163         protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException {
164             throw new UnsupportedOperationException("Not supported yet.");
165         }
166     }
167 }
168 
169