1 /* BigDecimalHelper.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
41 import java.io.ByteArrayInputStream;
42 import java.io.ByteArrayOutputStream;
43 import java.io.IOException;
45 import java.math.BigDecimal;
46 import java.math.BigInteger;
48 import org.omg.CORBA.TypeCodePackage.BadKind;
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.
60 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
62 public class BigDecimalHelper
68 * @todo remove from the release version.
70 public static void main(String[] args)
74 ByteArrayOutputStream b = new ByteArrayOutputStream();
75 BigDecimal d = new BigDecimal("12234.54689");
79 byte[] a = b.toByteArray();
81 for (int i = 0; i < a.length; i++)
83 int k = a [ i ] & 0xFF;
84 System.out.print(Integer.toHexString(k) + " ");
87 System.out.println("Now reading");
89 ByteArrayInputStream bin = new ByteArrayInputStream(a);
91 BigDecimal r = read(bin, d.scale());
93 System.out.println(r);
102 * Read the CORBA fixed, autodetecting the number of bytes
103 * and assuming the given scale.
105 public static BigDecimal read(java.io.InputStream in, int scale)
108 ByteArrayOutputStream bout = new ByteArrayOutputStream();
118 // The last byte has 0xC or 0xD in the last halfbyte.
119 while ((f & 0xF) <= 0x9);
121 return createFixed(scale, bout.toByteArray());
125 * Write the big decimal as CORBA <code>fixed<.code>.
126 * The scale will not be stored.
128 * @param out a stream to write into.
129 * @param x a big decimal to write.
131 * @throws IOException if the stream write method throws one.
132 * @throws BadKind if this BigDecimal has more digits than
135 public static void write(java.io.OutputStream out, BigDecimal x)
136 throws IOException, BadKind
138 StringBuffer v = new StringBuffer(x.unscaledValue().toString());
140 boolean negative = v.charAt(0) == '-';
143 v = v.deleteCharAt(0);
145 if ( (v.length() & 1) == 0)
150 for (int i = 0; i < v.length() - 1; i = i + 2)
152 c = ((v.charAt(i) - '0') << 4) | (v.charAt(i + 1) - '0');
156 c = ((v.charAt(v.length() - 1) - '0') << 4) | (negative ? 0xD : 0xC);
162 * Convert the loaded byte array, representing
163 * CORBA <code>fixed</code>, into an instance of
164 * the {@link BigDecimal}
166 private static BigDecimal createFixed(int scale, byte[] d)
168 StringBuffer s = new StringBuffer(2 * d.length);
170 int last = d.length - 1;
172 if ((d [ last ] & 0xF) == 0xD)
176 for (int i = 0; i < last; i++)
178 s.append((char) (((d [ i ] >> 4) & 0xF) + '0'));
179 s.append((char) (((d [ i ]) & 0xF) + '0'));
182 s.append((char) (((d [ last ] >> 4) & 0xF) + '0'));
184 BigInteger b = new BigInteger(s.toString());
185 BigDecimal dec = new BigDecimal(b, scale);