Here you can find the source of writeBigDecimal(final OutputStream out, @Nullable final BigDecimal value)
public static void writeBigDecimal(final OutputStream out, @Nullable final BigDecimal value) throws IOException
//package com.java2s; /*//from w w w . j a va 2 s .c o m * Copyright (c) 2014 Haixing Hu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import java.io.IOException; import java.io.OutputStream; import java.math.BigDecimal; import java.math.BigInteger; import javax.annotation.Nullable; public class Main { private static final String UNSUPPORTED_VAR_NUMBER = "The variable length integer format does not support negative value."; public static void writeBigDecimal(final OutputStream out, @Nullable final BigDecimal value) throws IOException { if (!writeNullMark(out, value)) { if (value.signum() == 0) { // value == 0 writeVarInt(out, 0); } else { final BigInteger unscaledValue = value.unscaledValue(); final byte[] bits = unscaledValue.toByteArray(); writeVarInt(out, bits.length); out.write(bits, 0, bits.length); writeInt(out, value.scale()); } } } public static boolean writeNullMark(final OutputStream out, final Object object) throws IOException { if (object == null) { out.write(1); return true; } else { out.write(0); return false; } } public static void writeVarInt(final OutputStream out, int value) throws IOException { if (value < 0) { throw new IllegalArgumentException(UNSUPPORTED_VAR_NUMBER); } while (value > 0x7F) { out.write((value & 0x7F) | 0x80); value >>>= 7; } out.write(value); } public static void writeInt(final OutputStream out, final int value) throws IOException { out.write(value >>> 24); out.write(value >>> 16); out.write(value >>> 8); out.write(value); } }