Java BigInteger to toPushbackReader(BigInteger x)

Here you can find the source of toPushbackReader(BigInteger x)

Description

to Pushback Reader

License

LGPL

Declaration

public static PushbackReader toPushbackReader(BigInteger x)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.math.BigInteger;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackReader;

import java.io.IOException;

public class Main {
    public static PushbackReader toPushbackReader(BigInteger x)
            throws IOException {
        byte[] bytes = x.abs().toByteArray();
        final int startB = (x.signum() < 0 ? '-' : -1); // Start the stream with minus sign in case of negative number
        InputStream in = new ByteArrayInputStream(bytes) {
            public int read() {
                int c;
                if (this.b == -1) {
                    this.b = super.read();
                    if (this.b == -1) {
                        c = -1;/*from   w  w w. j a  va 2 s .  co m*/
                    } else {
                        c = Character.forDigit(this.b >> 4, 16);
                        this.b = Character.forDigit(this.b & 0x0F, 16);
                    }
                } else {
                    c = this.b;
                    this.b = -1;
                }
                return c;
            }

            public int read(byte[] b, int off, int len) {
                int i = 0;
                for (; i < len; i++) {
                    int c = read();
                    if (c == -1) {
                        i = (i == 0 ? -1 : i); // In case of EOF; there was nothing to read in the stream
                        break;
                    }
                    b[i + off] = (byte) c;
                }
                return i;
            }

            private int b = startB;
        };

        return new PushbackReader(new InputStreamReader(in, "ISO-8859-1"));
    }
}

Related

  1. toInteger(BigInteger integer)
  2. toIntegerBytes(final BigInteger bigInt)
  3. toMinimalByteArray(BigInteger value)
  4. toObject(BigInteger x)
  5. toPaddedBytes(BigInteger bi, int length)
  6. toStr(BigInteger number)
  7. toString(BigInteger bi)
  8. toString(BigInteger[] vector)
  9. toUnsigned(final BigInteger[] ulong)