Here you can find the source of writeBigInteger(OutputStream output, BigInteger value)
Parameter | Description |
---|---|
output | the stream to write to |
value | the value to output |
Parameter | Description |
---|---|
IOException | an exception |
static void writeBigInteger(OutputStream output, BigInteger value) throws IOException
//package com.java2s; /**/*from ww w. j ava2s . c o m*/ * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF 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.IOException; import java.io.OutputStream; import java.math.BigInteger; public class Main { /** * Write the arbitrarily sized signed BigInteger in vint format. * * Signed integers are encoded using the low bit as the sign bit using zigzag * encoding. * * Each byte uses the low 7 bits for data and the high bit for stop/continue. * * Bytes are stored LSB first. * @param output the stream to write to * @param value the value to output * @throws IOException */ static void writeBigInteger(OutputStream output, BigInteger value) throws IOException { // encode the signed number as a positive integer value = value.shiftLeft(1); int sign = value.signum(); if (sign < 0) { value = value.negate(); value = value.subtract(BigInteger.ONE); } int length = value.bitLength(); while (true) { long lowBits = value.longValue() & 0x7fffffffffffffffL; length -= 63; // write out the next 63 bits worth of data for (int i = 0; i < 9; ++i) { // if this is the last byte, leave the high bit off if (length <= 0 && (lowBits & ~0x7f) == 0) { output.write((byte) lowBits); return; } else { output.write((byte) (0x80 | (lowBits & 0x7f))); lowBits >>>= 7; } } value = value.shiftRight(63); } } }