001    package edu.rice.cs.cunit.classFile.constantPool;
002    
003    import java.io.DataInputStream;
004    import java.io.DataOutputStream;
005    import java.io.IOException;
006    import java.util.ArrayList;
007    
008    /**
009     * Represents UTF data in the constant pool.
010     *
011     * @author Mathias Ricken
012     */
013    public abstract class AUTFPoolInfo extends APoolInfo {
014        /**
015         * Data.
016         */
017        protected String _strValue;
018    
019        /**
020         * Constructor.
021         *
022         * @param type constant pool object type
023         * @param s    data
024         */
025        public AUTFPoolInfo(int type, String s, ConstantPool cp) {
026            super(type, cp);
027            _strValue = s;
028        }
029    
030        /**
031         * Constructor reading from a stream.
032         *
033         * @param type type, either ASCII or UNICODE
034         * @param dis  input stream
035         * @param cp   constant pool
036         *
037         * @throws IOException
038         */
039        public AUTFPoolInfo(int type, DataInputStream dis, ConstantPool cp) throws IOException {
040            super(type, cp);
041            StringBuilder xxBuf = new StringBuilder();
042    
043            int len = dis.readShort();
044            while(len > 0) {
045                char c = (char)(dis.readByte());
046                xxBuf.append(c);
047                --len;
048            }
049            _strValue = xxBuf.toString();
050        }
051    
052        /**
053         * Mutator for the data.
054         *
055         * @param strValue new data
056         */
057        public void setStrValue(String strValue) {
058            _strValue = strValue;
059        }
060    
061        /**
062         * Write this constant pool object into the stream, including the type byte.
063         *
064         * @param dos stream
065         *
066         * @throws java.io.IOException
067         */
068        public void write(DataOutputStream dos) throws IOException {
069            reindex();
070            dos.writeByte(_type);
071            dos.writeShort(_strValue.length());
072            dos.writeBytes(_strValue);
073        }
074    
075        /**
076         * Resolve constant pool objects. This makes sure that the object links match the index links.
077         */
078        public void resolve() {
079            // do nothing
080        }
081    
082        /**
083         * Reindex constant pool indices. This makes sure the index links match the object links.
084         */
085        public void reindex() {
086            // do nothing
087        }
088    
089        /**
090         * Return a human-readable version of this constant pool object.
091         *
092         * @return string
093         */
094        public abstract String toStringVerbose();
095    
096        /**
097         * Return a human-readable version of this constant pool object.
098         *
099         * @return string
100         */
101        public String toString() {
102            return _strValue;
103        }
104    
105        /**
106         * Return a hash code.
107         *
108         * @return hash code
109         */
110        public int hashCode() {
111            return _strValue.hashCode();
112        }
113    
114        /**
115         * Compare this object and another one.
116         *
117         * @param obj other object
118         *
119         * @return true if the same
120         */
121        public abstract boolean equals(Object obj);
122    }