Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.jdi/share/classes/com/sun/tools/jdi/Packet.java
41161 views
1
/*
2
* Copyright (c) 1998, 2017, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.sun.tools.jdi;
27
28
import java.io.IOException;
29
30
public class Packet extends Object {
31
public final static short NoFlags = 0x0;
32
public final static short Reply = 0x80;
33
public final static short ReplyNoError = 0x0;
34
35
static int uID = 1;
36
final static byte[] nullData = new byte[0];
37
38
// Note! flags, cmdSet, and cmd are all byte values.
39
// We represent them as shorts to make them easier
40
// to work with.
41
int id;
42
short flags;
43
short cmdSet;
44
short cmd;
45
short errorCode;
46
byte[] data;
47
volatile boolean replied = false;
48
49
/**
50
* Return byte representation of the packet
51
*/
52
public byte[] toByteArray() {
53
int len = data.length + 11;
54
byte b[] = new byte[len];
55
b[0] = (byte)((len >>> 24) & 0xff);
56
b[1] = (byte)((len >>> 16) & 0xff);
57
b[2] = (byte)((len >>> 8) & 0xff);
58
b[3] = (byte)((len >>> 0) & 0xff);
59
b[4] = (byte)((id >>> 24) & 0xff);
60
b[5] = (byte)((id >>> 16) & 0xff);
61
b[6] = (byte)((id >>> 8) & 0xff);
62
b[7] = (byte)((id >>> 0) & 0xff);
63
b[8] = (byte)flags;
64
if ((flags & Packet.Reply) == 0) {
65
b[9] = (byte)cmdSet;
66
b[10] = (byte)cmd;
67
} else {
68
b[9] = (byte)((errorCode >>> 8) & 0xff);
69
b[10] = (byte)((errorCode >>> 0) & 0xff);
70
}
71
if (data.length > 0) {
72
System.arraycopy(data, 0, b, 11, data.length);
73
}
74
return b;
75
}
76
77
/**
78
* Create a packet from its byte array representation
79
*/
80
public static Packet fromByteArray(byte b[]) throws IOException {
81
if (b.length < 11) {
82
throw new IOException("packet is insufficient size");
83
}
84
85
int b0 = b[0] & 0xff;
86
int b1 = b[1] & 0xff;
87
int b2 = b[2] & 0xff;
88
int b3 = b[3] & 0xff;
89
int len = ((b0 << 24) | (b1 << 16) | (b2 << 8) | (b3 << 0));
90
if (len != b.length) {
91
throw new IOException("length size mis-match");
92
}
93
94
int b4 = b[4] & 0xff;
95
int b5 = b[5] & 0xff;
96
int b6 = b[6] & 0xff;
97
int b7 = b[7] & 0xff;
98
99
Packet p = new Packet();
100
p.id = ((b4 << 24) | (b5 << 16) | (b6 << 8) | (b7 << 0));
101
102
p.flags = (short)(b[8] & 0xff);
103
104
if ((p.flags & Packet.Reply) == 0) {
105
p.cmdSet = (short)(b[9] & 0xff);
106
p.cmd = (short)(b[10] & 0xff);
107
} else {
108
short b9 = (short)(b[9] & 0xff);
109
short b10 = (short)(b[10] & 0xff);
110
p.errorCode = (short)((b9 << 8) + (b10 << 0));
111
}
112
113
p.data = new byte[b.length - 11];
114
System.arraycopy(b, 11, p.data, 0, p.data.length);
115
return p;
116
}
117
118
Packet() {
119
id = uniqID();
120
flags = NoFlags;
121
data = nullData;
122
}
123
124
static synchronized private int uniqID() {
125
/*
126
* JDWP spec does not require this id to be sequential and
127
* increasing, but our implementation does. See
128
* VirtualMachine.notifySuspend, for example.
129
*/
130
return uID++;
131
}
132
}
133
134