home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / jgl_1_1.exe / src / ByteArray.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  2.7 KB  |  120 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. import java.util.Enumeration;
  7.  
  8. /**
  9.  * ByteArray allows a native array of bytes to be accessed like a Container.
  10.  * It is particularly useful for applying generic algorithms like Sorting.sort()
  11.  * to a native array.  The Byte array deals with Object requests as Integer
  12.  * objects
  13.  * <p>
  14.  * @version 1.1
  15.  * @author ObjectSpace, Inc.
  16.  */
  17.  
  18. public class ByteArray extends ArrayAdapter
  19.   {
  20.   byte myArray[];
  21.  
  22.   public ByteArray( byte array[] )
  23.     {
  24.     myArray = array;
  25.     }
  26.  
  27.   public ByteArray( ByteArray array )
  28.     {
  29.     myArray = array.myArray;
  30.     }
  31.  
  32.   /**
  33.    * Return a shallow copy of myself.
  34.    */
  35.   public Object clone()
  36.     {
  37.     return new ByteArray( this );
  38.     }
  39.  
  40.   /**
  41.    * Return a string that describes me.
  42.    */
  43.   public String toString()
  44.     {
  45.     return Printing.toString( this, "byte[]" );
  46.     }
  47.  
  48.   /**
  49.    * Return true if I'm equal to a specified object.
  50.    * @param object The object to compare myself against.
  51.    * @return true if I'm equal to the specified object.
  52.    */
  53.   public boolean equals( Object object )
  54.     {
  55.     return Comparing.equal( this, (ByteArray) object );
  56.     }
  57.   
  58.   /**
  59.    * Return the number of objects that I contain.
  60.    */
  61.   public int size()
  62.     {
  63.     return myArray.length;
  64.     }
  65.  
  66.   /**
  67.    * Return the maximum number of objects that I can contain.
  68.    */
  69.   public int maxSize()
  70.     {
  71.     return myArray.length;
  72.     }
  73.  
  74.   /**
  75.    * Return an Enumeration of my components.
  76.    */
  77.   public Enumeration elements()
  78.     {
  79.     return ByteIterator.begin( myArray, this );
  80.     }
  81.  
  82.   /**
  83.    * Return an iterator positioned at my first item.
  84.    */
  85.   public ForwardIterator start()
  86.     {
  87.     return ByteIterator.begin( myArray, this );
  88.     }
  89.  
  90.   /**
  91.    * Return an iterator positioned immediately after my last item.
  92.    */
  93.   public ForwardIterator finish()
  94.     {
  95.     return ByteIterator.end( myArray, this );
  96.     }
  97.  
  98.   /**
  99.    * Return the integer at the specified index as a Integer object.
  100.    * @param index The index.
  101.    */
  102.   public Object at( int index )
  103.     {
  104.     return new Integer( myArray[index] );
  105.     }
  106.  
  107.   /**
  108.    * Set the object at a specified index.  The object must be a Integer
  109.    * @param index The index.
  110.    * @param object The object to place at the specified index.
  111.    * @exception java.lang.ClassCastException if object is not a Integer
  112.    * @exception java.lang.IndexOutOfBoundsException if object is not a Integer
  113.    */
  114.   public void put( int index, Object object )
  115.     {
  116.     myArray[index] = (byte)((Integer)object).intValue();
  117.     }
  118.   }
  119.  
  120.