39 |
|
|
40 |
package gnu.classpath.jdwp.transport; |
package gnu.classpath.jdwp.transport; |
41 |
|
|
42 |
|
import java.io.DataOutputStream; |
43 |
|
import java.io.IOException; |
44 |
|
|
45 |
/** |
/** |
46 |
* All command and reply packets in JDWP share |
* All command and reply packets in JDWP share |
47 |
* common header type information: |
* common header type information: |
241 |
return null; |
return null; |
242 |
} |
} |
243 |
|
|
244 |
// Put subclass information into bytes |
/** |
245 |
protected abstract int myToBytes (byte[] bytes, int index); |
* Put subclass information onto the stream |
246 |
|
* |
247 |
|
* @param dos the output stream to which to write |
248 |
|
*/ |
249 |
|
protected abstract void myWrite (DataOutputStream dos) |
250 |
|
throws IOException; |
251 |
|
|
252 |
// Convert this packet to it byte representation (ready to send on the wire) |
/** |
253 |
// NOTE: All integers should be big-endian. |
* Writes the packet to the output stream |
254 |
public byte[] toBytes () |
* |
255 |
|
* @param dos the output stream to which to write the packet |
256 |
|
*/ |
257 |
|
public void write (DataOutputStream dos) |
258 |
|
throws IOException |
259 |
{ |
{ |
260 |
// Allocate a new array to hold contents of packet |
// length |
261 |
int length = getLength (); |
int length = getLength (); |
262 |
byte[] bytes = new byte[length]; |
dos.writeInt (length); |
|
|
|
|
int i = 0; |
|
263 |
|
|
264 |
// |
// ID |
265 |
// Packet layout: length, id, flags, packet-specific, data (optional) |
dos.writeInt (getId ()); |
|
// |
|
|
|
|
|
// length |
|
|
bytes[i++] = (byte) (length >>> 24); |
|
|
bytes[i++] = (byte) (length >>> 16); |
|
|
bytes[i++] = (byte) (length >>> 8); |
|
|
bytes[i++] = (byte) length; |
|
|
|
|
|
// id |
|
|
bytes[i++] = (byte) (getId () >>> 24); |
|
|
bytes[i++] = (byte) (getId () >>> 16); |
|
|
bytes[i++] = (byte) (getId () >>> 8); |
|
|
bytes[i++] = (byte) getId (); |
|
266 |
|
|
267 |
// flag |
// flag |
268 |
bytes[i++] = getFlags (); |
dos.writeByte (getFlags ()); |
269 |
|
|
270 |
// packet-specific stuff |
// packet-specific stuff |
271 |
i += myToBytes (bytes, i); |
myWrite (dos); |
272 |
|
|
273 |
// data (if any) |
// data (if any) |
274 |
byte[] data = getData (); |
byte[] data = getData (); |
275 |
if (data.length > 0 && i < length) |
if (data != null && data.length > 0) |
276 |
{ |
dos.write (data, 0, data.length); |
|
// Would it pay to be over cautious? |
|
|
System.arraycopy (data, 0, bytes, i, data.length); |
|
|
} |
|
|
|
|
|
return bytes; |
|
277 |
} |
} |
278 |
} |
} |