01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package com.db4o.internal.handlers.net;
22:
23: import java.math.*;
24:
25: import com.db4o.internal.*;
26:
27: /**
28: * .NET decimal layout is (bytewise)
29: * |M3|M2|M1|M0|M7|M6|M5|M4|M11|M10|M9|M8|S[7]|E[4-0]|X|X|
30: * (M=mantissa, E=exponent(negative powers of 10), S=sign, X=unused/unknown)
31: *
32: * @exclude
33: * @sharpen.ignore
34: */
35: public class NetDecimal extends NetSimpleTypeHandler {
36:
37: private static final BigInteger BYTESHIFT_FACTOR = new BigInteger(
38: "100", 16);
39:
40: private static final BigInteger ZERO = new BigInteger("0", 16);
41:
42: private static final BigDecimal TEN = new BigDecimal("10");
43:
44: public NetDecimal(ObjectContainerBase stream) {
45: super (stream, 21, 16);
46: }
47:
48: public String toString(byte[] bytes) {
49: BigInteger mantissa = ZERO;
50: for (int blockoffset = 8; blockoffset >= 0; blockoffset -= 4) {
51: for (int byteidx = 0; byteidx < 4; byteidx++) {
52: mantissa = mantissa.multiply(BYTESHIFT_FACTOR);
53: int idx = blockoffset + byteidx;
54: mantissa = mantissa.add(new BigInteger(String
55: .valueOf(bytes[idx] & 0xff), 10));
56: }
57: }
58:
59: // The exponent is stored negative by .NET so we change it back here !!!
60: int exponent = -bytes[13] & 0x1f;
61:
62: boolean negative = bytes[12] != 0;
63:
64: BigDecimal result = new BigDecimal(mantissa);
65:
66: if (exponent < 0) {
67: for (int i = exponent; i < 0; i++) {
68: result = result.divide(TEN, BigDecimal.ROUND_HALF_DOWN);
69: }
70: } else {
71: for (int i = 0; i < exponent; i++) {
72: result = result.multiply(TEN);
73: }
74: }
75:
76: if (negative) {
77: result = result.negate();
78: }
79: return result.toString();
80: }
81: }
|