src/share/vm/classfile/classFileStream.hpp

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP
  26 #define SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP
  27 
  28 #include "utilities/bytes.hpp"
  29 #include "utilities/top.hpp"
  30 
  31 // Input stream for reading .class file
  32 //
  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 using
  35 // ResourceMarks appropriately when constructing streams.
  36 
  37 class ClassFileStream: public ResourceObj {
  38  private:
  39   u1*   _buffer_start; // Buffer bottom
  40   u1*   _buffer_end;   // Buffer top (one past last element)
  41   u1*   _current;      // Current buffer position
  42   char* _source;       // Source of stream (directory name, ZIP/JAR archive name)
  43   bool  _need_verify;  // True if verification is on for the class file
  44 
  45   void truncated_file_error(TRAPS);
  46  public:
  47   // Constructor
  48   ClassFileStream(u1* buffer, int length, char* source);
  49 
  50   // Buffer access
  51   u1* buffer() const           { return _buffer_start; }
  52   int length() const           { return _buffer_end - _buffer_start; }
  53   u1* current() const          { return _current; }
  54   void set_current(u1* pos)    { _current = pos; }
  55   char* source() const         { return _source; }
  56   void set_verify(bool flag)   { _need_verify = flag; }
  57 
  58   void check_truncated_file(bool b, TRAPS) {
  59     if (b) {
  60       truncated_file_error(THREAD);
  61     }
  62   }
  63 
  64   void guarantee_more(int size, TRAPS) {
  65     size_t remaining = (size_t)(_buffer_end - _current);
  66     unsigned int usize = (unsigned int)size;
  67     check_truncated_file(usize > remaining, CHECK);
  68   }
  69 
  70   // Read u1 from stream
  71   u1 get_u1(TRAPS);
  72   u1 get_u1_fast() {
  73     return *_current++;
  74   }
  75 


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP
  26 #define SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP
  27 
  28 #include "utilities/bytes.hpp"
  29 #include "utilities/top.hpp"
  30 
  31 // Input stream for reading .class file
  32 //
  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 using
  35 // ResourceMarks appropriately when constructing streams.
  36 
  37 class ClassFileStream: public ResourceObj {
  38  private:
  39   u1*   _buffer_start; // Buffer bottom
  40   u1*   _buffer_end;   // Buffer top (one past last element)
  41   u1*   _current;      // Current buffer position
  42   const char* _source; // Source of stream (directory name, ZIP/JAR archive name)
  43   bool  _need_verify;  // True if verification is on for the class file
  44 
  45   void truncated_file_error(TRAPS);
  46  public:
  47   // Constructor
  48   ClassFileStream(u1* buffer, int length, const char* source);
  49 
  50   // Buffer access
  51   u1* buffer() const           { return _buffer_start; }
  52   int length() const           { return _buffer_end - _buffer_start; }
  53   u1* current() const          { return _current; }
  54   void set_current(u1* pos)    { _current = pos; }
  55   const char* source() const   { return _source; }
  56   void set_verify(bool flag)   { _need_verify = flag; }
  57 
  58   void check_truncated_file(bool b, TRAPS) {
  59     if (b) {
  60       truncated_file_error(THREAD);
  61     }
  62   }
  63 
  64   void guarantee_more(int size, TRAPS) {
  65     size_t remaining = (size_t)(_buffer_end - _current);
  66     unsigned int usize = (unsigned int)size;
  67     check_truncated_file(usize > remaining, CHECK);
  68   }
  69 
  70   // Read u1 from stream
  71   u1 get_u1(TRAPS);
  72   u1 get_u1_fast() {
  73     return *_current++;
  74   }
  75