001: /**
002: * YGuard -- an obfuscation library for Java(TM) classfiles.
003: *
004: * Original Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
005: * Modifications Copyright (c) 2002 yWorks GmbH (yguard@yworks.com)
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * The author may be contacted at yguard@yworks.com
022: *
023: * Java and all Java-based marks are trademarks or registered
024: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
025: */package com.yworks.yguard.obf.classfile;
026:
027: import java.io.*;
028: import java.util.*;
029:
030: /**
031: * Representation of a 'UTF8' entry in the ConstantPool.
032: *
033: * @author Mark Welsh
034: */
035: public class Utf8CpInfo extends CpInfo {
036: // Constants -------------------------------------------------------------
037:
038: // Fields ----------------------------------------------------------------
039: private int u2length;
040: private byte[] bytes;
041: private String utf8string;
042:
043: // Class Methods ---------------------------------------------------------
044:
045: // Instance Methods ------------------------------------------------------
046: protected Utf8CpInfo() {
047: super (CONSTANT_Utf8);
048: }
049:
050: /** Ctor used when appending fresh Utf8 entries to the constant pool. */
051: public Utf8CpInfo(String s) {
052: super (CONSTANT_Utf8);
053: setString(s);
054: refCount = 1;
055: }
056:
057: /** Decrement the reference count, blanking the entry if no more references. */
058: public void decRefCount() {
059: super .decRefCount();
060: if (refCount == 0) {
061: clearString();
062: }
063: }
064:
065: /** Return UTF8 data as a String. */
066: public String getString() {
067: if (utf8string == null) {
068: try {
069: utf8string = new String(bytes, "UTF8");
070: } catch (UnsupportedEncodingException uee) {
071: throw new RuntimeException("Could not decode UTF8");
072: }
073: }
074: return utf8string;
075: }
076:
077: /** Set UTF8 data as String. */
078: public void setString(String str) {
079: utf8string = str;
080: try {
081: bytes = str.getBytes("UTF8");
082: } catch (UnsupportedEncodingException uee) {
083: throw new RuntimeException("Could not encode UTF8");
084: }
085: u2length = bytes.length;
086: }
087:
088: /** Set the UTF8 data to empty. */
089: public void clearString() {
090: u2length = 0;
091: bytes = new byte[0];
092: utf8string = null;
093: getString();
094: }
095:
096: /** Read the 'info' data following the u1tag byte. */
097: protected void readInfo(DataInput din) throws java.io.IOException {
098: u2length = din.readUnsignedShort();
099: bytes = new byte[u2length];
100: din.readFully(bytes);
101: getString();
102: }
103:
104: /** Write the 'info' data following the u1tag byte. */
105: protected void writeInfo(DataOutput dout)
106: throws java.io.IOException {
107: dout.writeShort(u2length);
108: if (bytes.length > 0) {
109: dout.write(bytes);
110: }
111: }
112: }
|