1 26 package net.sf.javaguard.classfile; 27 28 import java.io.*; 29 30 31 36 public class Utf8CpInfo extends CpInfo { 37 38 private int length; 39 40 private byte[] dataBytes; 41 42 private String utf8dataString; 43 44 45 46 47 49 protected Utf8CpInfo() { 50 super(CONSTANT_Utf8); 51 } 52 53 54 58 public Utf8CpInfo(String str){ 59 super(CONSTANT_Utf8); 60 setString(str); 61 resetRefCount(); 63 incRefCount(); 64 } 65 66 67 68 69 72 public void decRefCount() { 73 super.decRefCount(); 74 if (0 == getRefCount()) { 75 clearString(); 76 } 77 } 78 79 80 81 82 86 public String getString() { 87 if (null == getUtf8String()) { 88 try { 89 setUtf8String(new String (getBytes(), "UTF8")); 90 } catch (UnsupportedEncodingException uex) { 91 setUtf8String(null); 93 } 94 } 95 return getUtf8String(); 96 } 97 98 99 103 public void setString(String str) { 104 setUtf8String(str); 105 byte[] bytes; 106 try { 107 bytes = str.getBytes("UTF8"); 108 } catch (UnsupportedEncodingException uex) { 109 bytes = new byte[0]; 111 } 112 setLength(bytes.length); 113 setBytes(bytes); 114 } 115 116 117 119 public void clearString() { 120 setLength(0); 121 setBytes(new byte[0]); 122 setUtf8String(null); 123 getString(); 124 } 125 126 127 128 129 133 protected void setLength(int len) { 134 this.length = len; 135 } 136 137 138 142 protected int getLength() { 143 return length; 144 } 145 146 147 148 149 153 protected void setBytes(byte[] bytes) { 154 this.dataBytes = bytes; 155 } 156 157 158 162 protected byte[] getBytes() { 163 return dataBytes; 164 } 165 166 167 168 169 173 protected void setUtf8String(String str) { 174 this.utf8dataString = str; 175 } 176 177 178 182 protected String getUtf8String() { 183 return utf8dataString; 184 } 185 186 187 188 189 193 protected void readInfo(DataInput din) 194 throws IOException { 195 setLength(din.readUnsignedShort()); 196 byte[] bytes = new byte[getLength()]; 197 din.readFully(bytes); 198 setBytes(bytes); 199 getString(); 200 } 201 202 203 207 protected void writeInfo(DataOutput dout) 208 throws IOException { 209 dout.writeShort(getLength()); 210 if (getBytes().length > 0) { 211 dout.write(getBytes()); 212 } 213 } 214 215 216 217 218 223 public void dump(PrintWriter pw, ClassFile cf, int index) { 224 pw.print('['); pw.print(index); pw.println("]: Utf8CpInfo"); 225 pw.print(" -> Utf8 string: '"); 226 pw.print(getString()); 227 pw.println('\''); 228 } 229 } 230 | Popular Tags |