0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: *
0015: * See the License for the specific language governing permissions and
0016: * limitations under the License.
0017: */
0018:
0019: /**
0020: * @author Khen G. Kim, Aleksey V. Yantsen
0021: * @version $Revision: 1.6 $
0022: */
0023:
0024: /**
0025: * Created on 10.01.2004
0026: */package org.apache.harmony.jpda.tests.framework.jdwp;
0027:
0028: import java.io.UnsupportedEncodingException;
0029:
0030: import org.apache.harmony.jpda.tests.framework.TestErrorException;
0031: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
0032: import org.apache.harmony.jpda.tests.framework.jdwp.TypesLengths;
0033:
0034: /**
0035: * This base class represents JDWP packet.
0036: */
0037: public class Packet {
0038:
0039: public static final int REPLY_PACKET_FLAG = 0x80;
0040:
0041: public static final int FLAGS_INDEX = 8;
0042:
0043: public static final int HEADER_SIZE = 11;
0044:
0045: /**
0046: * The size in bytes of the BYTE type value.
0047: */
0048: protected static final int BYTE_SIZE = 1;
0049:
0050: /**
0051: * The size in bytes of the SHORT type value.
0052: */
0053: protected static final int SHORT_SIZE = 2;
0054:
0055: /**
0056: * The size in bytes of the INT type value.
0057: */
0058: protected static final int INT_SIZE = 4;
0059:
0060: /**
0061: * The size in bytes of the LONG type value.
0062: */
0063: protected static final int LONG_SIZE = 8;
0064:
0065: private static final int LENGTH_INDEX = 0;
0066:
0067: private static final int ID_INDEX = 4;
0068:
0069: private int id;
0070:
0071: private byte flags;
0072:
0073: private int length;
0074:
0075: private byte data[];
0076:
0077: private int reading_data_index;
0078:
0079: /**
0080: * A constructor that creates an empty CommandPacket with empty header
0081: * fields and no data.
0082: */
0083: public Packet() {
0084: reading_data_index = 0;
0085: data = new byte[0];
0086: }
0087:
0088: /**
0089: * A constructor that creates Packet from array of bytes including header
0090: * and data sections.
0091: *
0092: * @param p array of bytes for new packet.
0093: */
0094: public Packet(byte p[]) {
0095: length = (int) readFromByteArray(p, LENGTH_INDEX, INT_SIZE);
0096: if (length < HEADER_SIZE) {
0097: throw new TestErrorException(
0098: "Packet creation error: size of packet = " + length
0099: + "is less than header size = "
0100: + HEADER_SIZE);
0101: }
0102: id = (int) readFromByteArray(p, ID_INDEX, INT_SIZE);
0103: flags = p[FLAGS_INDEX];
0104: data = new byte[p.length - HEADER_SIZE];
0105: System.arraycopy(p, HEADER_SIZE, data, 0, p.length
0106: - HEADER_SIZE);
0107: reading_data_index = 0;
0108: }
0109:
0110: /**
0111: * Gets the length value of the header of the Packet.
0112: *
0113: * @return the length value of the header of the Packet.
0114: */
0115: public int getLength() {
0116: return length;
0117: }
0118:
0119: /**
0120: * Sets the id value of the header of the Packet.
0121: *
0122: * @param i
0123: * the id value of the header of the Packet.
0124: */
0125: public void setId(int i) {
0126: id = i;
0127: }
0128:
0129: /**
0130: * Gets the id value of the header of the Packet.
0131: *
0132: * @return the id value of the header of the Packet.
0133: */
0134: public int getId() {
0135: return id;
0136: }
0137:
0138: /**
0139: * Sets the flags value of the header of the Packet.
0140: *
0141: * @param f
0142: * the flags value of the header of the Packet.
0143: */
0144: public void setFlags(byte f) {
0145: flags = f;
0146: }
0147:
0148: /**
0149: * Gets the flags value of the header of the Packet.
0150: *
0151: * @return the flags value of the header of the Packet.
0152: */
0153: public byte getFlags() {
0154: return flags;
0155: }
0156:
0157: /**
0158: * Gets the flags value from the header of the Packet.
0159: *
0160: * @param tag
0161: * Type tag (see JDWP.tag)
0162: * @return the flags value of the header of the Packet.
0163: */
0164: public boolean isValuePrimitiveType(byte tag) {
0165: switch (tag) {
0166: case JDWPConstants.Tag.ARRAY_TAG: {
0167: return false;
0168: }
0169: case JDWPConstants.Tag.BYTE_TAG: {
0170: return true;
0171: }
0172: case JDWPConstants.Tag.CHAR_TAG: {
0173: return true;
0174: }
0175: case JDWPConstants.Tag.OBJECT_TAG: {
0176: return false;
0177: }
0178: case JDWPConstants.Tag.FLOAT_TAG: {
0179: return true;
0180: }
0181: case JDWPConstants.Tag.DOUBLE_TAG: {
0182: return true;
0183: }
0184: case JDWPConstants.Tag.INT_TAG: {
0185: return true;
0186: }
0187: case JDWPConstants.Tag.LONG_TAG: {
0188: return true;
0189: }
0190: case JDWPConstants.Tag.SHORT_TAG: {
0191: return true;
0192: }
0193: case JDWPConstants.Tag.VOID_TAG: {
0194: return true;
0195: }
0196: case JDWPConstants.Tag.BOOLEAN_TAG: {
0197: return true;
0198: }
0199: case JDWPConstants.Tag.STRING_TAG: {
0200: return false;
0201: }
0202: case JDWPConstants.Tag.THREAD_TAG: {
0203: return false;
0204: }
0205: case JDWPConstants.Tag.THREAD_GROUP_TAG: {
0206: return false;
0207: }
0208: case JDWPConstants.Tag.CLASS_LOADER_TAG: {
0209: return false;
0210: }
0211: case JDWPConstants.Tag.CLASS_OBJECT_TAG: {
0212: return false;
0213: }
0214: case JDWPConstants.Tag.NO_TAG: {
0215: return true;
0216: }
0217: default: {
0218: throw new TestErrorException("Improper JDWP.tag value = "
0219: + tag);
0220: }
0221: }
0222: }
0223:
0224: /**
0225: * Sets the next value of the data of the Packet as byte.
0226: *
0227: * @param val
0228: * the byte value.
0229: */
0230: public void setNextValueAsByte(byte val) {
0231: int new_data_size = data.length + BYTE_SIZE;
0232: byte data_temp[] = data;
0233: data = new byte[new_data_size];
0234: System.arraycopy(data_temp, 0, data, 0, new_data_size
0235: - BYTE_SIZE);
0236: data[new_data_size - BYTE_SIZE] = val;
0237: }
0238:
0239: /**
0240: * Gets the next value of the data of the Packet as byte.
0241: *
0242: * @return the next value of the data of the Packet as byte.
0243: */
0244: public byte getNextValueAsByte() {
0245: reading_data_index = reading_data_index + BYTE_SIZE;
0246: return data[reading_data_index - BYTE_SIZE];
0247: }
0248:
0249: /**
0250: * Sets the next value of the data of the Packet as boolean.
0251: *
0252: * @param val
0253: * the boolean value.
0254: */
0255: public void setNextValueAsBoolean(boolean val) {
0256: int old_data_size = data.length;
0257: int new_data_size = old_data_size
0258: + TypesLengths.getTypeLength(TypesLengths.BOOLEAN_ID);
0259: byte data_temp[] = data;
0260: data = new byte[new_data_size];
0261: System.arraycopy(data_temp, 0, data, 0, old_data_size);
0262: if (val) {
0263: data[old_data_size] = 1;
0264: } else {
0265: data[old_data_size] = 0;
0266: }
0267: }
0268:
0269: /**
0270: * Gets the next value of the data of the Packet as boolean.
0271: *
0272: * @return the next value of the data of the Packet as boolean.
0273: */
0274: public boolean getNextValueAsBoolean() {
0275: int res = (int) data[reading_data_index] & 0xFF;
0276: reading_data_index = reading_data_index
0277: + TypesLengths.getTypeLength(TypesLengths.BOOLEAN_ID);
0278: return (res != 0);
0279: }
0280:
0281: /**
0282: * Sets the next value of the data of the Packet as short.
0283: *
0284: * @param val
0285: * the short value.
0286: */
0287: public void setNextValueAsShort(short val) {
0288: int new_data_size = data.length
0289: + TypesLengths.getTypeLength(TypesLengths.SHORT_ID);
0290: byte data_temp[] = data;
0291: data = new byte[new_data_size];
0292: System.arraycopy(data_temp, 0, data, 0, new_data_size
0293: - TypesLengths.getTypeLength(TypesLengths.SHORT_ID));
0294: this .writeAtByteArray((long) val, data, new_data_size
0295: - TypesLengths.getTypeLength(TypesLengths.SHORT_ID),
0296: TypesLengths.getTypeLength(TypesLengths.SHORT_ID));
0297: }
0298:
0299: /**
0300: * Gets the next value of the data of the Packet as short.
0301: *
0302: * @return the next value of the data of the Packet as short.
0303: */
0304: public short getNextValueAsShort() {
0305: reading_data_index = reading_data_index
0306: + TypesLengths.getTypeLength(TypesLengths.SHORT_ID);
0307: return (short) readFromByteArray(data, reading_data_index
0308: - TypesLengths.getTypeLength(TypesLengths.SHORT_ID),
0309: TypesLengths.getTypeLength(TypesLengths.SHORT_ID));
0310: }
0311:
0312: /**
0313: * Sets the next value of the data of the Packet as int.
0314: *
0315: * @param val
0316: * the int value.
0317: */
0318: public void setNextValueAsInt(int val) {
0319: int new_data_size = data.length
0320: + TypesLengths.getTypeLength(TypesLengths.INT_ID);
0321: byte data_temp[] = data;
0322: data = new byte[new_data_size];
0323: System.arraycopy(data_temp, 0, data, 0, new_data_size
0324: - TypesLengths.getTypeLength(TypesLengths.INT_ID));
0325: this .writeAtByteArray((long) val, data, new_data_size
0326: - TypesLengths.getTypeLength(TypesLengths.INT_ID),
0327: TypesLengths.getTypeLength(TypesLengths.INT_ID));
0328: }
0329:
0330: /**
0331: * Gets the next value of the data of the Packet as int.
0332: *
0333: * @return the next value of the data of the Packet as int.
0334: */
0335: public int getNextValueAsInt() {
0336: reading_data_index = reading_data_index
0337: + TypesLengths.getTypeLength(TypesLengths.INT_ID);
0338: return (int) readFromByteArray(data, reading_data_index
0339: - TypesLengths.getTypeLength(TypesLengths.INT_ID),
0340: TypesLengths.getTypeLength(TypesLengths.INT_ID));
0341: }
0342:
0343: /**
0344: * Sets the next value of the data of the Packet as double.
0345: *
0346: * @param dval
0347: * the double value.
0348: */
0349: public void setNextValueAsDouble(double dval) {
0350: int new_data_size = data.length
0351: + TypesLengths.getTypeLength(TypesLengths.DOUBLE_ID);
0352: byte data_temp[] = data;
0353: long val = Double.doubleToLongBits(dval);
0354: data = new byte[new_data_size];
0355: System.arraycopy(data_temp, 0, data, 0, new_data_size
0356: - TypesLengths.getTypeLength(TypesLengths.DOUBLE_ID));
0357: this .writeAtByteArray((long) val, data, new_data_size
0358: - TypesLengths.getTypeLength(TypesLengths.DOUBLE_ID),
0359: TypesLengths.getTypeLength(TypesLengths.DOUBLE_ID));
0360: }
0361:
0362: /**
0363: * Gets the next value of the data of the Packet as double.
0364: *
0365: * @return the next value of the data of the Packet as double.
0366: */
0367: public double getNextValueAsDouble() {
0368: reading_data_index = reading_data_index
0369: + TypesLengths.getTypeLength(TypesLengths.DOUBLE_ID);
0370: long res = readFromByteArray(data, reading_data_index
0371: - TypesLengths.getTypeLength(TypesLengths.DOUBLE_ID),
0372: TypesLengths.getTypeLength(TypesLengths.DOUBLE_ID));
0373:
0374: return Double.longBitsToDouble(res);
0375: }
0376:
0377: /**
0378: * Sets the next value of the data of the Packet as float.
0379: *
0380: * @param fval
0381: * the float value.
0382: */
0383: public void setNextValueAsFloat(float fval) {
0384: int new_data_size = data.length
0385: + TypesLengths.getTypeLength(TypesLengths.FLOAT_ID);
0386: byte data_temp[] = data;
0387: long val = Float.floatToIntBits(fval);
0388: data = new byte[new_data_size];
0389: System.arraycopy(data_temp, 0, data, 0, new_data_size
0390: - TypesLengths.getTypeLength(TypesLengths.FLOAT_ID));
0391: this .writeAtByteArray((long) val, data, new_data_size
0392: - TypesLengths.getTypeLength(TypesLengths.FLOAT_ID),
0393: TypesLengths.getTypeLength(TypesLengths.FLOAT_ID));
0394: }
0395:
0396: /**
0397: * Gets the next value of the data of the Packet as float.
0398: *
0399: * @return the next value of the data of the Packet as float.
0400: */
0401: public float getNextValueAsFloat() {
0402: reading_data_index = reading_data_index
0403: + TypesLengths.getTypeLength(TypesLengths.FLOAT_ID);
0404: long res = readFromByteArray(data, reading_data_index
0405: - TypesLengths.getTypeLength(TypesLengths.FLOAT_ID),
0406: TypesLengths.getTypeLength(TypesLengths.FLOAT_ID));
0407:
0408: return Float.intBitsToFloat((int) res);
0409: }
0410:
0411: /**
0412: * Sets the next value of the data of the Packet as char.
0413: *
0414: * @param val
0415: * the char value.
0416: */
0417: public void setNextValueAsChar(char val) {
0418: int new_data_size = data.length
0419: + TypesLengths.getTypeLength(TypesLengths.CHAR_ID);
0420: byte data_temp[] = data;
0421: data = new byte[new_data_size];
0422: System.arraycopy(data_temp, 0, data, 0, new_data_size
0423: - TypesLengths.getTypeLength(TypesLengths.CHAR_ID));
0424: this .writeAtByteArray((long) val, data, new_data_size
0425: - TypesLengths.getTypeLength(TypesLengths.CHAR_ID),
0426: TypesLengths.getTypeLength(TypesLengths.CHAR_ID));
0427: }
0428:
0429: /**
0430: * Gets the next value of the data of the Packet as char.
0431: *
0432: * @return the next value of the data of the Packet as char.
0433: */
0434: public char getNextValueAsChar() {
0435: reading_data_index = reading_data_index
0436: + TypesLengths.getTypeLength(TypesLengths.CHAR_ID);
0437: return (char) readFromByteArray(data, reading_data_index
0438: - TypesLengths.getTypeLength(TypesLengths.CHAR_ID),
0439: TypesLengths.getTypeLength(TypesLengths.CHAR_ID));
0440: }
0441:
0442: /**
0443: * Sets the next value of the data of the Packet as long.
0444: *
0445: * @param val
0446: * the long value.
0447: */
0448: public void setNextValueAsLong(long val) {
0449: int new_data_size = data.length
0450: + TypesLengths.getTypeLength(TypesLengths.LONG_ID);
0451: byte data_temp[] = data;
0452: data = new byte[new_data_size];
0453: System.arraycopy(data_temp, 0, data, 0, new_data_size
0454: - TypesLengths.getTypeLength(TypesLengths.LONG_ID));
0455: this .writeAtByteArray(val, data, new_data_size
0456: - TypesLengths.getTypeLength(TypesLengths.LONG_ID),
0457: TypesLengths.getTypeLength(TypesLengths.LONG_ID));
0458: }
0459:
0460: /**
0461: * Gets the next value of the data of the Packet as long.
0462: *
0463: * @return the next value of the data of the Packet as long.
0464: */
0465: public long getNextValueAsLong() {
0466: reading_data_index = reading_data_index
0467: + TypesLengths.getTypeLength(TypesLengths.LONG_ID);
0468: return readFromByteArray(data, reading_data_index
0469: - TypesLengths.getTypeLength(TypesLengths.LONG_ID),
0470: TypesLengths.getTypeLength(TypesLengths.LONG_ID));
0471: }
0472:
0473: /**
0474: * Sets the next value of the data of the Packet as String in the "UTF-8"
0475: * Charset.
0476: *
0477: * @param val
0478: * the String in the "UTF-8" Charset.
0479: */
0480: public void setNextValueAsString(String val) {
0481: byte data_temp[] = data;
0482: byte val_as_bytes[];
0483: try {
0484: val_as_bytes = val.getBytes("UTF-8");
0485: } catch (UnsupportedEncodingException e) {
0486: throw new TestErrorException(e);
0487: }
0488: int new_data_size = data.length + val_as_bytes.length
0489: + TypesLengths.getTypeLength(TypesLengths.INT_ID);
0490: data = new byte[new_data_size];
0491: System.arraycopy(data_temp, 0, data, 0, new_data_size
0492: - val_as_bytes.length
0493: - TypesLengths.getTypeLength(TypesLengths.INT_ID));
0494: this .writeAtByteArray((long) val_as_bytes.length, data,
0495: new_data_size
0496: - val_as_bytes.length
0497: - TypesLengths
0498: .getTypeLength(TypesLengths.INT_ID),
0499: TypesLengths.getTypeLength(TypesLengths.INT_ID));
0500: System.arraycopy(val_as_bytes, 0, data, new_data_size
0501: - val_as_bytes.length, val_as_bytes.length);
0502: }
0503:
0504: /**
0505: * Gets the next value of the data of the Packet as String in the "UTF-8"
0506: * Charset.
0507: *
0508: * @return the next value of the data of the Packet as String in the "UTF-8"
0509: * Charset.
0510: */
0511: public String getNextValueAsString() {
0512: int string_length = this .getNextValueAsInt();
0513: String res = null;
0514: try {
0515: res = new String(data, reading_data_index, string_length,
0516: "UTF-8");
0517: } catch (UnsupportedEncodingException e) {
0518: throw new TestErrorException(e);
0519: }
0520: reading_data_index = reading_data_index + string_length;
0521: return res;
0522: }
0523:
0524: /**
0525: * Sets the next value of the data of the Packet as objectID VM-sensitive
0526: * value. If length is less than 8 bytes, the appropriate high bits in the
0527: * val value will be ignored.
0528: *
0529: * @param val
0530: * the ObjectID value.
0531: */
0532: public void setNextValueAsObjectID(long val) {
0533: if (TypesLengths.getTypeLength(TypesLengths.OBJECT_ID) < 0
0534: || TypesLengths.getTypeLength(TypesLengths.OBJECT_ID) > 8) {
0535: throw new TestErrorException(
0536: "Improper ObjectID value length = "
0537: + TypesLengths
0538: .getTypeLength(TypesLengths.OBJECT_ID));
0539: }
0540: int new_data_size = data.length
0541: + TypesLengths.getTypeLength(TypesLengths.OBJECT_ID);
0542: byte data_temp[] = data;
0543: data = new byte[new_data_size];
0544: System.arraycopy(data_temp, 0, data, 0, new_data_size
0545: - TypesLengths.getTypeLength(TypesLengths.OBJECT_ID));
0546: this .writeAtByteArray(val, data, new_data_size
0547: - TypesLengths.getTypeLength(TypesLengths.OBJECT_ID),
0548: TypesLengths.getTypeLength(TypesLengths.OBJECT_ID));
0549: }
0550:
0551: /**
0552: * Gets the next value of the data of the Packet as objectID VM-sensitive
0553: * value. If length is less than 8 bytes, the appropriate high bits in the
0554: * returned value can be ignored.
0555: *
0556: * @return the next value of the data of the Packet as VM-sensitive value.
0557: */
0558: public long getNextValueAsObjectID() {
0559: if (TypesLengths.getTypeLength(TypesLengths.OBJECT_ID) < 0
0560: || TypesLengths.getTypeLength(TypesLengths.OBJECT_ID) > 8) {
0561: throw new TestErrorException(
0562: "Improper ObjectID value length = "
0563: + TypesLengths
0564: .getTypeLength(TypesLengths.OBJECT_ID)
0565: + "!");
0566: }
0567: reading_data_index = reading_data_index
0568: + TypesLengths.getTypeLength(TypesLengths.OBJECT_ID);
0569: return (int) readFromByteArray(data, reading_data_index
0570: - TypesLengths.getTypeLength(TypesLengths.OBJECT_ID),
0571: TypesLengths.getTypeLength(TypesLengths.OBJECT_ID));
0572: }
0573:
0574: /**
0575: * Sets the next value of the data of the Packet as ThreadID VM-sensitive
0576: * value. If length is less than 8 bytes, the appropriate high bits in the
0577: * val value will be ignored.
0578: *
0579: * @param val
0580: * the ThreadID value.
0581: */
0582: public void setNextValueAsThreadID(long val) {
0583: this .setNextValueAsObjectID(val);
0584: }
0585:
0586: /**
0587: * Gets the next value of the data of the Packet as ThreadID VM-sensitive
0588: * value. If length is less than 8 bytes, the appropriate high bits in the
0589: * returned value can be ignored.
0590: *
0591: * @return the next value of the data of the Packet as VM-sensitive value.
0592: */
0593: public long getNextValueAsThreadID() {
0594: return this .getNextValueAsObjectID();
0595: }
0596:
0597: /**
0598: * Sets the next value of the data of the Packet as ThreadGroupID
0599: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0600: * bits in the val value will be ignored.
0601: *
0602: * @param val
0603: * the ThreadGroupID value.
0604: */
0605: public void setNextValueAsThreadGroupID(long val) {
0606: this .setNextValueAsObjectID(val);
0607: }
0608:
0609: /**
0610: * Gets the next value of the data of the Packet as ThreadGroupID
0611: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0612: * bits in the returned value can be ignored.
0613: *
0614: * @return the next value of the data of the Packet as VM-sensitive value.
0615: */
0616: public long getNextValueAsThreadGroupID() {
0617: return this .getNextValueAsObjectID();
0618: }
0619:
0620: /**
0621: * Sets the next value of the data of the Packet as StringID VM-sensitive
0622: * value. If length is less than 8 bytes, the appropriate high bits in the
0623: * val value will be ignored.
0624: *
0625: * @param val
0626: * the StringID value.
0627: */
0628: public void setNextValueAsStringID(long val) {
0629: this .setNextValueAsObjectID(val);
0630: }
0631:
0632: /**
0633: * Gets the next value of the data of the Packet as StringID VM-sensitive
0634: * value. If length is less than 8 bytes, the appropriate high bits in the
0635: * returned value can be ignored.
0636: *
0637: * @return the next value of the data of the Packet as VM-sensitive value.
0638: */
0639: public long getNextValueAsStringID() {
0640: return this .getNextValueAsObjectID();
0641: }
0642:
0643: /**
0644: * Sets the next value of the data of the Packet as ClassLoaderID
0645: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0646: * bits in the val value will be ignored.
0647: *
0648: * @param val
0649: * the ClassLoaderID value.
0650: */
0651: public void setNextValueAsClassLoaderID(long val) {
0652: this .setNextValueAsObjectID(val);
0653: }
0654:
0655: /**
0656: * Gets the next value of the data of the Packet as ClassLoaderID
0657: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0658: * bits in the returned value can be ignored.
0659: *
0660: * @return the next value of the data of the Packet as VM-sensitive value.
0661: */
0662: public long getNextValueAsClassLoaderID() {
0663: return this .getNextValueAsObjectID();
0664: }
0665:
0666: /**
0667: * Sets the next value of the data of the Packet as ClassObjectID
0668: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0669: * bits in the val value will be ignored.
0670: *
0671: * @param val
0672: * the ClassObjectID value.
0673: */
0674: public void setNextValueAsClassObjectID(long val) {
0675: this .setNextValueAsObjectID(val);
0676: }
0677:
0678: /**
0679: * Gets the next value of the data of the Packet as ClassObjectID
0680: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0681: * bits in the returned value can be ignored.
0682: *
0683: * @return the next value of the data of the Packet as VM-sensitive value.
0684: */
0685: public long getNextValueAsClassObjectID() {
0686: return this .getNextValueAsObjectID();
0687: }
0688:
0689: /**
0690: * Sets the next value of the data of the Packet as ArrayID VM-sensitive
0691: * value. If length is less than 8 bytes, the appropriate high bits in the
0692: * val value will be ignored.
0693: *
0694: * @param val
0695: * the ArrayID value.
0696: */
0697: public void setNextValueAsArrayID(long val) {
0698: this .setNextValueAsObjectID(val);
0699: }
0700:
0701: /**
0702: * Gets the next value of the data of the Packet as ArrayID VM-sensitive
0703: * value. If length is less than 8 bytes, the appropriate high bits in the
0704: * returned value can be ignored.
0705: *
0706: * @return the next value of the data of the Packet as VM-sensitive value.
0707: */
0708: public long getNextValueAsClassArrayID() {
0709: return this .getNextValueAsObjectID();
0710: }
0711:
0712: /**
0713: * Sets the next value of the data of the Packet as ReferenceTypeID
0714: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0715: * bits in the val value will be ignored.
0716: *
0717: * @param val
0718: * the ReferenceTypeID value.
0719: */
0720: public void setNextValueAsReferenceTypeID(long val) {
0721: this .setNextValueAsObjectID(val);
0722: }
0723:
0724: /**
0725: * Gets the next value of the data of the Packet as ReferenceTypeID
0726: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0727: * bits in the returned value can be ignored.
0728: *
0729: * @return the next value of the data of the Packet as VM-sensitive value.
0730: */
0731: public long getNextValueAsReferenceTypeID() {
0732: return this .getNextValueAsObjectID();
0733: }
0734:
0735: /**
0736: * Sets the next value of the data of the Packet as ClassID VM-sensitive
0737: * value. If length is less than 8 bytes, the appropriate high bits in the
0738: * val value will be ignored.
0739: *
0740: * @param val
0741: * the ClassID value.
0742: */
0743: public void setNextValueAsClassID(long val) {
0744: this .setNextValueAsObjectID(val);
0745: }
0746:
0747: /**
0748: * Gets the next value of the data of the Packet as ClassID VM-sensitive
0749: * value. If length is less than 8 bytes, the appropriate high bits in the
0750: * returned value can be ignored.
0751: *
0752: * @return the next value of the data of the Packet as VM-sensitive value.
0753: */
0754: public long getNextValueAsClassID() {
0755: return this .getNextValueAsObjectID();
0756: }
0757:
0758: /**
0759: * Sets the next value of the data of the Packet as InterfaceID VM-sensitive
0760: * value. If length is less than 8 bytes, the appropriate high bits in the
0761: * val value will be ignored.
0762: *
0763: * @param val
0764: * the InterfaceID value.
0765: */
0766: public void setNextValueAsInterfaceID(long val) {
0767: this .setNextValueAsObjectID(val);
0768: }
0769:
0770: /**
0771: * Gets the next value of the data of the Packet as InterfaceID VM-sensitive
0772: * value. If length is less than 8 bytes, the appropriate high bits in the
0773: * returned value can be ignored.
0774: *
0775: * @return the next value of the data of the Packet as VM-sensitive value.
0776: */
0777: public long getNextValueAsInterfaceID() {
0778: return this .getNextValueAsObjectID();
0779: }
0780:
0781: /**
0782: * Sets the next value of the data of the Packet as ArrayTypeID VM-sensitive
0783: * value. If length is less than 8 bytes, the appropriate high bits in the
0784: * val value will be ignored.
0785: *
0786: * @param val
0787: * the ArrayTypeID value.
0788: */
0789: public void setNextValueAsArrayTypeID(long val) {
0790: this .setNextValueAsObjectID(val);
0791: }
0792:
0793: /**
0794: * Gets the next value of the data of the Packet as ArrayTypeID VM-sensitive
0795: * value. If length is less than 8 bytes, the appropriate high bits in the
0796: * returned value can be ignored.
0797: *
0798: * @return the next value of the data of the Packet as VM-sensitive value.
0799: */
0800: public long getNextValueAsArrayTypeID() {
0801: return this .getNextValueAsObjectID();
0802: }
0803:
0804: /**
0805: * Sets the next value of the data of the Packet as tagged-objectID
0806: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0807: * bits in the val value will be ignored.
0808: *
0809: * @param taggedObject
0810: * TaggedObject value.
0811: */
0812: public void setNextValueAsTaggedObject(TaggedObject taggedObject) {
0813: this .setNextValueAsByte(taggedObject.tag);
0814: this .setNextValueAsObjectID(taggedObject.objectID);
0815: }
0816:
0817: /**
0818: * Gets the next value of the data of the Packet as tagged-objectID
0819: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
0820: * bits in the returned value can be ignored.
0821: *
0822: * @return the next value of the data of the Packet as VM-sensitive value.
0823: */
0824: public TaggedObject getNextValueAsTaggedObject() {
0825: if (TypesLengths.getTypeLength(TypesLengths.OBJECT_ID) < 0
0826: || TypesLengths.getTypeLength(TypesLengths.OBJECT_ID) > 8) {
0827: throw new TestErrorException(
0828: "Improper ObjectID value length = "
0829: + TypesLengths
0830: .getTypeLength(TypesLengths.OBJECT_ID));
0831: }
0832: TaggedObject taggedObject = new TaggedObject();
0833: taggedObject.tag = this .getNextValueAsByte();
0834: taggedObject.objectID = this .getNextValueAsObjectID();
0835: return taggedObject;
0836: }
0837:
0838: /**
0839: * Sets the next value of the data of the Packet as MethodID VM-sensitive
0840: * value. If length is less than 8 bytes, the appropriate high bits in the
0841: * val value will be ignored.
0842: *
0843: * @param methodID
0844: * MethodID value.
0845: */
0846: public void setNextValueAsMethodID(long methodID) {
0847: if (TypesLengths.getTypeLength(TypesLengths.METHOD_ID) < 0
0848: || TypesLengths.getTypeLength(TypesLengths.METHOD_ID) > 8) {
0849: throw new TestErrorException(
0850: "Improper MethodID value length = "
0851: + TypesLengths
0852: .getTypeLength(TypesLengths.METHOD_ID));
0853: }
0854: int new_data_size = data.length
0855: + TypesLengths.getTypeLength(TypesLengths.METHOD_ID);
0856: byte data_temp[] = data;
0857: data = new byte[new_data_size];
0858: System.arraycopy(data_temp, 0, data, 0, new_data_size
0859: - TypesLengths.getTypeLength(TypesLengths.METHOD_ID));
0860: this .writeAtByteArray(methodID, data, new_data_size
0861: - TypesLengths.getTypeLength(TypesLengths.METHOD_ID),
0862: TypesLengths.getTypeLength(TypesLengths.METHOD_ID));
0863: }
0864:
0865: /**
0866: * Gets the next value of the data of the Packet as MethodID VM-sensitive
0867: * value. If length is less than 8 bytes, the appropriate high bits in the
0868: * returned value can be ignored.
0869: *
0870: * @return the next value of the data of the Packet as VM-sensitive value.
0871: */
0872: public long getNextValueAsMethodID() {
0873: if (TypesLengths.getTypeLength(TypesLengths.METHOD_ID) < 0
0874: || TypesLengths.getTypeLength(TypesLengths.METHOD_ID) > 8) {
0875: throw new TestErrorException(
0876: "Improper MethodID value length = "
0877: + TypesLengths
0878: .getTypeLength(TypesLengths.METHOD_ID));
0879: }
0880: reading_data_index = reading_data_index
0881: + TypesLengths.getTypeLength(TypesLengths.METHOD_ID);
0882: long result = readFromByteArray(data, reading_data_index
0883: - TypesLengths.getTypeLength(TypesLengths.METHOD_ID),
0884: TypesLengths.getTypeLength(TypesLengths.METHOD_ID));
0885: return result;
0886: }
0887:
0888: /**
0889: * Sets the next value of the data of the Packet as FieldID VM-sensitive
0890: * value. If length is less than 8 bytes, the appropriate high bits in the
0891: * val value will be ignored.
0892: *
0893: * @param fieldID
0894: * FieldID value.
0895: */
0896: public void setNextValueAsFieldID(long fieldID) {
0897: if (TypesLengths.getTypeLength(TypesLengths.FIELD_ID) < 0
0898: || TypesLengths.getTypeLength(TypesLengths.FIELD_ID) > 8) {
0899: throw new TestErrorException(
0900: "Improper FieldID value length = "
0901: + TypesLengths
0902: .getTypeLength(TypesLengths.FIELD_ID));
0903: }
0904: int new_data_size = data.length
0905: + TypesLengths.getTypeLength(TypesLengths.FIELD_ID);
0906: byte data_temp[] = data;
0907: data = new byte[new_data_size];
0908: System.arraycopy(data_temp, 0, data, 0, new_data_size
0909: - TypesLengths.getTypeLength(TypesLengths.FIELD_ID));
0910: this .writeAtByteArray(fieldID, data, new_data_size
0911: - TypesLengths.getTypeLength(TypesLengths.FIELD_ID),
0912: TypesLengths.getTypeLength(TypesLengths.FIELD_ID));
0913: }
0914:
0915: /**
0916: * Gets the next value of the data of the Packet as FieldID VM-sensitive
0917: * value. If length is less than 8 bytes, the appropriate high bits in the
0918: * returned value can be ignored.
0919: *
0920: * @return the next value of the data of the Packet as VM-sensitive value.
0921: */
0922: public long getNextValueAsFieldID() {
0923: if (TypesLengths.getTypeLength(TypesLengths.FIELD_ID) < 0
0924: || TypesLengths.getTypeLength(TypesLengths.FIELD_ID) > 8) {
0925: throw new TestErrorException(
0926: "Improper FieldID value length = "
0927: + TypesLengths
0928: .getTypeLength(TypesLengths.FIELD_ID));
0929: }
0930: reading_data_index = reading_data_index
0931: + TypesLengths.getTypeLength(TypesLengths.FIELD_ID);
0932: long result = readFromByteArray(data, reading_data_index
0933: - TypesLengths.getTypeLength(TypesLengths.FIELD_ID),
0934: TypesLengths.getTypeLength(TypesLengths.FIELD_ID));
0935: return result;
0936: }
0937:
0938: /**
0939: * Sets the next value of the data of the Packet as FrameID VM-sensitive
0940: * value. If length is less than 8 bytes, the appropriate high bits in the
0941: * val value will be ignored.
0942: *
0943: * @param frameID
0944: * FrameID value.
0945: */
0946: public void setNextValueAsFrameID(long frameID) {
0947: if (TypesLengths.getTypeLength(TypesLengths.FRAME_ID) < 0
0948: || TypesLengths.getTypeLength(TypesLengths.FRAME_ID) > 8) {
0949: throw new TestErrorException(
0950: "Improper FrameID value length = "
0951: + TypesLengths
0952: .getTypeLength(TypesLengths.FRAME_ID));
0953: }
0954: int new_data_size = data.length
0955: + TypesLengths.getTypeLength(TypesLengths.FRAME_ID);
0956: byte data_temp[] = data;
0957: data = new byte[new_data_size];
0958: System.arraycopy(data_temp, 0, data, 0, new_data_size
0959: - TypesLengths.getTypeLength(TypesLengths.FRAME_ID));
0960: this .writeAtByteArray(frameID, data, new_data_size
0961: - TypesLengths.getTypeLength(TypesLengths.FRAME_ID),
0962: TypesLengths.getTypeLength(TypesLengths.FRAME_ID));
0963: }
0964:
0965: /**
0966: * Gets the next value of the data of the Packet as FrameID VM-sensitive
0967: * value. If length is less than 8 bytes, the appropriate high bits in the
0968: * returned value can be ignored.
0969: *
0970: * @return the next value of the data of the Packet as VM-sensitive value.
0971: */
0972: public long getNextValueAsFrameID() {
0973: if (TypesLengths.getTypeLength(TypesLengths.FRAME_ID) < 0
0974: || TypesLengths.getTypeLength(TypesLengths.FRAME_ID) > 8) {
0975: throw new TestErrorException(
0976: "Improper FrameID value length = "
0977: + TypesLengths
0978: .getTypeLength(TypesLengths.FRAME_ID));
0979: }
0980: reading_data_index = reading_data_index
0981: + TypesLengths.getTypeLength(TypesLengths.FRAME_ID);
0982: long result = readFromByteArray(data, reading_data_index
0983: - TypesLengths.getTypeLength(TypesLengths.FRAME_ID),
0984: TypesLengths.getTypeLength(TypesLengths.FRAME_ID));
0985: return result;
0986: }
0987:
0988: /**
0989: * Sets the next value of the data of the Packet as Location VM-sensitive
0990: * value. If length is less than 8 bytes, the appropriate high bits in the
0991: * val value will be ignored.
0992: *
0993: * @param location
0994: * Location value.
0995: */
0996: public void setNextValueAsLocation(Location location) {
0997: this .setNextValueAsByte(location.tag);
0998: this .setNextValueAsClassID(location.classID);
0999: this .setNextValueAsMethodID(location.methodID);
1000: this .setNextValueAsLong(location.index);
1001: }
1002:
1003: /**
1004: * Gets the next value of the data of the Packet as Location VM-sensitive
1005: * value. If length is less than 8 bytes, the appropriate high bits in the
1006: * returned value can be ignored.
1007: *
1008: * @return the next value of the data of the Packet as VM-sensitive value.
1009: */
1010: public Location getNextValueAsLocation() {
1011: Location location = new Location();
1012: location.tag = this .getNextValueAsByte();
1013: location.classID = this .getNextValueAsClassID();
1014: location.methodID = this .getNextValueAsMethodID();
1015: location.index = this .getNextValueAsLong();
1016: return location;
1017: }
1018:
1019: /**
1020: * Sets the next value of the data of the Packet as Value VM-sensitive
1021: * value. If length is less than 8 bytes, the appropriate high bits in the
1022: * val value will be ignored.
1023: *
1024: * @param value
1025: * Value value.
1026: * @throws UnsupportedEncodingException
1027: */
1028: public void setNextValueAsValue(Value value) {
1029: this .setNextValueAsByte(value.getTag());
1030: setNextValueAsUntaggedValue(value);
1031: }
1032:
1033: /**
1034: * Gets the next value of the data of the Packet as Value VM-sensitive
1035: * value. If length is less than 8 bytes, the appropriate high bits in the
1036: * returned value can be ignored.
1037: *
1038: * @return the next value of the data of the Packet as VM-sensitive value.
1039: */
1040: public Value getNextValueAsValue() {
1041: byte tag = this .getNextValueAsByte();
1042: return getNextValueAsUntaggedValue(tag);
1043: }
1044:
1045: /**
1046: * Sets the next value of the data of the Packet as UntaggedValue
1047: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
1048: * bits in the val value will be ignored.
1049: *
1050: * @param value
1051: * UntaggedValue value.
1052: * @throws UnsupportedEncodingException
1053: */
1054: public void setNextValueAsUntaggedValue(Value value) {
1055: switch (value.getTag()) {
1056: case JDWPConstants.Tag.BOOLEAN_TAG:
1057: this .setNextValueAsBoolean(value.getBooleanValue());
1058: break;
1059: case JDWPConstants.Tag.BYTE_TAG:
1060: this .setNextValueAsByte(value.getByteValue());
1061: break;
1062: case JDWPConstants.Tag.CHAR_TAG:
1063: this .setNextValueAsChar(value.getCharValue());
1064: break;
1065: case JDWPConstants.Tag.DOUBLE_TAG:
1066: this .setNextValueAsDouble(value.getDoubleValue());
1067: break;
1068: case JDWPConstants.Tag.FLOAT_TAG:
1069: this .setNextValueAsFloat(value.getFloatValue());
1070: break;
1071: case JDWPConstants.Tag.INT_TAG:
1072: this .setNextValueAsInt(value.getIntValue());
1073: break;
1074: case JDWPConstants.Tag.LONG_TAG:
1075: this .setNextValueAsLong(value.getLongValue());
1076: break;
1077: case JDWPConstants.Tag.SHORT_TAG:
1078: this .setNextValueAsShort(value.getShortValue());
1079: break;
1080: case JDWPConstants.Tag.STRING_TAG:
1081: case JDWPConstants.Tag.ARRAY_TAG:
1082: case JDWPConstants.Tag.CLASS_LOADER_TAG:
1083: case JDWPConstants.Tag.CLASS_OBJECT_TAG:
1084: case JDWPConstants.Tag.OBJECT_TAG:
1085: case JDWPConstants.Tag.THREAD_GROUP_TAG:
1086: case JDWPConstants.Tag.THREAD_TAG:
1087: this .setNextValueAsObjectID(value.getLongValue());
1088: break;
1089: default:
1090: throw new TestErrorException("Illegal tag value = "
1091: + value.getTag());
1092: }
1093: }
1094:
1095: /**
1096: * Gets the next value of the data of the Packet as UntaggedValue
1097: * VM-sensitive value. If length is less than 8 bytes, the appropriate high
1098: * bits in the returned value can be ignored.
1099: *
1100: * @return the next value of the data of the Packet as VM-sensitive value.
1101: */
1102: public Value getNextValueAsUntaggedValue(byte tag) {
1103: Value value = null;
1104: switch (tag) {
1105: case JDWPConstants.Tag.BOOLEAN_TAG:
1106: value = new Value(this .getNextValueAsBoolean());
1107: break;
1108: case JDWPConstants.Tag.BYTE_TAG:
1109: value = new Value(this .getNextValueAsByte());
1110: break;
1111: case JDWPConstants.Tag.CHAR_TAG:
1112: value = new Value(this .getNextValueAsChar());
1113: break;
1114: case JDWPConstants.Tag.DOUBLE_TAG:
1115: value = new Value(this .getNextValueAsDouble());
1116: break;
1117: case JDWPConstants.Tag.FLOAT_TAG:
1118: value = new Value(this .getNextValueAsFloat());
1119: break;
1120: case JDWPConstants.Tag.INT_TAG:
1121: value = new Value(this .getNextValueAsInt());
1122: break;
1123: case JDWPConstants.Tag.LONG_TAG:
1124: value = new Value(this .getNextValueAsLong());
1125: break;
1126: case JDWPConstants.Tag.SHORT_TAG:
1127: value = new Value(this .getNextValueAsShort());
1128: break;
1129: case JDWPConstants.Tag.STRING_TAG:
1130: case JDWPConstants.Tag.ARRAY_TAG:
1131: case JDWPConstants.Tag.CLASS_LOADER_TAG:
1132: case JDWPConstants.Tag.CLASS_OBJECT_TAG:
1133: case JDWPConstants.Tag.OBJECT_TAG:
1134: case JDWPConstants.Tag.THREAD_GROUP_TAG:
1135: case JDWPConstants.Tag.THREAD_TAG:
1136: value = new Value(tag, this .getNextValueAsObjectID());
1137: break;
1138: default:
1139: throw new TestErrorException("Illegal tag value = " + tag);
1140: }
1141: return value;
1142: }
1143:
1144: /**
1145: * Sets the next value of the data of the Packet as ArrayRegion VM-sensitive
1146: * value. If length is less than 8 bytes, the appropriate high bits in the
1147: * val value will be ignored.
1148: *
1149: * @param array
1150: * ArrayRegion value.
1151: * @throws UnsupportedEncodingException
1152: */
1153: // public void setNextValueAsArrayRegion(ArrayRegion array) throws
1154: // UnsupportedEncodingException {
1155: public void setNextValueAsArrayRegion(ArrayRegion array) {
1156: this .setNextValueAsByte(array.getTag());
1157: this .setNextValueAsInt(array.getLength());
1158: for (int i = 0; i < array.getLength(); i++) {
1159: if (isValuePrimitiveType(array.getTag())) {
1160: switch (array.getTag()) {
1161: case JDWPConstants.Tag.BOOLEAN_TAG:
1162: this .setNextValueAsBoolean(array.getValue(i)
1163: .getBooleanValue());
1164: break;
1165: case JDWPConstants.Tag.BYTE_TAG:
1166: this .setNextValueAsByte(array.getValue(i)
1167: .getByteValue());
1168: break;
1169: case JDWPConstants.Tag.DOUBLE_TAG:
1170: this .setNextValueAsDouble(array.getValue(i)
1171: .getDoubleValue());
1172: break;
1173: case JDWPConstants.Tag.FLOAT_TAG:
1174: this .setNextValueAsFloat(array.getValue(i)
1175: .getFloatValue());
1176: break;
1177: case JDWPConstants.Tag.INT_TAG:
1178: this .setNextValueAsInt(array.getValue(i)
1179: .getIntValue());
1180: break;
1181: case JDWPConstants.Tag.LONG_TAG:
1182: this .setNextValueAsLong(array.getValue(i)
1183: .getLongValue());
1184: break;
1185: case JDWPConstants.Tag.SHORT_TAG:
1186: this .setNextValueAsShort(array.getValue(i)
1187: .getShortValue());
1188: break;
1189: default:
1190: throw new TestErrorException("Illegal tag value = "
1191: + array.getTag());
1192: }
1193: } else {
1194: this .setNextValueAsValue(array.getValue(i));
1195: }
1196: }
1197: }
1198:
1199: /**
1200: * Gets the next value of the data of the Packet as ArrayRegion VM-sensitive
1201: * value. If length is less than 8 bytes, the appropriate high bits in the
1202: * returned value can be ignored.
1203: *
1204: * @return the next value of the data of the Packet as VM-sensitive value.
1205: */
1206: public ArrayRegion getNextValueAsArrayRegion() {
1207: byte array_tag = this .getNextValueAsByte();
1208: int array_length = this .getNextValueAsInt();
1209:
1210: ArrayRegion array = new ArrayRegion(array_tag, array_length);
1211:
1212: for (int i = 0; i < array_length; i++) {
1213: if (isValuePrimitiveType(array_tag))
1214: array.setValue(i, this
1215: .getNextValueAsUntaggedValue(array_tag));
1216: else
1217: array.setValue(i, this .getNextValueAsValue());
1218: }
1219: return array;
1220: }
1221:
1222: /**
1223: * Gets the representation of the Packet as array of bytes in the JDWP
1224: * format including header and data sections.
1225: *
1226: * @return bytes representation of this packet
1227: */
1228: public byte[] toBytesArray() {
1229: byte res[] = new byte[data.length + HEADER_SIZE];
1230: writeAtByteArray(data.length + HEADER_SIZE, res, LENGTH_INDEX,
1231: INT_SIZE);
1232: writeAtByteArray(id, res, ID_INDEX, INT_SIZE);
1233: res[FLAGS_INDEX] = flags;
1234: System.arraycopy(data, 0, res, HEADER_SIZE, data.length);
1235: return res;
1236: }
1237:
1238: /**
1239: * Reads value from array of bytes ar[] starting form index and reading size
1240: * bytes. If size is less than 8, the appropriate high bits in the resulting
1241: * long value will be zero.
1242: *
1243: * @param ar
1244: * the array of bytes where the value is read from.
1245: * @param from
1246: * index to start reading bytes.
1247: * @param size
1248: * number of bytes to read
1249: */
1250: protected static long readFromByteArray(byte ar[], int from,
1251: int size) {
1252: long res = 0;
1253: byte temp;
1254: for (int i = 0; i < size; i++) {
1255: temp = ar[from + i];
1256: res = (res << 8) | (((long) temp) & 0xFF);
1257: }
1258: return res;
1259: }
1260:
1261: /**
1262: * Tells whether the packet is reply.
1263: *
1264: * @return true if this packet is reply, false if it is command
1265: */
1266: public boolean isReply() {
1267: return (flags & REPLY_PACKET_FLAG) != 0;
1268: }
1269:
1270: /**
1271: * Checks whether all data has been read from the packet.
1272: *
1273: * @return boolean
1274: */
1275: public boolean isAllDataRead() {
1276: return reading_data_index == data.length;
1277: }
1278:
1279: /**
1280: * Writes value - val to the array of bytes ar[], beginning from index - to,
1281: * size of value is - size bytes. If size is less than 8, the appropriate
1282: * high bits in the val value will be ignored.
1283: *
1284: * @param val
1285: * the value, which will be written in the array.
1286: * @param ar
1287: * the array of bytes where the value is read from.
1288: * @param to
1289: * the beginning index in the array of bytes.
1290: * @param size
1291: * size of value in bytes.
1292: */
1293: protected void writeAtByteArray(long val, byte ar[], int to,
1294: int size) {
1295: for (int i = 0; i < size; i++) {
1296: ar[to + i] = (byte) (val >> 8 * (size - 1 - i));
1297: }
1298: }
1299:
1300: /**
1301: * Returns true if this bytes array can be interpreted as reply packet.
1302: *
1303: * @param p
1304: * bytes representation of packet
1305: * @return true or false
1306: */
1307: public static boolean isReply(byte[] p) {
1308: if (p.length < FLAGS_INDEX)
1309: return false;
1310: return (p[FLAGS_INDEX] & REPLY_PACKET_FLAG) != 0;
1311: }
1312:
1313: /**
1314: * Returns packet length from header of given packet bytes.
1315: *
1316: * @param p
1317: * bytes representation of packet
1318: * @return true or false
1319: */
1320: public static int getPacketLength(byte[] p) {
1321: return (int) readFromByteArray(p, LENGTH_INDEX, INT_SIZE);
1322: }
1323:
1324: /**
1325: * Enwraps this bytes array either to ReplyPacket or EventPacket instance,
1326: * according to result of isReply().
1327: *
1328: * @param p
1329: * bytes array to enwrap into packet
1330: * @return new created ReplyPacket or CommandPacket
1331: */
1332: public static Packet interpretPacket(byte[] p) {
1333: if (p.length < HEADER_SIZE)
1334: throw new TestErrorException("Wrong packet");
1335: if (Packet.isReply(p))
1336: return new ReplyPacket(p);
1337: return new EventPacket(p);
1338: }
1339: }
|