libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / CORBA / BigDecimalHelper.java
blob33b74bb778083c1ca32f5261c49ac72ba03c6190
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)
9 any later version.
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
19 02110-1301 USA.
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.
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. */
39 package gnu.CORBA;
41 import gnu.java.lang.CPStringBuilder;
43 import java.io.ByteArrayInputStream;
44 import java.io.ByteArrayOutputStream;
45 import java.io.IOException;
47 import java.math.BigDecimal;
48 import java.math.BigInteger;
50 import org.omg.CORBA.TypeCodePackage.BadKind;
52 /**
53 * Reads and writes BigDecimal as CORBA <code>fixed</code>.
54 * The format, described in CORBA specification, requires to store
55 * data in hexadecimal format, two digits per byte (oceted), most
56 * significant digit first. The last half-byte in the representation
57 * stores the sign, being 0xD for negative numbers and 0xC for
58 * zero and positive numbers. To have the even number of half bytes,
59 * 0x0 is appended to the beginning, if required. The position of the
60 * decimal point is not stored.
62 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
64 public class BigDecimalHelper
69 /**
70 * @todo remove from the release version.
72 public static void main(String[] args)
74 try
76 ByteArrayOutputStream b = new ByteArrayOutputStream();
77 BigDecimal d = new BigDecimal("12234.54689");
79 write(b, d);
81 byte[] a = b.toByteArray();
83 for (int i = 0; i < a.length; i++)
85 int k = a [ i ] & 0xFF;
86 System.out.print(Integer.toHexString(k) + " ");
89 System.out.println("Now reading");
91 ByteArrayInputStream bin = new ByteArrayInputStream(a);
93 BigDecimal r = read(bin, d.scale());
95 System.out.println(r);
97 catch (Exception ex)
99 ex.printStackTrace();
104 * Read the CORBA fixed, autodetecting the number of bytes
105 * and assuming the given scale.
107 public static BigDecimal read(java.io.InputStream in, int scale)
108 throws IOException
110 ByteArrayOutputStream bout = new ByteArrayOutputStream();
112 int f;
116 f = in.read();
117 if (f >= 0)
118 bout.write(f);
120 // The last byte has 0xC or 0xD in the last halfbyte.
121 while ((f & 0xF) <= 0x9);
123 return createFixed(scale, bout.toByteArray());
127 * Write the big decimal as CORBA <code>fixed<.code>.
128 * The scale will not be stored.
130 * @param out a stream to write into.
131 * @param x a big decimal to write.
133 * @throws IOException if the stream write method throws one.
134 * @throws BadKind if this BigDecimal has more digits than
135 * specified.
137 public static void write(java.io.OutputStream out, BigDecimal x)
138 throws IOException, BadKind
140 CPStringBuilder v = new CPStringBuilder(x.unscaledValue().toString());
142 boolean negative = v.charAt(0) == '-';
144 if (negative)
145 v = v.deleteCharAt(0);
147 if ( (v.length() & 1) == 0)
148 v.insert(0, '0');
150 int c;
152 for (int i = 0; i < v.length() - 1; i = i + 2)
154 c = ((v.charAt(i) - '0') << 4) | (v.charAt(i + 1) - '0');
155 out.write(c);
158 c = ((v.charAt(v.length() - 1) - '0') << 4) | (negative ? 0xD : 0xC);
160 out.write(c);
164 * Convert the loaded byte array, representing
165 * CORBA <code>fixed</code>, into an instance of
166 * the {@link BigDecimal}
168 private static BigDecimal createFixed(int scale, byte[] d)
170 CPStringBuilder s = new CPStringBuilder(2 * d.length);
172 int last = d.length - 1;
174 if ((d [ last ] & 0xF) == 0xD)
175 s.append('-');
177 if (last > 0)
178 for (int i = 0; i < last; i++)
180 s.append((char) (((d [ i ] >> 4) & 0xF) + '0'));
181 s.append((char) (((d [ i ]) & 0xF) + '0'));
184 s.append((char) (((d [ last ] >> 4) & 0xF) + '0'));
186 BigInteger b = new BigInteger(s.toString());
187 BigDecimal dec = new BigDecimal(b, scale);
189 return dec;