|
47 | 47 | * private static final int MYTYPEID = registerType(MYTYPENAME); |
48 | 48 | * private static MyTypeTransfer _instance = new MyTypeTransfer(); |
49 | 49 | * |
50 | | - * private MyTypeTransfer() {} |
| 50 | + * private MyTypeTransfer() { |
| 51 | + * } |
51 | 52 | * |
52 | | - * public static MyTypeTransfer getInstance () { |
| 53 | + * public static MyTypeTransfer getInstance() { |
53 | 54 | * return _instance; |
54 | 55 | * } |
55 | | - * public void javaToNative (Object object, TransferData transferData) { |
56 | | - * if (object == null || !(object instanceof MyType[])) return; |
57 | | - * |
58 | | - * if (isSupportedType(transferData)) { |
59 | | - * MyType[] myTypes = (MyType[]) object; |
60 | | - * try { |
61 | | - * // write data to a byte array and then ask super to convert to pMedium |
62 | | - * ByteArrayOutputStream out = new ByteArrayOutputStream(); |
63 | | - * DataOutputStream writeOut = new DataOutputStream(out); |
64 | | - * for (int i = 0, length = myTypes.length; i < length; i++){ |
65 | | - * byte[] buffer = myTypes[i].fileName.getBytes(); |
66 | | - * writeOut.writeInt(buffer.length); |
67 | | - * writeOut.write(buffer); |
68 | | - * writeOut.writeLong(myTypes[i].fileLength); |
69 | | - * writeOut.writeLong(myTypes[i].lastModified); |
70 | | - * } |
71 | | - * byte[] buffer = out.toByteArray(); |
72 | | - * writeOut.close(); |
73 | | - * |
74 | | - * super.javaToNative(buffer, transferData); |
75 | | - * |
76 | | - * } catch (IOException e) { |
77 | | - * } |
| 56 | + * |
| 57 | + * @Override |
| 58 | + * public void javaToNative(Object object, TransferData transferData) { |
| 59 | + * if (!checkMyType(object) || !isSupportedType(transferData)) { |
| 60 | + * DND.error(DND.ERROR_INVALID_DATA); |
| 61 | + * } |
| 62 | + * |
| 63 | + * MyType myType = (MyType) object; |
| 64 | + * // write data to a byte array and then ask super to convert to native |
| 65 | + * ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 66 | + * try (DataOutputStream writeOut = new DataOutputStream(out)) { |
| 67 | + * byte[] fileNameBytes = myType.fileName.getBytes(StandardCharsets.UTF_8); |
| 68 | + * writeOut.writeInt(fileNameBytes.length); |
| 69 | + * writeOut.write(fileNameBytes); |
| 70 | + * writeOut.writeLong(myType.fileLength); |
| 71 | + * writeOut.writeLong(myType.lastModified); |
| 72 | + * super.javaToNative(out.toByteArray(), transferData); |
| 73 | + * } catch (IOException e) { |
78 | 74 | * } |
79 | 75 | * } |
80 | | - * public Object nativeToJava(TransferData transferData){ |
81 | | - * |
82 | | - * if (isSupportedType(transferData)) { |
83 | | - * |
84 | | - * byte[] buffer = (byte[])super.nativeToJava(transferData); |
85 | | - * if (buffer == null) return null; |
86 | | - * |
87 | | - * MyType[] myData = new MyType[0]; |
88 | | - * try { |
89 | | - * ByteArrayInputStream in = new ByteArrayInputStream(buffer); |
90 | | - * DataInputStream readIn = new DataInputStream(in); |
91 | | - * while(readIn.available() > 20) { |
92 | | - * MyType datum = new MyType(); |
93 | | - * int size = readIn.readInt(); |
94 | | - * byte[] name = new byte[size]; |
95 | | - * readIn.read(name); |
96 | | - * datum.fileName = new String(name); |
97 | | - * datum.fileLength = readIn.readLong(); |
98 | | - * datum.lastModified = readIn.readLong(); |
99 | | - * MyType[] newMyData = new MyType[myData.length + 1]; |
100 | | - * System.arraycopy(myData, 0, newMyData, 0, myData.length); |
101 | | - * newMyData[myData.length] = datum; |
102 | | - * myData = newMyData; |
103 | | - * } |
104 | | - * readIn.close(); |
105 | | - * } catch (IOException ex) { |
106 | | - * return null; |
107 | | - * } |
108 | | - * return myData; |
| 76 | + * |
| 77 | + * @Override |
| 78 | + * public Object nativeToJava(TransferData transferData) { |
| 79 | + * if (!isSupportedType(transferData)) { |
| 80 | + * return null; |
| 81 | + * } |
| 82 | + * |
| 83 | + * byte[] buffer = (byte[]) super.nativeToJava(transferData); |
| 84 | + * if (buffer == null) { |
| 85 | + * return null; |
109 | 86 | * } |
110 | 87 | * |
| 88 | + * ByteArrayInputStream in = new ByteArrayInputStream(buffer); |
| 89 | + * try (DataInputStream readIn = new DataInputStream(in)) { |
| 90 | + * MyType myType = new MyType(); |
| 91 | + * int size = readIn.readInt(); |
| 92 | + * byte[] name = new byte[size]; |
| 93 | + * readIn.read(name); |
| 94 | + * myType.fileName = new String(name); |
| 95 | + * myType.fileLength = readIn.readLong(); |
| 96 | + * myType.lastModified = readIn.readLong(); |
| 97 | + * return myType; |
| 98 | + * } catch (IOException ex) { |
| 99 | + * } |
111 | 100 | * return null; |
112 | 101 | * } |
113 | | - * protected String[] getTypeNames(){ |
114 | | - * return new String[]{MYTYPENAME}; |
| 102 | + * |
| 103 | + * @Override |
| 104 | + * protected String[] getTypeNames() { |
| 105 | + * return new String[] { MYTYPENAME }; |
| 106 | + * } |
| 107 | + * |
| 108 | + * @Override |
| 109 | + * protected int[] getTypeIds() { |
| 110 | + * return new int[] { MYTYPEID }; |
115 | 111 | * } |
116 | | - * protected int[] getTypeIds(){ |
117 | | - * return new int[] {MYTYPEID}; |
| 112 | + * |
| 113 | + * private boolean checkMyType(Object object) { |
| 114 | + * return (object instanceof MyType); |
| 115 | + * } |
| 116 | + * |
| 117 | + * @Override |
| 118 | + * protected boolean validate(Object object) { |
| 119 | + * return checkMyType(object); |
118 | 120 | * } |
119 | 121 | * } |
120 | 122 | * </code></pre> |
|
0 commit comments