hotspot/src/share/vm/code/compressedStream.hpp
changeset 1 489c9b5090e2
child 5547 f4b087cbb361
equal deleted inserted replaced
0:fd16c54261b3 1:489c9b5090e2
       
     1 /*
       
     2  * Copyright 1997-2005 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  *
       
    23  */
       
    24 
       
    25 // Simple interface for filing out and filing in basic types
       
    26 // Used for writing out and reading in debugging information.
       
    27 
       
    28 class CompressedStream : public ResourceObj {
       
    29   friend class VMStructs;
       
    30  protected:
       
    31   u_char* _buffer;
       
    32   int     _position;
       
    33 
       
    34   enum {
       
    35     // Constants for UNSIGNED5 coding of Pack200
       
    36     lg_H = 6, H = 1<<lg_H,    // number of high codes (64)
       
    37     L = (1<<BitsPerByte)-H,   // number of low codes (192)
       
    38     MAX_i = 4                 // bytes are numbered in (0..4), max 5 bytes
       
    39   };
       
    40 
       
    41   // these inlines are defined only in compressedStream.cpp
       
    42   static inline juint encode_sign(jint  value);  // for Pack200 SIGNED5
       
    43   static inline jint  decode_sign(juint value);  // for Pack200 SIGNED5
       
    44   static inline juint reverse_int(juint bits);   // to trim trailing float 0's
       
    45 
       
    46  public:
       
    47   CompressedStream(u_char* buffer, int position = 0) {
       
    48     _buffer   = buffer;
       
    49     _position = position;
       
    50   }
       
    51 
       
    52   u_char* buffer() const               { return _buffer; }
       
    53 
       
    54   // Positioning
       
    55   int position() const                 { return _position; }
       
    56   void set_position(int position)      { _position = position; }
       
    57 };
       
    58 
       
    59 
       
    60 class CompressedReadStream : public CompressedStream {
       
    61  private:
       
    62   inline u_char read()                 { return _buffer[_position++]; }
       
    63 
       
    64   jint     read_int_mb(jint b0);  // UNSIGNED5 coding, 2-5 byte cases
       
    65 
       
    66  public:
       
    67   CompressedReadStream(u_char* buffer, int position = 0)
       
    68   : CompressedStream(buffer, position) {}
       
    69 
       
    70   jboolean read_bool()                 { return (jboolean) read();      }
       
    71   jbyte    read_byte()                 { return (jbyte   ) read();      }
       
    72   jchar    read_char()                 { return (jchar   ) read_int();  }
       
    73   jshort   read_short()                { return (jshort  ) read_signed_int(); }
       
    74   jint     read_int()                  { jint   b0 = read();
       
    75                                          if (b0 < L)  return b0;
       
    76                                          else         return read_int_mb(b0);
       
    77                                        }
       
    78   jint     read_signed_int();
       
    79   jfloat   read_float();               // jfloat_cast(reverse_int(read_int()))
       
    80   jdouble  read_double();              // jdouble_cast(2*reverse_int(read_int))
       
    81   jlong    read_long();                // jlong_from(2*read_signed_int())
       
    82 };
       
    83 
       
    84 
       
    85 class CompressedWriteStream : public CompressedStream {
       
    86  private:
       
    87   bool full() {
       
    88     return _position >= _size;
       
    89   }
       
    90   void store(u_char b) {
       
    91     _buffer[_position++] = b;
       
    92   }
       
    93   void write(u_char b) {
       
    94     if (full()) grow();
       
    95     store(b);
       
    96   }
       
    97   void grow();
       
    98 
       
    99   void write_int_mb(jint value);  // UNSIGNED5 coding, 1-5 byte cases
       
   100 
       
   101  protected:
       
   102   int _size;
       
   103 
       
   104  public:
       
   105   CompressedWriteStream(int initial_size);
       
   106   CompressedWriteStream(u_char* buffer, int initial_size, int position = 0)
       
   107   : CompressedStream(buffer, position) { _size = initial_size; }
       
   108 
       
   109   void write_bool(jboolean value)      { write(value);      }
       
   110   void write_byte(jbyte value)         { write(value);      }
       
   111   void write_char(jchar value)         { write_int(value); }
       
   112   void write_short(jshort value)       { write_signed_int(value);  }
       
   113   void write_int(jint value)           { if ((juint)value < L && !full())
       
   114                                                store((u_char)value);
       
   115                                          else  write_int_mb(value);  }
       
   116   void write_signed_int(jint value);   // write_int(encode_sign(value))
       
   117   void write_float(jfloat value);      // write_int(reverse_int(jint_cast(v)))
       
   118   void write_double(jdouble value);    // write_int(reverse_int(<low,high>))
       
   119   void write_long(jlong value);        // write_signed_int(<low,high>)
       
   120 };