duke@435: /* stefank@2314: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP stefank@2314: #define SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP stefank@2314: stefank@2314: #include "utilities/top.hpp" stefank@2314: #ifdef TARGET_ARCH_x86 stefank@2314: # include "bytes_x86.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_sparc stefank@2314: # include "bytes_sparc.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_zero stefank@2314: # include "bytes_zero.hpp" stefank@2314: #endif stefank@2314: duke@435: // Input stream for reading .class file duke@435: // duke@435: // The entire input stream is present in a buffer allocated by the caller. duke@435: // The caller is responsible for deallocating the buffer and for using duke@435: // ResourceMarks appropriately when constructing streams. duke@435: duke@435: class ClassFileStream: public ResourceObj { duke@435: private: duke@435: u1* _buffer_start; // Buffer bottom duke@435: u1* _buffer_end; // Buffer top (one past last element) duke@435: u1* _current; // Current buffer position duke@435: char* _source; // Source of stream (directory name, ZIP/JAR archive name) duke@435: bool _need_verify; // True if verification is on for the class file duke@435: duke@435: void truncated_file_error(TRAPS); duke@435: public: duke@435: // Constructor duke@435: ClassFileStream(u1* buffer, int length, char* source); duke@435: duke@435: // Buffer access duke@435: u1* buffer() const { return _buffer_start; } duke@435: int length() const { return _buffer_end - _buffer_start; } duke@435: u1* current() const { return _current; } duke@435: void set_current(u1* pos) { _current = pos; } duke@435: char* source() const { return _source; } duke@435: void set_verify(bool flag) { _need_verify = flag; } duke@435: duke@435: void check_truncated_file(bool b, TRAPS) { duke@435: if (b) { duke@435: truncated_file_error(THREAD); duke@435: } duke@435: } duke@435: duke@435: void guarantee_more(int size, TRAPS) { duke@435: size_t remaining = (size_t)(_buffer_end - _current); duke@435: unsigned int usize = (unsigned int)size; duke@435: check_truncated_file(usize > remaining, CHECK); duke@435: } duke@435: duke@435: // Read u1 from stream duke@435: u1 get_u1(TRAPS); duke@435: u1 get_u1_fast() { duke@435: return *_current++; duke@435: } duke@435: duke@435: // Read u2 from stream duke@435: u2 get_u2(TRAPS); duke@435: u2 get_u2_fast() { duke@435: u2 res = Bytes::get_Java_u2(_current); duke@435: _current += 2; duke@435: return res; duke@435: } duke@435: duke@435: // Read u4 from stream duke@435: u4 get_u4(TRAPS); duke@435: u4 get_u4_fast() { duke@435: u4 res = Bytes::get_Java_u4(_current); duke@435: _current += 4; duke@435: return res; duke@435: } duke@435: duke@435: // Read u8 from stream duke@435: u8 get_u8(TRAPS); duke@435: u8 get_u8_fast() { duke@435: u8 res = Bytes::get_Java_u8(_current); duke@435: _current += 8; duke@435: return res; duke@435: } duke@435: duke@435: // Get direct pointer into stream at current position. duke@435: // Returns NULL if length elements are not remaining. The caller is duke@435: // responsible for calling skip below if buffer contents is used. duke@435: u1* get_u1_buffer() { duke@435: return _current; duke@435: } duke@435: duke@435: u2* get_u2_buffer() { duke@435: return (u2*) _current; duke@435: } duke@435: duke@435: // Skip length u1 or u2 elements from stream duke@435: void skip_u1(int length, TRAPS); duke@435: void skip_u1_fast(int length) { duke@435: _current += length; duke@435: } duke@435: duke@435: void skip_u2(int length, TRAPS); duke@435: void skip_u2_fast(int length) { duke@435: _current += 2 * length; duke@435: } duke@435: duke@435: // Tells whether eos is reached duke@435: bool at_eos() const { return _current == _buffer_end; } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP