01: /* ===========================================================================
02: * $RCSfile: Utf8CpInfo.java,v $
03: * ===========================================================================
04: *
05: * RetroGuard -- an obfuscation package for Java classfiles.
06: *
07: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
08: *
09: * This program can be redistributed and/or modified under the terms of the
10: * Version 2 of the GNU General Public License as published by the Free
11: * Software Foundation.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: */
19:
20: package COM.rl.obf.classfile;
21:
22: import java.io.*;
23: import java.util.*;
24:
25: /**
26: * Representation of a 'UTF8' entry in the ConstantPool.
27: *
28: * @author Mark Welsh
29: */
30: public class Utf8CpInfo extends CpInfo {
31: // Constants -------------------------------------------------------------
32:
33: // Fields ----------------------------------------------------------------
34: private int u2length;
35: private byte[] bytes;
36: private String utf8string;
37:
38: // Class Methods ---------------------------------------------------------
39:
40: // Instance Methods ------------------------------------------------------
41: protected Utf8CpInfo() {
42: super (CONSTANT_Utf8);
43: }
44:
45: /** Ctor used when appending fresh Utf8 entries to the constant pool. */
46: public Utf8CpInfo(String s) throws Exception {
47: super (CONSTANT_Utf8);
48: setString(s);
49: refCount = 1;
50: }
51:
52: /** Decrement the reference count, blanking the entry if no more references. */
53: public void decRefCount() throws Exception {
54: super .decRefCount();
55: if (refCount == 0) {
56: clearString();
57: }
58: }
59:
60: /** Return UTF8 data as a String. */
61: public String getString() throws Exception {
62: if (utf8string == null) {
63: utf8string = new String(bytes, "UTF8");
64: }
65: return utf8string;
66: }
67:
68: /** Set UTF8 data as String. */
69: public void setString(String str) throws Exception {
70: utf8string = str;
71: bytes = str.getBytes("UTF8");
72: u2length = bytes.length;
73: }
74:
75: /** Set the UTF8 data to empty. */
76: public void clearString() throws Exception {
77: u2length = 0;
78: bytes = new byte[0];
79: utf8string = null;
80: getString();
81: }
82:
83: /** Read the 'info' data following the u1tag byte. */
84: protected void readInfo(DataInput din) throws Exception {
85: u2length = din.readUnsignedShort();
86: bytes = new byte[u2length];
87: din.readFully(bytes);
88: getString();
89: }
90:
91: /** Write the 'info' data following the u1tag byte. */
92: protected void writeInfo(DataOutput dout) throws Exception {
93: dout.writeShort(u2length);
94: if (bytes.length > 0) {
95: dout.write(bytes);
96: }
97: }
98: }
|