1
1
/*
2
- * Copyright (c) 2008, 2021 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2008, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* Copyright (c) 2015, 2019 SAP SE. All rights reserved.
4
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
5
*
25
25
*/
26
26
package sun .tools .attach ;
27
27
28
- import com .sun .tools .attach .AttachOperationFailedException ;
29
28
import com .sun .tools .attach .AgentLoadException ;
30
29
import com .sun .tools .attach .AttachNotSupportedException ;
31
30
import com .sun .tools .attach .spi .AttachProvider ;
@@ -57,13 +56,8 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine {
57
56
super (provider , vmid );
58
57
59
58
// This provider only understands pids
60
- int pid ;
61
- try {
62
- pid = Integer .parseInt (vmid );
63
- if (pid < 1 ) {
64
- throw new NumberFormatException ();
65
- }
66
- } catch (NumberFormatException x ) {
59
+ int pid = Integer .parseInt (vmid );
60
+ if (pid < 1 ) {
67
61
throw new AttachNotSupportedException ("Invalid process identifier: " + vmid );
68
62
}
69
63
@@ -137,9 +131,6 @@ public void detach() throws IOException {
137
131
// protocol version
138
132
private static final String PROTOCOL_VERSION = "1" ;
139
133
140
- // known errors
141
- private static final int ATTACH_ERROR_BADVERSION = 101 ;
142
-
143
134
/**
144
135
* Execute the given command in the target VM.
145
136
*/
@@ -185,46 +176,10 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
185
176
186
177
187
178
// Create an input stream to read reply
188
- SocketInputStream sis = new SocketInputStream (s );
189
-
190
- // Read the command completion status
191
- int completionStatus ;
192
- try {
193
- completionStatus = readInt (sis );
194
- } catch (IOException x ) {
195
- sis .close ();
196
- if (ioe != null ) {
197
- throw ioe ;
198
- } else {
199
- throw x ;
200
- }
201
- }
179
+ SocketInputStreamImpl sis = new SocketInputStreamImpl (s );
202
180
203
- if (completionStatus != 0 ) {
204
- // read from the stream and use that as the error message
205
- String message = readErrorMessage (sis );
206
- sis .close ();
207
-
208
- // In the event of a protocol mismatch then the target VM
209
- // returns a known error so that we can throw a reasonable
210
- // error.
211
- if (completionStatus == ATTACH_ERROR_BADVERSION ) {
212
- throw new IOException ("Protocol mismatch with target VM" );
213
- }
214
-
215
- // Special-case the "load" command so that the right exception is
216
- // thrown.
217
- if (cmd .equals ("load" )) {
218
- String msg = "Failed to load agent library" ;
219
- if (!message .isEmpty ())
220
- msg += ": " + message ;
221
- throw new AgentLoadException (msg );
222
- } else {
223
- if (message .isEmpty ())
224
- message = "Command failed in target VM" ;
225
- throw new AttachOperationFailedException (message );
226
- }
227
- }
181
+ // Process the command completion status
182
+ processCompletionStatus (ioe , cmd , sis );
228
183
229
184
// Return the input stream so that the command output can be read
230
185
return sis ;
@@ -233,39 +188,19 @@ InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOEx
233
188
/*
234
189
* InputStream for the socket connection to get target VM
235
190
*/
236
- private static class SocketInputStream extends InputStream {
237
- int s ;
238
-
239
- public SocketInputStream (int s ) {
240
- this .s = s ;
191
+ private static class SocketInputStreamImpl extends SocketInputStream {
192
+ public SocketInputStreamImpl (long fd ) {
193
+ super (fd );
241
194
}
242
195
243
- public synchronized int read () throws IOException {
244
- byte b [] = new byte [1 ];
245
- int n = this .read (b , 0 , 1 );
246
- if (n == 1 ) {
247
- return b [0 ] & 0xff ;
248
- } else {
249
- return -1 ;
250
- }
196
+ @ Override
197
+ protected int read (long fd , byte [] bs , int off , int len ) throws IOException {
198
+ return VirtualMachineImpl .read ((int )fd , bs , off , len );
251
199
}
252
200
253
- public synchronized int read (byte [] bs , int off , int len ) throws IOException {
254
- if ((off < 0 ) || (off > bs .length ) || (len < 0 ) ||
255
- ((off + len ) > bs .length ) || ((off + len ) < 0 )) {
256
- throw new IndexOutOfBoundsException ();
257
- } else if (len == 0 )
258
- return 0 ;
259
-
260
- return VirtualMachineImpl .read (s , bs , off , len );
261
- }
262
-
263
- public synchronized void close () throws IOException {
264
- if (s != -1 ) {
265
- int toClose = s ;
266
- s = -1 ;
267
- VirtualMachineImpl .close (toClose );
268
- }
201
+ @ Override
202
+ protected void close (long fd ) throws IOException {
203
+ VirtualMachineImpl .close ((int )fd );
269
204
}
270
205
}
271
206
0 commit comments