01: package org.bouncycastle.bcpg;
02:
03: import java.io.*;
04: import java.math.BigInteger;
05:
06: /**
07: * a multiple precision integer
08: */
09: public class MPInteger extends BCPGObject {
10: BigInteger value = null;
11:
12: public MPInteger(BCPGInputStream in) throws IOException {
13: int length = (in.read() << 8) | in.read();
14: byte[] bytes = new byte[(length + 7) / 8];
15:
16: in.readFully(bytes);
17:
18: value = new BigInteger(1, bytes);
19: }
20:
21: public MPInteger(BigInteger value) {
22: if (value == null || value.signum() < 0) {
23: throw new IllegalArgumentException(
24: "value must not be null, or negative");
25: }
26:
27: this .value = value;
28: }
29:
30: public BigInteger getValue() {
31: return value;
32: }
33:
34: public void encode(BCPGOutputStream out) throws IOException {
35: int length = value.bitLength();
36:
37: out.write(length >> 8);
38: out.write(length);
39:
40: byte[] bytes = value.toByteArray();
41:
42: if (bytes[0] == 0) {
43: out.write(bytes, 1, bytes.length - 1);
44: } else {
45: out.write(bytes, 0, bytes.length);
46: }
47: }
48: }
|