BigDecimalHelper.java 5.4 KB

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