On Mon, 2005-06-27 at 17:23 -0400, Bryce McKinlay wrote:

> Yes, this looks better.
[snip]
> However, there is still a bit too much copying going on - couldn't 
> JdwpPacket.toBytes() write directly to the stream, instead of creating 
> another temporary byte[]? That can be fixed in another patch, though.

> I think I liked the old sendPacket() name better, but maybe thats just me :)

I've addressed these concerns in the following two patches. Patch #1
contains the original changes submitted. Patch #2 addresses the
JdwpPacket.toBytes issue.

?
Keith

ChangeLog #1

2005-06-30  Keith Seitz  <[EMAIL PROTECTED]>

        * gnu/classpath/jdwp/transport/JdwpConnection.java (sendEvent): New
        method.
        (_bytes): New member.
        (_doStream): New member.
        (JdwpConnection): Initialize new members.

ChangeLog #2
2005-06-30  Keith Seitz  <[EMAIL PROTECTED]>

        * gnu/classpath/jdwp/transport/JdwpPacket.java (write): New method.
        (myWrite): New abstract method.
        (toBytes): Remove.
        (myToBytes): Remove.
        * gnu/classpath/jdwp/transport/JdwpReplyPacket.java (myWrite): New
        method.
        * gnu/classpath/jdwp/transport/JdwpCommandPacket.java (myWrite): New
        method.
        * gnu/classpath/jdwp/transport/JdwpConnection.java (sendPacket): Use
        JdwpPacket.write instead of JdwpPacket.toBytes.

Index: JdwpConnection.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/classpath/jdwp/transport/JdwpConnection.java,v
retrieving revision 1.2
diff -u -p -r1.2 JdwpConnection.java
--- JdwpConnection.java	30 Jun 2005 03:18:34 -0000	1.2
+++ JdwpConnection.java	30 Jun 2005 16:15:30 -0000
@@ -40,7 +40,10 @@ exception statement from your version. *
 package gnu.classpath.jdwp.transport;
 
 import gnu.classpath.jdwp.Jdwp;
+import gnu.classpath.jdwp.event.Event;
+import gnu.classpath.jdwp.event.EventRequest;
 
+import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
@@ -64,7 +67,8 @@ public class JdwpConnection
   extends Thread
 {
   // The JDWP handshake
-  private static final byte[] _HANDSHAKE = {'J', 'D', 'W', 'P', '-', 'H', 'a', 'n', 'd', 's', 'h', 'a', 'k', 'e'};
+  private static final byte[] _HANDSHAKE = {'J', 'D', 'W', 'P', '-', 'H', 'a',
+					    'n', 'd', 's', 'h', 'a', 'k', 'e'};
 
   // Transport method
   private ITransport _transport;
@@ -81,6 +85,12 @@ public class JdwpConnection
   // Output stream from transprot
   private DataOutputStream _outStream;
 
+  // A buffer used to construct the packet data
+  private ByteArrayOutputStream _bytes;
+
+  // A DataOutputStream for the byte buffer
+  private DataOutputStream _doStream;
+
   /**
    * Creates a new <code>JdwpConnection</code> instance
    *
@@ -91,6 +101,8 @@ public class JdwpConnection
     _transport = transport;
     _commandQueue = new ArrayList ();
     _shutdown = false;
+    _bytes = new ByteArrayOutputStream ();
+    _doStream = new DataOutputStream (_bytes);
   }
 
   /**
@@ -241,7 +253,7 @@ public class JdwpConnection
    * Send a packet to the debugger
    *
    * @param pkt a <code>JdwpPacket</code> to send
-   * @throws TransportException
+   * @throws IOException
    */
   public void sendPacket (JdwpPacket pkt)
     throws IOException
@@ -251,6 +263,28 @@ public class JdwpConnection
   }
 
   /**
+   * Send an event notification to the debugger
+   *
+   * @param request  the debugger request that wanted this event
+   * @param event    the event
+   * @throws IOException
+   */
+  public void sendEvent (EventRequest request, Event event)
+    throws IOException
+  {
+    JdwpPacket pkt;
+
+    synchronized (_bytes)
+      {
+	_bytes.reset ();
+	pkt = event.toPacket (_doStream, request);
+	pkt.setData (_bytes.toByteArray ());
+      }
+
+    sendPacket (pkt);
+  }
+
+  /**
    * Shutdown the connection
    */
   public void shutdown ()
