1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2000,2008 Oracle.  All rights reserved.
5 *
6 * $Id: BigIntegerBinding.java,v 12.3 2008/01/08 20:58:36 bostic Exp $
7 */
8
9package com.sleepycat.bind.tuple;
10
11import java.math.BigInteger;
12
13import com.sleepycat.db.DatabaseEntry;
14
15/**
16 * A concrete <code>TupleBinding</code> for a <code>BigInteger</code> value.
17 */
18public class BigIntegerBinding extends TupleBinding {
19
20    // javadoc is inherited
21    public Object entryToObject(TupleInput input) {
22
23        return input.readBigInteger();
24    }
25
26    // javadoc is inherited
27    public void objectToEntry(Object object, TupleOutput output) {
28
29        output.writeBigInteger((BigInteger) object);
30    }
31
32    // javadoc is inherited
33    protected TupleOutput getTupleOutput(Object object) {
34
35        return sizedOutput((BigInteger) object);
36    }
37
38    /**
39     * Converts an entry buffer into a <code>BigInteger</code> value.
40     *
41     * @param entry is the source entry buffer.
42     *
43     * @return the resulting value.
44     */
45    public static BigInteger entryToBigInteger(DatabaseEntry entry) {
46
47        return entryToInput(entry).readBigInteger();
48    }
49
50    /**
51     * Converts a <code>BigInteger</code> value into an entry buffer.
52     *
53     * @param val is the source value.
54     *
55     * @param entry is the destination entry buffer.
56     */
57    public static void bigIntegerToEntry(BigInteger val, DatabaseEntry entry) {
58
59        outputToEntry(sizedOutput(val).writeBigInteger(val), entry);
60    }
61
62    /**
63     * Returns a tuple output object of the exact size needed, to avoid
64     * wasting space when a single primitive is output.
65     */
66    private static TupleOutput sizedOutput(BigInteger val) {
67
68        int len = TupleOutput.getBigIntegerByteLength(val);
69        return new TupleOutput(new byte[len]);
70    }
71}
72