Source for gnu.CORBA.CDR.BufferedCdrOutput

   1: /* BufferedCdrOutput.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.CDR;
  40: 
  41: import java.io.ByteArrayOutputStream;
  42: 
  43: /**
  44:  * A CORBA output stream, writing data into the internal buffer ({@link ByteArrayOutputStream}).
  45:  *
  46:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  47:  */
  48: public class BufferedCdrOutput
  49:   extends AbstractCdrOutput
  50:   implements gnuValueStream
  51: {
  52:   /**
  53:    * Use serialVersionUID for interoperability.
  54:    */
  55:   private static final long serialVersionUID = 1;
  56: 
  57:   /**
  58:    * The byte buffer.
  59:    */
  60:   public final AligningOutput buffer;
  61: 
  62:   /**
  63:    * Creates the instance with the given initial buffer size.
  64:    *
  65:    * @param bufSize the buffer size.
  66:    */
  67:   public BufferedCdrOutput(int bufSize)
  68:   {
  69:     buffer = new AligningOutput(bufSize);
  70:     setOutputStream(buffer);
  71:   }
  72: 
  73:   /**
  74:    * Creates the instance with the default buffer size.
  75:    */
  76:   public BufferedCdrOutput()
  77:   {
  78:     buffer = new AligningOutput();
  79:     setOutputStream(buffer);
  80:   }
  81: 
  82:   /**
  83:    * Set the alignment offset, if the index of the first byte in the stream is
  84:    * different from 0.
  85:    */
  86:   public void setOffset(int an_offset)
  87:   {
  88:     buffer.setOffset(an_offset);
  89:   }
  90: 
  91:   /**
  92:    * Align the curretn position at the given natural boundary.
  93:    */
  94:   public void align(int boundary)
  95:   {
  96:     buffer.align(boundary);
  97:   }
  98: 
  99:   /**
 100:    * Return the input stream that reads the previously written values.
 101:    */
 102:   public org.omg.CORBA.portable.InputStream create_input_stream()
 103:   {
 104:     BufferredCdrInput in = new BufferredCdrInput(buffer.toByteArray());
 105:     in.setOrb(orb);
 106: 
 107:     in.setVersion(giop);
 108:     in.setCodeSet(getCodeSet());
 109: 
 110:     return in;
 111:   }
 112: 
 113:   /**
 114:    * Resets (clears) the buffer.
 115:    */
 116:   public void reset()
 117:   {
 118:     buffer.reset();
 119:     setOutputStream(buffer);
 120:   }
 121: 
 122:   /**
 123:    * Get the current position in the buffer.
 124:    *
 125:    * @return The position in the buffer, taking offset into consideration.
 126:    */
 127:   public int getPosition()
 128:   {
 129:     return buffer.getPosition();
 130:   }
 131: 
 132:   /**
 133:    * Get the associated RunTime.
 134:    */
 135:   public gnuRuntime getRunTime()
 136:   {
 137:     return runtime;
 138:   }
 139: 
 140:   /**
 141:    * Replace the instance of RunTime.
 142:    */
 143:   public void setRunTime(gnuRuntime a_runtime)
 144:   {
 145:     runtime = a_runtime;
 146:   }
 147: 
 148:   /**
 149:    * Seek to the given position.
 150:    */
 151:   public void seek(int position)
 152:   {
 153:     buffer.seek(position);
 154:   }
 155: 
 156: }