001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.bytecode;
030:
031: import com.caucho.util.ByteBuffer;
032: import com.caucho.util.L10N;
033:
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: /**
038: * Interface to the bytecode compiler.
039: */
040: public class ByteCodeWriter {
041: private static final L10N L = new L10N(ByteCodeWriter.class);
042:
043: private OutputStream _os;
044: private JavaClass _javaClass;
045: private ByteBuffer _bb = new ByteBuffer();
046:
047: ByteCodeWriter(OutputStream os, JavaClass javaClass) {
048: _os = os;
049: _javaClass = javaClass;
050: }
051:
052: /**
053: * Returns the java class for the writer.
054: */
055: public JavaClass getJavaClass() {
056: return _javaClass;
057: }
058:
059: /**
060: * Writes a class constant.
061: */
062: public void writeClass(String className) throws IOException {
063: ConstantPool pool = _javaClass.getConstantPool();
064: ClassConstant classConst = pool.getClass(className);
065:
066: if (classConst != null)
067: writeShort(classConst.getIndex());
068: else
069: writeShort(0);
070: }
071:
072: /**
073: * Writes a UTF8 constant.
074: */
075: public void writeUTF8Const(String value) throws IOException {
076: ConstantPool pool = _javaClass.getConstantPool();
077: Utf8Constant entry = pool.getUTF8(value);
078:
079: if (entry != null)
080: writeShort(entry.getIndex());
081: else
082: throw new NullPointerException(L.l(
083: "utf8 constant {0} does not exist", value));
084: }
085:
086: /**
087: * Writes a byte
088: */
089: public void write(int v) throws IOException {
090: _os.write(v);
091: }
092:
093: /**
094: * Writes a buffer
095: */
096: public void write(byte[] buffer, int offset, int length)
097: throws IOException {
098: _os.write(buffer, offset, length);
099: }
100:
101: /**
102: * Writes a short.
103: */
104: public void writeShort(int v) throws IOException {
105: _os.write(v >> 8);
106: _os.write(v);
107: }
108:
109: /**
110: * Writes an int
111: */
112: public void writeInt(int v) throws IOException {
113: _os.write(v >> 24);
114: _os.write(v >> 16);
115: _os.write(v >> 8);
116: _os.write(v);
117: }
118:
119: /**
120: * Writes an int
121: */
122: public void writeLong(long v) throws IOException {
123: _os.write((int) (v >> 56));
124: _os.write((int) (v >> 48));
125: _os.write((int) (v >> 40));
126: _os.write((int) (v >> 32));
127:
128: _os.write((int) (v >> 24));
129: _os.write((int) (v >> 16));
130: _os.write((int) (v >> 8));
131: _os.write((int) v);
132: }
133:
134: /**
135: * Writes a float
136: */
137: public void writeFloat(float v) throws IOException {
138: int bits = Float.floatToIntBits(v);
139:
140: _os.write(bits >> 24);
141: _os.write(bits >> 16);
142: _os.write(bits >> 8);
143: _os.write(bits);
144: }
145:
146: /**
147: * Writes a double
148: */
149: public void writeDouble(double v) throws IOException {
150: long bits = Double.doubleToLongBits(v);
151:
152: _os.write((int) (bits >> 56));
153: _os.write((int) (bits >> 48));
154: _os.write((int) (bits >> 40));
155: _os.write((int) (bits >> 32));
156:
157: _os.write((int) (bits >> 24));
158: _os.write((int) (bits >> 16));
159: _os.write((int) (bits >> 8));
160: _os.write((int) bits);
161: }
162:
163: /**
164: * Writes UTF-8
165: */
166: public void writeUTF8(String value) throws IOException {
167: writeUTF8(_bb, value);
168:
169: writeShort(_bb.size());
170: _os.write(_bb.getBuffer(), 0, _bb.size());
171: }
172:
173: /**
174: * Writes UTF-8
175: */
176: public void writeIntUTF8(String value) throws IOException {
177: writeUTF8(_bb, value);
178:
179: writeInt(_bb.size());
180: _os.write(_bb.getBuffer(), 0, _bb.size());
181: }
182:
183: /**
184: * Writes UTF-8
185: */
186: public void writeUTF8(ByteBuffer bb, String value) {
187: bb.clear();
188:
189: for (int i = 0; i < value.length(); i++) {
190: int ch = value.charAt(i);
191:
192: if (ch > 0 && ch < 0x80)
193: bb.append(ch);
194: else if (ch < 0x800) {
195: bb.append(0xc0 + (ch >> 6));
196: bb.append(0x80 + (ch & 0x3f));
197: } else {
198: bb.append(0xe0 + (ch >> 12));
199: bb.append(0x80 + ((ch >> 6) & 0x3f));
200: bb.append(0x80 + ((ch) & 0x3f));
201: }
202: }
203: }
204: }
|