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