KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > handlers > NetDecimal


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 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.handlers;
22
23 import java.math.*;
24
25 import com.db4o.*;
26
27 /**
28  * @exclude
29  */

30 /**
31  * .NET decimal layout is (bytewise)
32  * |M3|M2|M1|M0|M7|M6|M5|M4|M11|M10|M9|M8|S[7]|E[4-0]|X|X|
33  * (M=mantissa, E=exponent(negative powers of 10), S=sign, X=unused/unknown)
34  */

35 public class NetDecimal extends NetSimpleTypeHandler{
36     
37     private static final BigInteger BYTESHIFT_FACTOR=new BigInteger("100",16);
38      
39     private static final BigInteger ZERO = new BigInteger("0", 16);
40     
41     private static final BigDecimal TEN = new BigDecimal("10");
42
43     public NetDecimal(YapStream stream) {
44         super(stream, 21, 16);
45     }
46     
47     public String JavaDoc toString(byte[] bytes) {
48         BigInteger mantissa=ZERO;
49         for(int blockoffset=8;blockoffset>=0;blockoffset-=4) {
50             for(int byteidx=0;byteidx<4;byteidx++) {
51                 mantissa=mantissa.multiply(BYTESHIFT_FACTOR);
52                 int idx=blockoffset+byteidx;
53                 mantissa=mantissa.add(new BigInteger(String.valueOf(bytes[idx]&0xff),10));
54             }
55         }
56         
57         // The exponent is stored negative by .NET so we change it back here !!!
58
int exponent = - bytes[13]&0x1f;
59         
60         boolean negative=bytes[12]!=0;
61         
62         BigDecimal result=new BigDecimal(mantissa);
63         
64         if(exponent < 0) {
65             for (int i = exponent; i < 0; i++) {
66                 result = result.divide(TEN, BigDecimal.ROUND_HALF_DOWN);
67             }
68         }else {
69             for (int i = 0; i < exponent; i++) {
70                 result = result.multiply(TEN);
71             }
72         }
73         
74         if(negative) {
75             result=result.negate();
76         }
77         return result.toString();
78     }
79 }
80
Popular Tags