KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > coffi > CONSTANT_Utf8_info


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997 Clark Verbrugge
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.coffi;
28 import soot.*;
29
30 import java.io.*;
31 import java.util.Enumeration JavaDoc;
32
33 /** A constant pool entry of type CONSTANT_Utf8; note this is <b>not</b>
34  * multithread safe. It is, however, immutable.
35  * @see cp_info
36  * @author Clark Verbrugge
37  */

38 public class CONSTANT_Utf8_info extends cp_info {
39    // Some local private objects to help with efficient comparisons.
40
private int sHashCode;
41    // for caching the conversion.
42
private String JavaDoc s;
43    /** Byte array of actual utf8 string. */
44    private byte bytes[];
45    /** Constructor from a DataInputSream */
46    public CONSTANT_Utf8_info(DataInputStream d) throws IOException {
47           int len;
48           len = d.readUnsignedShort();
49           bytes = new byte[len+2];
50           bytes[0] = (byte)(len>>8);
51           bytes[1] = (byte)(len & 0xff);
52           if (len>0) {
53           int j;
54                  for (j=0; j<len;j++)
55                     bytes[j+2] = (byte)d.readUnsignedByte();
56           }
57    }
58    /** For writing out the byte stream for this utf8 properly (incl size). */
59    public void writeBytes(DataOutputStream dd) throws IOException {
60           int len;
61           len = bytes.length;
62           dd.writeShort(len-2);
63           dd.write(bytes,2,len-2);
64    }
65    /** Length in bytes of byte array. */
66    public int length() {
67       return (((((int)(bytes[0]))&0xff)<<8) + (((int)(bytes[1]))&0xff));
68    }
69    /** Returns the size of this cp_info object.
70     * @return number of bytes occupied by this object.
71     * @see cp_info#size
72     */

73    public int size() { return length()+3; }
74    /** Converts internal representation into an actual String.
75     * @return String version of this utf8 object.
76     */

77    public String JavaDoc convert() {
78       if (s==null) {
79          try {
80             ByteArrayInputStream bs = new ByteArrayInputStream(bytes);
81             DataInputStream d = new DataInputStream(bs);
82             String JavaDoc buf = d.readUTF();
83             sHashCode = buf.hashCode();
84             return buf;
85          } catch(IOException e) {
86             return "!!IOException!!";
87          }
88       }
89       return s;
90    }
91    /** Fixes the actual String used to represent the internal representation.
92     * We must have rep == convert(); we verify hashCodes() to spot-check this.
93     * No user-visible effects.
94     */

95    public void fixConversion(String JavaDoc rep) {
96       if (sHashCode != rep.hashCode())
97          throw new RuntimeException JavaDoc("bad use of fixConversion!");
98
99       if (s == null) {
100          s = rep;
101       }
102    }
103    /** Answers whether this utf8 string is the same as a given one.
104     * @param cu utf8 object with which to compare.
105     * @return <i>true</i> if they are equal, <i>false</i> if they are not.
106     */

107    public boolean equals(CONSTANT_Utf8_info cu) {
108       int i,j;
109       j = bytes.length;
110       if (j!=cu.bytes.length) return false;
111       for (i=0; i<j; i++) {
112          if (bytes[i]!=cu.bytes[i]) return false;
113       }
114       return true;
115    }
116    /** Compares this entry with another cp_info object (which may reside
117     * in a different constant pool).
118     * @param constant_pool constant pool of ClassFile for this.
119     * @param cp constant pool entry to compare against.
120     * @param cp_constant_pool constant pool of ClassFile for cp.
121     * @return a value <0, 0, or >0 indicating whether this is smaller,
122     * the same or larger than cp.
123     * @see cp_info#compareTo
124     * @see CONSTANT_Utf8_info#compareTo(cp_info)
125     */

126    public int compareTo(cp_info constant_pool[],cp_info cp,cp_info cp_constant_pool[]) {
127       return compareTo(cp);
128    }
129    /** Compares this entry with another cp_info object; note that for Utf8
130     * object it really doesn't matter whether they're in the same or a different
131     * constant pool, since they really do carry all their data.
132     * @param cp constant pool entry to compare against.
133     * @return a value <0, 0, or >0 indicating whether this is smaller,
134     * the same or larger than cp.
135     * @see cp_info#compareTo
136     * @see CONSTANT_Utf8_info#compareTo(cp_info[],cp_info,cp_info[])
137     */

138    public int compareTo(cp_info cp) {
139       if (tag!=cp.tag) return tag-cp.tag;
140       CONSTANT_Utf8_info cu = (CONSTANT_Utf8_info)cp;
141       G.v().coffi_CONSTANT_Utf8_info_e1.reset(bytes);
142       G.v().coffi_CONSTANT_Utf8_info_e2.reset(cu.bytes);
143       for (;G.v().coffi_CONSTANT_Utf8_info_e1.hasMoreElements() && G.v().coffi_CONSTANT_Utf8_info_e2.hasMoreElements();) {
144          G.v().coffi_CONSTANT_Utf8_info_e1.nextElement();
145          G.v().coffi_CONSTANT_Utf8_info_e2.nextElement();
146          if (G.v().coffi_CONSTANT_Utf8_info_e1.c<G.v().coffi_CONSTANT_Utf8_info_e2.c) return -1;
147          if (G.v().coffi_CONSTANT_Utf8_info_e2.c<G.v().coffi_CONSTANT_Utf8_info_e1.c) return 1;
148       }
149       if (G.v().coffi_CONSTANT_Utf8_info_e1.hasMoreElements()) return -1;
150       if (G.v().coffi_CONSTANT_Utf8_info_e2.hasMoreElements()) return 1;
151       return 0;
152    }
153    /** Utility method; converts the given String into a utf8 encoded array
154     * of bytes.
155     * @param s String to encode.
156     * @return array of bytes, utf8 encoded version of s.
157     */

158    public static byte[] toUtf8(String JavaDoc s) {
159       try {
160          ByteArrayOutputStream bs = new ByteArrayOutputStream(s.length());
161          DataOutputStream d = new DataOutputStream(bs);
162          d.writeUTF(s);
163          return bs.toByteArray();
164       } catch(IOException e) {
165          G.v().out.println("Some sort of IO exception in toUtf8 with " + s);
166       }
167       return null;
168    }
169    /** Returns a String representation of this entry.
170     * @param constant_pool constant pool of ClassFile.
171     * @return String representation of this entry.
172     * @see cp_info#toString
173     */

174    public String JavaDoc toString(cp_info constant_pool[]) {
175       return convert();
176    }
177    /** Returns a String description of what kind of entry this is.
178     * @return the String "utf8".
179     * @see cp_info#typeName
180     */

181    public String JavaDoc typeName() { return "utf8"; }
182 }
183
Popular Tags