/[classpath]/classpath/java/nio/ByteBuffer.java
ViewVC logotype

Diff of /classpath/java/nio/ByteBuffer.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by mkoch, Wed Nov 13 11:53:17 2002 UTC revision 1.5 by mkoch, Sat Nov 16 16:02:56 2002 UTC
# Line 37  exception statement from your version. * Line 37  exception statement from your version. *
37    
38  package java.nio;  package java.nio;
39    
40    import gnu.java.nio.ByteBufferImpl;
41    
42    /**
43     * @since 1.4
44     */
45  public abstract class ByteBuffer extends Buffer  public abstract class ByteBuffer extends Buffer
46  {  {
47    private ByteOrder endian = ByteOrder.BIG_ENDIAN;    private ByteOrder endian = ByteOrder.BIG_ENDIAN;
48        
49      /**
50       * Allocates a new direct byte buffer.
51       */
52      public static ByteBuffer allocateDirect (int capacity)
53    protected byte [] backing_buffer;    protected byte [] backing_buffer;
54        
   public static ByteBuffer allocateDirect(int capacity)  
   {  
     ByteBuffer b = new gnu.java.nio. ByteBufferImpl(capacity, 0, capacity);  
     return b;  
   }  
     
   public static ByteBuffer allocate(int capacity)  
55    {    {
56      ByteBuffer b = new gnu.java.nio. ByteBufferImpl(capacity, 0, capacity);      ByteBuffer b = new gnu.java.nio. ByteBufferImpl(capacity, 0, capacity);
57      return b;      return b;
58    }    }
59      
60    final public static ByteBuffer wrap(byte[] array, int offset, int length)    /**
61    {     * Allocates a new byte buffer.
62      gnu.java.nio.ByteBufferImpl b = new gnu.java.nio. ByteBufferImpl(array, offset, length);     */
63      return b;    public static ByteBuffer allocate (int capacity)
64    }    {
65          return new ByteBufferImpl (capacity, 0, capacity);
66    final public static ByteBuffer wrap(String a)    }
67    {  
68      return wrap(a.getBytes(), 0, a.length());    /**
69    }     * Wraps a byte array into a buffer.
70         *
71    final public static ByteBuffer wrap(byte[] array)     * @exception IndexOutOfBoundsException If the preconditions on the offset
72       * and length parameters do not hold
73       */
74      final public static ByteBuffer wrap (byte[] array, int offset, int length)
75      {
76        return new ByteBufferImpl (array, offset, length);
77      }
78    
79      /**
80       * Wraps a byte array into a buffer.
81       */
82      final public static ByteBuffer wrap (byte[] array)
83    {    {
84      return wrap(array, 0, array.length);      return wrap (array, 0, array.length);
85    }    }
86      
87    final public ByteBuffer get(byte[] dst, int offset, int length)    /**
88    {     * This method transfers bytes from this buffer into
89       * the given destination array.
90       *
91       * @param dst The destination array
92       * @param offset The offset within the array of the first byte to be written;
93       * must be non-negative and no larger than dst.length.
94       * @param length The maximum number of bytes to be written to the given array;
95       * must be non-negative and no larger than dst.length - offset.
96       *
97       * @exception BufferUnderflowException If there are fewer than length bytes
98       * remaining in this buffer.
99       * @exception IndexOutOfBoundsException - If the preconditions on the offset
100       * and length parameters do not hold.
101       */
102      final public ByteBuffer get (byte[] dst, int offset, int length)
103      {
104        if ((offset < 0) ||
105            (offset > dst.length) ||
106            (length < 0) ||
107            (length > (dst.length - offset)))
108          throw new IndexOutOfBoundsException ();
109    
110      for (int i = offset; i < offset + length; i++)      for (int i = offset; i < offset + length; i++)
111        {        {
112          dst[i] = get();          dst [i] = get();
113        }        }
114        
115      return this;      return this;
116    }    }
117      
118    final public ByteBuffer get(byte[] dst)    /**
119    {     * This method transfers bytes from this buffer into the given
120      return get(dst, 0, dst.length);     * destination array.
121    }     *
122         * @param dst The byte array to write into.
123    final public ByteBuffer put(ByteBuffer src)     *
124       * @exception BufferUnderflowException If there are fewer than dst.length
125       * bytes remaining in this buffer.
126       */
127      final public ByteBuffer get (byte[] dst)
128      {
129        return get (dst, 0, dst.length);
130      }
131    
132      /**
133       * Writes the content of src into the buffer.
134       *
135       * @param src The source data.
136       *
137       * @exception BufferOverflowException If there is insufficient space in this
138       * buffer for the remaining bytes in the source buffer.
139       * @exception IllegalArgumentException If the source buffer is this buffer.
140       * @exception ReadOnlyBufferException If this buffer is read only.
141       */
142      final public ByteBuffer put (ByteBuffer src)
143    {    {
144      while (src.hasRemaining())      if (src == this)
145        put(src.get());        throw new IllegalArgumentException ();
146      
147        while (src.hasRemaining ())
148          {
149            put (src.get ());
150          }
151        
152      return this;      return this;
153    }    }
154      
155    final public ByteBuffer put(byte[] src, int offset, int length)    /**
156    {     * Writes the content of the the array src into the buffer.
157       *
158       * @param src The array to copy into the buffer.
159       * @param offset The offset within the array of the first byte to be read;
160       * must be non-negative and no larger than src.length.
161       * @param length The number of bytes to be read from the given array;
162       * must be non-negative and no larger than src.length - offset.
163       *
164       * @exception BufferOverflowException If there is insufficient space in this
165       * buffer for the remaining bytes in the source buffer.
166       * @exception IndexOutOfBoundsException If the preconditions on the offset
167       * and length parameters do not hold.
168       * @exception ReadOnlyBufferException If this buffer is read only.
169       */
170      final public ByteBuffer put (byte[] src, int offset, int length)
171      {
172        if ((offset < 0) ||
173            (offset > src.length) ||
174            (length < 0) ||
175            (length > src.length - offset))
176          throw new IndexOutOfBoundsException ();
177    
178      for (int i = offset; i < offset + length; i++)      for (int i = offset; i < offset + length; i++)
179        put(src[i]);        {
180            put (src [i]);
181          }
182    
183      return this;      return this;
184    }    }
185    
186    public final ByteBuffer put(byte[] src)    /**
187       * Writes the content of the the array src into the buffer.
188       *
189       * @param src The array to copy into the buffer.
190       *
191       * @exception BufferOverflowException If there is insufficient space in this
192       * buffer for the remaining bytes in the source buffer.
193       * @exception ReadOnlyBufferException If this buffer is read only.
194       */
195      public final ByteBuffer put (byte[] src)
196    {    {
197      return put(src, 0, src.length);      return put (src, 0, src.length);
198    }    }
199    
200      /**
201       * Tells whether or not this buffer is backed by an accessible byte array.
202       */
203    public final boolean hasArray()    public final boolean hasArray()
204    {    {
205      return (backing_buffer != null);      return (backing_buffer != null);
206    }    }
207    
208      /**
209       * Returns the byte array that backs this buffer.
210       *
211       * @exception ReadOnlyBufferException If this buffer is backed by an array
212       * but is read-only.
213       * @exception UnsupportedOperationException If this buffer is not backed
214       * by an accessible array.
215       */
216    public final byte[] array()    public final byte[] array()
217    {    {
218        if (backing_buffer == null)
219          throw new UnsupportedOperationException ();
220    
221        if (isReadOnly ())
222          throw new ReadOnlyBufferException ();
223    
224      return backing_buffer;      return backing_buffer;
225    }    }
226      
227      /**
228       * Returns the offset within this buffer's backing array of the first element
229       * of the buffer  
230       *
231       * @exception ReadOnlyBufferException If this buffer is backed by an array
232       * but is read-only.
233       * @exception UnsupportedOperationException If this buffer is not backed
234       * by an accessible array.
235       */
236    public final int arrayOffset()    public final int arrayOffset()
237    {    {
238        if (backing_buffer == null)
239          throw new UnsupportedOperationException ();
240    
241        if (isReadOnly ())
242          throw new ReadOnlyBufferException ();
243    
244        // FIXME: Return correct value
245      return 0;      return 0;
246    }    }
247        
248      /**
249       * Returns the current hash code of this buffer.
250       */
251    public int hashCode()    public int hashCode()
252    {    {
253        // FIXME: Check what SUN calcs here
254      return super.hashCode();      return super.hashCode();
255    }    }
256      
257    public boolean equals(Object obj)    /**
258       * Tells whether or not this buffer is equal to another object.
259       */
260      public boolean equals (Object obj)
261    {    {
262      if (obj instanceof ByteBuffer)      if (obj != null &&
263            obj instanceof ByteBuffer)
264        {        {
265          return compareTo(obj) == 0;          return compareTo (obj) == 0;
266        }        }
267          
268      return false;      return false;
269    }    }
270      
271    public int compareTo(Object ob)    /**
272       * Compares this buffer to another object.
273       *
274       * @exception ClassCastException If the argument is not a byte buffer
275       */
276      public int compareTo (Object obj)
277    {    {
278      ByteBuffer a = (ByteBuffer) ob;      ByteBuffer a = (ByteBuffer) obj;
279    
280      if (a.remaining() != remaining())      if (a.remaining() != remaining())
281        return 1;        {
282            return 1;
283          }
284      
285      if (! hasArray() ||      if (! hasArray() ||
286          ! a.hasArray())          ! a.hasArray())
287        {        {
288          return 1;          return 1;
289        }        }
290      
291      int r = remaining();      int r = remaining();
292      int i1 = pos;      int i1 = position ();
293      int i2 = a.pos;      int i2 = a.position ();
294      for (int i=0;i<r;i++)    
295        for (int i = 0; i < r; i++)
296        {        {
297          int t = (int) (get(i1)- a.get(i2));          int t = (int) (get (i1) - a.get (i2));
298      
299          if (t != 0)          if (t != 0)
300            {            {
301              return (int) t;              return (int) t;
302            }            }
303        }        }
304      
305      return 0;      return 0;
306    }    }
307      
308      /**
309       * Retrieves this buffer's byte order.
310       */  
311    public final ByteOrder order()    public final ByteOrder order()
312    {    {
313      return endian;      return endian;
314    }    }
315        
316    public final ByteBuffer order(ByteOrder bo)    /**
317       * Modifies this buffer's byte order.
318       */
319      public final ByteBuffer order (ByteOrder endian)
320    {    {
321      endian = bo;      this.endian = endian;
322      return this;      return this;
323    }    }
324        
325    public abstract byte get();    /**
326       * Reads the byte at this buffer's current position,
327       * and then increments the position.
328       *
329       * @exception BufferUnderflowException If the buffer's current position
330       * is not smaller than its limit.
331       */
332      public abstract byte get ();
333        
334    public abstract java.nio. ByteBuffer put(byte b);    public abstract ByteBuffer put (byte b);
335        
336    public abstract byte get(int index);    public abstract byte get (int index);
337        
338    public abstract java.nio. ByteBuffer put(int index, byte b);    public abstract ByteBuffer put (int index, byte b);
339        
340    public abstract ByteBuffer compact();    public abstract ByteBuffer compact();
341      
342    public abstract boolean isDirect();    public abstract boolean isDirect();
343        
344    public abstract ByteBuffer slice();    public abstract ByteBuffer slice();

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26