001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdi.internal.connect;
011:
012: import java.io.DataInputStream;
013: import java.io.IOException;
014: import java.io.OutputStream;
015:
016: import com.sun.jdi.connect.spi.ClosedConnectionException;
017: import com.sun.jdi.connect.spi.Connection;
018:
019: public class SocketConnection extends Connection {
020:
021: private SocketTransportService fTransport;
022:
023: SocketConnection(SocketTransportService transport) {
024: fTransport = transport;
025: }
026:
027: /* (non-Javadoc)
028: * @see com.sun.jdi.connect.spi.Connection#close()
029: */
030: public synchronized void close() throws IOException {
031: if (fTransport == null)
032: return;
033:
034: fTransport.close();
035: fTransport = null;
036: }
037:
038: /* (non-Javadoc)
039: * @see com.sun.jdi.connect.spi.Connection#isOpen()
040: */
041: public synchronized boolean isOpen() {
042: return fTransport != null;
043: }
044:
045: /* (non-Javadoc)
046: * @see com.sun.jdi.connect.spi.Connection#readPacket()
047: */
048: public byte[] readPacket() throws IOException {
049: DataInputStream stream;
050: synchronized (this ) {
051: if (!isOpen()) {
052: throw new ClosedConnectionException();
053: }
054: stream = new DataInputStream(fTransport.getInputStream());
055: }
056: synchronized (stream) {
057: int packetLength = 0;
058: try {
059: packetLength = stream.readInt();
060: } catch (IOException e) {
061: throw new ClosedConnectionException();
062: }
063:
064: if (packetLength < 11) {
065: throw new IOException("JDWP Packet under 11 bytes"); //$NON-NLS-1$
066: }
067:
068: byte[] packet = new byte[packetLength];
069: packet[0] = (byte) ((packetLength >>> 24) & 0xFF);
070: packet[1] = (byte) ((packetLength >>> 16) & 0xFF);
071: packet[2] = (byte) ((packetLength >>> 8) & 0xFF);
072: packet[3] = (byte) ((packetLength >>> 0) & 0xFF);
073:
074: stream.readFully(packet, 4, packetLength - 4);
075: return packet;
076: }
077: }
078:
079: /* (non-Javadoc)
080: * @see com.sun.jdi.connect.spi.Connection#writePacket(byte[])
081: */
082: public void writePacket(byte[] packet) throws IOException {
083: if (!isOpen()) {
084: throw new ClosedConnectionException();
085: }
086: if (packet == null) {
087: throw new IllegalArgumentException(
088: "Invalid JDWP Packet, packet cannot be null"); //$NON-NLS-1$
089: }
090: if (packet.length < 11) {
091: throw new IllegalArgumentException(
092: "Invalid JDWP Packet, must be at least 11 bytes. PacketSize:" + packet.length); //$NON-NLS-1$
093: }
094:
095: int packetSize = getPacketLength(packet);
096: if (packetSize < 11) {
097: throw new IllegalArgumentException(
098: "Invalid JDWP Packet, must be at least 11 bytes. PacketSize:" + packetSize); //$NON-NLS-1$
099: }
100:
101: if (packetSize > packet.length) {
102: throw new IllegalArgumentException(
103: "Invalid JDWP packet: Specified length is greater than actual length"); //$NON-NLS-1$
104: }
105:
106: OutputStream stream = null;
107: synchronized (this ) {
108: if (!isOpen()) {
109: throw new ClosedConnectionException();
110: }
111: stream = fTransport.getOutputStream();
112: }
113:
114: synchronized (stream) {
115: // packet.length can be > packetSize. Sending too much will cause
116: // errors on the other side
117: stream.write(packet, 0, packetSize);
118: }
119: }
120:
121: private int getPacketLength(byte[] packet) {
122: int len = 0;
123: if (packet.length >= 4) {
124: len = (((packet[0] & 0xFF) << 24)
125: + ((packet[1] & 0xFF) << 16)
126: + ((packet[2] & 0xFF) << 8) + ((packet[3] & 0xFF) << 0));
127: }
128: return len;
129: }
130: }
|