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