Mercurial > projects > dwt-mac
comparison dwt/dnd/ByteArrayTransfer.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
11 module dwt.dnd; | |
12 | |
13 | |
14 /** | |
15 * The class <code>ByteArrayTransfer</code> provides a platform specific | |
16 * mechanism for converting a java <code>byte[]</code> to a platform | |
17 * specific representation of the byte array and vice versa. See | |
18 * <code>Transfer</code> for additional information. | |
19 * | |
20 * <p><code>ByteArrayTransfer</code> is never used directly but is sub-classed | |
21 * by transfer agents that convert between data in a java format such as a | |
22 * <code>String</code> and a platform specific byte array. | |
23 * | |
24 * <p>If the data you are converting <b>does not</b> map to a | |
25 * <code>byte[]</code>, you should sub-class <code>Transfer</code> directly | |
26 * and do your own mapping to a platform data type.</p> | |
27 * | |
28 * <p>The following snippet shows a subclass of ByteArrayTransfer that transfers | |
29 * data defined by the class <code>MyType</code>.</p> | |
30 * | |
31 * <pre><code> | |
32 * public class MyType { | |
33 * public String fileName; | |
34 * public long fileLength; | |
35 * public long lastModified; | |
36 * } | |
37 * </code></pre> | |
38 * | |
39 * <pre><code> | |
40 * public class MyTypeTransfer : ByteArrayTransfer { | |
41 * | |
42 * private static final String MYTYPENAME = "my_type_name"; | |
43 * private static final int MYTYPEID = registerType(MYTYPENAME); | |
44 * private static MyTypeTransfer _instance = new MyTypeTransfer(); | |
45 * | |
46 * private MyTypeTransfer() {} | |
47 * | |
48 * public static MyTypeTransfer getInstance () { | |
49 * return _instance; | |
50 * } | |
51 * public void javaToNative (Object object, TransferData transferData) { | |
52 * if (object is null || !(object instanceof MyType[])) return; | |
53 * | |
54 * if (isSupportedType(transferData)) { | |
55 * MyType[] myTypes = (MyType[]) object; | |
56 * try { | |
57 * // write data to a byte array and then ask super to convert to pMedium | |
58 * ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
59 * DataOutputStream writeOut = new DataOutputStream(out); | |
60 * for (int i = 0, length = myTypes.length; i < length; i++){ | |
61 * byte[] buffer = myTypes[i].fileName.getBytes(); | |
62 * writeOut.writeInt(buffer.length); | |
63 * writeOut.write(buffer); | |
64 * writeOut.writeLong(myTypes[i].fileLength); | |
65 * writeOut.writeLong(myTypes[i].lastModified); | |
66 * } | |
67 * byte[] buffer = out.toByteArray(); | |
68 * writeOut.close(); | |
69 * | |
70 * super.javaToNative(buffer, transferData); | |
71 * | |
72 * } catch (IOException e) { | |
73 * } | |
74 * } | |
75 * } | |
76 * public Object nativeToJava(TransferData transferData){ | |
77 * | |
78 * if (isSupportedType(transferData)) { | |
79 * | |
80 * byte[] buffer = (byte[])super.nativeToJava(transferData); | |
81 * if (buffer is null) return null; | |
82 * | |
83 * MyType[] myData = new MyType[0]; | |
84 * try { | |
85 * ByteArrayInputStream in = new ByteArrayInputStream(buffer); | |
86 * DataInputStream readIn = new DataInputStream(in); | |
87 * while(readIn.available() > 20) { | |
88 * MyType datum = new MyType(); | |
89 * int size = readIn.readInt(); | |
90 * byte[] name = new byte[size]; | |
91 * readIn.read(name); | |
92 * datum.fileName = new String(name); | |
93 * datum.fileLength = readIn.readLong(); | |
94 * datum.lastModified = readIn.readLong(); | |
95 * MyType[] newMyData = new MyType[myData.length + 1]; | |
96 * System.arraycopy(myData, 0, newMyData, 0, myData.length); | |
97 * newMyData[myData.length] = datum; | |
98 * myData = newMyData; | |
99 * } | |
100 * readIn.close(); | |
101 * } catch (IOException ex) { | |
102 * return null; | |
103 * } | |
104 * return myData; | |
105 * } | |
106 * | |
107 * return null; | |
108 * } | |
109 * protected String[] getTypeNames(){ | |
110 * return new String[]{MYTYPENAME}; | |
111 * } | |
112 * protected int[] getTypeIds(){ | |
113 * return new int[] {MYTYPEID}; | |
114 * } | |
115 * } | |
116 * </code></pre> | |
117 */ | |
118 public abstract class ByteArrayTransfer : Transfer { | |
119 | |
120 public TransferData[] getSupportedTypes() { | |
121 int[] types = getTypeIds(); | |
122 TransferData[] data = new TransferData[types.length]; | |
123 for (int i = 0; i < types.length; i++) { | |
124 data[i] = new TransferData(); | |
125 data[i].type = types[i]; | |
126 } | |
127 return data; | |
128 } | |
129 | |
130 public bool isSupportedType(TransferData transferData){ | |
131 if (transferData is null) return false; | |
132 int[] types = getTypeIds(); | |
133 for (int i = 0; i < types.length; i++) { | |
134 if (transferData.type is types[i]) return true; | |
135 } | |
136 return false; | |
137 } | |
138 | |
139 /** | |
140 * This implementation of <code>javaToNative</code> converts a java | |
141 * <code>byte[]</code> to a platform specific representation. For additional | |
142 * information see <code>Transfer#javaToNative</code>. | |
143 * | |
144 * @see Transfer#javaToNative | |
145 * | |
146 * @param object a java <code>byte[]</code> containing the data to be converted | |
147 * @param transferData an empty <code>TransferData</code> object; this | |
148 * object will be filled in on return with the platform specific format of the data | |
149 */ | |
150 protected void javaToNative (Object object, TransferData transferData) { | |
151 if (!checkByteArray(object) && !isSupportedType(transferData)) { | |
152 DND.error(DND.ERROR_INVALID_DATA); | |
153 } | |
154 byte[] orig = (byte[])object; | |
155 byte[] buffer = new byte[orig.length]; | |
156 System.arraycopy(orig, 0, buffer, 0, orig.length); | |
157 transferData.data = new byte[1][]; | |
158 transferData.data[0] = buffer; | |
159 transferData.result = 0; | |
160 } | |
161 | |
162 /** | |
163 * This implementation of <code>nativeToJava</code> converts a platform specific | |
164 * representation of a byte array to a java <code>byte[]</code>. | |
165 * For additional information see <code>Transfer#nativeToJava</code>. | |
166 * | |
167 * @see Transfer#nativeToJava | |
168 * | |
169 * @param transferData the platform specific representation of the data to be | |
170 * been converted | |
171 * @return a java <code>byte[]</code> containing the converted data if the | |
172 * conversion was successful; otherwise null | |
173 */ | |
174 protected Object nativeToJava(TransferData transferData) { | |
175 if (!isSupportedType(transferData) || transferData.data is null) return null; | |
176 if (transferData.data.length is 0 || transferData.data[0].length is 0) return null; | |
177 return transferData.data[0]; | |
178 } | |
179 bool checkByteArray(Object object) { | |
180 return (object !is null && object instanceof byte[] && ((byte[])object).length > 0); | |
181 } | |
182 } |