KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > connect > SocketConnection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal.connect;
12
13 import java.io.DataInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.OutputStream JavaDoc;
16
17 import com.sun.jdi.connect.spi.ClosedConnectionException;
18 import com.sun.jdi.connect.spi.Connection;
19
20 public class SocketConnection extends Connection {
21
22     private SocketTransportService fTransport;
23
24     SocketConnection(SocketTransportService transport) {
25         fTransport = transport;
26     }
27
28     /* (non-Javadoc)
29      * @see com.sun.jdi.connect.spi.Connection#close()
30      */

31     public synchronized void close() throws IOException JavaDoc {
32         if (fTransport == null)
33             return;
34
35         fTransport.close();
36         fTransport = null;
37     }
38
39     /* (non-Javadoc)
40      * @see com.sun.jdi.connect.spi.Connection#isOpen()
41      */

42     public synchronized boolean isOpen() {
43         return fTransport != null;
44     }
45
46     /* (non-Javadoc)
47      * @see com.sun.jdi.connect.spi.Connection#readPacket()
48      */

49     public byte[] readPacket() throws IOException JavaDoc {
50         DataInputStream JavaDoc stream;
51         synchronized (this) {
52             if (!isOpen()) {
53                 throw new ClosedConnectionException();
54             }
55             stream = new DataInputStream JavaDoc(fTransport.getInputStream());
56         }
57         synchronized (stream) {
58             int packetLength = 0;
59             try {
60                 packetLength = stream.readInt();
61             } catch (IOException JavaDoc e) {
62                 throw new ClosedConnectionException();
63             }
64
65             if (packetLength < 11) {
66                 throw new IOException JavaDoc("JDWP Packet under 11 bytes"); //$NON-NLS-1$
67
}
68
69             byte[] packet = new byte[packetLength];
70             packet[0] = (byte) ((packetLength >>> 24) & 0xFF);
71             packet[1] = (byte) ((packetLength >>> 16) & 0xFF);
72             packet[2] = (byte) ((packetLength >>> 8) & 0xFF);
73             packet[3] = (byte) ((packetLength >>> 0) & 0xFF);
74
75             stream.readFully(packet, 4, packetLength - 4);
76             return packet;
77         }
78     }
79
80     /* (non-Javadoc)
81      * @see com.sun.jdi.connect.spi.Connection#writePacket(byte[])
82      */

83     public void writePacket(byte[] packet) throws IOException JavaDoc {
84         if (!isOpen()) {
85             throw new ClosedConnectionException();
86         }
87         if (packet == null){
88             throw new IllegalArgumentException JavaDoc("Invalid JDWP Packet, packet cannot be null"); //$NON-NLS-1$
89
}
90         if (packet.length < 11) {
91             throw new IllegalArgumentException JavaDoc("Invalid JDWP Packet, must be at least 11 bytes. PacketSize:" + packet.length); //$NON-NLS-1$
92
}
93
94         int packetSize = getPacketLength(packet);
95         if (packetSize < 11) {
96             throw new IllegalArgumentException JavaDoc("Invalid JDWP Packet, must be at least 11 bytes. PacketSize:" + packetSize); //$NON-NLS-1$
97
}
98
99         if (packetSize > packet.length) {
100             throw new IllegalArgumentException JavaDoc("Invalid JDWP packet: Specified length is greater than actual length"); //$NON-NLS-1$
101
}
102
103         OutputStream JavaDoc stream = null;
104         synchronized(this) {
105             if (!isOpen()) {
106                 throw new ClosedConnectionException();
107             }
108             stream = fTransport.getOutputStream();
109         }
110         
111         synchronized (stream) {
112             // packet.length can be > packetSize. Sending too much will cause
113
// errors on the other side
114
stream.write(packet, 0, packetSize);
115         }
116     }
117
118     private int getPacketLength(byte[] packet) {
119         int len = 0;
120         if (packet.length >= 4) {
121             len = (((packet[0] & 0xFF) << 24) + ((packet[1] & 0xFF) << 16) + ((packet[2] & 0xFF) << 8) + ((packet[3] & 0xFF) << 0));
122         }
123         return len;
124     }
125 }
126
Popular Tags