1 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 ; 35 import java.io.OutputStream ; 36 37 40 public class ByteCodeWriter { 41 private static final L10N L = new L10N(ByteCodeWriter.class); 42 43 private OutputStream _os; 44 private JavaClass _javaClass; 45 private ByteBuffer _bb = new ByteBuffer(); 46 47 ByteCodeWriter(OutputStream os, JavaClass javaClass) 48 { 49 _os = os; 50 _javaClass = javaClass; 51 } 52 53 56 public JavaClass getJavaClass() 57 { 58 return _javaClass; 59 } 60 61 64 public void writeClass(String className) 65 throws IOException 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 79 public void writeUTF8Const(String value) 80 throws IOException 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 (L.l("utf8 constant {0} does not exist", value)); 89 } 90 91 94 public void write(int v) 95 throws IOException 96 { 97 _os.write(v); 98 } 99 100 103 public void write(byte []buffer, int offset, int length) 104 throws IOException 105 { 106 _os.write(buffer, offset, length); 107 } 108 109 112 public void writeShort(int v) 113 throws IOException 114 { 115 _os.write(v >> 8); 116 _os.write(v); 117 } 118 119 122 public void writeInt(int v) 123 throws IOException 124 { 125 _os.write(v >> 24); 126 _os.write(v >> 16); 127 _os.write(v >> 8); 128 _os.write(v); 129 } 130 131 134 public void writeLong(long v) 135 throws IOException 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 151 public void writeFloat(float v) 152 throws IOException 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 165 public void writeDouble(double v) 166 throws IOException 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 184 public void writeUTF8(String value) 185 throws IOException 186 { 187 writeUTF8(_bb, value); 188 189 writeShort(_bb.size()); 190 _os.write(_bb.getBuffer(), 0, _bb.size()); 191 } 192 193 196 public void writeIntUTF8(String value) 197 throws IOException 198 { 199 writeUTF8(_bb, value); 200 201 writeInt(_bb.size()); 202 _os.write(_bb.getBuffer(), 0, _bb.size()); 203 } 204 205 208 public void writeUTF8(ByteBuffer bb, String 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 |