Java BigDecimal readBigDecimal(DataInput in)

Here you can find the source of readBigDecimal(DataInput in)

Description

Reads BigDecimal value that was written via #writeBigDecimal(BigDecimal,DataOutput) .

License

Apache License

Parameter

Parameter Description
in Data input.

Exception

Parameter Description
IOException if failed to read value.

Return

Value.

Declaration

public static BigDecimal readBigDecimal(DataInput in) throws IOException 

Method Source Code


//package com.java2s;
/*//w w  w.  ja  v a 2  s.c om
 * Copyright 2019 The Hekate Project
 *
 * The Hekate Project licenses this file to you 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.DataInput;

import java.io.IOException;
import java.io.StreamCorruptedException;
import java.math.BigDecimal;
import java.math.BigInteger;

public class Main {
    private static final byte DECIMAL_ZERO = 1;
    private static final byte DECIMAL_ONE = 2;
    private static final byte DECIMAL_TEN = 3;
    private static final byte DECIMAL_SMALL_UNSCALED = 4;
    private static final byte DECIMAL_SMALL_SCALED = 5;
    private static final byte DECIMAL_BIG = 6;
    private static final int INT_BITS = 31;
    private static final int LONG_BITS = 63;

    /**
     * Reads {@link BigDecimal} value that was written via {@link #writeBigDecimal(BigDecimal, DataOutput)}.
     *
     * @param in Data input.
     *
     * @return Value.
     *
     * @throws IOException if failed to read value.
     */
    public static BigDecimal readBigDecimal(DataInput in) throws IOException {
        byte hint = in.readByte();

        switch (hint) {
        case DECIMAL_ZERO: {
            return BigDecimal.ZERO;
        }
        case DECIMAL_ONE: {
            return BigDecimal.ONE;
        }
        case DECIMAL_TEN: {
            return BigDecimal.TEN;
        }
        case DECIMAL_SMALL_UNSCALED: {
            long val = readVarLong(in);

            return BigDecimal.valueOf(val);
        }
        case DECIMAL_SMALL_SCALED: {
            int scale = readVarIntUnsigned(in);
            long unscaled = readVarLong(in);

            return BigDecimal.valueOf(unscaled, scale);
        }
        case DECIMAL_BIG: {
            int scale = readVarIntUnsigned(in);
            int bytesLen = readVarIntUnsigned(in);

            byte[] bytes = new byte[bytesLen];

            in.readFully(bytes);

            return new BigDecimal(new BigInteger(bytes), scale);
        }
        default: {
            throw new StreamCorruptedException(
                    "Unexpected hint for " + BigDecimal.class.getName() + " value [hint=" + hint + ']');
        }
        }
    }

    /**
     * Reads a value that was encoded via {@link #writeVarLong(long, DataOutput)}.
     *
     * @param in Data input.
     *
     * @return Value.
     *
     * @throws IOException if failed to read value.
     */
    // Code borrowed from 'stream-lib' (Apache 2.0 license) - see https://github.com/addthis/stream-lib
    public static long readVarLong(DataInput in) throws IOException {
        long raw = readVarLongUnsigned(in);
        // This undoes the trick in writeVarLong()
        long temp = (raw << LONG_BITS >> LONG_BITS ^ raw) >> 1;
        // This extra step lets us deal with the largest signed values by treating
        // negative results from read unsigned methods as like unsigned values
        // Must re-flip the top bit if the original read value had it set.
        return temp ^ (raw & 1L << LONG_BITS);
    }

    /**
     * Reads a value that was encoded via {@link #writeVarIntUnsigned(int, DataOutput)}.
     *
     * @param in Data input.
     *
     * @return Value.
     *
     * @throws IOException if failed to read value.
     */
    // Code borrowed from 'stream-lib' (Apache 2.0 license) - see https://github.com/addthis/stream-lib
    public static int readVarIntUnsigned(DataInput in) throws IOException {
        int value = 0;
        int i = 0;
        int b;

        while (((b = in.readByte()) & 0x80) != 0) {
            value |= (b & 0x7F) << i;

            i += 7;

            if (i > INT_BITS) {
                throw new StreamCorruptedException("Variable length size is too long");
            }
        }

        return value | b << i;
    }

    /**
     * Reads a value that was encoded via {@link #writeVarLongUnsigned(long, DataOutput)}.
     *
     * @param in Data input.
     *
     * @return Value.
     *
     * @throws IOException if failed to read value.
     */
    // Code borrowed from 'stream-lib' (Apache 2.0 license) - see https://github.com/addthis/stream-lib
    public static long readVarLongUnsigned(DataInput in) throws IOException {
        long value = 0L;
        int i = 0;
        long b;

        while (((b = in.readByte()) & 0x80L) != 0) {
            value |= (b & 0x7F) << i;

            i += 7;

            if (i > LONG_BITS) {
                throw new StreamCorruptedException("Variable length size is too long");
            }
        }

        return value | b << i;
    }
}

Related

  1. putBigDecimal(byte[] bytes, int offset, BigDecimal val)
  2. randomBigDecimal()
  3. randomBigDecimalList(BigDecimal min, BigDecimal max, int minLength, int maxLength)
  4. rangedValue(BigDecimal lower, BigDecimal upper, Random random)
  5. readBigDecimal(byte valueBytes[], int valueLength, int scale)
  6. readBigDecimal(final Object object)
  7. releaseNull(BigDecimal amount)
  8. removeTrailingZeros(final BigDecimal bd)
  9. removeVAT(int priceInCents, BigDecimal vat)