Source for gnu.CORBA.CDR.LittleEndianOutputStream

   1: /* LittleEndianOutputStream.java --
   2:    Copyright (C) 1998, 2001, 2003, 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.CDR;
  40: 
  41: import java.io.FilterOutputStream;
  42: import java.io.IOException;
  43: import java.io.OutputStream;
  44: 
  45: /**
  46:  * This stream writes data in the Little Endian format
  47:  * (less significant byte first). This is opposite to the
  48:  * usual data presentation in java platform.
  49:  *
  50:  * This class reuses code from DataOutputStream.
  51:  *
  52:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  53:  * @author Aaron M. Renn (arenn@urbanophile.com)
  54:  * @author Tom Tromey (tromey@cygnus.com)
  55:  */
  56: public class LittleEndianOutputStream
  57:   extends FilterOutputStream
  58:   implements AbstractDataOutput
  59: {
  60:   /**
  61:    * This method initializes an instance of <code>DataOutputStream</code> to
  62:    * write its data to the specified underlying <code>OutputStream</code>
  63:    *
  64:    * @param out The subordinate <code>OutputStream</code> to which this
  65:    * object will write
  66:    */
  67:   public LittleEndianOutputStream(OutputStream out)
  68:   {
  69:     super(out);
  70:   }
  71: 
  72:   /**
  73:    * This method flushes any unwritten bytes to the underlying stream.
  74:    *
  75:    * @exception IOException If an error occurs.
  76:    */
  77:   public void flush()
  78:              throws IOException
  79:   {
  80:     out.flush();
  81:   }
  82: 
  83:   /**
  84:    * This method writes the specified byte (passed as an <code>int</code>)
  85:    * to the underlying output stream.
  86:    *
  87:    * @param value The <code>byte</code> to write, passed as an <code>int</code>.
  88:    *
  89:    * @exception IOException If an error occurs.
  90:    */
  91:   public synchronized void write(int value)
  92:                           throws IOException
  93:   {
  94:     out.write(value);
  95:   }
  96: 
  97:   /**
  98:    * This method writes <code>len</code> bytes from the specified byte array
  99:    * <code>buf</code> starting at position <code>offset</code> into the
 100:    * buffer to the underlying output stream.
 101:    *
 102:    * @param buf The byte array to write from.
 103:    * @param offset The index into the byte array to start writing from.
 104:    * @param len The number of bytes to write.
 105:    *
 106:    * @exception IOException If an error occurs.
 107:    */
 108:   public synchronized void write(byte[] buf, int offset, int len)
 109:                           throws IOException
 110:   {
 111:     out.write(buf, offset, len);
 112:   }
 113: 
 114:   /**
 115:    * This method writes a Java boolean value to an output stream.  If
 116:    * <code>value</code> is <code>true</code>, a byte with the value of
 117:    * 1 will be written, otherwise a byte with the value of 0 will be
 118:    * written.
 119:    *
 120:    * The value written can be read using the <code>readBoolean</code>
 121:    * method in <code>DataInput</code>.
 122:    *
 123:    * @param value The <code>boolean</code> value to write to the stream
 124:    *
 125:    * @exception IOException If an error occurs
 126:    *
 127:    * @see DataInput#readBoolean
 128:    */
 129:   public void writeBoolean(boolean value)
 130:                     throws IOException
 131:   {
 132:     write(value ? 1 : 0);
 133:   }
 134: 
 135:   /**
 136:    * This method writes a Java byte value to an output stream.  The
 137:    * byte to be written will be in the lowest 8 bits of the
 138:    * <code>int</code> value passed.
 139:    *
 140:    * The value written can be read using the <code>readByte</code> or
 141:    * <code>readUnsignedByte</code> methods in <code>DataInput</code>.
 142:    *
 143:    * @param value The <code>byte</code> to write to the stream, passed as
 144:    * the low eight bits of an <code>int</code>.
 145:    *
 146:    * @exception IOException If an error occurs
 147:    *
 148:    * @see DataInput#readByte
 149:    * @see DataInput#readUnsignedByte
 150:    */
 151:   public void writeByte(int value)
 152:                  throws IOException
 153:   {
 154:     write(value & 0xff);
 155:   }
 156: 
 157:   /**
 158:    * This method writes a Java short value to an output stream.
 159:    *
 160:    * @param value The <code>short</code> value to write to the stream,
 161:    * passed as an <code>int</code>.
 162:    *
 163:    * @exception IOException If an error occurs
 164:    */
 165:   public synchronized void writeShort(int value)
 166:                                throws IOException
 167:   {
 168:     write((byte) (0xff & value));
 169:     write((byte) (0xff & (value >> 8)));
 170:   }
 171: 
 172:   /**
 173:    * Writes char in Little Endian.
 174:    */
 175:   public synchronized void writeChar(int value)
 176:                               throws IOException
 177:   {
 178:     write((byte) (0xff & value));
 179:     write((byte) (0xff & (value >> 8)));
 180:   }
 181: 
 182:   /**
 183:    * Writes int in Little Endian.
 184:    */
 185:   public synchronized void writeInt(int value)
 186:                              throws IOException
 187:   {
 188:     write((byte) (0xff & value));
 189:     write((byte) (0xff & (value >> 8)));
 190:     write((byte) (0xff & (value >> 16)));
 191:     write((byte) (0xff & (value >> 24)));
 192:   }
 193: 
 194:   /**
 195:    * Writes long in Little Endian.
 196:    */
 197:   public synchronized void writeLong(long value)
 198:                               throws IOException
 199:   {
 200:     write((byte) (0xff & value));
 201:     write((byte) (0xff & (value >> 8)));
 202:     write((byte) (0xff & (value >> 16)));
 203:     write((byte) (0xff & (value >> 24)));
 204:     write((byte) (0xff & (value >> 32)));
 205:     write((byte) (0xff & (value >> 40)));
 206:     write((byte) (0xff & (value >> 48)));
 207:     write((byte) (0xff & (value >> 56)));
 208:   }
 209: 
 210:   /**
 211:    * This method writes a Java <code>float</code> value to the stream.  This
 212:    * value is written by first calling the method
 213:    * <code>Float.floatToIntBits</code>
 214:    * to retrieve an <code>int</code> representing the floating point number,
 215:    * then writing this <code>int</code> value to the stream exactly the same
 216:    * as the <code>writeInt()</code> method does.
 217:    *
 218:    * @param value The <code>float</code> value to write to the stream
 219:    *
 220:    * @exception IOException If an error occurs
 221:    *
 222:    * @see writeInt
 223:    * @see DataInput#readFloat
 224:    * @see Float#floatToIntBits
 225:    */
 226:   public void writeFloat(float value)
 227:                   throws IOException
 228:   {
 229:     writeInt(Float.floatToIntBits(value));
 230:   }
 231: 
 232:   /**
 233:    * This method writes a Java <code>double</code> value to the stream.  This
 234:    * value is written by first calling the method
 235:    * <code>Double.doubleToLongBits</code>
 236:    * to retrieve an <code>long</code> representing the floating point number,
 237:    * then writing this <code>long</code> value to the stream exactly the same
 238:    * as the <code>writeLong()</code> method does.
 239:    *
 240:    * @param value The <code>double</code> value to write to the stream
 241:    *
 242:    * @exception IOException If an error occurs
 243:    *
 244:    * @see writeLong
 245:    * @see DataInput#readDouble
 246:    * @see Double#doubleToLongBits
 247:    */
 248:   public void writeDouble(double value)
 249:                    throws IOException
 250:   {
 251:     writeLong(Double.doubleToLongBits(value));
 252:   }
 253: }