Path: blob/master/test/hotspot/jtreg/serviceability/jdwp/JdwpCmd.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.nio.ByteBuffer;2526/**27* Generic JDWP command28* @param <T> the corresponding JDWP reply class, to construct a reply object29*/30public abstract class JdwpCmd<T extends JdwpReply> {3132private ByteBuffer data;33private static int id = 1;34private final byte FLAGS = 0;35private T reply;36private final int dataLen;37private final int HEADER_LEN = 11;3839/**40* JDWWp command41* @param cmd command code42* @param cmdSet command set43* @param replyClz command reply class44* @param dataLen length of additional data for the command45*/46JdwpCmd(int cmd, int cmdSet, Class<T> replyClz, int dataLen) {47this.dataLen = dataLen;48data = ByteBuffer.allocate(HEADER_LEN + dataLen);49data.putInt(HEADER_LEN + dataLen);50data.putInt(id++);51data.put(FLAGS);52data.put((byte) cmdSet);53data.put((byte) cmd);54if (replyClz != null) {55try {56reply = replyClz.newInstance();57} catch (Exception ex) {58ex.printStackTrace();59}60}61}6263JdwpCmd(int cmd, int cmdSet, Class<T> replyClz) {64this(cmd, cmdSet, replyClz, 0);65}6667int getDataLength() {68return dataLen;69}7071public final T send(JdwpChannel channel) throws IOException {72channel.write(data.array(), HEADER_LEN + getDataLength());73if (reply != null) {74reply.initFromStream(channel.getInputStream());75}76return (T) reply;77}7879protected void putRefId(long refId) {80data.putLong(refId);81}8283protected void putInt(int val) {84data.putInt(val);85}8687protected static int refLen() {88return 8;89}9091}929394