1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.net.Socket; 27 import java.util.Arrays; 28 29 /** 30 * JDWP socket transport 31 */ 32 public class JdwpChannel { 33 34 private Socket sock; 35 connect()36 public void connect() throws IOException { 37 sock = new Socket("localhost", DebuggeeLauncher.getJdwpPort()); 38 handshake(); 39 } 40 41 /** 42 * Sends JDWP handshake and verifies the reply 43 * @throws IOException 44 */ handshake()45 private void handshake() throws IOException { 46 final byte[] HANDSHAKE = "JDWP-Handshake".getBytes(); 47 sock.getOutputStream().write(HANDSHAKE, 0, HANDSHAKE.length); 48 49 byte[] reply = new byte[14]; 50 sock.getInputStream().read(reply, 0, 14); 51 if (!Arrays.equals(HANDSHAKE, reply)) { 52 throw new RuntimeException("Error during handshake. Reply was: " + new String(reply) + " expected " + new String(HANDSHAKE)); 53 } 54 } 55 disconnect()56 public void disconnect() { 57 try { 58 sock.close(); 59 } catch (IOException x) { 60 } 61 } 62 write(byte[] data, int length)63 public void write(byte[] data, int length) throws IOException { 64 sock.getOutputStream().write(data, 0, length); 65 } 66 getInputStream()67 public InputStream getInputStream() throws IOException { 68 return sock.getInputStream(); 69 } 70 71 } 72