1 30 31 package oracle.toplink.libraries.asm.attrs; 32 33 import oracle.toplink.libraries.asm.Attribute; 34 import oracle.toplink.libraries.asm.ByteVector; 35 import oracle.toplink.libraries.asm.ClassReader; 36 import oracle.toplink.libraries.asm.ClassWriter; 37 import oracle.toplink.libraries.asm.Label; 38 39 74 75 public class SourceDebugExtensionAttribute extends Attribute { 76 77 public String debugExtension; 78 79 public SourceDebugExtensionAttribute () { 80 super("SourceDebugExtension"); 81 } 82 83 public SourceDebugExtensionAttribute (String debugExtension) { 84 this(); 85 this.debugExtension = debugExtension; 86 } 87 88 protected Attribute read (ClassReader cr, int off, 89 int len, char[] buf, int codeOff, Label[] labels) { 90 return new SourceDebugExtensionAttribute(readUTF8(cr, off, len)); 91 } 92 93 protected ByteVector write (ClassWriter cw, byte[] code, 94 int len, int maxStack, int maxLocals) { 95 byte[] b = putUTF8(debugExtension); 96 return new ByteVector().putByteArray(b, 0, b.length); 97 } 98 99 private String readUTF8 (ClassReader cr, int index, int utfLen) { 100 int endIndex = index + utfLen; 101 byte[] b = cr.b; 102 char[] buf = new char[utfLen]; 103 int strLen = 0; 104 int c, d, e; 105 while (index < endIndex) { 106 c = b[index++] & 0xFF; 107 switch (c >> 4) { 108 case 0: 109 case 1: 110 case 2: 111 case 3: 112 case 4: 113 case 5: 114 case 6: 115 case 7: 116 buf[strLen++] = (char)c; 118 break; 119 120 case 12: 121 case 13: 122 d = b[index++]; 124 buf[strLen++] = (char)(((c & 0x1F) << 6) | (d & 0x3F)); 125 break; 126 127 default: 128 d = b[index++]; 130 e = b[index++]; 131 buf[strLen++] = (char)(((c & 0x0F) << 12) | ((d & 0x3F) << 6) | (e & 0x3F)); 132 break; 133 } 134 } 135 136 return new String (buf, 0, strLen); 137 } 138 139 private byte[] putUTF8 (String s) { 140 int charLength = s.length(); 141 int byteLength = 0; 142 for (int i = 0; i < charLength; ++i) { 143 char c = s.charAt(i); 144 if (c >= '\001' && c <= '\177') { 145 byteLength++; 146 } else if (c > '\u07FF') { 147 byteLength += 3; 148 } else { 149 byteLength += 2; 150 } 151 } 152 155 byte[] data = new byte[byteLength]; 156 for (int i = 0; i < charLength;) { 157 char c = s.charAt(i); 158 if (c >= '\001' && c <= '\177') { 159 data[i++] = (byte)c; 160 } else if (c > '\u07FF') { 161 data[i++] = (byte)(0xE0 | c >> 12 & 0xF); 162 data[i++] = (byte)(0x80 | c >> 6 & 0x3F); 163 data[i++] = (byte)(0x80 | c & 0x3F); 164 } else { 165 data[i++] = (byte)(0xC0 | c >> 6 & 0x1F); 166 data[i++] = (byte)(0x80 | c & 0x3F); 167 } 168 } 169 return data; 170 } 171 172 public String toString () { 173 return debugExtension; 174 } 175 } 176 | Popular Tags |