OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / gnu / CORBA / BigDecimalHelper.java
1 /* BigDecimalHelper.java --
2    Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.CORBA;
40
41 import java.io.ByteArrayInputStream;
42 import java.io.ByteArrayOutputStream;
43 import java.io.IOException;
44
45 import java.math.BigDecimal;
46 import java.math.BigInteger;
47
48 import org.omg.CORBA.TypeCodePackage.BadKind;
49
50 /**
51  * Reads and writes BigDecimal as CORBA <code>fixed</code>.
52  * The format, described in CORBA specification, requires to store
53  * data in hexadecimal format, two digits per byte (oceted), most
54  * significant digit first. The last half-byte in the representation
55  * stores the sign, being 0xD for negative numbers and 0xC for
56  * zero and positive numbers. To have the even number of half bytes,
57  * 0x0 is appended to the beginning, if required. The position of the
58  * decimal point is not stored.
59  *
60  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
61  */
62 public class BigDecimalHelper
63 {
64   {
65   }
66
67   /**
68    * @todo remove from the release version.
69    */
70   public static void main(String[] args)
71   {
72     try
73       {
74         ByteArrayOutputStream b = new ByteArrayOutputStream();
75         BigDecimal d = new BigDecimal("12234.54689");
76
77         write(b, d);
78
79         byte[] a = b.toByteArray();
80
81         for (int i = 0; i < a.length; i++)
82           {
83             int k = a [ i ] & 0xFF;
84             System.out.print(Integer.toHexString(k) + " ");
85           }
86
87         System.out.println("Now reading");
88
89         ByteArrayInputStream bin = new ByteArrayInputStream(a);
90
91         BigDecimal r = read(bin, d.scale());
92
93         System.out.println(r);
94       }
95     catch (Exception ex)
96       {
97         ex.printStackTrace();
98       }
99   }
100
101   /**
102    * Read the CORBA fixed, autodetecting the number of bytes
103    * and assuming the given scale.
104    */
105   public static BigDecimal read(java.io.InputStream in, int scale)
106                          throws IOException
107   {
108     ByteArrayOutputStream bout = new ByteArrayOutputStream();
109
110     int f;
111
112     do
113       {
114         f = in.read();
115         if (f >= 0)
116           bout.write(f);
117       }
118     // The last byte has 0xC or 0xD in the last halfbyte.  
119     while ((f & 0xF) <= 0x9);
120
121     return createFixed(scale, bout.toByteArray());
122   }
123
124   /**
125    * Write the big decimal as CORBA <code>fixed<.code>.
126    * The scale will not be stored.
127    * 
128    * @param out a stream to write into.
129    * @param x a big decimal to write.
130    *
131    * @throws IOException if the stream write method throws one.
132    * @throws BadKind if this BigDecimal has more digits than
133    * specified.
134    */
135   public static void write(java.io.OutputStream out, BigDecimal x)
136                     throws IOException, BadKind
137   {
138     StringBuffer v = new StringBuffer(x.unscaledValue().toString());
139
140     boolean negative = v.charAt(0) == '-';
141
142     if (negative)
143       v = v.deleteCharAt(0);
144
145     if ( (v.length() & 1) == 0)
146       v.insert(0, '0');
147
148     int c;
149
150     for (int i = 0; i < v.length() - 1; i = i + 2)
151       {
152         c = ((v.charAt(i) - '0') << 4) | (v.charAt(i + 1) - '0');
153         out.write(c);
154       }
155
156     c = ((v.charAt(v.length() - 1) - '0') << 4) | (negative ? 0xD : 0xC);
157
158     out.write(c);
159   }
160
161   /**
162    * Convert the loaded byte array, representing
163    * CORBA <code>fixed</code>, into an instance of
164    * the {@link BigDecimal}
165    */
166   private static BigDecimal createFixed(int scale, byte[] d)
167   {
168     StringBuffer s = new StringBuffer(2 * d.length);
169
170     int last = d.length - 1;
171
172     if ((d [ last ] & 0xF) == 0xD)
173       s.append('-');
174
175     if (last > 0)
176       for (int i = 0; i < last; i++)
177         {
178           s.append((char) (((d [ i ] >> 4) & 0xF) + '0'));
179           s.append((char) (((d [ i ]) & 0xF) + '0'));
180         }
181
182     s.append((char) (((d [ last ] >> 4) & 0xF) + '0'));
183
184     BigInteger b = new BigInteger(s.toString());
185     BigDecimal dec = new BigDecimal(b, scale);
186
187     return dec;
188   }
189 }