OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [java/] [nio/] [DirectByteBufferImpl.java] - Blame information for rev 771

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 771 jeremybenn
/* DirectByteBufferImpl.java --
2
   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
 
39
package java.nio;
40
 
41
import gnu.classpath.Pointer;
42
 
43
abstract class DirectByteBufferImpl extends ByteBuffer
44
{
45
  /**
46
   * The owner is used to keep alive the object that actually owns the
47
   * memory. There are three possibilities:
48
   *  1) owner == this: We allocated the memory and we should free it,
49
   *                    but *only* in finalize (if we've been sliced
50
   *                    other objects will also have access to the
51
   *                    memory).
52
   *  2) owner == null: The byte buffer was created thru
53
   *                    JNI.NewDirectByteBuffer. The JNI code is
54
   *                    responsible for freeing the memory.
55
   *  3) owner == some other object: The other object allocated the
56
   *                                 memory and should free it.
57
   */
58
  private final Object owner;
59
 
60
  static final class ReadOnly extends DirectByteBufferImpl
61
  {
62
    ReadOnly(Object owner, Pointer address,
63
             int capacity, int limit,
64
             int position)
65
    {
66
      super(owner, address, capacity, limit, position);
67
    }
68
 
69
    public ByteBuffer put(byte value)
70
    {
71
      throw new ReadOnlyBufferException ();
72
    }
73
 
74
    public ByteBuffer put(int index, byte value)
75
    {
76
      throw new ReadOnlyBufferException ();
77
    }
78
 
79
    public boolean isReadOnly()
80
    {
81
      return true;
82
    }
83
  }
84
 
85
  static final class ReadWrite extends DirectByteBufferImpl
86
  {
87
    ReadWrite(int capacity)
88
    {
89
      super(capacity);
90
    }
91
 
92
    ReadWrite(Pointer address, int capacity)
93
    {
94
      super(address, capacity);
95
    }
96
 
97
    ReadWrite(Object owner, Pointer address,
98
              int capacity, int limit,
99
              int position)
100
    {
101
      super(owner, address, capacity, limit, position);
102
    }
103
 
104
    public boolean isReadOnly()
105
    {
106
      return false;
107
    }
108
  }
109
 
110
  DirectByteBufferImpl(int capacity)
111
  {
112
    super(capacity, capacity, 0, -1,
113
          VMDirectByteBuffer.allocate(capacity), null, 0);
114
    this.owner = this;
115
  }
116
 
117
  DirectByteBufferImpl(Pointer address, int capacity)
118
  {
119
    super(capacity, capacity, 0, -1, address, null, 0);
120
    this.owner = null;
121
  }
122
 
123
  DirectByteBufferImpl(Object owner, Pointer address,
124
                       int capacity, int limit,
125
                       int position)
126
  {
127
    super(capacity, limit, position, -1, address, null, 0);
128
    this.owner = owner;
129
  }
130
 
131
  /**
132
   * Allocates a new direct byte buffer.
133
   */
134
  public static ByteBuffer allocate(int capacity)
135
  {
136
    return new DirectByteBufferImpl.ReadWrite(capacity);
137
  }
138
 
139
  protected void finalize() throws Throwable
140
  {
141
    if (owner == this)
142
        VMDirectByteBuffer.free(address);
143
  }
144
 
145
  public byte get()
146
  {
147
    checkForUnderflow();
148
 
149
    int pos = position();
150
    byte result = VMDirectByteBuffer.get(address, pos);
151
    position(pos + 1);
152
    return result;
153
  }
154
 
155
  public byte get(int index)
156
  {
157
    checkIndex(index);
158
 
159
    return VMDirectByteBuffer.get(address, index);
160
  }
161
 
162
  public ByteBuffer get(byte[] dst, int offset, int length)
163
  {
164
    checkArraySize(dst.length, offset, length);
165
    checkForUnderflow(length);
166
 
167
    int index = position();
168
    VMDirectByteBuffer.get(address, index, dst, offset, length);
169
    position(index+length);
170
 
171
    return this;
172
  }
173
 
174
  public ByteBuffer put(byte value)
175
  {
176
    checkForOverflow();
177
 
178
    int pos = position();
179
    VMDirectByteBuffer.put(address, pos, value);
180
    position(pos + 1);
181
    return this;
182
  }
183
 
184
  public ByteBuffer put(int index, byte value)
185
  {
186
    checkIndex(index);
187
 
188
    VMDirectByteBuffer.put(address, index, value);
189
    return this;
190
  }
191
 
192
  public ByteBuffer put (byte[] src, int offset, int length)
193
  {
194
    checkArraySize (src.length, offset, length);
195
    checkForUnderflow (length);
196
    int index = position ();
197
    VMDirectByteBuffer.put (address, index, src, offset, length);
198
    position (index + length);
199
 
200
    return this;
201
  }
202
 
203
  void shiftDown(int dst_offset, int src_offset, int count)
204
  {
205
    VMDirectByteBuffer.shiftDown(address, dst_offset, src_offset, count);
206
  }
207
 
208
  public ByteBuffer compact()
209
  {
210
    checkIfReadOnly();
211
    mark = -1;
212
    int pos = position();
213
    if (pos > 0)
214
      {
215
        int count = remaining();
216
        VMDirectByteBuffer.shiftDown(address, 0, pos, count);
217
        position(count);
218
        limit(capacity());
219
      }
220
    else
221
      {
222
        position(limit());
223
        limit(capacity());
224
      }
225
    return this;
226
  }
227
 
228
  public ByteBuffer slice()
