1 11 package org.eclipse.jdi.internal.spy; 12 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.DataInputStream ; 16 import java.io.DataOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.lang.reflect.Field ; 21 import java.lang.reflect.Modifier ; 22 23 28 public abstract class JdwpPacket { 29 30 public static final byte FLAG_REPLY_PACKET = (byte)0x80; 31 protected static final int MIN_PACKET_LENGTH = 11; 32 33 34 private static String [] fgFlagStrings = null; 35 36 37 protected int fId = 0; 38 protected byte fFlags = 0; 39 protected byte[] fDataBuf = null; 40 41 44 void setId(int id) { 45 fId = id; 46 } 47 48 51 public int getId() { 52 return fId; 53 } 54 55 58 void setFlags(byte flags) { 59 fFlags = flags; 60 } 61 62 65 public byte getFlags() { 66 return fFlags; 67 } 68 69 72 public int getLength() { 73 return MIN_PACKET_LENGTH + getDataLength(); 74 } 75 76 79 public int getDataLength() { 80 return fDataBuf == null ? 0 : fDataBuf.length; 81 } 82 83 86 public byte[] data() { 87 return fDataBuf; 88 } 89 90 93 public DataInputStream dataInStream() { 94 if (fDataBuf != null) { 95 return new DataInputStream (new ByteArrayInputStream (fDataBuf)); 96 } 97 98 return new DataInputStream (new ByteArrayInputStream (new byte[0])); 99 } 100 101 104 public void setData(byte[] data) { 105 fDataBuf = data; 106 } 107 108 111 protected abstract void readSpecificHeaderFields(DataInputStream dataInStream) throws IOException ; 112 113 116 protected abstract void writeSpecificHeaderFields(DataOutputStream dataOutStream) throws IOException ; 117 118 121 public static JdwpPacket read(InputStream inStream) throws IOException { 122 DataInputStream dataInStream = new DataInputStream (inStream); 123 124 int packetLength = dataInStream.readInt(); 126 int id = dataInStream.readInt(); 127 byte flags = dataInStream.readByte(); 128 129 JdwpPacket packet; 131 if ((flags & FLAG_REPLY_PACKET) != 0) 132 packet = new JdwpReplyPacket(); 133 else 134 packet = new JdwpCommandPacket(); 135 136 packet.setId(id); 138 packet.setFlags(flags); 139 140 packet.readSpecificHeaderFields(dataInStream); 142 if (packetLength - MIN_PACKET_LENGTH > 0) { 143 packet.fDataBuf = new byte[packetLength - MIN_PACKET_LENGTH]; 144 dataInStream.readFully(packet.fDataBuf); 145 } 146 147 return packet; 148 } 149 150 153 public void write(OutputStream outStream) throws IOException { 154 DataOutputStream dataOutStream = new DataOutputStream (outStream); 155 156 writeHeader(dataOutStream); 157 writeData(dataOutStream); 158 } 159 160 163 protected void writeHeader(DataOutputStream dataOutStream) throws IOException { 164 dataOutStream.writeInt(getLength()); 165 dataOutStream.writeInt(getId()); 166 dataOutStream.writeByte(getFlags()); 167 writeSpecificHeaderFields(dataOutStream); 168 } 169 170 173 protected void writeData(DataOutputStream dataOutStream) throws IOException { 174 if (fDataBuf != null) { 175 dataOutStream.write(fDataBuf); 176 } 177 } 178 179 180 183 public static void getConstantMaps() { 184 if (fgFlagStrings != null) { 185 return; 186 } 187 188 Field [] fields = JdwpPacket.class.getDeclaredFields(); 189 fgFlagStrings = new String [8]; 190 191 for (int i = 0; i < fields.length; i++) { 192 Field field = fields[i]; 193 if ((field.getModifiers() & Modifier.PUBLIC) == 0 || (field.getModifiers() & Modifier.STATIC) == 0 || (field.getModifiers() & Modifier.FINAL) == 0) { 194 continue; 195 } 196 197 String name = field.getName(); 198 if (!name.startsWith("FLAG_")) { continue; 200 } 201 202 name = name.substring(5); 203 204 try { 205 byte value = field.getByte(null); 206 207 for (int j = 0; j < fgFlagStrings.length; j++) { 208 if ((1 << j & value) != 0) { 209 fgFlagStrings[j]= name; 210 break; 211 } 212 } 213 } catch (IllegalAccessException e) { 214 } catch (IllegalArgumentException e) { 216 } 220 } 221 } 222 223 226 public static String [] getFlagMap() { 227 getConstantMaps(); 228 return fgFlagStrings; 229 } 230 } 231 | Popular Tags |