1 /*
2  * Copyright (c) 2001, 2018, 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.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package nsk.share.jdwp;
25 
26 import nsk.share.*;
27 import java.util.Vector;
28 
29 /**
30  * This class represents a JDWP reply packet.
31  */
32 public class ReplyPacket extends Packet {
33 
34     /** Error code constant. */
35 //    public final static int errOk               = JDWP.Error.NONE;
36     /** Error code constant. */
37 //    public final static int errWrongPacketSize  = 0x400;
38     /** Error code constant. */
39 //    public final static int errNotAvailable     = 0x401;
40     /** Error code constant. */
41 //    public final static int errEvent            = 0x4064;
42 
43     /**
44      * Make empty reply packet.
45      */
ReplyPacket()46     public ReplyPacket() {
47         super();
48     }
49 
50     /**
51      * Make reply packet with data from the specified byte buffer.
52      */
53 //    public ReplyPacket(ByteBuffer packet) {
ReplyPacket(Packet packet)54     public ReplyPacket(Packet packet) {
55         super(packet);
56     }
57 
58     /**
59      * Return value of the "error code" field of JDWP reply packet.
60      */
getErrorCode()61     public int getErrorCode() {
62 
63         int err = 0;
64         try {
65             err = (int) getID(ErrorCodeOffset, 2);
66         }
67         catch (BoundException e) {
68             throw new Failure("Caught unexpected exception while getting error code from header:\n\t"
69                             + e);
70         }
71 
72         return err;
73     }
74 
75     /**
76      * Set value of the "error code" field of JDWP reply packet.
77      */
setErrorCode(long err)78     public void setErrorCode(long err) {
79         try {
80             putID(ErrorCodeOffset, err, 2);
81         }
82         catch (BoundException e) {
83             throw new Failure("Caught unexpected exception while setting error code into header:\n\t"
84                             + e);
85         }
86     }
87 
88     /**
89      * Check reply packet header.
90      * This method check if reply packet has valid values in the header fields.
91      *
92      * @throws PacketFormatException if packet header fields have error or invalid values
93      */
94 /*
95     protected void checkHeaderForReplyOrEvent() throws PacketFormatException {
96         super.checkHeader();
97     }
98  */
99 
100     /**
101      * Check reply packet header.
102      * This method check if reply packet has valid values in the header fields.
103      *
104      * @throws PacketFormatException if packet header fields have error or invalid values
105      */
checkHeader()106     public void checkHeader() throws PacketFormatException {
107 //        checkHeaderForReplyOrEvent();
108         super.checkHeader();
109         if (getFlags() != JDWP.Flag.REPLY_PACKET) {
110             throw new PacketFormatException("Unexpected flags in reply packet header: "
111                                             + "0x" + toHexDecString(getFlags(), 2));
112         }
113         if (getErrorCode() != JDWP.Error.NONE) {
114             throw new PacketFormatException("Unexpected error code in reply packet header: "
115                                             + "0x" + toHexDecString(getErrorCode(), 4));
116         }
117     }
118 
119     /**
120      * Check reply packet header for specified reply ID.
121      * This method check if reply packet has valid values in the header fields.
122      *
123      * @throws PacketFormatException if packet header fields have error or invalid values
124      */
checkHeader(int id)125     public void checkHeader(int id) throws PacketFormatException {
126         checkHeader();
127         if (getPacketID() != id) {
128             throw new PacketFormatException("Unexpected ID in reply packet header: "
129                                             + getPacketID());
130         }
131     }
132 
133     /**
134      * Return string representation of the reply packet header.
135      */
headerToString()136     public String headerToString() {
137         String s;
138 
139         if (getFlags() == 0)
140             s = " (command)";
141         else
142             s = " (error)  ";
143 
144         return super.headerToString()
145              + "    " + toHexString(CommandOffset,  4) + s + ": 0x" + toHexDecString(getErrorCode(), 4) + "\n";
146     }
147 
148 }
149