home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / src / COM / objectspace / jgl / ByteArray.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  3.3 KB  |  138 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package COM.objectspace.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 2.0.2
  15.  * @author ObjectSpace, Inc.
  16.  */
  17.  
  18. public class ByteArray extends ArrayAdapter
  19.   {
  20.   byte myArray[];
  21.  
  22.   public ByteArray()
  23.     {
  24.     myArray = new byte[ 0 ];
  25.     }
  26.  
  27.   public ByteArray( byte array[] )
  28.     {
  29.     myArray = array;
  30.     }
  31.  
  32.   public ByteArray( ByteArray array )
  33.     {
  34.     myArray = array.myArray;
  35.     }
  36.  
  37.   /**
  38.    * Return a shallow copy of myself.
  39.    */
  40.   public synchronized Object clone()
  41.     {
  42.     return new ByteArray( this );
  43.     }
  44.  
  45.   /**
  46.    * Return a string that describes me.
  47.    */
  48.   public synchronized String toString()
  49.     {
  50.     return Printing.toString( this, "byte[]" );
  51.     }
  52.  
  53.   /**
  54.    * Return true if I'm equal to a specified object.
  55.    * @param object The object to compare myself against.
  56.    * @return true if I'm equal to the specified object.
  57.    */
  58.   public boolean equals( Object object )
  59.     {
  60.     return object instanceof ByteArray && equals( (ByteArray)object );
  61.     }
  62.  
  63.   /**
  64.    * Return true if I contain the same items in the same order as
  65.    * another ByteArray. Use equals() to compare the individual elements.
  66.    * @param array The ByteArray to compare myself against.
  67.    * @return true if I'm equal to the specified object.
  68.    */
  69.   public synchronized boolean equals( ByteArray object )
  70.     {
  71.     synchronized( object )
  72.       {
  73.       return Comparing.equal( this, object );
  74.       }
  75.     }
  76.  
  77.   /**
  78.    * Return the number of objects that I contain.
  79.    */
  80.   public int size()
  81.     {
  82.     return myArray.length;
  83.     }
  84.  
  85.   /**
  86.    * Return the maximum number of objects that I can contain.
  87.    */
  88.   public int maxSize()
  89.     {
  90.     return myArray.length;
  91.     }
  92.  
  93.   /**
  94.    * Return an Enumeration of my components.
  95.    */
  96.   public synchronized Enumeration elements()
  97.     {
  98.     return ByteIterator.begin( myArray, this );
  99.     }
  100.  
  101.   /**
  102.    * Return an iterator positioned at my first item.
  103.    */
  104.   public synchronized ForwardIterator start()
  105.     {
  106.     return ByteIterator.begin( myArray, this );
  107.     }
  108.  
  109.   /**
  110.    * Return an iterator positioned immediately after my last item.
  111.    */
  112.   public synchronized ForwardIterator finish()
  113.     {
  114.     return ByteIterator.end( myArray, this );
  115.     }
  116.  
  117.   /**
  118.    * Return the integer at the specified index as a Integer object.
  119.    * @param index The index.
  120.    */
  121.   public synchronized Object at( int index )
  122.     {
  123.     return new Integer( myArray[index] );
  124.     }
  125.  
  126.   /**
  127.    * Set the object at a specified index.  The object must be a Integer
  128.    * @param index The index.
  129.    * @param object The object to place at the specified index.
  130.    * @exception java.lang.ClassCastException if object is not a Integer
  131.    * @exception java.lang.IndexOutOfBoundsException if object is not a Integer
  132.    */
  133.   public synchronized void put( int index, Object object )
  134.     {
  135.     myArray[index] = (byte)((Integer)object).intValue();
  136.     }
  137.   }
  138.