KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > ByteCodeWriter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.bytecode;
30
31 import com.caucho.util.ByteBuffer;
32 import com.caucho.util.L10N;
33
34 import java.io.IOException JavaDoc;
35 import java.io.OutputStream JavaDoc;
36
37 /**
38  * Interface to the bytecode compiler.
39  */

40 public class ByteCodeWriter {
41   private static final L10N L = new L10N(ByteCodeWriter.class);
42   
43   private OutputStream JavaDoc _os;
44   private JavaClass _javaClass;
45   private ByteBuffer _bb = new ByteBuffer();
46
47   ByteCodeWriter(OutputStream JavaDoc os, JavaClass javaClass)
48   {
49     _os = os;
50     _javaClass = javaClass;
51   }
52
53   /**
54    * Returns the java class for the writer.
55    */

56   public JavaClass getJavaClass()
57   {
58     return _javaClass;
59   }
60
61   /**
62    * Writes a class constant.
63    */

64   public void writeClass(String JavaDoc className)
65     throws IOException JavaDoc
66   {
67     ConstantPool pool = _javaClass.getConstantPool();
68     ClassConstant classConst = pool.getClass(className);
69
70     if (classConst != null)
71       writeShort(classConst.getIndex());
72     else
73       writeShort(0);
74   }
75
76   /**
77    * Writes a UTF8 constant.
78    */

79   public void writeUTF8Const(String JavaDoc value)
80     throws IOException JavaDoc
81   {
82     ConstantPool pool = _javaClass.getConstantPool();
83     Utf8Constant entry = pool.getUTF8(value);
84
85     if (entry != null)
86       writeShort(entry.getIndex());
87     else
88       throw new NullPointerException JavaDoc(L.l("utf8 constant {0} does not exist", value));
89   }
90
91   /**
92    * Writes a byte
93    */

94   public void write(int v)
95     throws IOException JavaDoc
96   {
97     _os.write(v);
98   }
99
100   /**
101    * Writes a buffer
102    */

103   public void write(byte []buffer, int offset, int length)
104     throws IOException JavaDoc
105   {
106     _os.write(buffer, offset, length);
107   }
108
109   /**
110    * Writes a short.
111    */

112   public void writeShort(int v)
113     throws IOException JavaDoc
114   {
115     _os.write(v >> 8);
116     _os.write(v);
117   }
118
119   /**
120    * Writes an int
121    */

122   public void writeInt(int v)
123     throws IOException JavaDoc
124   {
125     _os.write(v >> 24);
126     _os.write(v >> 16);
127     _os.write(v >> 8);
128     _os.write(v);
129   }
130
131   /**
132    * Writes an int
133    */

134   public void writeLong(long v)
135     throws IOException JavaDoc
136   {
137     _os.write((int) (v >> 56));
138     _os.write((int) (v >> 48));
139     _os.write((int) (v >> 40));
140     _os.write((int) (v >> 32));
141     
142     _os.write((int) (v >> 24));
143     _os.write((int) (v >> 16));
144     _os.write((int) (v >> 8));
145     _os.write((int) v);
146   }
147
148   /**
149    * Writes a float
150    */

151   public void writeFloat(float v)
152     throws IOException JavaDoc
153   {
154     int bits = Float.floatToIntBits(v);
155     
156     _os.write(bits >> 24);
157     _os.write(bits >> 16);
158     _os.write(bits >> 8);
159     _os.write(bits);
160   }
161
162   /**
163    * Writes a double
164    */

165   public void writeDouble(double v)
166     throws IOException JavaDoc
167   {
168     long bits = Double.doubleToLongBits(v);
169     
170     _os.write((int) (bits >> 56));
171     _os.write((int) (bits >> 48));
172     _os.write((int) (bits >> 40));
173     _os.write((int) (bits >> 32));
174     
175     _os.write((int) (bits >> 24));
176     _os.write((int) (bits >> 16));
177     _os.write((int) (bits >> 8));
178     _os.write((int) bits);
179   }
180
181   /**
182    * Writes UTF-8
183    */

184   public void writeUTF8(String JavaDoc value)
185     throws IOException JavaDoc
186   {
187     writeUTF8(_bb, value);
188
189     writeShort(_bb.size());
190     _os.write(_bb.getBuffer(), 0, _bb.size());
191   }
192
193   /**
194    * Writes UTF-8
195    */

196   public void writeIntUTF8(String JavaDoc value)
197     throws IOException JavaDoc
198   {
199     writeUTF8(_bb, value);
200
201     writeInt(_bb.size());
202     _os.write(_bb.getBuffer(), 0, _bb.size());
203   }
204
205   /**
206    * Writes UTF-8
207    */

208   public void writeUTF8(ByteBuffer bb, String JavaDoc value)
209   {
210     bb.clear();
211
212     for (int i = 0; i < value.length(); i++) {
213       int ch = value.charAt(i);
214
215       if (ch > 0 && ch < 0x80)
216     bb.append(ch);
217       else if (ch < 0x800) {
218     bb.append(0xc0 + (ch >> 6));
219     bb.append(0x80 + (ch & 0x3f));
220       }
221       else {
222     bb.append(0xe0 + (ch >> 12));
223     bb.append(0x80 + ((ch >> 6) & 0x3f));
224     bb.append(0x80 + ((ch) & 0x3f));
225       }
226     }
227   }
228 }
229
Popular Tags