Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassReader.java
41161 views
/*1* Copyright (c) 2007, 2020, 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;32import java.util.Objects;3334/**35* <p><b>This is NOT part of any supported API.36* If you write code that depends on this, you do so at your own risk.37* This code and its internal interfaces are subject to change or38* deletion without notice.</b>39*/40public class ClassReader {41ClassReader(ClassFile classFile, InputStream in, Attribute.Factory attributeFactory) throws IOException {42this.classFile = Objects.requireNonNull(classFile);43this.attributeFactory = Objects.requireNonNull(attributeFactory);44this.in = new DataInputStream(new BufferedInputStream(in));45}4647ClassFile getClassFile() {48return classFile;49}5051ConstantPool getConstantPool() {52return classFile.constant_pool;53}5455public Attribute readAttribute() throws IOException {56int name_index = readUnsignedShort();57int length = readInt();58if (length < 0) { // we have an overflow as max_value(u4) > max_value(int)59String attrName;60try {61attrName = getConstantPool().getUTF8Value(name_index);62} catch (ConstantPool.InvalidIndex | ConstantPool.UnexpectedEntry e) {63attrName = "";64}65throw new FatalError(String.format("attribute %s too big to handle", attrName));66}67byte[] data = new byte[length];68readFully(data);6970DataInputStream prev = in;71in = new DataInputStream(new ByteArrayInputStream(data));72try {73return attributeFactory.createAttribute(this, name_index, data);74} finally {75in = prev;76}77}7879public void readFully(byte[] b) throws IOException {80in.readFully(b);81}8283public int readUnsignedByte() throws IOException {84return in.readUnsignedByte();85}8687public int readUnsignedShort() throws IOException {88return in.readUnsignedShort();89}9091public int readInt() throws IOException {92return in.readInt();93}9495public long readLong() throws IOException {96return in.readLong();97}9899public float readFloat() throws IOException {100return in.readFloat();101}102103public double readDouble() throws IOException {104return in.readDouble();105}106107public String readUTF() throws IOException {108return in.readUTF();109}110111private DataInputStream in;112private ClassFile classFile;113private Attribute.Factory attributeFactory;114}115116117