Patches from Robert Kraske (robekras):
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / PHPDBGFrame.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9         Vicente Fernando - www.alfersoft.com.ar - Initial implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.internal.debug.core;
12
13 import java.util.Vector;
14
15 public class PHPDBGFrame {
16
17         public static final int FRAME_HEADER_SIZE       = 8;                            // Header consist of 4 byte frame type and 4 byte frame length
18         private char[]          frameType                       = new char[4];
19         private Vector          frameData                       = new Vector ();
20         private int             frameSize                       = 0;
21
22         /**
23          * Construct a new frame.
24          *
25          * @param frameType The type of the frame which is created.
26          */
27         public PHPDBGFrame (int frameType) {
28                 this.frameType = PHPDBGBase.IntToChar4 (frameType);
29                 frameSize     += FRAME_HEADER_SIZE;
30         }
31
32         /**
33          * Add an integer to the frame.
34          *
35          * @param num
36          */
37         public void addInt (int num) {
38                 char[] newData = PHPDBGBase.IntToChar4 (num);               // Convert the integer to four bytes big endian
39                 frameData.add (newData);                                    // Add the four bytes to the frame data
40                 frameSize     += 4;                                         // Calculate the new fram size
41         }
42
43         /**
44          * Add a character to the frame.
45          *
46          * @param ch The character which is to add to the frame.
47          */
48         public void addChar (char ch) {
49                 char[] newData = new char[1];
50
51                 newData[0] = ch;                                            //
52                 frameData.add (newData);                                    // Add the character to the frame data
53                 frameSize += 1;                                             // Calculate the new fram size
54         }
55
56         /**
57          * @param str
58          */
59         public void addString (String str) {
60                 frameData.add (str);
61                 frameSize += str.length ();
62         }
63
64         /**
65          * Get the size of the frame, including the frame header.
66          *
67          * @return The size of the entire frame.
68          */
69         public int getSize () {
70                 return frameSize;
71         }
72
73         /**
74          * Return the size of the frame, which is the number of all bytes
75          * without the 8 byte from frame header.
76          *
77          * @return The size of the frame (without the frame header).
78          */
79         public int getSizeOfData () {
80                 return frameSize - FRAME_HEADER_SIZE;
81         }
82
83         /**
84          * Get the header of this frame.
85          *
86          * @return The eight char array which forms the header of this frame.
87          */
88         public char[] getHeader () {
89                 char[] ret = new char[FRAME_HEADER_SIZE];                   // Allocate 8 chars for the header
90
91                 PHPDBGBase.copyChars (ret, frameType, 4);                   // The first four chars are the frame type
92                 PHPDBGBase.copyCharsTo (ret, PHPDBGBase.IntToChar4 (getSizeOfData ()), 4, 4); // The second four chars is for the size of the data area
93
94                 return ret;                                                                                                     // Return the header
95         }
96
97         /**
98          * Get the data array of this frame
99          *
100          * TODO Finish commenting
101          *
102          * @return The char array which holds the data of this frame.
103          */
104         public char[] getFrameData () {
105                 char[] ret      = new char[getSizeOfData ()];                                                                   // The frame data (data without the frame header)
106                 int pos         = 0;                                                                                                                                    // The current position for the 'ret' array
107                 int i;                                                                              // The current position for the frame data list
108
109                 for (i = 0; i < frameData.size (); i++) {                                           // For frame data entries within the list
110                         if (frameData.get (i).getClass ().getName ().equals ("[C")) {                   // What kind of type is the frame data
111                                 char[] conv = (char[]) frameData.get (i);                                   //
112
113                                 PHPDBGBase.copyCharsTo (ret, conv, conv.length, pos);                       //
114                                 pos        += conv.length;
115                         } else {
116                                 if (frameData.get (i).getClass ().getName ().equals ("java.lang.String")) { //
117                                         String conv = (String) frameData.get (i);                               //
118
119                                         PHPDBGBase.copyCharsTo (ret, conv.toCharArray (), conv.length (), pos); //
120                                         pos        += conv.length ();                                           //
121                                 }
122                         }
123                 }
124
125                 return ret;                                                                                                                                                     // Return the data frame array
126         }
127 }