001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License. 
016     *
017     */
018    package org.apache.bcel.classfile;
019    
020    import java.io.DataInput;
021    import java.io.DataOutputStream;
022    import java.io.IOException;
023    
024    import org.apache.bcel.Constants;
025    
026    /** 
027     * This class is derived from the abstract 
028     * <A HREF="org.apache.bcel.classfile.Constant.html">Constant</A> class 
029     * and represents a reference to a Utf8 encoded string.
030     *
031     * @version $Id: ConstantUtf8.java 1152072 2011-07-29 01:54:05Z dbrosius $
032     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
033     * @see     Constant
034     */
035    public final class ConstantUtf8 extends Constant {
036    
037        private static final long serialVersionUID = -8709101585611518985L;
038        private String bytes;
039    
040    
041        /**
042         * Initialize from another object.
043         */
044        public ConstantUtf8(ConstantUtf8 c) {
045            this(c.getBytes());
046        }
047    
048    
049        /**
050         * Initialize instance from file data.
051         *
052         * @param file Input stream
053         * @throws IOException
054         */
055        ConstantUtf8(DataInput file) throws IOException {
056            super(Constants.CONSTANT_Utf8);
057            bytes = file.readUTF();
058        }
059    
060    
061        /**
062         * @param bytes Data
063         */
064        public ConstantUtf8(String bytes) {
065            super(Constants.CONSTANT_Utf8);
066            if (bytes == null) {
067                throw new IllegalArgumentException("bytes must not be null!");
068            }
069            this.bytes = bytes;
070        }
071    
072    
073        /**
074         * Called by objects that are traversing the nodes of the tree implicitely
075         * defined by the contents of a Java class. I.e., the hierarchy of methods,
076         * fields, attributes, etc. spawns a tree of objects.
077         *
078         * @param v Visitor object
079         */
080        @Override
081        public void accept( Visitor v ) {
082            v.visitConstantUtf8(this);
083        }
084    
085    
086        /**
087         * Dump String in Utf8 format to file stream.
088         *
089         * @param file Output file stream
090         * @throws IOException
091         */
092        @Override
093        public final void dump( DataOutputStream file ) throws IOException {
094            file.writeByte(tag);
095            file.writeUTF(bytes);
096        }
097    
098    
099        /**
100         * @return Data converted to string.
101         */
102        public final String getBytes() {
103            return bytes;
104        }
105    
106    
107        /**
108         * @param bytes the raw bytes of this Utf-8
109         */
110        public final void setBytes( String bytes ) {
111            this.bytes = bytes;
112        }
113    
114    
115        /**
116         * @return String representation
117         */
118        @Override
119        public final String toString() {
120            return super.toString() + "(\"" + Utility.replace(bytes, "\n", "\\n") + "\")";
121        }
122    }