|
17 | 17 | import java.util.UUID; |
18 | 18 | import java.util.concurrent.TimeUnit; |
19 | 19 |
|
20 | | -import static com.google.common.primitives.Bytes.concat; |
21 | | - |
22 | 20 | /** |
23 | 21 | * @author Dmitry Andreev <a href="mailto:[email protected]"></a> |
24 | 22 | */ |
@@ -168,44 +166,27 @@ public void writeFloat64(double value) throws IOException { |
168 | 166 | out.writeDouble(value); |
169 | 167 | } |
170 | 168 |
|
171 | | - public static void reverse(byte[] array) { |
172 | | - if (array == null) { |
173 | | - return; |
174 | | - } |
175 | | - int i = 0; |
176 | | - int j = array.length - 1; |
177 | | - byte tmp; |
178 | | - while (j > i) { |
179 | | - tmp = array[j]; |
180 | | - array[j] = array[i]; |
181 | | - array[i] = tmp; |
182 | | - j--; |
183 | | - i++; |
184 | | - } |
185 | | - } |
186 | | - |
187 | | - byte[] bigDecimalToByte(BigDecimal num, int bytesLen, int scale) { |
| 169 | + private BigInteger removeComma(BigDecimal num, int scale) { |
188 | 170 | BigDecimal ten = BigDecimal.valueOf(10); |
189 | 171 | BigDecimal s = ten.pow(scale); |
190 | | - BigInteger res = num.multiply(s).toBigInteger(); |
191 | | - byte[] r = res.toByteArray(); |
192 | | - reverse(r); |
193 | | - return concat(r, new byte[bytesLen - r.length]); |
| 172 | + return num.multiply(s).toBigInteger(); |
194 | 173 | } |
195 | 174 |
|
196 | | - public void writeDecimal128(BigDecimal v, int scale) throws IOException { |
197 | | - byte[] x = bigDecimalToByte(v, 16, scale); |
198 | | - out.write(x); |
| 175 | + public void writeDecimal128(BigDecimal num, int scale) throws IOException { |
| 176 | + BigInteger bi = removeComma(num, scale); |
| 177 | + byte[] r = bi.toByteArray(); |
| 178 | + for (int i = r.length; i > 0; i--) { |
| 179 | + out.write(r[i - 1]); |
| 180 | + } |
| 181 | + out.write(new byte[16 - r.length]); |
199 | 182 | } |
200 | 183 |
|
201 | | - public void writeDecimal64(BigDecimal v, int scale) throws IOException { |
202 | | - byte[] x = bigDecimalToByte(v, 8, scale); |
203 | | - out.write(x); |
| 184 | + public void writeDecimal64(BigDecimal num, int scale) throws IOException { |
| 185 | + out.writeLong(removeComma(num, scale).longValue()); |
204 | 186 | } |
205 | 187 |
|
206 | | - public void writeDecimal32(BigDecimal v, int scale) throws IOException { |
207 | | - byte[] x = bigDecimalToByte(v, 4, scale); |
208 | | - out.write(x); |
| 188 | + public void writeDecimal32(BigDecimal num, int scale) throws IOException { |
| 189 | + out.writeInt(removeComma(num, scale).intValue()); |
209 | 190 | } |
210 | 191 |
|
211 | 192 | public void writeDateArray(Date[] dates) throws IOException { |
|
0 commit comments