Java BigDecimal writeBigDecimal(final OutputStream out, @Nullable final BigDecimal value)

Here you can find the source of writeBigDecimal(final OutputStream out, @Nullable final BigDecimal value)

Description

write Big Decimal

License

Apache License

Declaration

public static void writeBigDecimal(final OutputStream out, @Nullable final BigDecimal value)
            throws IOException 

Method Source Code


//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);
    }
}

Related

  1. tryToStoreAsRealBigDecimal(Object ob)
  2. unformattedFromBigDecimal(BigDecimal n)
  3. unformattedToBigDecimal(String str)
  4. valueOf(final BigDecimal value)
  5. writeBigDecimal(BigDecimal val, DataOutput out)
  6. zeroOrMore(BigDecimal decimal)