Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

CONSTANT_Utf8_info.java

00001 package ca.mcgill.sable.soot.coffi;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Jimple, a 3-address code Java(TM) bytecode representation.        *
00005  * Copyright (C) 1997, 1998 Raja Vallee-Rai (kor@sable.mcgill.ca)    *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project of the Sable Research Group,      *
00009  * School of Computer Science, McGill University, Canada             *
00010  * (http://www.sable.mcgill.ca/).  It is understood that any         *
00011  * modification not identified as such is not covered by the         *
00012  * preceding statement.                                              *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this library; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other Sable Research Group projects, please      *
00033  * visit the web site: http://www.sable.mcgill.ca/                   *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 
00036 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00037  * Coffi, a bytecode parser for the Java(TM) language.               *
00038  * Copyright (C) 1996, 1997 Clark Verbrugge (clump@sable.mcgill.ca). *
00039  * All rights reserved.                                              *
00040  *                                                                   *
00041  * This work was done as a project of the Sable Research Group,      *
00042  * School of Computer Science, McGill University, Canada             *
00043  * (http://www.sable.mcgill.ca/).  It is understood that any         *
00044  * modification not identified as such is not covered by the         *
00045  * preceding statement.                                              *
00046  *                                                                   *
00047  * This work is free software; you can redistribute it and/or        *
00048  * modify it under the terms of the GNU Library General Public       *
00049  * License as published by the Free Software Foundation; either      *
00050  * version 2 of the License, or (at your option) any later version.  *
00051  *                                                                   *
00052  * This work is distributed in the hope that it will be useful,      *
00053  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00054  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00055  * Library General Public License for more details.                  *
00056  *                                                                   *
00057  * You should have received a copy of the GNU Library General Public *
00058  * License along with this library; if not, write to the             *
00059  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00060  * Boston, MA  02111-1307, USA.                                      *
00061  *                                                                   *
00062  * Java is a trademark of Sun Microsystems, Inc.                     *
00063  *                                                                   *
00064  * To submit a bug report, send a comment, or get the latest news on *
00065  * this project and other Sable Research Group projects, please      *
00066  * visit the web site: http://www.sable.mcgill.ca/                   *
00067  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00068 
00069 /*
00070  Reference Version
00071  -----------------
00072  This is the latest official version on which this file is based.
00073  The reference version is: $CoffiVersion: 1.1 $
00074                            $SootVersion: 1.beta.4 $
00075 
00076  Change History
00077  --------------
00078  A) Notes:
00079 
00080  Please use the following template.  Most recent changes should
00081  appear at the top of the list.
00082 
00083  - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate]
00084    [description of modification].
00085 
00086  Any Modification flagged with "(*)" was done as a project of the
00087  Sable Research Group, School of Computer Science,
00088  McGill University, Canada (http://www.sable.mcgill.ca/).
00089 
00090  You should add your copyright, using the following template, at
00091  the top of this file, along with other copyrights.
00092 
00093  *                                                                   *
00094  * Modifications by [name] are                                       *
00095  * Copyright (C) [year(s)] [your name (or company)].  All rights     *
00096  * reserved.                                                         *
00097  *                                                                   *
00098 
00099  B) Changes:
00100 
00101  - Modified on November 2, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca) (*)
00102    Repackaged all source files and performed extensive modifications.
00103    First initial release of Soot.
00104 
00105  - Modified on 15-Jun-1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00106    First internal release (Version 0.1).
00107 */
00108 
00109 import java.io.*;
00110 import java.util.Enumeration;
00111 
00112 /** A constant pool entry of type CONSTANT_Utf8; note this is <b>not</b>
00113  * multithread safe.
00114  * @see cp_info
00115  * @author Clark Verbrugge
00116  */
00117 class CONSTANT_Utf8_info extends cp_info {
00118    // Some local private objects to help with efficient comparisons.
00119    private static Utf8_Enumeration e1 = new Utf8_Enumeration();
00120    private static Utf8_Enumeration e2 = new Utf8_Enumeration();
00121    // for caching the conversion.
00122    private String s;
00123    /** Byte array of actual utf8 string. */
00124    public byte bytes[];
00125    /** Compares this entry with another cp_info object (which may reside
00126     * in a different constant pool).
00127     * @param constant_pool constant pool of ClassFile for this.
00128     * @param cp constant pool entry to compare against.
00129     * @param cp_constant_pool constant pool of ClassFile for cp.
00130     * @return a value <0, 0, or >0 indicating whether this is smaller,
00131     * the same or larger than cp.
00132     * @see cp_info#compareTo
00133     * @see CONSTANT_Utf8_info#compareTo(cp_info)
00134     */
00135    public int compareTo(cp_info constant_pool[],cp_info cp,cp_info cp_constant_pool[]) {
00136       return compareTo(cp);
00137    }   
00138    /** Compares this entry with another cp_info object; note that for Utf8
00139     * object it really doesn't matter whether they're in the same or a different
00140     * constant pool, since they really do carry all their data.
00141     * @param cp constant pool entry to compare against.
00142     * @return a value <0, 0, or >0 indicating whether this is smaller,
00143     * the same or larger than cp.
00144     * @see cp_info#compareTo
00145     * @see CONSTANT_Utf8_info#compareTo(cp_info[],cp_info,cp_info[])
00146     */
00147    public int compareTo(cp_info cp) {
00148       if (tag!=cp.tag) return tag-cp.tag;
00149       CONSTANT_Utf8_info cu = (CONSTANT_Utf8_info)cp;
00150       e1.reset(bytes);
00151       e2.reset(cu.bytes);
00152       for (;e1.hasMoreElements() && e2.hasMoreElements();) {
00153          e1.nextElement();
00154          e2.nextElement();
00155          if (e1.c<e2.c) return -1;
00156          if (e2.c<e1.c) return 1;
00157       }
00158       if (e1.hasMoreElements()) return -1;
00159       if (e2.hasMoreElements()) return 1;
00160       return 0;
00161    }   
00162    /** Converts internal representation into an actual String.
00163     * @return String version of this utf8 object.
00164     */
00165    public String convert() {
00166       if (s==null) {
00167          try {
00168             ByteArrayInputStream bs = new ByteArrayInputStream(bytes);
00169             DataInputStream d = new DataInputStream(bs);
00170             s = d.readUTF();
00171          } catch(IOException e) {
00172             return "!!IOException!!";
00173          }
00174       }
00175       return s;
00176    }   
00177    /** Answers whether this utf8 string is the same as a given one.
00178     * @param cu utf8 object with which to compare.
00179     * @return <i>true</i> if they are equal, <i>false</i> if they are not.
00180     */
00181    public boolean equals(CONSTANT_Utf8_info cu) {
00182       int i,j;
00183       j = bytes.length;
00184       if (j!=cu.bytes.length) return false;
00185       for (i=0; i<j; i++) {
00186          if (bytes[i]!=cu.bytes[i]) return false;
00187       }
00188       return true;
00189    }   
00190    /** Length in bytes of byte array. */
00191    public int length() {
00192       return (((((int)(bytes[0]))&0xff)<<8) + (((int)(bytes[1]))&0xff));
00193    }   
00194    /** Returns the size of this cp_info object.
00195     * @return number of bytes occupied by this object.
00196     * @see cp_info#size
00197     */
00198    public int size() { return length()+3; }   
00199    /** Returns a String representation of this entry.
00200     * @param constant_pool constant pool of ClassFile.
00201     * @return String representation of this entry.
00202     * @see cp_info#toString
00203     */
00204    public String toString(cp_info constant_pool[]) {
00205       return convert();
00206    }   
00207    /** Utility method; converts the given String into a utf8 encoded array
00208     * of bytes.
00209     * @param s String to encode.
00210     * @return array of bytes, utf8 encoded version of s.
00211     */
00212    public static byte[] toUtf8(String s) {
00213       try {
00214          ByteArrayOutputStream bs = new ByteArrayOutputStream(s.length());
00215          DataOutputStream d = new DataOutputStream(bs);
00216          d.writeUTF(s);
00217          return bs.toByteArray();
00218       } catch(IOException e) {
00219          System.out.println("Some sort of IO exception in toUtf8 with " + s);
00220       }
00221       return null;
00222    }   
00223    /** Returns a String description of what kind of entry this is.
00224     * @return the String "utf8".
00225     * @see cp_info#typeName
00226     */
00227    public String typeName() { return "utf8"; }   
00228 }

Generated at Thu Feb 7 06:42:53 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001