Java BigInteger from Stream readLuposBigInteger(final int numberOfBits, final InputStream is)

Here you can find the source of readLuposBigInteger(final int numberOfBits, final InputStream is)

Description

readLuposBigInteger.

License

Open Source License

Parameter

Parameter Description
numberOfBits a int.
is a java.io.InputStream object.

Exception

Parameter Description

Return

a object.

Declaration

public final static BigInteger readLuposBigInteger(final int numberOfBits, final InputStream is)
        throws IOException 

Method Source Code


//package com.java2s;

import java.io.IOException;
import java.io.InputStream;

import java.math.BigInteger;

public class Main {
    /**//from  w  w  w  .j a  va2 s .c om
     * <p>readLuposBigInteger.</p>
     *
     * @param numberOfBits a int.
     * @param is a {@link java.io.InputStream} object.
     * @return a {@link java.math.BigInteger} object.
     * @throws java.io.IOException if any.
     */
    public final static BigInteger readLuposBigInteger(final int numberOfBits, final InputStream is)
            throws IOException {
        BigInteger result = BigInteger.ZERO;
        BigInteger factor = BigInteger.ONE;
        final BigInteger BYTE = BigInteger.valueOf(256);
        int remainingBits = numberOfBits;
        while (remainingBits > 0) {
            final int currentValueByte = is.read();
            if (currentValueByte < 0) {
                // EOF reached!
                return null;
            }
            final BigInteger currentValue = BigInteger.valueOf(currentValueByte);
            result = result.add(currentValue.multiply(factor));
            factor = factor.multiply(BYTE);
            remainingBits -= 8;
        }
        return result;
    }
}

Related

  1. readBigInteger(ByteArrayInputStream bais)
  2. readBigInteger(DataInputStream dis)
  3. readBigInteger(InputStream input)
  4. readBigInteger(int n, DataInput dis)
  5. readLineAsBigInteger(BufferedReader br)