/[classpath]/classpath/gnu/java/nio/ByteBufferImpl.java
ViewVC logotype

Diff of /classpath/gnu/java/nio/ByteBufferImpl.java

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

revision 1.12 by mkoch, Tue Mar 11 11:48:51 2003 UTC revision 1.13 by mkoch, Mon May 19 08:36:27 2003 UTC
# Line 1  Line 1 
1  /* ByteBufferImpl.java --  /* ByteBufferImpl.java --
2     Copyright (C) 2002 Free Software Foundation, Inc.     Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 35  this exception to your version of the li Line 35  this exception to your version of the li
35  obligated to do so.  If you do not wish to do so, delete this  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */  exception statement from your version. */
37    
38    
39  package gnu.java.nio;  package gnu.java.nio;
40    
41  import java.nio.ByteBuffer;  import java.nio.ByteBuffer;
# Line 52  import java.nio.ShortBuffer; Line 53  import java.nio.ShortBuffer;
53  public final class ByteBufferImpl extends ByteBuffer  public final class ByteBufferImpl extends ByteBuffer
54  {  {
55    private boolean readOnly;    private boolean readOnly;
     
   public ByteBufferImpl (int cap, int off, int lim)  
   {  
     super (cap, lim, off, 0);  
     this.backing_buffer = new byte [cap];  
     readOnly = false;  
   }  
   
   public ByteBufferImpl (byte[] array, int offset, int length)  
   {  
     super (array.length, length, offset, 0);  
     this.backing_buffer = array;  
     readOnly = false;  
   }  
56    
57    public ByteBufferImpl (ByteBufferImpl copy)    public ByteBufferImpl (int capacity)
58    {    {
59      super (copy.capacity (), copy.limit (), copy.position (), 0);      this (new byte [capacity], 0, capacity, capacity, 0, -1, false);
     backing_buffer = copy.backing_buffer;  
     readOnly = copy.isReadOnly ();  
60    }    }
   
   void inc_pos (int toAdd)  
   {  
     position (position () + toAdd);  
   }  
   
   private static native byte[] nio_cast(byte[]copy);  
   private static native byte[] nio_cast(char[]copy);  
   private static native byte[] nio_cast(short[]copy);  
   private static native byte[] nio_cast(long[]copy);  
   private static native byte[] nio_cast(int[]copy);  
   private static native byte[] nio_cast(float[]copy);  
   private static native byte[] nio_cast(double[]copy);  
   
   ByteBufferImpl (byte[] copy)  
   {  
     super (copy.length, copy.length, 0, 0);  
     this.backing_buffer = copy != null ? nio_cast (copy) : null;  
     readOnly = false;  
   }  
   
   private static native byte nio_get_Byte (ByteBufferImpl b, int index, int limit);  
     
   private static native void nio_put_Byte (ByteBufferImpl b, int index, int limit, byte value);  
61        
62    public ByteBuffer asByteBuffer ()    public ByteBufferImpl (byte[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly)
   {  
     ByteBufferImpl res = new ByteBufferImpl (backing_buffer);  
     res.limit ((limit () * 1) / 1);  
     return res;  
   }  
   
   ByteBufferImpl (char[] copy)  
63    {    {
64      super (copy.length * 2, copy.length * 2, 0, 0);      super (buffer, offset, capacity, limit, position, mark);
65      this.backing_buffer = copy != null ? nio_cast (copy) : null;      this.readOnly = readOnly;
     readOnly = false;  
66    }    }
67      
   private static native char nio_get_Char (ByteBufferImpl b, int index, int limit);  
   
   private static native void nio_put_Char (ByteBufferImpl b, int index, int limit, char value);  
   
68    public CharBuffer asCharBuffer ()    public CharBuffer asCharBuffer ()
69    {    {
70      CharBufferImpl res = new CharBufferImpl (backing_buffer);      throw new Error ("Not implemented");
     res.limit ((limit () * 2) / 1);  
     return res;  
71    }    }
72    
   ByteBufferImpl (short[] copy)  
   {  
     super (copy.length, copy.length, 0, 0);  
     this.backing_buffer = copy != null ? nio_cast (copy) : null;  
     readOnly = false;  
   }  
     
   private static native short nio_get_Short (ByteBufferImpl b, int index, int limit);  
     
   private static native void nio_put_Short (ByteBufferImpl b, int index, int limit, short value);  
     
73    public ShortBuffer asShortBuffer ()    public ShortBuffer asShortBuffer ()
74    {    {
75      ShortBufferImpl res = new ShortBufferImpl (backing_buffer);      throw new Error ("Not implemented");
     res.limit ((limit () * 2) / 1);  
     return res;  
76    }    }
77    
   ByteBufferImpl (int[] copy)  
   {  
     super (copy.length * 4, copy.length * 4, 0, 0);  
     this.backing_buffer = copy != null ? nio_cast(copy) : null;  
     readOnly = false;  
   }  
     
   private static native int nio_get_Int (ByteBufferImpl b, int index, int limit);  
     
   private static native void nio_put_Int (ByteBufferImpl b, int index, int limit, int value);  
     
78    public IntBuffer asIntBuffer ()    public IntBuffer asIntBuffer ()
79    {    {
80      IntBufferImpl res = new IntBufferImpl (backing_buffer);      throw new Error ("Not implemented");
     res.limit ((limit() * 4) / 1);  
     return res;  
81    }    }
82    
   ByteBufferImpl (long[] copy)  
   {  
     super (copy.length * 8, copy.length * 8, 0, 0);  
     this.backing_buffer = copy != null ? nio_cast (copy) : null;  
     readOnly = false;  
   }  
     
   private static native long nio_get_Long (ByteBufferImpl b, int index, int limit);  
     
   private static native void nio_put_Long (ByteBufferImpl b, int index, int limit, long value);  
     
83    public LongBuffer asLongBuffer ()    public LongBuffer asLongBuffer ()
84    {    {
85      LongBufferImpl res = new LongBufferImpl (backing_buffer);      throw new Error ("Not implemented");
     res.limit ((limit() * 8) / 1);  
     return res;  
86    }    }
87    
   ByteBufferImpl (float[] copy)  
   {  
     super (copy.length * 4, copy.length * 4, 0, 0);  
     this.backing_buffer = copy != null ? nio_cast (copy) : null;  
     readOnly = false;  
   }  
     
   private static native float nio_get_Float (ByteBufferImpl b, int index, int limit);  
     
   private static native void nio_put_Float (ByteBufferImpl b, int index, int limit, float value);  
     
88    public FloatBuffer asFloatBuffer ()    public FloatBuffer asFloatBuffer ()
89    {    {
90      FloatBufferImpl res = new FloatBufferImpl (backing_buffer);      throw new Error ("Not implemented");
     res.limit ((limit() * 4) / 1);  
     return res;  
91    }    }
92    
   ByteBufferImpl (double[] copy)  
   {  
     super (copy.length * 8, copy.length * 8, 0, 0);  
     this.backing_buffer = copy != null ? nio_cast (copy) : null;  
     readOnly = false;  
   }  
     
   private static native double nio_get_Double (ByteBufferImpl b, int index, int limit);  
     
   private static native void nio_put_Double (ByteBufferImpl b, int index, int limit, double value);  
     
93    public DoubleBuffer asDoubleBuffer ()    public DoubleBuffer asDoubleBuffer ()
94    {    {
95      DoubleBufferImpl res = new DoubleBufferImpl (backing_buffer);      throw new Error ("Not implemented");
     res.limit ((limit () * 8) / 1);  
     return res;  
96    }    }
97    
98    public boolean isReadOnly()    public boolean isReadOnly ()
99    {    {
100      return readOnly;      return readOnly;
101    }    }
102        
103    public ByteBuffer slice()    public ByteBuffer slice ()
104    {    {
105      return new ByteBufferImpl(this);      return new ByteBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ());
106    }    }
107      
108    public ByteBuffer duplicate()    public ByteBuffer duplicate ()
109    {    {
110      return new ByteBufferImpl(this);      return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ());
111    }    }
112      
113    public ByteBuffer asReadOnlyBuffer()    public ByteBuffer asReadOnlyBuffer ()
114    {    {
115      ByteBufferImpl a = new ByteBufferImpl(this);      return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true);
     a.readOnly = true;  
     return a;  
116    }    }
117      
118    public ByteBuffer compact()    public ByteBuffer compact ()
119    {    {
120        int copied = 0;
121        
122        while (remaining () > 0)
123          {
124            put (copied, get ());
125            copied++;
126          }
127    
128        position (copied);
129      return this;      return this;
130    }    }
131      
132    public boolean isDirect()    public boolean isDirect ()
133    {    {
134      return false;      return false;
135    }    }
136      
137    final public byte get()    /**
138       * Relative get method. Reads the next <code>byte</code> from the buffer.
139       */
140      final public byte get ()
141    {    {
142      byte e = backing_buffer[position()];      byte result = backing_buffer [position ()];
143      position(position()+1);      position (position () + 1);
144      return e;      return result;
145    }    }
146        
147    final public ByteBuffer put(byte b)    /**
148       * Relative put method. Writes <code>value</code> to the next position
149       * in the buffer.
150       *
151       * @exception ReadOnlyBufferException If this buffer is read-only.
152       */
153      final public ByteBuffer put (byte value)
154    {    {
155      if (readOnly)      if (readOnly)
156        throw new ReadOnlyBufferException ();        throw new ReadOnlyBufferException ();
157                            
158      backing_buffer[position()] = b;      backing_buffer [position ()] = value;
159      position(position()+1);      position (position () + 1);
160      return this;      return this;
161    }    }
162        
163    final public byte get(int index)    /**
164       * Absolute get method. Reads the <code>byte</code> at position
165       * <code>index</code>.
166       *
167       * @exception IndexOutOfBoundsException If index is negative or not smaller
168       * than the buffer's limit.
169       */
170      final public byte get (int index)
171    {    {
172      return backing_buffer[index];      return backing_buffer [index];
173    }    }
174        
175    final public ByteBuffer put(int index, byte b)    /**
176       * Absolute put method. Writes <code>value</value> to position
177       * <code>index</code> in the buffer.
178       *
179       * @exception IndexOutOfBoundsException If index is negative or not smaller
180       * than the buffer's limit.
181       * @exception ReadOnlyBufferException If this buffer is read-only.
182       */
183      final public ByteBuffer put (int index, byte value)
184    {    {
185      if (readOnly)      if (readOnly)
186        throw new ReadOnlyBufferException ();        throw new ReadOnlyBufferException ();
187                    
188      backing_buffer[index] = b;      backing_buffer [index] = value;
189      return this;      return this;
190    }    }
191        
192    final public char getChar ()    final public char getChar ()
193    {    {
194      char a = nio_get_Char (this, position (), limit ());      // FIXME: this handles big endian only
195      inc_pos (2);      return (char) (((get () & 0xff) << 8) + (get () & 0xff));
     return a;  
196    }    }
197        
198    final public ByteBuffer putChar (char value)    final public ByteBuffer putChar (char value)
199    {    {
200      if (readOnly)      // FIXME: this handles big endian only
201        throw new ReadOnlyBufferException ();      put ((byte) ((((int) value) & 0xff00) >> 8));
202            put ((byte) (((int) value) & 0x00ff));
     nio_put_Char (this, position (), limit (), value);  
     inc_pos (2);  
203      return this;      return this;
204    }    }
205        
206    final public char getChar (int index)    final public char getChar (int index)
207    {    {
208      char a = nio_get_Char (this, index, limit ());      // FIXME: this handles big endian only
209      return a;      return (char) (((get (index) & 0xff) << 8) + (get (index + 1) & 0xff));
210    }    }
211        
212    final public ByteBuffer putChar (int index, char value)    final public ByteBuffer putChar (int index, char value)
213    {    {
214      if (readOnly)      // FIXME: this handles big endian only
215        throw new ReadOnlyBufferException ();      put (index, (byte) ((((int) value) & 0xff00) >> 8));
216            put (index + 1, (byte) (((int) value) & 0x00ff));
     nio_put_Char (this, index, limit (), value);  
217      return this;      return this;
218    }    }
219    
220    final public short getShort ()    final public short getShort ()
221    {    {
222      short a = nio_get_Short (this, position (), limit ());      // FIXME: this handles big endian only
223      inc_pos (2);      return (short) (((get () & 0xff) << 8) + (get () & 0xff));
     return a;  
224    }    }
225        
226    final public ByteBuffer putShort (short value)    final public ByteBuffer putShort (short value)
227    {    {
228      if (readOnly)      // FIXME: this handles big endian only
229        throw new ReadOnlyBufferException ();      put ((byte) ((((int) value) & 0xff00) >> 8));
230            put ((byte) (((int) value) & 0x00ff));
     nio_put_Short (this, position (), limit (), value);  
     inc_pos (2);  
231      return this;      return this;
232    }    }
233        
234    final public short getShort (int index)    final public short getShort (int index)
235    {    {
236      short a = nio_get_Short (this, index, limit ());      // FIXME: this handles big endian only
237      return a;      return (short) (((get (index) & 0xff) << 8) + (get (index + 1) & 0xff));
238    }    }
239        
240    final public ByteBuffer putShort (int index, short value)    final public ByteBuffer putShort (int index, short value)
241    {    {
242      if (readOnly)      // FIXME: this handles big endian only
243        throw new ReadOnlyBufferException ();      put (index, (byte) ((((int) value) & 0xff00) >> 8));
244            put (index + 1, (byte) (((int) value) & 0x00ff));
     nio_put_Short (this, index, limit (), value);  
245      return this;      return this;
246    }    }
247    
248    final public int getInt ()    final public int getInt ()
249    {    {
250      int a = nio_get_Int (this, position (), limit ());      // FIXME: this handles big endian only
251      inc_pos (4);      return (int) (((get () & 0xff) << 24)
252      return a;                    + (get () & 0xff) << 16
253                      + (get () & 0xff) << 8
254                      + (get () & 0xff));
255    }    }
256        
257    final public ByteBuffer putInt (int value)    final public ByteBuffer putInt (int value)
258    {    {
259      if (readOnly)      // FIXME: this handles big endian only
260        throw new ReadOnlyBufferException ();      put ((byte) ((((int) value) & 0xff000000) >> 24));
261            put ((byte) ((((int) value) & 0x00ff0000) >> 16));
262      nio_put_Int (this, position (), limit (), value);      put ((byte) ((((int) value) & 0x0000ff00) >> 8));
263      inc_pos (4);      put ((byte) (((int) value) & 0x000000ff));
264      return this;      return this;
265    }    }
266        
267    final public int getInt (int index)    final public int getInt (int index)
268    {    {
269      int a = nio_get_Int (this, index, limit ());      // FIXME: this handles big endian only
270      return a;      return (int) (((get (index) & 0xff) << 24)
271                      + (get (index + 1) & 0xff) << 16
272                      + (get (index + 2) & 0xff) << 8
273                      + (get (index + 3) & 0xff));
274    }    }
275        
276    final public ByteBuffer putInt (int index, int value)    final public ByteBuffer putInt (int index, int value)
277    {    {
278      if (readOnly)      // FIXME: this handles big endian only
279        throw new ReadOnlyBufferException ();      put (index, (byte) ((((int) value) & 0xff000000) >> 24));
280            put (index + 1, (byte) ((((int) value) & 0x00ff0000) >> 16));
281      nio_put_Int(this, index, limit (), value);      put (index + 2, (byte) ((((int) value) & 0x0000ff00) >> 8));
282        put (index + 3, (byte) (((int) value) & 0x000000ff));
283      return this;      return this;
284    }    }
285    
286    final public long getLong ()    final public long getLong ()
287    {    {
288      long a = nio_get_Long (this, position (), limit ());      // FIXME: this handles big endian only
289      inc_pos (8);      return (long) (((get () & 0xff) << 56)
290      return a;                     + (get () & 0xff) << 48
291                       + (get () & 0xff) << 40
292                       + (get () & 0xff) << 32
293                       + (get () & 0xff) << 24
294                       + (get () & 0xff) << 16
295                       + (get () & 0xff) << 8
296                       + (get () & 0xff));
297    }    }
298        
299    final public ByteBuffer putLong (long value)    final public ByteBuffer putLong (long value)
300    {    {
301      if (readOnly)      // FIXME: this handles big endian only
302        throw new ReadOnlyBufferException ();      put ((byte) ((value & 0xff00000000000000L) >> 56));
303            put ((byte) ((value & 0x00ff000000000000L) >> 48));
304      nio_put_Long (this, position (), limit (), value);      put ((byte) ((value & 0x0000ff0000000000L) >> 40));
305      inc_pos (8);      put ((byte) ((value & 0x000000ff00000000L) >> 32));
306        put ((byte) ((value & 0x00000000ff000000L) >> 24));
307        put ((byte) ((value & 0x0000000000ff0000L) >> 16));
308        put ((byte) ((value & 0x000000000000ff00L) >> 8));
309        put ((byte) (value & 0x00000000000000ffL));
310      return this;      return this;
311    }    }
312        
313    final public long getLong (int index)    final public long getLong (int index)
314    {    {
315      long a = nio_get_Long (this, index, limit ());      // FIXME: this handles big endian only
316      return a;      return (long) (((get (index) & 0xff) << 56)
317                       + (get (index + 1) & 0xff) << 48
318                       + (get (index + 2) & 0xff) << 40
319                       + (get (index + 3) & 0xff) << 32
320                       + (get (index + 4) & 0xff) << 24
321                       + (get (index + 5) & 0xff) << 16
322                       + (get (index + 6) & 0xff) << 8
323                       + (get (index + 7) & 0xff));
324    }    }
325        
326    final public ByteBuffer putLong (int index, long value)    final public ByteBuffer putLong (int index, long value)
327    {    {
328      if (readOnly)      // FIXME: this handles big endian only
329        throw new ReadOnlyBufferException ();      put (index, (byte) ((value & 0xff00000000000000L) >> 56));
330            put (index + 1, (byte) ((value & 0x00ff000000000000L) >> 48));
331      nio_put_Long (this, index, limit (), value);      put (index + 2, (byte) ((value & 0x0000ff0000000000L) >> 40));
332        put (index + 3, (byte) ((value & 0x000000ff00000000L) >> 32));
333        put (index + 4, (byte) ((value & 0x00000000ff000000L) >> 24));
334        put (index + 5, (byte) ((value & 0x0000000000ff0000L) >> 16));
335        put (index + 6, (byte) ((value & 0x000000000000ff00L) >> 8));
336        put (index + 7, (byte) (value & 0x00000000000000ffL));
337      return this;      return this;
338    }    }
339    
340    final public float getFloat ()    final public float getFloat ()
341    {    {
342      float a = nio_get_Float (this, position (), limit ());      // FIXME: this handles big endian only
343      inc_pos (4);      return (float) (((get () & 0xff) << 24)
344      return a;                      + (get () & 0xff) << 16
345                        + (get () & 0xff) << 8
346                        + (get () & 0xff));
347    }    }
348        
349    final public ByteBuffer putFloat (float value)    final public ByteBuffer putFloat (float value)
350    {    {
351      if (readOnly)      // FIXME: this handles big endian only
352        throw new ReadOnlyBufferException ();      put ((byte) ((((int) value) & 0xff000000) >> 24));
353            put ((byte) ((((int) value) & 0x00ff0000) >> 16));
354      nio_put_Float (this, position (), limit (), value);      put ((byte) ((((int) value) & 0x0000ff00) >> 8));
355      inc_pos (4);      put ((byte) (((int) value) & 0x000000ff));
356      return this;      return this;
357    }    }
358        
359    final public float getFloat (int index)    final public float getFloat (int index)
360    {    {
361      float a = nio_get_Float (this, index, limit ());      // FIXME: this handles big endian only
362      return a;      return (float) (((get (index) & 0xff) << 24)
363                        + (get (index + 1) & 0xff) << 16
364                        + (get (index + 2) & 0xff) << 8
365                        + (get (index + 3) & 0xff));
366    }    }
367    
368    final public ByteBuffer putFloat (int index, float value)    final public ByteBuffer putFloat (int index, float value)
369    {    {
370      if (readOnly)      // FIXME: this handles big endian only
371        throw new ReadOnlyBufferException ();      put (index, (byte) ((((int) value) & 0xff000000) >> 24));
372            put (index + 1, (byte) ((((int) value) & 0x00ff0000) >> 16));
373      nio_put_Float (this, index, limit(), value);      put (index + 2, (byte) ((((int) value) & 0x0000ff00) >> 8));
374        put (index + 3, (byte) (((int) value) & 0x000000ff));
375      return this;      return this;
376    }    }
377    
378    final public double getDouble ()    final public double getDouble ()
379    {    {
380      double a = nio_get_Double (this, position (), limit ());      // FIXME: this handles big endian only
381      inc_pos (8);      return (double) (((get () & 0xff) << 56)
382      return a;                       + (get () & 0xff) << 48
383                         + (get () & 0xff) << 40
384                         + (get () & 0xff) << 32
385                         + (get () & 0xff) << 24
386                         + (get () & 0xff) << 16
387                         + (get () & 0xff) << 8
388                         + (get () & 0xff));
389    }    }
390    
391    final public ByteBuffer putDouble (double value)    final public ByteBuffer putDouble (double value)
392    {    {
393      if (readOnly)      // FIXME: this handles big endian only
394        throw new ReadOnlyBufferException ();      put ((byte) ((((long) value) & 0xff00000000000000L) >> 56));
395            put ((byte) ((((long) value) & 0x00ff000000000000L) >> 48));
396      nio_put_Double (this, position(), limit (), value);      put ((byte) ((((long) value) & 0x0000ff0000000000L) >> 40));
397      inc_pos (8);      put ((byte) ((((long) value) & 0x000000ff00000000L) >> 32));
398        put ((byte) ((((long) value) & 0x00000000ff000000L) >> 24));
399        put ((byte) ((((long) value) & 0x0000000000ff0000L) >> 16));
400        put ((byte) ((((long) value) & 0x000000000000ff00L) >> 8));
401        put ((byte) (((long) value) & 0x00000000000000ffL));
402      return this;      return this;
403    }    }
404        
405    final public double getDouble (int index)    final public double getDouble (int index)
406    {    {
407      return nio_get_Double (this, index, limit ());      // FIXME: this handles big endian only
408        return (double) (((get (index) & 0xff) << 56)
409                         + (get (index + 1) & 0xff) << 48
410                         + (get (index + 2) & 0xff) << 40
411                         + (get (index + 3) & 0xff) << 32
412                         + (get (index + 4) & 0xff) << 24
413                         + (get (index + 5) & 0xff) << 16
414                         + (get (index + 6) & 0xff) << 8
415                         + (get (index + 7) & 0xff));
416    }    }
417        
418    final public ByteBuffer putDouble (int index, double value)    final public ByteBuffer putDouble (int index, double value)
419    {    {
420      if (readOnly)      // FIXME: this handles big endian only
421        throw new ReadOnlyBufferException ();      put (index, (byte) ((((long) value) & 0xff00000000000000L) >> 56));
422            put (index + 1, (byte) ((((long) value) & 0x00ff000000000000L) >> 48));
423      nio_put_Double (this, index, limit (), value);      put (index + 2, (byte) ((((long) value) & 0x0000ff0000000000L) >> 40));
424        put (index + 3, (byte) ((((long) value) & 0x000000ff00000000L) >> 32));
425        put (index + 4, (byte) ((((long) value) & 0x00000000ff000000L) >> 24));
426        put (index + 5, (byte) ((((long) value) & 0x0000000000ff0000L) >> 16));
427        put (index + 6, (byte) ((((long) value) & 0x000000000000ff00L) >> 8));
428        put (index + 7, (byte) (((long) value) & 0x00000000000000ffL));
429      return this;      return this;
430    }    }
431  }  }

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.13

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