--- JdwpConnection.java.o	2005-06-30 09:15:40.000000000 -0700
+++ JdwpConnection.java	2005-06-30 09:15:52.000000000 -0700
@@ -258,8 +258,7 @@ public class JdwpConnection
   public void sendPacket (JdwpPacket pkt)
     throws IOException
   {
-    byte[] data = pkt.toBytes ();
-    _outStream.write (data, 0, data.length);
+    pkt.write (_outStream);
   }
 
   /**
Index: JdwpPacket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/classpath/jdwp/transport/JdwpPacket.java,v
retrieving revision 1.3
diff -u -p -r1.3 JdwpPacket.java
--- JdwpPacket.java	30 Jun 2005 03:18:34 -0000	1.3
+++ JdwpPacket.java	30 Jun 2005 16:16:54 -0000
@@ -39,6 +39,9 @@ exception statement from your version. *
 
 package gnu.classpath.jdwp.transport;
 
+import java.io.DataOutputStream;
+import java.io.IOException;
+
 /**
  * All command and reply packets in JDWP share
  * common header type information:
@@ -238,49 +241,38 @@ public abstract class JdwpPacket
     return null;
   }
 
-  // Put subclass information into bytes
-  protected abstract int myToBytes (byte[] bytes, int index);
+  /**
+   * Put subclass information onto the stream
+   *
+   * @param dos the output stream to which to write
+   */
+  protected abstract void myWrite (DataOutputStream dos)
+    throws IOException;
 
-  // Convert this packet to it byte representation (ready to send on the wire)
-  // NOTE: All integers should be big-endian.
-  public byte[] toBytes ()
+  /**
+   * Writes the packet to the output stream
+   *
+   * @param dos  the output stream to which to write the packet
+   */
+  public void write (DataOutputStream dos)
+    throws IOException
   {
-    // Allocate a new array to hold contents of packet
+    // length
     int length = getLength ();
-    byte[] bytes = new byte[length];
-	
-    int i = 0;
+    dos.writeInt (length);
 
-    //
-    // Packet layout: length, id, flags, packet-specific, data (optional)
-    //
-
-    // 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 ();
+    // ID
+    dos.writeInt (getId ());
 
     // flag
-    bytes[i++] = getFlags ();
+    dos.writeByte (getFlags ());
 
     // packet-specific stuff
-    i += myToBytes (bytes, i);
+    myWrite (dos);
 
     // data (if any)
     byte[] data = getData ();
-    if (data.length > 0 && i < length)
-      {
-	// Would it pay to be over cautious?
-	System.arraycopy (data, 0, bytes, i, data.length);
-      }
-
-    return bytes;
+    if (data != null && data.length > 0)
+      dos.write (data, 0, data.length);
   }
 }
Index: JdwpCommandPacket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/classpath/jdwp/transport/JdwpCommandPacket.java,v
retrieving revision 1.2
diff -u -p -r1.2 JdwpCommandPacket.java
--- JdwpCommandPacket.java	30 Jun 2005 03:18:34 -0000	1.2
+++ JdwpCommandPacket.java	30 Jun 2005 16:17:13 -0000
@@ -39,6 +39,9 @@ exception statement from your version. *
 
 package gnu.classpath.jdwp.transport;
 
+import java.io.DataOutputStream;
+import java.io.IOException;
+
 /**
  * A class representing a JDWP command packet.
  * This class adds command set and command to the packet header
@@ -137,13 +140,10 @@ public class JdwpCommandPacket extends J
   }
 
   // Writes the command packet data into the given buffer
-  protected int myToBytes (byte[] bytes, int index)
+  protected void myWrite (DataOutputStream dos)
+    throws IOException
   {
-    // Need to add command set & command
-    int i = 0;
-    bytes[index + i++] = getCommandSet ();
-    bytes[index + i++] = getCommand ();
-
-    return i;
+    dos.writeByte (getCommandSet ());
+    dos.writeByte (getCommand ());
   }
 }
Index: JdwpReplyPacket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/classpath/jdwp/transport/JdwpReplyPacket.java,v
retrieving revision 1.2
diff -u -p -r1.2 JdwpReplyPacket.java
--- JdwpReplyPacket.java	30 Jun 2005 03:18:34 -0000	1.2
+++ JdwpReplyPacket.java	30 Jun 2005 16:17:33 -0000
@@ -39,6 +39,9 @@ exception statement from your version. *
 
 package gnu.classpath.jdwp.transport;
 
+import java.io.DataOutputStream;
+import java.io.IOException;
+
 /**
  * A class represents a JDWP reply packet.
  * This class adds an error code to the packet header information
@@ -115,13 +118,9 @@ public class JdwpReplyPacket extends Jdw
   }
 
   // Writes the command packet data into the given buffer
-  protected int myToBytes (byte[] bytes, int index)
-  {
-    // Need to add error code
-    int i = 0;
-    bytes[index + i++] = (byte) (getErrorCode () >>> 8);
-    bytes[index + i++] = (byte) getErrorCode ();
-    
-    return i;
+  protected void myWrite (DataOutputStream dos)
+    throws IOException
+ {
+    dos.writeShort (getErrorCode ());
   }
 }
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to