KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > javaguard > classfile > Utf8CpInfo


1 /**
2  * JavaGuard -- an obfuscation package for Java classfiles.
3  *
4  * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
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 theit@gmx.de.
22  *
23  *
24  * $Id: Utf8CpInfo.java,v 1.3 2002/05/11 19:04:03 glurk Exp $
25  */

26 package net.sf.javaguard.classfile;
27
28 import java.io.*;
29
30
31 /** Representation of a 'UTF8' entry in the ConstantPool.
32  *
33  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
34  * @author <a HREF="mailto:markw@retrologic.com">Mark Welsh</a>
35  */

36 public class Utf8CpInfo extends CpInfo {
37   /** The length of the byte array in the class file. */
38   private int length;
39   /** The byte array in the class file that holds the Utf8 string. */
40   private byte[] dataBytes;
41   /** String representation of the Utf8 string. */
42   private String JavaDoc utf8dataString;
43   
44   
45   
46   
47   /** Default constructor that creates a new Utf8CpInfo object.
48    */

49   protected Utf8CpInfo() {
50     super(CONSTANT_Utf8);
51   }
52   
53   
54   /** Constructor that is used when appending fresh Utf8 entries to the
55    * constant pool.
56    * @param str the new Utf8 string
57    */

58   public Utf8CpInfo(String JavaDoc str){
59     super(CONSTANT_Utf8);
60     setString(str);
61     // set the reference count to "1"
62
resetRefCount();
63     incRefCount();
64   }
65   
66   
67   
68   
69   /** Decrement the reference count. Blanks the entry if no more references
70    * are available.
71    */

72   public void decRefCount() {
73     super.decRefCount();
74     if (0 == getRefCount()) {
75       clearString();
76     }
77   }
78   
79   
80   
81   
82   /** Return the Utf8 data as a string.
83    * @return the Utf8 data as a string
84    * @see #setString
85    */

86   public String JavaDoc getString() {
87     if (null == getUtf8String()) {
88       try {
89         setUtf8String(new String JavaDoc(getBytes(), "UTF8"));
90       } catch (UnsupportedEncodingException uex) {
91         // shouldn't happen normally
92
setUtf8String(null);
93       }
94     }
95     return getUtf8String();
96   }
97   
98   
99   /** Stores the Utf8 string in the internal byte array.
100    * @param str the Utf8 string
101    * @see #getString
102    */

103   public void setString(String JavaDoc str) {
104     setUtf8String(str);
105     byte[] bytes;
106     try {
107       bytes = str.getBytes("UTF8");
108     } catch (UnsupportedEncodingException uex) {
109       // shouldn't happen normally
110
bytes = new byte[0];
111     }
112     setLength(bytes.length);
113     setBytes(bytes);
114   }
115   
116   
117   /** Empties the internal byte array that holds the Utf8 string.
118    */

119   public void clearString() {
120     setLength(0);
121     setBytes(new byte[0]);
122     setUtf8String(null);
123     getString();
124   }
125   
126   
127   
128   
129   /** Sets the length of the Utf8 string.
130    * @param len the length of the Utf8 string
131    * @see #getLength
132    */

133   protected void setLength(int len) {
134     this.length = len;
135   }
136   
137   
138   /** Returns the length of the Utf8 string.
139    * @return length of the Utf8 string
140    * @see #setLength
141    */

142   protected int getLength() {
143     return length;
144   }
145   
146   
147   
148   
149   /** Sets the byte representation of the Utf8 string.
150    * @param bytes the byte representation of the Utf8 string
151    * @see #getBytes
152    */

153   protected void setBytes(byte[] bytes) {
154     this.dataBytes = bytes;
155   }
156   
157   
158   /** Returns the byte representation of the Utf8 string.
159    * @return byte representation of the Utf8 string
160    * @see #setBytes
161    */

162   protected byte[] getBytes() {
163     return dataBytes;
164   }
165   
166   
167   
168   
169   /** Sets the string representation of the Utf8 data.
170    * @param str the string representation
171    * @see #getUtf8String
172    */

173   protected void setUtf8String(String JavaDoc str) {
174     this.utf8dataString = str;
175   }
176   
177   
178   /** Returns the string representation of the Utf8 data.
179    * @return string representation
180    * @see #setUtf8String
181    */

182   protected String JavaDoc getUtf8String() {
183     return utf8dataString;
184   }
185   
186   
187   
188   
189   /** Read the 'info' data (the Utf8 string) following the u1tag byte.
190    * @param din the input stream
191    * @throws IOException if an I/O error occurs
192    */

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   /** Write the 'info' data (the Utf8 string) following the u1tag byte.
204    * @param dout the output stream
205    * @throws IOException if an I/O error occurs
206    */

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   /** Dump the content of the entry to the specified file (used for debugging).
219    * @param pw the print writer
220    * @param cf the class file the element belongs to
221    * @param index the index in the constant pool
222    */

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