/*****************************************************************************/
/* File name: VariableLengthOpaque.java                                      */
/* Purpose: to represent variable length opaque data type of XDR             */
/*                                                                           */
/* Last modified: 31.05.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*                                                                           */
/* This program is distributed in the hope that it will be useful, but       */
/* WITHOUT ANY WARRANTY; without even the implied warranty of                */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General */
/* Public License for more details.                                          */
/*                                                                           */
/* This software is distributed under the terms                              */
/* of the GNU General Public License as published                            */
/* by the Free Software Foundation; either version 2, or (at your option)    */
/* any later version.                                                        */
/* See file COPYING for your rights (GNU GPL)                                */
/*****************************************************************************/

package xdr;


import java.util.*;


public
class

VariableLengthOpaque
{

  private final static String RCSID = "$Header: /d/NFS/xdr/RCS/VariableLengthOpaque.java,v 1.1 1999/06/03 17:36:53 vklimov Exp $";

  private long length = 0;
  private Vector bytes = null;


  public

  VariableLengthOpaque()
/*****************************************************************************/
/* Function name: VariableLengthOpaque                                       */
/* File name: VariableLengthOpaque.java                                      */
/* Purpose: constructor for the VariableLengthOpaque class                   */
/* Parameters:                                                               */
/* Returns:                                                                  */
/* Last modified: 31.05.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
                                             /* VariableLengthOpaque */
/*---------------------------------------------------------------------------*/

    }
/*end of VariableLengthOpaque*/


  public

  VariableLengthOpaque( long length )
/*****************************************************************************/
/* Function name: VariableLengthOpaque                                       */
/* File name: VariableLengthOpaque.java                                      */
/* Purpose: constructor for the VariableLengthOpaque class                   */
/*          the bytes vector of capacity length will be created              */
/* Parameters: length - number of bytes in the opaque                        */
/* Returns:                                                                  */
/* Last modified: 31.05.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
                                             /* VariableLengthOpaque */
/*---------------------------------------------------------------------------*/

      this.length = length;
      bytes = new Vector( (int) length );

    }
/*end of VariableLengthOpaque*/


  public
  void

  addByte( byte byteToAdd )
/*****************************************************************************/
/* Function name: addByte                                                    */
/* File name: VariableLengthOpaque.java                                      */
/* Purpose: to add a byte to the bytes vector                                */
/* Parameters: byteToAdd                                                     */
/* Returns:                                                                  */
/* Last modified: 31.05.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
                                             /* VariableLengthOpaque */
/*---------------------------------------------------------------------------*/

      if (bytes == null)
        bytes = new Vector();
      else
        ;
    /*end of if*/

      bytes.addElement( (Object) (new Byte( byteToAdd )) );

    }
/*end of VariableLengthOpaque*/


  public
  String

  toString()
/*****************************************************************************/
/* Function name: toString                                                   */
/* File name: VariableLengthOpaque.java                                      */
/* Purpose: to represent the VariableLengthOpaque object as a string         */
/* Parameters:                                                               */
/* Returns: the string representation of the object                          */
/* Last modified: 31.05.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      int i;
      long   byteToConvert;
      int vectorLength;
      String hexString = null;
      String result;
                                                      /* toString */
/*---------------------------------------------------------------------------*/

      result = "length=" + length +
        "\nbytes=\n";

      vectorLength = bytes.size();

      if (vectorLength != length)
        result = result +
          "\nthe \'bytes\' vector contains " +
          vectorLength +
          "bytes,\nthis is not equal to the length " +
          "of the VariableLengthOpaque object !\n";
      else
        ;
    /*end of if*/
          
      for (i=0;i<vectorLength;i++)
        {
          byteToConvert = (long)
            ((Byte) bytes.elementAt(i)).byteValue();
          if (byteToConvert < 0)
            {
              byteToConvert = byteToConvert + 0x100;
            }
          else
            ;
        /*end of if*/

          hexString = Integer.toHexString(
                        (int) byteToConvert );
          if ( (hexString.length() % 2) == 1 )
            hexString = "0" + hexString;
          else
            ;
        /*end of if*/

          result = result +
                "0x" + hexString + " ";
        }
    /*end of for-loop*/

      result = result + "\n";

      return result;

    }
/*end of toString*/

} /* end of VariableLengthOpaque class */

/*****************************************************************************/
/* End of file: VariableLengthOpaque.java                                    */
/*****************************************************************************/
