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