KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > yworks > yguard > obf > classfile > Utf8CpInfo


1 /**
2  * YGuard -- an obfuscation library for Java(TM) classfiles.
3  *
4  * Original Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Modifications Copyright (c) 2002 yWorks GmbH (yguard@yworks.com)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * The author may be contacted at yguard@yworks.com
22  *
23  * Java and all Java-based marks are trademarks or registered
24  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
25  */

26 package com.yworks.yguard.obf.classfile;
27
28 import java.io.*;
29 import java.util.*;
30
31 /**
32  * Representation of a 'UTF8' entry in the ConstantPool.
33  *
34  * @author Mark Welsh
35  */

36 public class Utf8CpInfo extends CpInfo
37 {
38     // Constants -------------------------------------------------------------
39

40
41     // Fields ----------------------------------------------------------------
42
private int u2length;
43     private byte[] bytes;
44     private String JavaDoc utf8string;
45
46
47     // Class Methods ---------------------------------------------------------
48

49
50     // Instance Methods ------------------------------------------------------
51
protected Utf8CpInfo()
52     {
53         super(CONSTANT_Utf8);
54     }
55
56     /** Ctor used when appending fresh Utf8 entries to the constant pool. */
57     public Utf8CpInfo(String JavaDoc s)
58     {
59         super(CONSTANT_Utf8);
60         setString(s);
61         refCount = 1;
62     }
63
64     /** Decrement the reference count, blanking the entry if no more references. */
65     public void decRefCount()
66     {
67         super.decRefCount();
68         if (refCount == 0)
69         {
70             clearString();
71         }
72     }
73
74     /** Return UTF8 data as a String. */
75     public String JavaDoc getString()
76     {
77         if (utf8string == null)
78         {
79           try{
80             utf8string = new String JavaDoc(bytes, "UTF8");
81           } catch (UnsupportedEncodingException uee){
82             throw new RuntimeException JavaDoc("Could not decode UTF8");
83           }
84         }
85         return utf8string;
86     }
87
88     /** Set UTF8 data as String. */
89     public void setString(String JavaDoc str)
90     {
91         utf8string = str;
92         try{
93           bytes = str.getBytes("UTF8");
94         } catch (UnsupportedEncodingException uee){
95           throw new RuntimeException JavaDoc("Could not encode UTF8");
96         }
97         u2length = bytes.length;
98     }
99
100     /** Set the UTF8 data to empty. */
101     public void clearString()
102     {
103         u2length = 0;
104         bytes = new byte[0];
105         utf8string = null;
106         getString();
107     }
108
109     /** Read the 'info' data following the u1tag byte. */
110     protected void readInfo(DataInput din) throws java.io.IOException JavaDoc
111     {
112         u2length = din.readUnsignedShort();
113         bytes = new byte[u2length];
114         din.readFully(bytes);
115         getString();
116     }
117
118     /** Write the 'info' data following the u1tag byte. */
119     protected void writeInfo(DataOutput dout) throws java.io.IOException JavaDoc
120     {
121         dout.writeShort(u2length);
122     if (bytes.length > 0) {
123         dout.write(bytes);
124     }
125     }
126 }
127
Popular Tags