Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/tools/jdi/Packet.java
38920 views
/*1* Copyright (c) 1998, 2003, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdi;2627import com.sun.jdi.*;28import java.io.IOException;2930public class Packet extends Object {31public final static short NoFlags = 0x0;32public final static short Reply = 0x80;33public final static short ReplyNoError = 0x0;3435static int uID = 1;36final static byte[] nullData = new byte[0];3738// Note! flags, cmdSet, and cmd are all byte values.39// We represent them as shorts to make them easier40// to work with.41int id;42short flags;43short cmdSet;44short cmd;45short errorCode;46byte[] data;47volatile boolean replied = false;4849/**50* Return byte representation of the packet51*/52public byte[] toByteArray() {53int len = data.length + 11;54byte b[] = new byte[len];55b[0] = (byte)((len >>> 24) & 0xff);56b[1] = (byte)((len >>> 16) & 0xff);57b[2] = (byte)((len >>> 8) & 0xff);58b[3] = (byte)((len >>> 0) & 0xff);59b[4] = (byte)((id >>> 24) & 0xff);60b[5] = (byte)((id >>> 16) & 0xff);61b[6] = (byte)((id >>> 8) & 0xff);62b[7] = (byte)((id >>> 0) & 0xff);63b[8] = (byte)flags;64if ((flags & Packet.Reply) == 0) {65b[9] = (byte)cmdSet;66b[10] = (byte)cmd;67} else {68b[9] = (byte)((errorCode >>> 8) & 0xff);69b[10] = (byte)((errorCode >>> 0) & 0xff);70}71if (data.length > 0) {72System.arraycopy(data, 0, b, 11, data.length);73}74return b;75}7677/**78* Create a packet from its byte array representation79*/80public static Packet fromByteArray(byte b[]) throws IOException {81if (b.length < 11) {82throw new IOException("packet is insufficient size");83}8485int b0 = b[0] & 0xff;86int b1 = b[1] & 0xff;87int b2 = b[2] & 0xff;88int b3 = b[3] & 0xff;89int len = ((b0 << 24) | (b1 << 16) | (b2 << 8) | (b3 << 0));90if (len != b.length) {91throw new IOException("length size mis-match");92}9394int b4 = b[4] & 0xff;95int b5 = b[5] & 0xff;96int b6 = b[6] & 0xff;97int b7 = b[7] & 0xff;9899Packet p = new Packet();100p.id = ((b4 << 24) | (b5 << 16) | (b6 << 8) | (b7 << 0));101102p.flags = (short)(b[8] & 0xff);103104if ((p.flags & Packet.Reply) == 0) {105p.cmdSet = (short)(b[9] & 0xff);106p.cmd = (short)(b[10] & 0xff);107} else {108short b9 = (short)(b[9] & 0xff);109short b10 = (short)(b[10] & 0xff);110p.errorCode = (short)((b9 << 8) + (b10 << 0));111}112113p.data = new byte[b.length - 11];114System.arraycopy(b, 11, p.data, 0, p.data.length);115return p;116}117118Packet()119{120id = uniqID();121flags = NoFlags;122data = nullData;123}124125static synchronized private int uniqID()126{127/*128* JDWP spec does not require this id to be sequential and129* increasing, but our implementation does. See130* VirtualMachine.notifySuspend, for example.131*/132return uID++;133}134}135136137