Java ByteBuffer Write writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length)

Here you can find the source of writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length)

Description

write Big Integer

License

Open Source License

Declaration

private static void writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length) throws IOException 

Method Source Code


//package com.java2s;
/*//  ww  w .j a v  a 2 s  . c om
 * Copyright 2004 - 2013 Wayne Grant
 *           2013 - 2018 Kai Kramer
 *
 * This file is part of KeyStore Explorer.
 *
 * KeyStore Explorer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * KeyStore Explorer 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KeyStore Explorer.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;

import java.math.BigInteger;
import java.nio.ByteBuffer;

public class Main {
    private static void writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length) throws IOException {
        // Get big-endian two's compliment representation of big integer
        byte[] bigInt = bigInteger.toByteArray();

        // Remove leading zero bytes
        int skipZeroPos = 0;

        for (int i = 0; i < bigInt.length; i++) {
            if (bigInt[i] != 0) {
                break;
            }
            skipZeroPos++;
        }

        byte[] tmp = new byte[bigInt.length - skipZeroPos];

        System.arraycopy(bigInt, skipZeroPos, tmp, 0, tmp.length);

        bigInt = tmp;

        // Convert to little-endian
        reverseBytes(bigInt);

        // Pad out byte array with zeros
        int padByteLength = length - bigInt.length;

        if (padByteLength > 0) {
            tmp = new byte[length];
            System.arraycopy(bigInt, 0, tmp, 0, bigInt.length);
            bigInt = tmp;
        }

        bb.put(bigInt);
    }

    private static void reverseBytes(byte[] bytes) {
        int halfWay = bytes.length / 2;
        for (int i = 0; i < halfWay; i++) {
            byte b = bytes[i];
            bytes[i] = bytes[bytes.length - 1 - i];
            bytes[bytes.length - 1 - i] = b;
        }
    }
}

Related

  1. writeAll(ByteChannel channel, ByteBuffer buffer)
  2. writeAll(GatheringByteChannel ch, ByteBuffer... bbs)
  3. writeAllToChannel(List buffers, WritableByteChannel channel)
  4. writeBE(ByteBuffer bb, int elementWidth, long value)
  5. writeBER32(ByteBuffer buffer, int value)
  6. writeBlock(String fileName, long startOffset, ByteBuffer buffer, Logger log)
  7. writeBooleanArray(boolean[] array, ByteBuffer out)
  8. writeBuffer(ByteBuffer dest, ByteBuffer src)
  9. writeBuffer(ByteChannel channel, ByteBuffer buffer)