229
  {
230
    int rem = remaining();
231
    if (isReadOnly())
232
        return new DirectByteBufferImpl.ReadOnly
233
      (owner, VMDirectByteBuffer.adjustAddress(address, position()),
234
       rem, rem, 0);
235
    else
236
        return new DirectByteBufferImpl.ReadWrite
237
      (owner, VMDirectByteBuffer.adjustAddress(address, position()),
238
       rem, rem, 0);
239
  }
240
 
241
  private ByteBuffer duplicate(boolean readOnly)
242
  {
243
    int pos = position();
244
    if (this.mark != -1)
245
      reset();
246
    int mark = position();
247
    position(pos);
248
    DirectByteBufferImpl result;
249
    if (readOnly)
250
        result = new DirectByteBufferImpl.ReadOnly(owner, address, capacity(),
251
                                                   limit(), pos);
252
    else
253
        result = new DirectByteBufferImpl.ReadWrite(owner, address, capacity(),
254
                                                    limit(), pos);
255
 
256
    if (mark != pos)
257
      {
258
        result.position(mark);
259
        result.mark();
260
        result.position(pos);
261
      }
262
    return result;
263
  }
264
 
265
  public ByteBuffer duplicate()
266
  {
267
    return duplicate(isReadOnly());
268
  }
269
 
270
  public ByteBuffer asReadOnlyBuffer()
271
  {
272
    return duplicate(true);
273
  }
274
 
275
  public boolean isDirect()
276
  {
277
    return true;
278
  }
279
 
280
  public CharBuffer asCharBuffer()
281
  {
282
    return new CharViewBufferImpl(this, remaining() >> 1);
283
  }
284
 
285
  public ShortBuffer asShortBuffer()
286
  {
287
    return new ShortViewBufferImpl(this, remaining() >> 1);
288
  }
289
 
290
  public IntBuffer asIntBuffer()
291
  {
292
    return new IntViewBufferImpl(this, remaining() >> 2);
293
  }
294
 
295
  public LongBuffer asLongBuffer()
296
  {
297
    return new LongViewBufferImpl(this, remaining() >> 3);
298
  }
299
 
300
  public FloatBuffer asFloatBuffer()
301
  {
302
    return new FloatViewBufferImpl(this, remaining() >> 2);
303
  }
304
 
305
  public DoubleBuffer asDoubleBuffer()
306
  {
307
    return new DoubleViewBufferImpl(this, remaining() >> 3);
308
  }
309
 
310
  public char getChar()
311
  {
312
    return ByteBufferHelper.getChar(this, order());
313
  }
314
 
315
  public ByteBuffer putChar(char value)
316
  {
317
    ByteBufferHelper.putChar(this, value, order());
318
    return this;
319
  }
320
 
321
  public char getChar(int index)
322
  {
323
    return ByteBufferHelper.getChar(this, index, order());
324
  }
325
 
326
  public ByteBuffer putChar(int index, char value)
327
  {
328
    ByteBufferHelper.putChar(this, index, value, order());
329
    return this;
330
  }
331
 
332
  public short getShort()
333
  {
334
    return ByteBufferHelper.getShort(this, order());
335
  }
336
 
337
  public ByteBuffer putShort(short value)
338
  {
339
    ByteBufferHelper.putShort(this, value, order());
340
    return this;
341
  }
342
 
343
  public short getShort(int index)
344
  {
345
    return ByteBufferHelper.getShort(this, index, order());
346
  }
347
 
348
  public ByteBuffer putShort(int index, short value)
349
  {
350
    ByteBufferHelper.putShort(this, index, value, order());
351
    return this;
352
  }
353
 
354
  public int getInt()
355
  {
356
    return ByteBufferHelper.getInt(this, order());
357
  }
358
 
359
  public ByteBuffer putInt(int value)
360
  {
361
    ByteBufferHelper.putInt(this, value, order());
362
    return this;
363
  }
364
 
365
  public int getInt(int index)
366
  {
367
    return ByteBufferHelper.getInt(this, index, order());
368
  }
369
 
370
  public ByteBuffer putInt(int index, int value)
371
  {
372
    ByteBufferHelper.putInt(this, index, value, order());
373
    return this;
374
  }
375
 
376
  public long getLong()
377
  {
378
    return ByteBufferHelper.getLong(this, order());
379
  }
380
 
381
  public ByteBuffer putLong(long value)
382
  {
383
    ByteBufferHelper.putLong(this, value, order());
384
    return this;
385
  }
386
 
387
  public long getLong(int index)
388
  {
389
    return ByteBufferHelper.getLong(this, index, order());
390
  }
391
 
392
  public ByteBuffer putLong(int index, long value)
393
  {
394
    ByteBufferHelper.putLong(this, index, value, order());
395
    return this;
396
  }
397
 
398
  public float getFloat()
399
  {
400
    return ByteBufferHelper.getFloat(this, order());
401
  }
402
 
403
  public ByteBuffer putFloat(float value)
404
  {
405
    ByteBufferHelper.putFloat(this, value, order());
406
    return this;
407
  }
408
 
409
  public float getFloat(int index)
410
  {
411
    return ByteBufferHelper.getFloat(this, index, order());
412
  }
413
 
414
  public ByteBuffer putFloat(int index, float value)
415
  {
416
    ByteBufferHelper.putFloat(this, index, value, order());
417
    return this;
418
  }
419
 
420
  public double getDouble()
421
  {
422
    return ByteBufferHelper.getDouble(this, order());
423
  }
424
 
425
  public ByteBuffer putDouble(double value)
426
  {
427
    ByteBufferHelper.putDouble(this, value, order());
428
    return this;
429
  }
430
 
431
  public double getDouble(int index)
432
  {
433
    return ByteBufferHelper.getDouble(this, index, order());
434
  }
435
 
436
  public ByteBuffer putDouble(int index, double value)
437
  {
438
    ByteBufferHelper.putDouble(this, index, value, order());
439
    return this;
440
  }
441
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.