1 /*
2  * Copyright (c) 2000, 2020, 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 package sun.jvm.hotspot.code;
26 
27 import sun.jvm.hotspot.debugger.*;
28 
29 public class CompressedReadStream extends CompressedStream {
30   /** Equivalent to CompressedReadStream(buffer, 0) */
CompressedReadStream(Address buffer)31   public CompressedReadStream(Address buffer) {
32     this(buffer, 0);
33   }
34 
CompressedReadStream(Address buffer, int position)35   public CompressedReadStream(Address buffer, int position) {
36     super(buffer, position);
37   }
38 
readBoolean()39   public boolean readBoolean() {
40     return (read() != 0);
41   }
42 
readByte()43   public byte readByte() {
44     return (byte) read();
45   }
46 
readChar()47   public char readChar() {
48     return (char) readInt();
49   }
50 
readShort()51   public short readShort() {
52     return (short) readSignedInt();
53   }
54 
readSignedInt()55   public int readSignedInt() {
56     return decodeSign(readInt());
57   }
58 
readInt()59   public int readInt() {
60     int b0 = read();
61     if (b0 < L) {
62       return b0;
63     } else {
64       return readIntMb(b0);
65     }
66   }
67 
68 
readFloat()69   public float readFloat() {
70     return Float.intBitsToFloat(reverseInt(readInt()));
71   }
72 
readDouble()73   public double readDouble() {
74     int rh = readInt();
75     int rl = readInt();
76     int h = reverseInt(rh);
77     int l = reverseInt(rl);
78     return Double.longBitsToDouble(((long)h << 32) | ((long)l & 0x00000000FFFFFFFFL));
79   }
80 
readLong()81   public long readLong() {
82     long low = readSignedInt() & 0x00000000FFFFFFFFL;
83     long high = readSignedInt();
84     return (high << 32) | low;
85   }
86 
87   //--------------------------------------------------------------------------------
88   // Internals only below this point
89   //
90 
91 
92   // This encoding, called UNSIGNED5, is taken from J2SE Pack200.
93   // It assumes that most values have lots of leading zeroes.
94   // Very small values, in the range [0..191], code in one byte.
95   // Any 32-bit value (including negatives) can be coded, in
96   // up to five bytes.  The grammar is:
97   //    low_byte  = [0..191]
98   //    high_byte = [192..255]
99   //    any_byte  = low_byte | high_byte
100   //    coding = low_byte
101   //           | high_byte low_byte
102   //           | high_byte high_byte low_byte
103   //           | high_byte high_byte high_byte low_byte
104   //           | high_byte high_byte high_byte high_byte any_byte
105   // Each high_byte contributes six bits of payload.
106   // The encoding is one-to-one (except for integer overflow)
107   // and easy to parse and unparse.
108 
readIntMb(int b0)109   private int readIntMb(int b0) {
110     int pos = position - 1;
111     int sum = b0;
112     // must collect more bytes: b[1]...b[4]
113     int lg_H_i = lg_H;
114     for (int i = 0; ;) {
115       int b_i = read(pos + (++i));
116       sum += b_i << lg_H_i; // sum += b[i]*(64**i)
117       if (b_i < L || i == MAX_i) {
118         setPosition(pos+i+1);
119         return sum;
120       }
121       lg_H_i += lg_H;
122     }
123   }
124 
read(int index)125   private short read(int index) {
126     return (short) buffer.getCIntegerAt(index, 1, true);
127   }
128 
129   /** Reads an unsigned byte, but returns it as a short */
read()130   private short read() {
131     short retval = (short) buffer.getCIntegerAt(position, 1, true);
132     ++position;
133     return retval;
134   }
135 }
136