Java BigInteger From bytesToBigInteger(byte[] data, int[] offset)

Here you can find the source of bytesToBigInteger(byte[] data, int[] offset)

Description

Return the BigInteger represented by the bytes in data staring at offset offset[0].

License

Open Source License

Parameter

Parameter Description
data the array from which to read
offset A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.

Return

the BigInteger decoded

Declaration

public static final BigInteger bytesToBigInteger(byte[] data, int[] offset) 

Method Source Code


//package com.java2s;
/*/*from   ww  w .  j a  va 2s  . com*/
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.math.BigInteger;

public class Main {
    public static final int SIZE_INT = 4;

    /**
     * Return the <code>BigInteger</code> represented by the bytes in
     * <code>data</code> staring at offset <code>offset[0]</code>.
     *
     * @param data the array from which to read
     * @param offset A single element array whose first element is the index in
     *         data from which to begin reading on  function entry, and which
     *         on function exit has been incremented by the number of bytes
     *         read.
     *
     * @return the <code>BigInteger</code> decoded
     */
    public static final BigInteger bytesToBigInteger(byte[] data, int[] offset) {
        int length = bytesToInt(data, offset);
        byte[] bytes = new byte[length];
        offset[0] += memcpy(bytes, 0, data, offset[0], length);

        return new BigInteger(bytes);
    }

    /**
     * Return the <code>int</code> represented by the bytes in
     * <code>data</code> staring at offset <code>offset[0]</code>.
     *
     * @param data the array from which to read
     * @param offset A single element array whose first element is the index in
     *         data from which to begin reading on function entry, and which on
     *         function exit has been incremented by the number of bytes read.
     *
     * @return the value of the <code>int</code> decoded
     */
    public static final int bytesToInt(byte[] data, int[] offset) {
        /**
         * TODO: We use network-order within OceanStore, but temporarily
         * supporting intel-order to work with some JNI code until JNI code is
         * set to interoperate with network-order.
         */
        int result = 0;

        for (int i = 0; i < SIZE_INT; ++i) {
            result <<= 8;
            result |= byteToUnsignedInt(data[offset[0]++]);
        }

        return result;
    }

    /**
     * Copy contents of one array of <code>bytes</code> into another. If either
     * array is <code>null</code>, simply return the <code>length</code>
     * parameter directly.
     *
     * @param dst the array to write, or <code>null</code>
     * @param dst_offset the start offset in <code>dst</code>
     * @param src the array to read, or <code>null</code>
     * @param src_offset the start offset in <code>src</code>
     * @param length the number of <code>byte</code>s to copy.
     *
     * @return DOCUMENT ME!
     */
    public static int memcpy(byte[] dst, int dst_offset, byte[] src, int src_offset, int length) {
        if ((dst != null) && (src != null)) {
            if (dst.length < (dst_offset + length)) {
                croak("dst.length = " + dst.length + ", but " + "dst_offset = " + dst_offset + " and length = "
                        + length + ".");
            }

            if (src.length < (src_offset + length)) {
                croak("src.length = " + src.length + ", but " + "src_offset = " + src_offset + " and length = "
                        + length + ".");
            }

            for (int i = 0; i < length; ++i, ++dst_offset, ++src_offset)
                dst[dst_offset] = src[src_offset];
        }

        return length;
    }

    /**
     * Convert a <code>byte</code> into an unsigned integer.
     *
     * @param b the <code>byte</code> to cast
     *
     * @return a postiive <code>int</code> whose lowest byte contains the bits
     *          of <code>b</code>.
     */
    public static final int byteToUnsignedInt(byte b) {
        return ((int) b) & 0xff;
    }

    /**
     * DOCUMENT ME!
     *
     * @param msg DOCUMENT ME!
     */
    private static void croak(String msg) {
        // throw new java.AssertionViolatedException(msg);
    }
}

Related

  1. byteArrayToBigInteger(final byte[] data)
  2. ByteArrayToBigIntegerWithoutSign(byte[] array)
  3. bytesToBigInteger(byte[] buffer)
  4. bytesToBigInteger(byte[] data)
  5. convertToBigInteger(String s)
  6. convertToBigInteger(String sequence)
  7. getUnsignedBigInteger(byte[] data, int offset, int length)
  8. getUnsignedBigInteger(long x)