Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/classfile/ClassReader.java
38899 views
/*1* Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.classfile;2627import java.io.BufferedInputStream;28import java.io.ByteArrayInputStream;29import java.io.DataInputStream;30import java.io.IOException;31import java.io.InputStream;3233/**34* <p><b>This is NOT part of any supported API.35* If you write code that depends on this, you do so at your own risk.36* This code and its internal interfaces are subject to change or37* deletion without notice.</b>38*/39public class ClassReader {40ClassReader(ClassFile classFile, InputStream in, Attribute.Factory attributeFactory) throws IOException {41// null checks42classFile.getClass();43attributeFactory.getClass();4445this.classFile = classFile;46this.in = new DataInputStream(new BufferedInputStream(in));47this.attributeFactory = attributeFactory;48}4950ClassFile getClassFile() {51return classFile;52}5354ConstantPool getConstantPool() {55return classFile.constant_pool;56}5758public Attribute readAttribute() throws IOException {59int name_index = readUnsignedShort();60int length = readInt();61byte[] data = new byte[length];62readFully(data);6364DataInputStream prev = in;65in = new DataInputStream(new ByteArrayInputStream(data));66try {67return attributeFactory.createAttribute(this, name_index, data);68} finally {69in = prev;70}71}7273public void readFully(byte[] b) throws IOException {74in.readFully(b);75}7677public int readUnsignedByte() throws IOException {78return in.readUnsignedByte();79}8081public int readUnsignedShort() throws IOException {82return in.readUnsignedShort();83}8485public int readInt() throws IOException {86return in.readInt();87}8889public long readLong() throws IOException {90return in.readLong();91}9293public float readFloat() throws IOException {94return in.readFloat();95}9697public double readDouble() throws IOException {98return in.readDouble();99}100101public String readUTF() throws IOException {102return in.readUTF();103}104105private DataInputStream in;106private ClassFile classFile;107private Attribute.Factory attributeFactory;108}109110111