Path: blob/master/src/hotspot/share/classfile/classFileStream.hpp
40949 views
/*1* Copyright (c) 1997, 2019, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_CLASSFILE_CLASSFILESTREAM_HPP25#define SHARE_CLASSFILE_CLASSFILESTREAM_HPP2627#include "memory/allocation.hpp"28#include "utilities/bytes.hpp"29#include "utilities/exceptions.hpp"3031// Input stream for reading .class file32//33// The entire input stream is present in a buffer allocated by the caller.34// The caller is responsible for deallocating the buffer and for using35// ResourceMarks appropriately when constructing streams.3637class ClassPathEntry;3839class ClassFileStream: public ResourceObj {40private:41const u1* const _buffer_start; // Buffer bottom42const u1* const _buffer_end; // Buffer top (one past last element)43mutable const u1* _current; // Current buffer position44const char* const _source; // Source of stream (directory name, ZIP/JAR archive name)45bool _need_verify; // True if verification is on for the class file46bool _from_boot_loader_modules_image; // True if this was created by ClassPathImageEntry.47void truncated_file_error(TRAPS) const ;4849protected:50const u1* clone_buffer() const;51const char* const clone_source() const;5253public:54static const bool verify;5556ClassFileStream(const u1* buffer,57int length,58const char* source,59bool verify_stream = verify, // to be verified by default60bool from_boot_loader_modules_image = false);6162virtual const ClassFileStream* clone() const;6364// Buffer access65const u1* buffer() const { return _buffer_start; }66int length() const { return _buffer_end - _buffer_start; }67const u1* current() const { return _current; }68void set_current(const u1* pos) const {69assert(pos >= _buffer_start && pos <= _buffer_end, "invariant");70_current = pos;71}7273// for relative positioning74juint current_offset() const {75return (juint)(_current - _buffer_start);76}77const char* source() const { return _source; }78bool need_verify() const { return _need_verify; }79void set_verify(bool flag) { _need_verify = flag; }80bool from_boot_loader_modules_image() const { return _from_boot_loader_modules_image; }8182void check_truncated_file(bool b, TRAPS) const {83if (b) {84truncated_file_error(THREAD);85}86}8788void guarantee_more(int size, TRAPS) const {89size_t remaining = (size_t)(_buffer_end - _current);90unsigned int usize = (unsigned int)size;91check_truncated_file(usize > remaining, CHECK);92}9394// Read u1 from stream95u1 get_u1_fast() const {96return *_current++;97}98u1 get_u1(TRAPS) const {99if (_need_verify) {100guarantee_more(1, CHECK_0);101} else {102assert(1 <= _buffer_end - _current, "buffer overflow");103}104return get_u1_fast();105}106107// Read u2 from stream108u2 get_u2_fast() const {109u2 res = Bytes::get_Java_u2((address)_current);110_current += 2;111return res;112}113u2 get_u2(TRAPS) const {114if (_need_verify) {115guarantee_more(2, CHECK_0);116} else {117assert(2 <= _buffer_end - _current, "buffer overflow");118}119return get_u2_fast();120}121122// Read u4 from stream123u4 get_u4_fast() const {124u4 res = Bytes::get_Java_u4((address)_current);125_current += 4;126return res;127}128129// Read u8 from stream130u8 get_u8_fast() const {131u8 res = Bytes::get_Java_u8((address)_current);132_current += 8;133return res;134}135136// Skip length elements from stream137void skip_u1(int length, TRAPS) const {138if (_need_verify) {139guarantee_more(length, CHECK);140}141skip_u1_fast(length);142}143void skip_u1_fast(int length) const {144_current += length;145}146147void skip_u2_fast(int length) const {148_current += 2 * length;149}150151void skip_u4_fast(int length) const {152_current += 4 * length;153}154155// Tells whether eos is reached156bool at_eos() const { return _current == _buffer_end; }157158uint64_t compute_fingerprint() const;159};160161#endif // SHARE_CLASSFILE_CLASSFILESTREAM_HPP162163164