KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > io > ByteCodeWriter


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.io;
9
10 import org.gjt.jclasslib.bytecode.AbstractInstruction;
11
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18     Converts a list of instructions as defined in the package
19     <tt>org.gjt.jclasslib.code</tt> to code.
20  
21     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
22     @version $Revision: 1.5 $ $Date: 2003/08/18 07:58:12 $
23 */

24 public class ByteCodeWriter {
25
26     private ByteCodeWriter() {
27     }
28     
29     /**
30         Converts a list of instructions to code.
31         @param instructions the <tt>java.util.List</tt> with the instructions
32         @return the code as an array of bytes
33         @throws IOException if an exception occurs with the code
34      */

35     public static byte[] writeByteCode(List JavaDoc instructions) throws IOException JavaDoc {
36
37         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
38         ByteCodeOutputStream bcos = new ByteCodeOutputStream(baos);
39         
40         Iterator JavaDoc it = instructions.iterator();
41         while (it.hasNext()) {
42             writeNextInstruction(bcos, (AbstractInstruction)it.next());
43         }
44         bcos.close();
45         return baos.toByteArray();
46     }
47     
48     private static void writeNextInstruction(ByteCodeOutputStream bcos,
49                                              AbstractInstruction instruction)
50         throws IOException JavaDoc
51     {
52         instruction.write(bcos);
53         
54     }
55     
56 }
57
Popular Tags