ClassReader.java revision 2942:08092deced3f
156893Sfenner/*
256893Sfenner * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
356893Sfenner * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
456893Sfenner *
556893Sfenner * This code is free software; you can redistribute it and/or modify it
656893Sfenner * under the terms of the GNU General Public License version 2 only, as
756893Sfenner * published by the Free Software Foundation.  Oracle designates this
856893Sfenner * particular file as subject to the "Classpath" exception as provided
956893Sfenner * by Oracle in the LICENSE file that accompanied this code.
1056893Sfenner *
1156893Sfenner * This code is distributed in the hope that it will be useful, but WITHOUT
1256893Sfenner * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1356893Sfenner * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1456893Sfenner * version 2 for more details (a copy is included in the LICENSE file that
1556893Sfenner * accompanied this code).
1656893Sfenner *
1756893Sfenner * You should have received a copy of the GNU General Public License version
1856893Sfenner * 2 along with this work; if not, write to the Free Software Foundation,
1956893Sfenner * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2056893Sfenner *
2156893Sfenner * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2256893Sfenner * or visit www.oracle.com if you need additional information or have any
23127668Sbms * questions.
24162017Ssam */
2556893Sfenner
2656893Sfennerpackage com.sun.tools.classfile;
2756893Sfenner
2856893Sfennerimport java.io.BufferedInputStream;
2956893Sfennerimport java.io.ByteArrayInputStream;
3056893Sfennerimport java.io.DataInputStream;
31127668Sbmsimport java.io.IOException;
3256893Sfennerimport java.io.InputStream;
3356893Sfennerimport java.util.Objects;
3456893Sfenner
3556893Sfenner/**
3656893Sfenner *  <p><b>This is NOT part of any supported API.
3756893Sfenner *  If you write code that depends on this, you do so at your own risk.
3875115Sfenner *  This code and its internal interfaces are subject to change or
3998524Sfenner *  deletion without notice.</b>
4056893Sfenner */
4175115Sfennerpublic class ClassReader {
4256893Sfenner    ClassReader(ClassFile classFile, InputStream in, Attribute.Factory attributeFactory) throws IOException {
4356893Sfenner        this.classFile = Objects.requireNonNull(classFile);
4456893Sfenner        this.attributeFactory = Objects.requireNonNull(attributeFactory);
4556893Sfenner        this.in = new DataInputStream(new BufferedInputStream(in));
46127668Sbms    }
47127668Sbms
4856893Sfenner    ClassFile getClassFile() {
4956893Sfenner        return classFile;
5056893Sfenner    }
5198524Sfenner
5298524Sfenner    ConstantPool getConstantPool() {
5398524Sfenner        return classFile.constant_pool;
54127668Sbms    }
5598524Sfenner
56162017Ssam    public Attribute readAttribute() throws IOException {
57162017Ssam        int name_index = readUnsignedShort();
5898524Sfenner        int length = readInt();
59162017Ssam        byte[] data = new byte[length];
60162017Ssam        readFully(data);
61162017Ssam
62162017Ssam        DataInputStream prev = in;
63162017Ssam        in = new DataInputStream(new ByteArrayInputStream(data));
6498524Sfenner        try {
6556893Sfenner            return attributeFactory.createAttribute(this, name_index, data);
6656893Sfenner        } finally {
6756893Sfenner            in = prev;
6856893Sfenner        }
6956893Sfenner    }
7056893Sfenner
7156893Sfenner    public void readFully(byte[] b) throws IOException {
7256893Sfenner        in.readFully(b);
7356893Sfenner    }
7456893Sfenner
7556893Sfenner    public int readUnsignedByte() throws IOException {
7656893Sfenner        return in.readUnsignedByte();
7756893Sfenner    }
7856893Sfenner
7956893Sfenner    public int readUnsignedShort() throws IOException {
8056893Sfenner        return in.readUnsignedShort();
8198524Sfenner    }
8275115Sfenner
8356893Sfenner    public int readInt() throws IOException {
84146773Ssam        return in.readInt();
8556893Sfenner    }
8656893Sfenner
8756893Sfenner    public long readLong() throws IOException {
8856893Sfenner        return in.readLong();
8956893Sfenner    }
9056893Sfenner
9156893Sfenner    public float readFloat() throws IOException {
9256893Sfenner        return in.readFloat();
9356893Sfenner    }
9456893Sfenner
9556893Sfenner    public double readDouble() throws IOException {
9656893Sfenner        return in.readDouble();
9756893Sfenner    }
9856893Sfenner
99127668Sbms    public String readUTF() throws IOException {
100127668Sbms        return in.readUTF();
101127668Sbms    }
102127668Sbms
103127668Sbms    private DataInputStream in;
104127668Sbms    private ClassFile classFile;
105127668Sbms    private Attribute.Factory attributeFactory;
106127668Sbms}
107127668Sbms