1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21 package autohit.vm;
22
23 import java.io.InputStream;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.io.OutputStream;
27 import java.util.ListIterator;
28
29 import autohit.common.Constants;
30 import autohit.vm.i.VMInstruction;
31
32 /**
33 * This is a wrapper for an executable. It provides helpers and stuff.
34 * You shouldn't log from these helps. Also, most exceptions should be
35 * propogated, rather than handled, unless it's part of the normal logic.
36 * <p>
37 * @author Erich P. Gatejen
38 * @version 1.0
39 * <i>Version History</i>
40 * <code>EPG - Initial - 15apr03</code>
41 *
42 */
43 public class VMExecutableWrapper {
44
45 private ListIterator dump;
46
47 /**
48 * This is a reference to the executable.
49 * @see autohit.vm.VMExecutable
50 */
51 public VMExecutable exec;
52
53 /**
54 * Default Constructor. The wrappers are always empty. You need
55 * to load or create the executable.
56 * @see #load(InputStream is)
57 * @see #create()
58 */
59 public VMExecutableWrapper() {
60 exec = null;
61 }
62
63 /**
64 * Emit an instruction helper
65 * @see autohit.vm.i.VMInstruction
66 */
67 public void emit(VMInstruction i) {
68 synchronized (exec) {
69 exec.core.add(i);
70 }
71 }
72
73 /**
74 * Next IP location, if instruction were to be added
75 * @see autohit.vm.i.VMInstruction
76 */
77 public int nextIP() {
78 synchronized (exec) {
79 return exec.core.size();
80 }
81 }
82
83 /**
84 * Clean the core helper
85 * @see autohit.vm.i.VMInstruction
86 */
87 public void clean() {
88 synchronized (exec) {
89 exec.core.trimToSize();
90 }
91 }
92
93 /**
94 * Create a fresh and new executable
95 * @see autohit.vm.VMExecutable
96 */
97 public void create() {
98 exec = new VMExecutable();
99 exec.init();
100 }
101
102 /**
103 * Load a VMExecutable from a stream. This will deserialize it.
104 * @see autohit.vm.VMExecutable
105 * @throws any exception
106 */
107 public void load(InputStream is) throws Exception {
108 ObjectInputStream p = new ObjectInputStream(is);
109 exec = (VMExecutable) p.readObject();
110 is.close();
111 }
112
113 /**
114 * Save a VMExecutable from a stream. This will serialize it.
115 * @see autohit.vm.VMExecutable
116 * @throws any exception
117 */
118 public void save(OutputStream os) throws Exception {
119 ObjectOutputStream sobj = new ObjectOutputStream(os);
120 sobj.writeObject(exec);
121 sobj.flush();
122 os.close();
123 }
124
125 /**
126 * This starts a dump of the executable. It will yield the first
127 * line. When there is no more to dump, it will return a null.
128 *
129 * @return a String containing the first line of the dump or null.
130 */
131 public String startDump() {
132
133 VMInstruction i;
134 String r;
135
136 // if something bad happens, just return an empty dump
137 try {
138 dump = exec.core.listIterator();
139 i = (VMInstruction) dump.next();
140 r = i.toString();
141 } catch (Exception e) {
142 r = null;
143 }
144 return r;
145 }
146
147 /**
148 * This returns the next line of the dump. It will return null if the
149 * dump is done or not valid.
150 *
151 * @return a String containing the next line of the dump or null.
152 */
153 public String nextDump() {
154
155 VMInstruction i;
156 String r;
157
158 // if something bad happens, just return an empty dump
159 try {
160 i = (VMInstruction) dump.next();
161 r = i.toString();
162 } catch (Exception e) {
163 // This also traps the no next element exception
164 r = null;
165 }
166 return r;
167 }
168
169 /**
170 * Creates a text dump of the executable. It will use line.seperator
171 * system property as the line terminator.
172 *
173 * @return a String containing the text dump.
174 */
175 public String toString() {
176 StringBuffer d = new StringBuffer();
177
178 int ip = 0;
179 String n = this.startDump();
180 while (n != null) {
181 d.append(ip + ":");
182 d.append(n);
183 d.append(Constants.CRUDE_SEPERATOR);
184 n = this.nextDump();
185 ip++;
186 }
187 return d.toString();
188 }
189 }
|