Skip to content

Commit c672e12

Browse files
jonahgrahamakurtakov
authored andcommitted
Modernize ByteArrayTransfer javadoc
Now that ByteArrayTransfer has a unit test, the unit test verifies that the code in this Javadoc operates correctly. This commit synchronizes the javadoc back to the contents of the test classes. This includes - Simplifying the code a little - Changing the transfer to support a single MyType instead of an array of them (which simplifies the example quite a bit) - Add `@Override` to the appropriate methods - Format the code to Eclipse default settings - Validating the objects Part of #2126
1 parent 0201e52 commit c672e12

File tree

4 files changed

+184
-174
lines changed

4 files changed

+184
-174
lines changed

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/cocoa/org/eclipse/swt/dnd/ByteArrayTransfer.java

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -47,74 +47,76 @@
4747
* private static final int MYTYPEID = registerType(MYTYPENAME);
4848
* private static MyTypeTransfer _instance = new MyTypeTransfer();
4949
*
50-
* private MyTypeTransfer() {}
50+
* private MyTypeTransfer() {
51+
* }
5152
*
52-
* public static MyTypeTransfer getInstance () {
53+
* public static MyTypeTransfer getInstance() {
5354
* return _instance;
5455
* }
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) {
7874
* }
7975
* }
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;
10986
* }
11087
*
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+
* }
111100
* return null;
112101
* }
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 };
115111
* }
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);
118120
* }
119121
* }
120122
* </code></pre>

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/gtk/org/eclipse/swt/dnd/ByteArrayTransfer.java

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -47,74 +47,76 @@
4747
* private static final int MYTYPEID = registerType(MYTYPENAME);
4848
* private static MyTypeTransfer _instance = new MyTypeTransfer();
4949
*
50-
* private MyTypeTransfer() {}
50+
* private MyTypeTransfer() {
51+
* }
5152
*
52-
* public static MyTypeTransfer getInstance () {
53+
* public static MyTypeTransfer getInstance() {
5354
* return _instance;
5455
* }
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 &lt; 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+
* &#64;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) {
7874
* }
7975
* }
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() &gt; 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+
* &#64;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;
10986
* }
11087
*
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+
* }
111100
* return null;
112101
* }
113-
* protected String[] getTypeNames(){
114-
* return new String[]{MYTYPENAME};
102+
*
103+
* &#64;Override
104+
* protected String[] getTypeNames() {
105+
* return new String[] { MYTYPENAME };
106+
* }
107+
*
108+
* &#64;Override
109+
* protected int[] getTypeIds() {
110+
* return new int[] { MYTYPEID };
115111
* }
116-
* protected int[] getTypeIds(){
117-
* return new int[] {MYTYPEID};
112+
*
113+
* private boolean checkMyType(Object object) {
114+
* return (object instanceof MyType);
115+
* }
116+
*
117+
* &#64;Override
118+
* protected boolean validate(Object object) {
119+
* return checkMyType(object);
118120
* }
119121
* }
120122
* </code></pre>

0 commit comments

Comments
 (0)