1 11 package org.eclipse.jdi.internal.connect; 12 13 import java.io.DataInputStream ; 14 import java.io.IOException ; 15 import java.io.OutputStream ; 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 31 public synchronized void close() throws IOException { 32 if (fTransport == null) 33 return; 34 35 fTransport.close(); 36 fTransport = null; 37 } 38 39 42 public synchronized boolean isOpen() { 43 return fTransport != null; 44 } 45 46 49 public byte[] readPacket() throws IOException { 50 DataInputStream stream; 51 synchronized (this) { 52 if (!isOpen()) { 53 throw new ClosedConnectionException(); 54 } 55 stream = new DataInputStream (fTransport.getInputStream()); 56 } 57 synchronized (stream) { 58 int packetLength = 0; 59 try { 60 packetLength = stream.readInt(); 61 } catch (IOException e) { 62 throw new ClosedConnectionException(); 63 } 64 65 if (packetLength < 11) { 66 throw new IOException ("JDWP Packet under 11 bytes"); } 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 83 public void writePacket(byte[] packet) throws IOException { 84 if (!isOpen()) { 85 throw new ClosedConnectionException(); 86 } 87 if (packet == null){ 88 throw new IllegalArgumentException ("Invalid JDWP Packet, packet cannot be null"); } 90 if (packet.length < 11) { 91 throw new IllegalArgumentException ("Invalid JDWP Packet, must be at least 11 bytes. PacketSize:" + packet.length); } 93 94 int packetSize = getPacketLength(packet); 95 if (packetSize < 11) { 96 throw new IllegalArgumentException ("Invalid JDWP Packet, must be at least 11 bytes. PacketSize:" + packetSize); } 98 99 if (packetSize > packet.length) { 100 throw new IllegalArgumentException ("Invalid JDWP packet: Specified length is greater than actual length"); } 102 103 OutputStream stream = null; 104 synchronized(this) { 105 if (!isOpen()) { 106 throw new ClosedConnectionException(); 107 } 108 stream = fTransport.getOutputStream(); 109 } 110 111 synchronized (stream) { 112 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 |