001: /*
002: * $Id: ConstantPool.java,v 1.3 2002/09/16 08:05:02 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.codec;
011:
012: import java.io.ByteArrayOutputStream;
013: import java.io.DataOutputStream;
014: import java.io.IOException;
015: import java.util.HashMap;
016: import java.util.ArrayList;
017: import java.util.Enumeration;
018:
019: public class ConstantPool implements CodecConstants {
020:
021: protected HashMap _set = new HashMap();
022: protected ArrayList _list = new ArrayList();
023: protected int _count = 1;
024:
025: public ConstantPool() {
026: }
027:
028: protected int getNextIndex(int width) {
029: int c = _count;
030: _count += width;
031: return c;
032: }
033:
034: protected int add(Info info) {
035: Info existing = (Info) _set.get(info);
036: if (existing != null) {
037: return existing.getIndex();
038: }
039: int width = info.getWidth();
040: info.setIndex(getNextIndex(width));
041: _set.put(info, info);
042: _list.add(info);
043: if (width == 2) {
044: _list.add(info);
045: }
046: return info.getIndex();
047: }
048:
049: public Info get(int index) {
050: return (Info) _list.get(index - 1);
051: }
052:
053: public String getString(int index) {
054: Info info = get(index);
055: if (info.getTag() == CONSTANT_Utf8) {
056: return ((Utf8Info) info).getUtf8();
057: }
058: return null;
059: }
060:
061: public int addUtf8(String str) {
062: return add(new Utf8Info(str));
063: }
064:
065: public int addInt(int i) {
066: return add(new IntegerInfo(i));
067: }
068:
069: public int addFloat(float f) {
070: return add(new FloatInfo(f));
071: }
072:
073: public int addLong(long l) {
074: return add(new LongInfo(l));
075: }
076:
077: public int addDouble(double d) {
078: return add(new DoubleInfo(d));
079: }
080:
081: public int addString(String str) {
082: return add(new StringInfo(this , str));
083: }
084:
085: public int addClass(String classname) {
086: return add(new ClassInfo(this , classname));
087: }
088:
089: public int addNameAndType(String name, String type) {
090: return add(new NameAndTypeInfo(this , name, type));
091: }
092:
093: public int addNameAndType(int name, int type) {
094: return add(new NameAndTypeInfo(this , name, type));
095: }
096:
097: public int addFieldRef(Info classinfo, String name, String type) {
098: return add(new FieldRefInfo(this , classinfo.getIndex(), name,
099: type));
100: }
101:
102: public int addFieldRef(String classname, String name, String type) {
103: return add(new FieldRefInfo(this , classname, name, type));
104: }
105:
106: public int addFieldRef(int classindex, String name, String type) {
107: return add(new FieldRefInfo(this , classindex, name, type));
108: }
109:
110: public int addFieldRef(int classindex, int name, int type) {
111: return add(new FieldRefInfo(this , classindex, name, type));
112: }
113:
114: public int addFieldRef(int classindex, int nametype) {
115: return add(new FieldRefInfo(this , classindex, nametype));
116: }
117:
118: public int addMethodRef(Info classinfo, String name, String type) {
119: return add(new MethodRefInfo(this , classinfo.getIndex(), name,
120: type));
121: }
122:
123: public int addMethodRef(String classname, String name, String type) {
124: return add(new MethodRefInfo(this , classname, name, type));
125: }
126:
127: public int addMethodRef(int classindex, String name, String type) {
128: return add(new MethodRefInfo(this , classindex, name, type));
129: }
130:
131: public int addMethodRef(int classindex, int name, int type) {
132: return add(new MethodRefInfo(this , classindex, name, type));
133: }
134:
135: public int addMethodRef(int classindex, int nametype) {
136: return add(new MethodRefInfo(this , classindex, nametype));
137: }
138:
139: public int addInterfaceMethodRef(Info classinfo, String name,
140: String type) {
141: return add(new FieldRefInfo(this , classinfo.getIndex(), name,
142: type));
143: }
144:
145: public int addInterfaceMethodRef(String classname, String name,
146: String type) {
147: return add(new InterfaceMethodRefInfo(this , classname, name,
148: type));
149: }
150:
151: public int addInterfaceMethodRef(int classindex, String name,
152: String type) {
153: return add(new InterfaceMethodRefInfo(this , classindex, name,
154: type));
155: }
156:
157: public int addInterfaceMethodRef(int classindex, int name, int type) {
158: return add(new InterfaceMethodRefInfo(this , classindex, name,
159: type));
160: }
161:
162: public int addInterfaceMethodRef(int classindex, int nametype) {
163: return add(new InterfaceMethodRefInfo(this , classindex,
164: nametype));
165: }
166:
167: public void write(DataOutputStream output) throws IOException {
168: output.writeShort(_count);
169: int size = _list.size();
170: for (int i = 0; i < size; i++) {
171: Info info = (Info) _list.get(i);
172: info.write(output);
173: if (info.getWidth() == 2) {
174: i++;
175: }
176: }
177: }
178:
179: public String toString() {
180: StringBuffer buffer = new StringBuffer();
181: int size = _list.size();
182: for (int i = 0; i < size; i++) {
183: Info info = (Info) _list.get(i);
184: buffer.append(info.getIndex());
185: buffer.append(": ");
186: buffer.append(info);
187: buffer.append("\n");
188: if (info.getWidth() == 2) {
189: i++;
190: }
191: }
192: return buffer.toString();
193: }
194:
195: }
|