1 11 package org.eclipse.jdt.internal.ui.refactoring.reorg; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.DataInputStream ; 16 import java.io.DataOutputStream ; 17 import java.io.IOException ; 18 19 import org.eclipse.core.runtime.Assert; 20 21 import org.eclipse.swt.dnd.ByteArrayTransfer; 22 import org.eclipse.swt.dnd.TransferData; 23 24 import org.eclipse.jdt.internal.corext.refactoring.TypedSource; 25 26 public class TypedSourceTransfer extends ByteArrayTransfer { 27 28 31 private static final TypedSourceTransfer fgInstance = new TypedSourceTransfer(); 32 33 private static final String TYPE_NAME = "typed-source-transfer-format:" + System.currentTimeMillis() + ":" + fgInstance.hashCode(); 37 private static final int TYPEID = registerType(TYPE_NAME); 38 39 private TypedSourceTransfer() { 40 } 41 42 47 public static TypedSourceTransfer getInstance() { 48 return fgInstance; 49 } 50 51 54 protected int[] getTypeIds() { 55 return new int[] {TYPEID}; 56 } 57 58 63 protected String [] getTypeNames() { 64 return new String [] {TYPE_NAME}; 65 } 66 67 70 protected void javaToNative(Object data, TransferData transferData) { 71 if (! (data instanceof TypedSource[])) 72 return; 73 TypedSource[] sources = (TypedSource[]) data; 74 75 82 83 try { 84 ByteArrayOutputStream out = new ByteArrayOutputStream (); 85 DataOutputStream dataOut = new DataOutputStream (out); 86 87 dataOut.writeInt(sources.length); 88 89 for (int i = 0; i < sources.length; i++) { 90 writeJavaElement(dataOut, sources[i]); 91 } 92 93 dataOut.close(); 94 out.close(); 95 96 super.javaToNative(out.toByteArray(), transferData); 97 } catch (IOException e) { 98 } 100 } 101 102 105 protected Object nativeToJava(TransferData transferData) { 106 107 byte[] bytes = (byte[]) super.nativeToJava(transferData); 108 if (bytes == null) 109 return null; 110 DataInputStream in = new DataInputStream (new ByteArrayInputStream (bytes)); 111 try { 112 int count = in.readInt(); 113 TypedSource[] results = new TypedSource[count]; 114 for (int i = 0; i < count; i++) { 115 results[i] = readJavaElement(in); 116 Assert.isNotNull(results[i]); 117 } 118 in.close(); 119 return results; 120 } catch (IOException e) { 121 return null; 122 } 123 } 124 125 private static TypedSource readJavaElement(DataInputStream dataIn) throws IOException { 126 int type= dataIn.readInt(); 127 String source= dataIn.readUTF(); 128 return TypedSource.create(source, type); 129 } 130 131 private static void writeJavaElement(DataOutputStream dataOut, TypedSource sourceReference) throws IOException { 132 dataOut.writeInt(sourceReference.getType()); 133 dataOut.writeUTF(sourceReference.getSource()); 134 } 135 } 136 137 | Popular Tags |