Path: blob/master/src/jdk.jdi/share/classes/com/sun/tools/jdi/Packet.java
41161 views
/*1* Copyright (c) 1998, 2017, 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 java.io.IOException;2829public class Packet extends Object {30public final static short NoFlags = 0x0;31public final static short Reply = 0x80;32public final static short ReplyNoError = 0x0;3334static int uID = 1;35final static byte[] nullData = new byte[0];3637// Note! flags, cmdSet, and cmd are all byte values.38// We represent them as shorts to make them easier39// to work with.40int id;41short flags;42short cmdSet;43short cmd;44short errorCode;45byte[] data;46volatile boolean replied = false;4748/**49* Return byte representation of the packet50*/51public byte[] toByteArray() {52int len = data.length + 11;53byte b[] = new byte[len];54b[0] = (byte)((len >>> 24) & 0xff);55b[1] = (byte)((len >>> 16) & 0xff);56b[2] = (byte)((len >>> 8) & 0xff);57b[3] = (byte)((len >>> 0) & 0xff);58b[4] = (byte)((id >>> 24) & 0xff);59b[5] = (byte)((id >>> 16) & 0xff);60b[6] = (byte)((id >>> 8) & 0xff);61b[7] = (byte)((id >>> 0) & 0xff);62b[8] = (byte)flags;63if ((flags & Packet.Reply) == 0) {64b[9] = (byte)cmdSet;65b[10] = (byte)cmd;66} else {67b[9] = (byte)((errorCode >>> 8) & 0xff);68b[10] = (byte)((errorCode >>> 0) & 0xff);69}70if (data.length > 0) {71System.arraycopy(data, 0, b, 11, data.length);72}73return b;74}7576/**77* Create a packet from its byte array representation78*/79public static Packet fromByteArray(byte b[]) throws IOException {80if (b.length < 11) {81throw new IOException("packet is insufficient size");82}8384int b0 = b[0] & 0xff;85int b1 = b[1] & 0xff;86int b2 = b[2] & 0xff;87int b3 = b[3] & 0xff;88int len = ((b0 << 24) | (b1 << 16) | (b2 << 8) | (b3 << 0));89if (len != b.length) {90throw new IOException("length size mis-match");91}9293int b4 = b[4] & 0xff;94int b5 = b[5] & 0xff;95int b6 = b[6] & 0xff;96int b7 = b[7] & 0xff;9798Packet p = new Packet();99p.id = ((b4 << 24) | (b5 << 16) | (b6 << 8) | (b7 << 0));100101p.flags = (short)(b[8] & 0xff);102103if ((p.flags & Packet.Reply) == 0) {104p.cmdSet = (short)(b[9] & 0xff);105p.cmd = (short)(b[10] & 0xff);106} else {107short b9 = (short)(b[9] & 0xff);108short b10 = (short)(b[10] & 0xff);109p.errorCode = (short)((b9 << 8) + (b10 << 0));110}111112p.data = new byte[b.length - 11];113System.arraycopy(b, 11, p.data, 0, p.data.length);114return p;115}116117Packet() {118id = uniqID();119flags = NoFlags;120data = nullData;121}122123static synchronized private int uniqID() {124/*125* JDWP spec does not require this id to be sequential and126* increasing, but our implementation does. See127* VirtualMachine.notifySuspend, for example.128*/129return uID++;130}131}132133134