Source for gnu.CORBA.BigDecimalHelper

   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.TypeCode;
  49: import org.omg.CORBA.TypeCodePackage.BadKind;
  50: 
  51: /**
  52:  * Reads and writes BigDecimal as CORBA <code>fixed</code>.
  53:  * The format, described in CORBA specification, requires to store
  54:  * data in hexadecimal format, two digits per byte (oceted), most
  55:  * significant digit first. The last half-byte in the representation
  56:  * stores the sign, being 0xD for negative numbers and 0xC for
  57:  * zero and positive numbers. To have the even number of half bytes,
  58:  * 0x0 is appended to the beginning, if required. The position of the
  59:  * decimal point is not stored.
  60:  *
  61:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  62:  */
  63: public class BigDecimalHelper
  64: {
  65:   {
  66:   }
  67: 
  68:   /**
  69:    * @todo remove from the release version.
  70:    */
  71:   public static void main(String[] args)
  72:   {
  73:     try
  74:       {
  75:         ByteArrayOutputStream b = new ByteArrayOutputStream();
  76:         BigDecimal d = new BigDecimal("12234.54689");
  77: 
  78:         write(b, d);
  79: 
  80:         byte[] a = b.toByteArray();
  81: 
  82:         for (int i = 0; i < a.length; i++)
  83:           {
  84:             int k = a [ i ] & 0xFF;
  85:             System.out.print(Integer.toHexString(k) + " ");
  86:           }
  87: 
  88:         System.out.println("Now reading");
  89: 
  90:         ByteArrayInputStream bin = new ByteArrayInputStream(a);
  91: 
  92:         BigDecimal r = read(bin, d.scale());
  93: 
  94:         System.out.println(r);
  95:       }
  96:     catch (Exception ex)
  97:       {
  98:         ex.printStackTrace();
  99:       }
 100:   }
 101: 
 102:   /**
 103:    * Read the CORBA fixed, autodetecting the number of bytes
 104:    * and assuming the given scale.
 105:    */
 106:   public static BigDecimal read(java.io.InputStream in, int scale)
 107:                          throws IOException
 108:   {
 109:     ByteArrayOutputStream bout = new ByteArrayOutputStream();
 110: 
 111:     int f;
 112: 
 113:     do
 114:       {
 115:         f = in.read();
 116:         if (f >= 0)
 117:           bout.write(f);
 118:       }
 119:     // The last byte has 0xC or 0xD in the last halfbyte.  
 120:     while ((f & 0xF) <= 0x9);
 121: 
 122:     return createFixed(scale, bout.toByteArray());
 123:   }
 124: 
 125:   /**
 126:    * Write the big decimal as CORBA <code>fixed<.code>.
 127:    * The scale will not be stored.
 128:    * 
 129:    * @param out a stream to write into.
 130:    * @param x a big decimal to write.
 131:    * @param digits a number of the decimal digits in the record
 132:    * being written. For the smaller
 133:    * numbers, zeroes are added to the left.
 134:    *
 135:    * @throws IOException if the stream write method throws one.
 136:    * @throws BadKind if this BigDecimal has more digits than
 137:    * specified.
 138:    */
 139:   public static void write(java.io.OutputStream out, BigDecimal x)
 140:                     throws IOException, BadKind
 141:   {
 142:     StringBuffer v = new StringBuffer(x.unscaledValue().toString());
 143: 
 144:     boolean negative = v.charAt(0) == '-';
 145: 
 146:     if (negative)
 147:       v = v.deleteCharAt(0);
 148: 
 149:     if ( (v.length() & 1) == 0)
 150:       v.insert(0, '0');
 151: 
 152:     int c;
 153: 
 154:     for (int i = 0; i < v.length() - 1; i = i + 2)
 155:       {
 156:         c = ((v.charAt(i) - '0') << 4) | (v.charAt(i + 1) - '0');
 157:         out.write(c);
 158:       }
 159: 
 160:     c = ((v.charAt(v.length() - 1) - '0') << 4) | (negative ? 0xD : 0xC);
 161: 
 162:     out.write(c);
 163:   }
 164: 
 165:   /**
 166:    * Convert the loaded byte array, representing
 167:    * CORBA <code>fixed</code>, into an instance of
 168:    * the {@link BigDecimal}
 169:    */
 170:   private static BigDecimal createFixed(int scale, byte[] d)
 171:   {
 172:     StringBuffer s = new StringBuffer(2 * d.length);
 173: 
 174:     int last = d.length - 1;
 175: 
 176:     if ((d [ last ] & 0xF) == 0xD)
 177:       s.append('-');
 178: 
 179:     if (last > 0)
 180:       for (int i = 0; i < last; i++)
 181:         {
 182:           s.append((char) (((d [ i ] >> 4) & 0xF) + '0'));
 183:           s.append((char) (((d [ i ]) & 0xF) + '0'));
 184:         }
 185: 
 186:     s.append((char) (((d [ last ] >> 4) & 0xF) + '0'));
 187: 
 188:     BigInteger b = new BigInteger(s.toString());
 189:     BigDecimal dec = new BigDecimal(b, scale);
 190: 
 191:     return dec;
 192:   }
 193: }