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