88
99import java .io .IOException ;
1010import java .io .OutputStream ;
11+ import java .math .BigDecimal ;
12+ import java .math .BigInteger ;
1113import java .nio .ByteBuffer ;
1214import java .nio .ByteOrder ;
1315import java .util .Date ;
@@ -51,6 +53,7 @@ public void writeUnsignedLeb128(int value) throws IOException {
5153 /**
5254 * Dangerous. Can only be used for rare optimizations, for example when the string is written in parts
5355 * without prior concatenation. The size of the string in bytes must be passed through writeUnsignedLeb128.
56+ *
5457 * @param bytes byte array will be written into stream
5558 * @throws IOException in case if an I/O error occurs
5659 */
@@ -163,19 +166,41 @@ public void writeFloat64(double value) throws IOException {
163166 out .writeDouble (value );
164167 }
165168
169+ private BigInteger removeComma (BigDecimal num , int scale ) {
170+ BigDecimal ten = BigDecimal .valueOf (10 );
171+ BigDecimal s = ten .pow (scale );
172+ return num .multiply (s ).toBigInteger ();
173+ }
174+
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 ]);
182+ }
183+
184+ public void writeDecimal64 (BigDecimal num , int scale ) throws IOException {
185+ out .writeLong (removeComma (num , scale ).longValue ());
186+ }
187+
188+ public void writeDecimal32 (BigDecimal num , int scale ) throws IOException {
189+ out .writeInt (removeComma (num , scale ).intValue ());
190+ }
166191
167192 public void writeDateArray (Date [] dates ) throws IOException {
168193 Preconditions .checkNotNull (dates );
169194 writeUnsignedLeb128 (dates .length );
170- for (Date date : dates ) {
195+ for (Date date : dates ) {
171196 writeDate (date );
172197 }
173198 }
174199
175200 public void writeDateTimeArray (Date [] dates ) throws IOException {
176201 Preconditions .checkNotNull (dates );
177202 writeUnsignedLeb128 (dates .length );
178- for (Date date : dates ) {
203+ for (Date date : dates ) {
179204 writeDateTime (date );
180205 }
181206 }
@@ -191,92 +216,94 @@ public void writeStringArray(String[] strings) throws IOException {
191216 public void writeInt8Array (byte [] bytes ) throws IOException {
192217 Preconditions .checkNotNull (bytes );
193218 writeUnsignedLeb128 (bytes .length );
194- for (byte b : bytes ) {
219+ for (byte b : bytes ) {
195220 writeInt8 (b );
196221 }
197222 }
223+
198224 public void writeInt8Array (int [] ints ) throws IOException {
199225 Preconditions .checkNotNull (ints );
200226 writeUnsignedLeb128 (ints .length );
201- for (int i : ints ) {
227+ for (int i : ints ) {
202228 writeInt8 (i );
203229 }
204230 }
205231
206232 public void writeUInt8Array (int [] ints ) throws IOException {
207233 Preconditions .checkNotNull (ints );
208234 writeUnsignedLeb128 (ints .length );
209- for (int i : ints ) {
235+ for (int i : ints ) {
210236 writeUInt8 (i );
211237 }
212238 }
213239
214240 public void writeInt16Array (short [] shorts ) throws IOException {
215241 Preconditions .checkNotNull (shorts );
216242 writeUnsignedLeb128 (shorts .length );
217- for (short s : shorts ) {
243+ for (short s : shorts ) {
218244 writeInt16 (s );
219245 }
220246 }
221247
222248 public void writeUInt16Array (int [] ints ) throws IOException {
223249 Preconditions .checkNotNull (ints );
224250 writeUnsignedLeb128 (ints .length );
225- for (int i : ints ) {
251+ for (int i : ints ) {
226252 writeUInt16 (i );
227253 }
228254 }
229255
230256 public void writeInt32Array (int [] ints ) throws IOException {
231257 Preconditions .checkNotNull (ints );
232258 writeUnsignedLeb128 (ints .length );
233- for (int i : ints ) {
259+ for (int i : ints ) {
234260 writeInt32 (i );
235261 }
236262 }
237263
238264 public void writeUInt32Array (long [] longs ) throws IOException {
239265 Preconditions .checkNotNull (longs );
240266 writeUnsignedLeb128 (longs .length );
241- for (long l : longs ) {
267+ for (long l : longs ) {
242268 writeUInt32 (l );
243269 }
244270 }
245271
246272 public void writeInt64Array (long [] longs ) throws IOException {
247273 Preconditions .checkNotNull (longs );
248274 writeUnsignedLeb128 (longs .length );
249- for (long l : longs ) {
275+ for (long l : longs ) {
250276 writeInt64 (l );
251277 }
252278 }
253279
254280 public void writeUInt64Array (long [] longs ) throws IOException {
255281 Preconditions .checkNotNull (longs );
256282 writeUnsignedLeb128 (longs .length );
257- for (long l : longs ) {
283+ for (long l : longs ) {
258284 writeUInt64 (l );
259285 }
260286 }
261287
262288 public void writeFloat32Array (float [] floats ) throws IOException {
263289 Preconditions .checkNotNull (floats );
264290 writeUnsignedLeb128 (floats .length );
265- for (float f : floats ) {
291+ for (float f : floats ) {
266292 writeFloat32 (f );
267293 }
268294 }
269295
270296 public void writeFloat64Array (double [] doubles ) throws IOException {
271297 Preconditions .checkNotNull (doubles );
272298 writeUnsignedLeb128 (doubles .length );
273- for (double d : doubles ) {
299+ for (double d : doubles ) {
274300 writeFloat64 (d );
275301 }
276302 }
277303
278- /** Write a marker indicating if value is nullable or not.
279- *
304+ /**
305+ * Write a marker indicating if value is nullable or not.
306+ * <p>
280307 * E.g., to write Nullable(Int32):
281308 *
282309 * <pre>
0 commit comments