View Javadoc

1   /*
2    * Copyright  2000-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License"); 
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License. 
15   *
16   */
17  package org.apache.bcel.classfile;
18  
19  import java.io.DataInputStream;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  import org.apache.bcel.Constants;
23  
24  /*** 
25   * This class is derived from the abstract 
26   * <A HREF="org.apache.bcel.classfile.Constant.html">Constant</A> class 
27   * and represents a reference to a Utf8 encoded string.
28   *
29   * @version $Id: ConstantUtf8.java 386056 2006-03-15 11:31:56Z tcurdt $
30   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31   * @see     Constant
32   */
33  public final class ConstantUtf8 extends Constant {
34  
35      private String bytes;
36  
37  
38      /***
39       * Initialize from another object.
40       */
41      public ConstantUtf8(ConstantUtf8 c) {
42          this(c.getBytes());
43      }
44  
45  
46      /***
47       * Initialize instance from file data.
48       *
49       * @param file Input stream
50       * @throws IOException
51       */
52      ConstantUtf8(DataInputStream file) throws IOException {
53          super(Constants.CONSTANT_Utf8);
54          bytes = file.readUTF();
55      }
56  
57  
58      /***
59       * @param bytes Data
60       */
61      public ConstantUtf8(String bytes) {
62          super(Constants.CONSTANT_Utf8);
63          if (bytes == null) {
64              throw new IllegalArgumentException("bytes must not be null!");
65          }
66          this.bytes = bytes;
67      }
68  
69  
70      /***
71       * Called by objects that are traversing the nodes of the tree implicitely
72       * defined by the contents of a Java class. I.e., the hierarchy of methods,
73       * fields, attributes, etc. spawns a tree of objects.
74       *
75       * @param v Visitor object
76       */
77      public void accept( Visitor v ) {
78          v.visitConstantUtf8(this);
79      }
80  
81  
82      /***
83       * Dump String in Utf8 format to file stream.
84       *
85       * @param file Output file stream
86       * @throws IOException
87       */
88      public final void dump( DataOutputStream file ) throws IOException {
89          file.writeByte(tag);
90          file.writeUTF(bytes);
91      }
92  
93  
94      /***
95       * @return Data converted to string.
96       */
97      public final String getBytes() {
98          return bytes;
99      }
100 
101 
102     /***
103      * @param bytes the raw bytes of this Utf-8
104      */
105     public final void setBytes( String bytes ) {
106         this.bytes = bytes;
107     }
108 
109 
110     /***
111      * @return String representation
112      */
113     public final String toString() {
114         return super.toString() + "(\"" + Utility.replace(bytes, "\n", "//n") + "\")";
115     }
116 }