/[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.11 by mkoch, Tue Dec 3 13:23:51 2002 UTC revision 1.12 by mkoch, Tue Mar 11 11:48:51 2003 UTC
# Line 43  import java.nio.DoubleBuffer; Line 43  import java.nio.DoubleBuffer;
43  import java.nio.FloatBuffer;  import java.nio.FloatBuffer;
44  import java.nio.IntBuffer;  import java.nio.IntBuffer;
45  import java.nio.LongBuffer;  import java.nio.LongBuffer;
46    import java.nio.ReadOnlyBufferException;
47  import java.nio.ShortBuffer;  import java.nio.ShortBuffer;
48    
49    /**
50     * This is a Heap memory implementation
51     */
52  public final class ByteBufferImpl extends ByteBuffer  public final class ByteBufferImpl extends ByteBuffer
53  {  {
54    private byte[] backing_buffer;    private boolean readOnly;
55    private int array_offset;    
   private boolean ro;  
   
56    public ByteBufferImpl (int cap, int off, int lim)    public ByteBufferImpl (int cap, int off, int lim)
57    {    {
58      this.cap = cap;      super (cap, lim, off, 0);
59      limit (lim);      this.backing_buffer = new byte [cap];
60      position (off);      readOnly = false;
     this.backing_buffer = new byte[cap];  
61    }    }
62    
63    public ByteBufferImpl (byte[] array, int off, int lim)    public ByteBufferImpl (byte[] array, int offset, int length)
64    {    {
65      this.cap = array.length;      super (array.length, length, offset, 0);
     limit (lim);  
     position (off);  
66      this.backing_buffer = array;      this.backing_buffer = array;
67        readOnly = false;
68    }    }
69    
70    public ByteBufferImpl (ByteBufferImpl copy)    public ByteBufferImpl (ByteBufferImpl copy)
71    {    {
72      this.cap = copy.capacity ();      super (copy.capacity (), copy.limit (), copy.position (), 0);
     limit (copy.limit ());  
     position (copy.position ());  
     ro = copy.ro;  
73      backing_buffer = copy.backing_buffer;      backing_buffer = copy.backing_buffer;
74        readOnly = copy.isReadOnly ();
75    }    }
76    
77    void inc_pos (int toAdd)    void inc_pos (int toAdd)
# Line 81  public final class ByteBufferImpl extend Line 79  public final class ByteBufferImpl extend
79      position (position () + toAdd);      position (position () + toAdd);
80    }    }
81    
82    private static native byte[] nio_cast (byte[] copy);    private static native byte[] nio_cast(byte[]copy);
83    private static native byte[] nio_cast (char[] copy);    private static native byte[] nio_cast(char[]copy);
84    private static native byte[] nio_cast (short[] copy);    private static native byte[] nio_cast(short[]copy);
85    private static native byte[] nio_cast (long[] copy);    private static native byte[] nio_cast(long[]copy);
86    private static native byte[] nio_cast (int[] copy);    private static native byte[] nio_cast(int[]copy);
87    private static native byte[] nio_cast (float[] copy);    private static native byte[] nio_cast(float[]copy);
88    private static native byte[] nio_cast (double[] copy);    private static native byte[] nio_cast(double[]copy);
89    
90    ByteBufferImpl (byte[] copy)    ByteBufferImpl (byte[] copy)
91    {    {
92        super (copy.length, copy.length, 0, 0);
93      this.backing_buffer = copy != null ? nio_cast (copy) : null;      this.backing_buffer = copy != null ? nio_cast (copy) : null;
94        readOnly = false;
95    }    }
96    
97    private static native byte nio_get_Byte (ByteBufferImpl b, int index, int limit);    private static native byte nio_get_Byte (ByteBufferImpl b, int index, int limit);
# Line 107  public final class ByteBufferImpl extend Line 107  public final class ByteBufferImpl extend
107    
108    ByteBufferImpl (char[] copy)    ByteBufferImpl (char[] copy)
109    {    {
110        super (copy.length * 2, copy.length * 2, 0, 0);
111      this.backing_buffer = copy != null ? nio_cast (copy) : null;      this.backing_buffer = copy != null ? nio_cast (copy) : null;
112        readOnly = false;
113    }    }
114    
115    private static native char nio_get_Char (ByteBufferImpl b, int index, int limit);    private static native char nio_get_Char (ByteBufferImpl b, int index, int limit);
# Line 117  public final class ByteBufferImpl extend Line 119  public final class ByteBufferImpl extend
119    public CharBuffer asCharBuffer ()    public CharBuffer asCharBuffer ()
120    {    {
121      CharBufferImpl res = new CharBufferImpl (backing_buffer);      CharBufferImpl res = new CharBufferImpl (backing_buffer);
122      res.limit((limit()*2)/1);      res.limit ((limit () * 2) / 1);
123      return res;      return res;
124    }    }
125    
126    ByteBufferImpl (short[] copy)    ByteBufferImpl (short[] copy)
127    {    {
128        super (copy.length, copy.length, 0, 0);
129      this.backing_buffer = copy != null ? nio_cast (copy) : null;      this.backing_buffer = copy != null ? nio_cast (copy) : null;
130        readOnly = false;
131    }    }
132        
133    private static native short nio_get_Short (ByteBufferImpl b, int index, int limit);    private static native short nio_get_Short (ByteBufferImpl b, int index, int limit);
# Line 139  public final class ByteBufferImpl extend Line 143  public final class ByteBufferImpl extend
143    
144    ByteBufferImpl (int[] copy)    ByteBufferImpl (int[] copy)
145    {    {
146        super (copy.length * 4, copy.length * 4, 0, 0);
147      this.backing_buffer = copy != null ? nio_cast(copy) : null;      this.backing_buffer = copy != null ? nio_cast(copy) : null;
148        readOnly = false;
149    }    }
150        
151    private static native int nio_get_Int (ByteBufferImpl b, int index, int limit);    private static native int nio_get_Int (ByteBufferImpl b, int index, int limit);
# Line 155  public final class ByteBufferImpl extend Line 161  public final class ByteBufferImpl extend
161    
162    ByteBufferImpl (long[] copy)    ByteBufferImpl (long[] copy)
163    {    {
164        super (copy.length * 8, copy.length * 8, 0, 0);
165      this.backing_buffer = copy != null ? nio_cast (copy) : null;      this.backing_buffer = copy != null ? nio_cast (copy) : null;
166        readOnly = false;
167    }    }
168        
169    private static native long nio_get_Long (ByteBufferImpl b, int index, int limit);    private static native long nio_get_Long (ByteBufferImpl b, int index, int limit);
# Line 171  public final class ByteBufferImpl extend Line 179  public final class ByteBufferImpl extend
179    
180    ByteBufferImpl (float[] copy)    ByteBufferImpl (float[] copy)
181    {    {
182        super (copy.length * 4, copy.length * 4, 0, 0);
183      this.backing_buffer = copy != null ? nio_cast (copy) : null;      this.backing_buffer = copy != null ? nio_cast (copy) : null;
184        readOnly = false;
185    }    }
186        
187    private static native float nio_get_Float (ByteBufferImpl b, int index, int limit);    private static native float nio_get_Float (ByteBufferImpl b, int index, int limit);
# Line 187  public final class ByteBufferImpl extend Line 197  public final class ByteBufferImpl extend
197    
198    ByteBufferImpl (double[] copy)    ByteBufferImpl (double[] copy)
199    {    {
200        super (copy.length * 8, copy.length * 8, 0, 0);
201      this.backing_buffer = copy != null ? nio_cast (copy) : null;      this.backing_buffer = copy != null ? nio_cast (copy) : null;
202        readOnly = false;
203    }    }
204        
205    private static native double nio_get_Double (ByteBufferImpl b, int index, int limit);    private static native double nio_get_Double (ByteBufferImpl b, int index, int limit);
# Line 203  public final class ByteBufferImpl extend Line 215  public final class ByteBufferImpl extend
215    
216    public boolean isReadOnly()    public boolean isReadOnly()
217    {    {
218      return ro;      return readOnly;
219    }    }
220        
221    public ByteBuffer slice()    public ByteBuffer slice()
222    {    {
223      ByteBufferImpl A = new ByteBufferImpl(this);      return new ByteBufferImpl(this);
     A.array_offset = position();  
     return A;  
224    }    }
225    
226    public ByteBuffer duplicate()    public ByteBuffer duplicate()
# Line 221  public final class ByteBufferImpl extend Line 231  public final class ByteBufferImpl extend
231    public ByteBuffer asReadOnlyBuffer()    public ByteBuffer asReadOnlyBuffer()
232    {    {
233      ByteBufferImpl a = new ByteBufferImpl(this);      ByteBufferImpl a = new ByteBufferImpl(this);
234      a.ro = true;      a.readOnly = true;
235      return a;      return a;
236    }    }
237    
# Line 232  public final class ByteBufferImpl extend Line 242  public final class ByteBufferImpl extend
242    
243    public boolean isDirect()    public boolean isDirect()
244    {    {
245      return backing_buffer != null;      return false;
246    }    }
247        
248    final public byte get()    final public byte get()
# Line 244  public final class ByteBufferImpl extend Line 254  public final class ByteBufferImpl extend
254        
255    final public ByteBuffer put(byte b)    final public ByteBuffer put(byte b)
256    {    {
257        if (readOnly)
258          throw new ReadOnlyBufferException ();
259        
260      backing_buffer[position()] = b;      backing_buffer[position()] = b;
261      position(position()+1);      position(position()+1);
262      return this;      return this;
# Line 256  public final class ByteBufferImpl extend Line 269  public final class ByteBufferImpl extend
269        
270    final public ByteBuffer put(int index, byte b)    final public ByteBuffer put(int index, byte b)
271    {    {
272        if (readOnly)
273          throw new ReadOnlyBufferException ();
274        
275      backing_buffer[index] = b;      backing_buffer[index] = b;
276      return this;      return this;
277    }    }
278        
279    final public char getChar() { char a = nio_get_Char(this, position(), limit()); inc_pos(2); return a; } final public ByteBuffer putChar(char value) { nio_put_Char(this, position(), limit(), value); inc_pos(2); return this; } final public char getChar(int index) { char a = nio_get_Char(this, index, limit()); return a; } final public ByteBuffer putChar(int index, char value) { nio_put_Char(this, index, limit(), value); return this; };    final public char getChar ()
280    final public short getShort() { short a = nio_get_Short(this, position(), limit()); inc_pos(2); return a; } final public ByteBuffer putShort(short value) { nio_put_Short(this, position(), limit(), value); inc_pos(2); return this; } final public short getShort(int index) { short a = nio_get_Short(this, index, limit()); return a; } final public ByteBuffer putShort(int index, short value) { nio_put_Short(this, index, limit(), value); return this; };    {
281    final public int getInt() { int a = nio_get_Int(this, position(), limit()); inc_pos(4); return a; } final public ByteBuffer putInt(int value) { nio_put_Int(this, position(), limit(), value); inc_pos(4); return this; } final public int getInt(int index) { int a = nio_get_Int(this, index, limit()); return a; } final public ByteBuffer putInt(int index, int value) { nio_put_Int(this, index, limit(), value); return this; };      char a = nio_get_Char (this, position (), limit ());
282    final public long getLong() { long a = nio_get_Long(this, position(), limit()); inc_pos(8); return a; } final public ByteBuffer putLong(long value) { nio_put_Long(this, position(), limit(), value); inc_pos(8); return this; } final public long getLong(int index) { long a = nio_get_Long(this, index, limit()); return a; } final public ByteBuffer putLong(int index, long value) { nio_put_Long(this, index, limit(), value); return this; };      inc_pos (2);
283    final public float getFloat() { float a = nio_get_Float(this, position(), limit()); inc_pos(4); return a; } final public ByteBuffer putFloat(float value) { nio_put_Float(this, position(), limit(), value); inc_pos(4); return this; } final public float getFloat(int index) { float a = nio_get_Float(this, index, limit()); return a; } final public ByteBuffer putFloat(int index, float value) { nio_put_Float(this, index, limit(), value); return this; };      return a;
284    final public double getDouble() { double a = nio_get_Double(this, position(), limit()); inc_pos(8); return a; } final public ByteBuffer putDouble(double value) { nio_put_Double(this, position(), limit(), value); inc_pos(8); return this; } final public double getDouble(int index) { double a = nio_get_Double(this, index, limit()); return a; } final public ByteBuffer putDouble(int index, double value) { nio_put_Double(this, index, limit(), value); return this; };    }
285      
286      final public ByteBuffer putChar (char value)
287      {
288        if (readOnly)
289          throw new ReadOnlyBufferException ();
290        
291        nio_put_Char (this, position (), limit (), value);
292        inc_pos (2);
293        return this;
294      }
295      
296      final public char getChar (int index)
297      {
298        char a = nio_get_Char (this, index, limit ());
299        return a;
300      }
301      
302      final public ByteBuffer putChar (int index, char value)
303      {
304        if (readOnly)
305          throw new ReadOnlyBufferException ();
306        
307        nio_put_Char (this, index, limit (), value);
308        return this;
309      }
310    
311      final public short getShort ()
312      {
313        short a = nio_get_Short (this, position (), limit ());
314        inc_pos (2);
315        return a;
316      }
317      
318      final public ByteBuffer putShort (short value)
319      {
320        if (readOnly)
321          throw new ReadOnlyBufferException ();
322        
323        nio_put_Short (this, position (), limit (), value);
324        inc_pos (2);
325        return this;
326      }
327      
328      final public short getShort (int index)
329      {
330        short a = nio_get_Short (this, index, limit ());
331        return a;
332      }
333      
334      final public ByteBuffer putShort (int index, short value)
335      {
336        if (readOnly)
337          throw new ReadOnlyBufferException ();
338        
339        nio_put_Short (this, index, limit (), value);
340        return this;
341      }
342    
343      final public int getInt ()
344      {
345        int a = nio_get_Int (this, position (), limit ());
346        inc_pos (4);
347        return a;
348      }
349      
350      final public ByteBuffer putInt (int value)
351      {
352        if (readOnly)
353          throw new ReadOnlyBufferException ();
354        
355        nio_put_Int (this, position (), limit (), value);
356        inc_pos (4);
357        return this;
358      }
359      
360      final public int getInt (int index)
361      {
362        int a = nio_get_Int (this, index, limit ());
363        return a;
364      }
365      
366      final public ByteBuffer putInt (int index, int value)
367      {
368        if (readOnly)
369          throw new ReadOnlyBufferException ();
370        
371        nio_put_Int(this, index, limit (), value);
372        return this;
373      }
374    
375      final public long getLong ()
376      {
377        long a = nio_get_Long (this, position (), limit ());
378        inc_pos (8);
379        return a;
380      }
381      
382      final public ByteBuffer putLong (long value)
383      {
384        if (readOnly)
385          throw new ReadOnlyBufferException ();
386        
387        nio_put_Long (this, position (), limit (), value);
388        inc_pos (8);
389        return this;
390      }
391      
392      final public long getLong (int index)
393      {
394        long a = nio_get_Long (this, index, limit ());
395        return a;
396      }
397      
398      final public ByteBuffer putLong (int index, long value)
399      {
400        if (readOnly)
401          throw new ReadOnlyBufferException ();
402        
403        nio_put_Long (this, index, limit (), value);
404        return this;
405      }
406    
407      final public float getFloat ()
408      {
409        float a = nio_get_Float (this, position (), limit ());
410        inc_pos (4);
411        return a;
412      }
413      
414      final public ByteBuffer putFloat (float value)
415      {
416        if (readOnly)
417          throw new ReadOnlyBufferException ();
418        
419        nio_put_Float (this, position (), limit (), value);
420        inc_pos (4);
421        return this;
422      }
423      
424      final public float getFloat (int index)
425      {
426        float a = nio_get_Float (this, index, limit ());
427        return a;
428      }
429    
430      final public ByteBuffer putFloat (int index, float value)
431      {
432        if (readOnly)
433          throw new ReadOnlyBufferException ();
434        
435        nio_put_Float (this, index, limit(), value);
436        return this;
437      }
438    
439      final public double getDouble ()
440      {
441        double a = nio_get_Double (this, position (), limit ());
442        inc_pos (8);
443        return a;
444      }
445    
446      final public ByteBuffer putDouble (double value)
447      {
448        if (readOnly)
449          throw new ReadOnlyBufferException ();
450        
451        nio_put_Double (this, position(), limit (), value);
452        inc_pos (8);
453        return this;
454      }
455      
456      final public double getDouble (int index)
457      {
458        return nio_get_Double (this, index, limit ());
459      }
460      
461      final public ByteBuffer putDouble (int index, double value)
462      {
463        if (readOnly)
464          throw new ReadOnlyBufferException ();
465        
466        nio_put_Double (this, index, limit (), value);
467        return this;
468      }
469  }  }

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

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