1 11 package org.eclipse.jdi.internal.jdwp; 12 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.DataInputStream ; 16 import java.io.IOException ; 17 import java.lang.reflect.Field ; 18 import java.lang.reflect.Modifier ; 19 20 25 public abstract class JdwpPacket { 26 27 public static final byte FLAG_REPLY_PACKET = (byte)0x80; 28 protected static final int MIN_PACKET_LENGTH = 11; 29 30 31 private static String [] fgFlagStrings = null; 32 33 34 protected int fId = 0; 35 protected byte fFlags = 0; 36 protected byte[] fDataBuf = null; 37 38 41 void setId(int id) { 42 fId = id; 43 } 44 45 48 public int getId() { 49 return fId; 50 } 51 52 55 void setFlags(byte flags) { 56 fFlags = flags; 57 } 58 59 62 public byte getFlags() { 63 return fFlags; 64 } 65 66 69 public int getLength() { 70 return MIN_PACKET_LENGTH + getDataLength(); 71 } 72 73 76 public int getDataLength() { 77 return fDataBuf == null ? 0 : fDataBuf.length; 78 } 79 80 83 public byte[] data() { 84 return fDataBuf; 85 } 86 87 90 public DataInputStream dataInStream() { 91 if (fDataBuf != null) { 92 return new DataInputStream (new ByteArrayInputStream (fDataBuf)); 93 } 94 95 return new DataInputStream (new ByteArrayInputStream (new byte[0])); 96 } 97 98 101 public void setData(byte[] data) { 102 fDataBuf = data; 103 } 104 105 108 protected abstract int readSpecificHeaderFields(byte[] bytes, int index) throws IOException ; 109 110 113 protected abstract int writeSpecificHeaderFields(byte[] bytes, int index) throws IOException ; 114 115 118 public static JdwpPacket build(byte[] bytes) throws IOException { 119 int a = (bytes[0]&0xff) << 24; 121 int b = (bytes[1]&0xff) << 16; 122 int c = (bytes[2]&0xff) << 8; 123 int d = (bytes[3]&0xff) << 0; 124 int packetLength = a+b+c+d; 125 126 a = (bytes[4]&0xff) << 24; 128 b = (bytes[5]&0xff) << 16; 129 c = (bytes[6]&0xff) << 8; 130 d = (bytes[7]&0xff) << 0; 131 int id = a+b+c+d; 132 133 byte flags = bytes[8]; 135 136 JdwpPacket packet; 138 if ((flags & FLAG_REPLY_PACKET) != 0) 139 packet = new JdwpReplyPacket(); 140 else 141 packet = new JdwpCommandPacket(); 142 143 packet.setId(id); 145 packet.setFlags(flags); 146 147 int index = 9; 149 index += packet.readSpecificHeaderFields(bytes, 9); 150 if (packetLength - MIN_PACKET_LENGTH > 0) { 151 packet.fDataBuf = new byte[packetLength - MIN_PACKET_LENGTH]; 152 System.arraycopy(bytes, index, packet.fDataBuf, 0, packet.fDataBuf.length); 153 } 154 155 return packet; 156 } 157 158 public byte[] getPacketAsBytes() throws IOException { 159 int len = getLength(); 160 byte[] bytes = new byte[len]; 161 162 bytes[0] = (byte) (len >>> 24); 164 bytes[1] = (byte) (len >>> 16); 165 bytes[2] = (byte) (len >>> 8); 166 bytes[3] = (byte) (len >>> 0); 167 168 int id = getId(); 170 bytes[4] = (byte) (id >>> 24); 171 bytes[5] = (byte) (id >>> 16); 172 bytes[6] = (byte) (id >>> 8); 173 bytes[7] = (byte) (id >>> 0); 174 175 bytes[8] = getFlags(); 177 178 int index = 9; 180 index += writeSpecificHeaderFields(bytes, index); 181 182 if (index < len && fDataBuf!=null) { 183 System.arraycopy(fDataBuf, 0, bytes, index, fDataBuf.length); 185 } 186 return bytes; 187 } 188 189 192 public static void getConstantMaps() { 193 if (fgFlagStrings != null) { 194 return; 195 } 196 197 Field [] fields = JdwpPacket.class.getDeclaredFields(); 198 fgFlagStrings = new String [8]; 199 200 for (int i = 0; i < fields.length; i++) { 201 Field field = fields[i]; 202 if ((field.getModifiers() & Modifier.PUBLIC) == 0 || (field.getModifiers() & Modifier.STATIC) == 0 || (field.getModifiers() & Modifier.FINAL) == 0) { 203 continue; 204 } 205 206 String name = field.getName(); 207 if (!name.startsWith("FLAG_")) { continue; 209 } 210 211 name = name.substring(5); 212 213 try { 214 byte value = field.getByte(null); 215 216 for (int j = 0; j < fgFlagStrings.length; j++) { 217 if ((1 << j & value) != 0) { 218 fgFlagStrings[j]= name; 219 break; 220 } 221 } 222 } catch (IllegalAccessException e) { 223 } catch (IllegalArgumentException e) { 225 } 229 } 230 } 231 232 235 public static String [] getFlagMap() { 236 getConstantMaps(); 237 return fgFlagStrings; 238 } 239 } 240 | Popular Tags |