110 assert(h == reverse_int(rh), "can re-read same bits");
111 assert(l == reverse_int(rl), "can re-read same bits");
112 write_int(rh);
113 write_int(rl);
114 }
115
116 void CompressedWriteStream::write_long(jlong value) {
117 write_signed_int(low(value));
118 write_signed_int(high(value));
119 }
120
121
122 /// The remaining details
123
124 #ifndef PRODUCT
125 // set this to trigger unit test
126 void test_compressed_stream(int trace);
127 bool test_compressed_stream_enabled = false;
128 #endif
129
130 // This encoding, called UNSIGNED5, is taken from J2SE Pack200.
131 // It assumes that most values have lots of leading zeroes.
132 // Very small values, in the range [0..191], code in one byte.
133 // Any 32-bit value (including negatives) can be coded, in
134 // up to five bytes. The grammar is:
135 // low_byte = [0..191]
136 // high_byte = [192..255]
137 // any_byte = low_byte | high_byte
138 // coding = low_byte
139 // | high_byte low_byte
140 // | high_byte high_byte low_byte
141 // | high_byte high_byte high_byte low_byte
142 // | high_byte high_byte high_byte high_byte any_byte
143 // Each high_byte contributes six bits of payload.
144 // The encoding is one-to-one (except for integer overflow)
145 // and easy to parse and unparse.
146
147 jint CompressedReadStream::read_int_mb(jint b0) {
148 int pos = position() - 1;
149 u_char* buf = buffer() + pos;
150 assert(buf[0] == b0 && b0 >= L, "correctly called");
151 jint sum = b0;
152 // must collect more bytes: b[1]...b[4]
153 int lg_H_i = lg_H;
154 for (int i = 0; ; ) {
155 jint b_i = buf[++i]; // b_i = read(); ++i;
156 sum += b_i << lg_H_i; // sum += b[i]*(64**i)
157 if (b_i < L || i == MAX_i) {
158 set_position(pos+i+1);
159 return sum;
160 }
161 lg_H_i += lg_H;
162 }
163 }
164
165 void CompressedWriteStream::write_int_mb(jint value) {
166 debug_only(int pos1 = position());
167 juint sum = value;
168 for (int i = 0; ; ) {
169 if (sum < L || i == MAX_i) {
170 // remainder is either a "low code" or the 5th byte
171 assert(sum == (u_char)sum, "valid byte");
172 write((u_char)sum);
173 break;
174 }
175 sum -= L;
176 int b_i = L + (sum % H); // this is a "high code"
177 sum >>= lg_H; // extracted 6 bits
178 write(b_i); ++i;
179 }
180
181 #ifndef PRODUCT
182 if (test_compressed_stream_enabled) { // hack to enable this stress test
183 test_compressed_stream_enabled = false;
184 test_compressed_stream(0);
|
110 assert(h == reverse_int(rh), "can re-read same bits");
111 assert(l == reverse_int(rl), "can re-read same bits");
112 write_int(rh);
113 write_int(rl);
114 }
115
116 void CompressedWriteStream::write_long(jlong value) {
117 write_signed_int(low(value));
118 write_signed_int(high(value));
119 }
120
121
122 /// The remaining details
123
124 #ifndef PRODUCT
125 // set this to trigger unit test
126 void test_compressed_stream(int trace);
127 bool test_compressed_stream_enabled = false;
128 #endif
129
130 void CompressedWriteStream::write_int_mb(jint value) {
131 debug_only(int pos1 = position());
132 juint sum = value;
133 for (int i = 0; ; ) {
134 if (sum < L || i == MAX_i) {
135 // remainder is either a "low code" or the 5th byte
136 assert(sum == (u_char)sum, "valid byte");
137 write((u_char)sum);
138 break;
139 }
140 sum -= L;
141 int b_i = L + (sum % H); // this is a "high code"
142 sum >>= lg_H; // extracted 6 bits
143 write(b_i); ++i;
144 }
145
146 #ifndef PRODUCT
147 if (test_compressed_stream_enabled) { // hack to enable this stress test
148 test_compressed_stream_enabled = false;
149 test_compressed_stream(0);
|