forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64FloatSupport.java
More file actions
224 lines (212 loc) · 7.94 KB
/
Base64FloatSupport.java
File metadata and controls
224 lines (212 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.jsoniter.extra;
import com.jsoniter.CodegenAccess;
import com.jsoniter.JsonIterator;
import com.jsoniter.spi.Slice;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import com.jsoniter.spi.Decoder;
import com.jsoniter.spi.Encoder;
import com.jsoniter.spi.JsonException;
import com.jsoniter.spi.JsoniterSpi;
import java.io.IOException;
/**
* encode float/double as base64, faster than PreciseFloatSupport
*/
public class Base64FloatSupport {
final static int[] DIGITS = new int[256];
final static int[] HEX = new int[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
final static int[] DEC = new int[127];
static {
for (int i = 0; i < 256; i++) {
DIGITS[i] = HEX[i >> 4] << 8 | HEX[i & 0xf];
}
DEC['0'] = 0;
DEC['1'] = 1;
DEC['2'] = 2;
DEC['3'] = 3;
DEC['4'] = 4;
DEC['5'] = 5;
DEC['6'] = 6;
DEC['7'] = 7;
DEC['8'] = 8;
DEC['9'] = 9;
DEC['a'] = 10;
DEC['b'] = 11;
DEC['c'] = 12;
DEC['d'] = 13;
DEC['e'] = 14;
DEC['f'] = 15;
}
private static boolean enabled;
public static synchronized void enableEncodersAndDecoders() {
if (enabled) {
throw new JsonException("BinaryFloatSupport.enable can only be called once");
}
enabled = true;
enableDecoders();
JsoniterSpi.registerTypeEncoder(Double.class, new Encoder.ReflectionEncoder() {
@Override
public void encode(Object obj, JsonStream stream) throws IOException {
Double number = (Double) obj;
long bits = Double.doubleToRawLongBits(number.doubleValue());
Base64.encodeLongBits(bits, stream);
}
@Override
public Any wrap(Object obj) {
Double number = (Double) obj;
return Any.wrap(number.doubleValue());
}
});
JsoniterSpi.registerTypeEncoder(double.class, new Encoder.DoubleEncoder() {
@Override
public void encodeDouble(double obj, JsonStream stream) throws IOException {
long bits = Double.doubleToRawLongBits(obj);
Base64.encodeLongBits(bits, stream);
}
});
JsoniterSpi.registerTypeEncoder(Float.class, new Encoder.ReflectionEncoder() {
@Override
public void encode(Object obj, JsonStream stream) throws IOException {
Float number = (Float) obj;
long bits = Double.doubleToRawLongBits(number.doubleValue());
Base64.encodeLongBits(bits, stream);
}
@Override
public Any wrap(Object obj) {
Float number = (Float) obj;
return Any.wrap(number.floatValue());
}
});
JsoniterSpi.registerTypeEncoder(float.class, new Encoder.FloatEncoder() {
@Override
public void encodeFloat(float obj, JsonStream stream) throws IOException {
long bits = Double.doubleToRawLongBits(obj);
Base64.encodeLongBits(bits, stream);
}
});
}
public static void enableDecoders() {
JsoniterSpi.registerTypeDecoder(Double.class, new Decoder() {
@Override
public Object decode(JsonIterator iter) throws IOException {
byte token = CodegenAccess.nextToken(iter);
CodegenAccess.unreadByte(iter);
if (token == '"') {
return Double.longBitsToDouble(Base64.decodeLongBits(iter));
} else {
return iter.readDouble();
}
}
});
JsoniterSpi.registerTypeDecoder(double.class, new Decoder.DoubleDecoder() {
@Override
public double decodeDouble(JsonIterator iter) throws IOException {
byte token = CodegenAccess.nextToken(iter);
CodegenAccess.unreadByte(iter);
if (token == '"') {
return Double.longBitsToDouble(Base64.decodeLongBits(iter));
}else {
return iter.readDouble();
}
}
});
JsoniterSpi.registerTypeDecoder(Float.class, new Decoder() {
@Override
public Object decode(JsonIterator iter) throws IOException {
byte token = CodegenAccess.nextToken(iter);
CodegenAccess.unreadByte(iter);
if (token == '"') {
return (float)Double.longBitsToDouble(Base64.decodeLongBits(iter));
}else {
return (float)iter.readDouble();
}
}
});
JsoniterSpi.registerTypeDecoder(float.class, new Decoder.FloatDecoder() {
@Override
public float decodeFloat(JsonIterator iter) throws IOException {
byte token = CodegenAccess.nextToken(iter);
CodegenAccess.unreadByte(iter);
if (token == '"') {
return (float)Double.longBitsToDouble(Base64.decodeLongBits(iter));
}else {
return (float)iter.readDouble();
}
}
});
}
private static long readLongBits(JsonIterator iter) throws IOException {
Slice slice = iter.readStringAsSlice();
byte[] data = slice.data();
long val = 0;
for (int i = slice.head(); i < slice.tail(); i++) {
byte b = data[i];
val = val << 4 | DEC[b];
}
return val;
}
private static void writeLongBits(long bits, JsonStream stream) throws IOException {
int digit = DIGITS[(int) (bits & 0xff)];
byte b2 = (byte) (digit >> 8);
byte b1 = (byte) digit;
bits = bits >> 8;
if (bits == 0) {
stream.write((byte) '"', b2, b1, (byte) '"');
}
digit = DIGITS[(int) (bits & 0xff)];
byte b4 = (byte) (digit >> 8);
byte b3 = (byte) digit;
bits = bits >> 8;
if (bits == 0) {
stream.write((byte) '"', b4, b3, b2, b1, (byte) '"');
}
digit = DIGITS[(int) (bits & 0xff)];
byte b6 = (byte) (digit >> 8);
byte b5 = (byte) digit;
bits = bits >> 8;
if (bits == 0) {
stream.write((byte) '"', b6, b5, b4, b3);
stream.write(b2, b1, (byte) '"');
}
digit = DIGITS[(int) (bits & 0xff)];
byte b8 = (byte) (digit >> 8);
byte b7 = (byte) digit;
bits = bits >> 8;
if (bits == 0) {
stream.write((byte) '"', b8, b7, b6, b5, b4);
stream.write(b3, b2, b1, (byte) '"');
}
digit = DIGITS[(int) (bits & 0xff)];
byte b10 = (byte) (digit >> 8);
byte b9 = (byte) digit;
bits = bits >> 8;
if (bits == 0) {
stream.write((byte) '"', b10, b9, b8, b7, b6);
stream.write(b5, b4, b3, b2, b1, (byte) '"');
}
digit = DIGITS[(int) (bits & 0xff)];
byte b12 = (byte) (digit >> 8);
byte b11 = (byte) digit;
bits = bits >> 8;
if (bits == 0) {
stream.write((byte) '"', b12, b11, b10, b9, b8);
stream.write(b7, b6, b5, b4, b3, b2);
stream.write(b1, (byte) '"');
}
digit = DIGITS[(int) (bits & 0xff)];
byte b14 = (byte) (digit >> 8);
byte b13 = (byte) digit;
bits = bits >> 8;
if (bits == 0) {
stream.write((byte) '"', b14, b13, b12, b11, b10);
stream.write(b9, b8, b7, b6, b5, b4);
stream.write(b3, b2, b1, (byte) '"');
}
digit = DIGITS[(int) (bits & 0xff)];
byte b16 = (byte) (digit >> 8);
byte b15 = (byte) digit;
stream.write((byte) '"', b16, b15, b14, b13, b12);
stream.write(b11, b10, b9, b8, b7, b6);
stream.write(b5, b4, b3, b2, b1, (byte) '"');
}
}