KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > classfile > ConstantPoolReader


1 /*
2  * ConstantPoolReader.java
3  *
4  * The contents of this file are subject to the terms of the Common Development
5  * and Distribution License (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9  * or http://www.netbeans.org/cddl.txt.
10  *
11  * When distributing Covered Code, include this CDDL Header Notice in each file
12  * and include the License file at http://www.netbeans.org/cddl.txt.
13  * If applicable, add the following below the CDDL Header, with the fields
14  * enclosed by brackets [] replaced by your own identifying information:
15  * "Portions Copyrighted [year] [name of copyright owner]"
16  *
17  * The Original Software is NetBeans. The Initial Developer of the Original
18  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
19  * Microsystems, Inc. All Rights Reserved.
20  *
21  * Contributor(s): Thomas Ball
22  *
23  * Version: $Revision: 1.5 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 import java.io.*;
29
30 /**
31  * A Java class file constant pool reader. This class is
32  * used rather than java.io.DataInputStream as it is
33  * optimized for constant pool reading only.
34  *
35  * @author Thomas Ball
36  */

37 public final class ConstantPoolReader extends FilterInputStream implements DataInput {
38
39     public ConstantPoolReader(InputStream in) {
40     super(in);
41     }
42
43     public void readFully(byte[] b) throws IOException {
44     readFully(b, 0, b.length);
45     }
46
47     public void readFully(byte[] b, int off, int len) throws IOException {
48     InputStream in = this.in;
49     int n = 0;
50     while (n < len) {
51         int count = in.read(b, off + n, len - n);
52         if (count < 0)
53         throw new EOFException();
54         n += count;
55     }
56     }
57
58     public int skipBytes(int n) throws IOException {
59     InputStream in = this.in;
60     int total = 0;
61     int cur = 0;
62
63     while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
64         total += cur;
65     }
66
67     return total;
68     }
69
70     public boolean readBoolean() throws IOException {
71     int ch = in.read();
72     if (ch < 0)
73         throw new EOFException();
74     return (ch != 0);
75     }
76
77     public byte readByte() throws IOException {
78     int ch = in.read();
79     if (ch < 0)
80         throw new EOFException();
81     return (byte)(ch);
82     }
83
84     public int readUnsignedByte() throws IOException {
85     int ch = in.read();
86     if (ch < 0)
87         throw new EOFException();
88     return ch;
89     }
90
91     public short readShort() throws IOException {
92     InputStream in = this.in;
93     int ch1 = in.read();
94     int ch2 = in.read();
95     if ((ch1 | ch2) < 0)
96          throw new EOFException();
97     return (short)((ch1 << 8) + ch2);
98     }
99
100     public int readUnsignedShort() throws IOException {
101     InputStream in = this.in;
102     int ch1 = in.read();
103     int ch2 = in.read();
104     if ((ch1 | ch2) < 0)
105          throw new EOFException();
106     return (ch1 << 8) + ch2;
107     }
108
109     public char readChar() throws IOException {
110     return (char)readUnsignedShort();
111     }
112
113     public int readInt() throws IOException {
114     InputStream in = this.in;
115     int ch1 = in.read();
116     int ch2 = in.read();
117     int ch3 = in.read();
118     int ch4 = in.read();
119     if ((ch1 | ch2 | ch3 | ch4) < 0)
120          throw new EOFException();
121     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4);
122     }
123
124     public long readLong() throws IOException {
125     return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
126     }
127
128     public float readFloat() throws IOException {
129     return Float.intBitsToFloat(readInt());
130     }
131
132     public double readDouble() throws IOException {
133     return Double.longBitsToDouble(readLong());
134     }
135
136     private char[] lineBuffer;
137
138     public String JavaDoc readLine() throws IOException {
139     InputStream in = this.in;
140     char buf[] = lineBuffer;
141
142     if (buf == null) {
143         buf = lineBuffer = new char[128];
144     }
145
146     int room = buf.length;
147     int offset = 0;
148     int c;
149
150 loop: while (true) {
151         switch (c = in.read()) {
152           case -1:
153           case '\n':
154         break loop;
155
156           case '\r':
157         int c2 = in.read();
158         if ((c2 != '\n') && (c2 != -1)) {
159             if (!(in instanceof PushbackInputStream)) {
160             in = this.in = new PushbackInputStream(in);
161             }
162             ((PushbackInputStream)in).unread(c2);
163         }
164         break loop;
165
166           default:
167         if (--room < 0) {
168             buf = new char[offset + 128];
169             room = buf.length - offset - 1;
170             System.arraycopy(lineBuffer, 0, buf, 0, offset);
171             lineBuffer = buf;
172         }
173         buf[offset++] = (char) c;
174         break;
175         }
176     }
177     if ((c == -1) && (offset == 0)) {
178         return null;
179     }
180     return String.copyValueOf(buf, 0, offset);
181     }
182
183     // NOT threadsafe: for performance
184
static char[] str = new char[1024];
185     byte[] bytearr = new byte[1024];
186
187     public String JavaDoc readUTF() throws IOException {
188         int utflen = readUnsignedShort();
189     if (utflen > bytearr.length)
190         bytearr = new byte[utflen];
191
192     readFully(bytearr, 0, utflen);
193     return readUTF(bytearr, utflen);
194     }
195
196     byte[] readRawUTF() throws IOException {
197         int utflen = readUnsignedShort();
198     byte[] buf = new byte[utflen];
199     readFully(buf, 0, utflen);
200     return buf;
201     }
202
203     static synchronized String JavaDoc readUTF(byte[] src, int utflen) {
204     int i = 0;
205     int strlen = 0;
206
207     if (utflen > str.length)
208         str = new char[utflen];
209
210         while (i < utflen) {
211             int b = src[i++] & 0xFF;
212             if (b >= 0xE0) {
213                 b = (b & 0x0F) << 12;
214                 b |= (src[i++] & 0x3F) << 6;
215                 b |= (src[i++] & 0x3F);
216             } else if (b >= 0xC0) {
217                 b = (b & 0x1F) << 6;
218                 b |= (src[i++] & 0x3F);
219             }
220             str[strlen++] = (char)b;
221         }
222
223         // The number of chars produced may be less than utflen
224
return new String JavaDoc(str, 0, strlen);
225     }
226 }
227
Popular Tags