1 |
/* IntBufferImpl.java -- |
/* IntBufferImpl.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 |
|
|
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; |
49 |
public final class IntBufferImpl extends IntBuffer |
public final class IntBufferImpl extends IntBuffer |
50 |
{ |
{ |
51 |
private boolean readOnly; |
private boolean readOnly; |
|
|
|
|
public IntBufferImpl(int cap, int off, int lim) |
|
|
{ |
|
|
super (cap, lim, off, 0); |
|
|
this.backing_buffer = new int[cap]; |
|
|
readOnly = false; |
|
|
} |
|
|
|
|
|
public IntBufferImpl(int[] array, int offset, int length) |
|
|
{ |
|
|
super (array.length, length, offset, 0); |
|
|
this.backing_buffer = array; |
|
|
readOnly = false; |
|
|
} |
|
|
|
|
|
public IntBufferImpl(IntBufferImpl copy) |
|
|
{ |
|
|
super (copy.capacity (), copy.limit (), copy.position (), 0); |
|
|
backing_buffer = copy.backing_buffer; |
|
|
readOnly = copy.isReadOnly (); |
|
|
} |
|
52 |
|
|
53 |
private static native int[] nio_cast (byte[] copy); |
public IntBufferImpl (int capacity) |
|
|
|
|
IntBufferImpl (byte[] copy) |
|
54 |
{ |
{ |
55 |
super (copy.length, copy.length, 0, 0); |
this (new int [capacity], 0, capacity, capacity, 0, -1, false); |
|
this.backing_buffer = copy != null ? nio_cast (copy) : null; |
|
|
readOnly = false; |
|
56 |
} |
} |
57 |
|
|
58 |
private static native byte nio_get_Byte (IntBufferImpl b, int index, int limit); |
public IntBufferImpl (int[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly) |
|
|
|
|
private static native void nio_put_Byte (IntBufferImpl b, int index, int limit, byte value); |
|
|
|
|
|
public ByteBuffer asByteBuffer () |
|
59 |
{ |
{ |
60 |
ByteBufferImpl res = new ByteBufferImpl (backing_buffer); |
super (buffer, offset, capacity, limit, position, mark); |
61 |
res.limit ((limit () * 1) / 4); |
this.readOnly = readOnly; |
|
return res; |
|
62 |
} |
} |
63 |
|
|
64 |
public boolean isReadOnly() |
public boolean isReadOnly () |
65 |
{ |
{ |
66 |
return readOnly; |
return readOnly; |
67 |
} |
} |
68 |
|
|
69 |
public IntBuffer slice() |
public IntBuffer slice () |
70 |
{ |
{ |
71 |
return new IntBufferImpl (this); |
return new IntBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ()); |
72 |
} |
} |
73 |
|
|
74 |
public IntBuffer duplicate() |
public IntBuffer duplicate () |
75 |
{ |
{ |
76 |
return new IntBufferImpl(this); |
return new IntBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ()); |
77 |
} |
} |
78 |
|
|
79 |
public IntBuffer asReadOnlyBuffer() |
public IntBuffer asReadOnlyBuffer () |
80 |
{ |
{ |
81 |
IntBufferImpl result = new IntBufferImpl (this); |
return new IntBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true); |
|
result.readOnly = true; |
|
|
return result; |
|
82 |
} |
} |
83 |
|
|
84 |
public IntBuffer compact() |
public IntBuffer compact () |
85 |
{ |
{ |
86 |
|
int copied = 0; |
87 |
|
|
88 |
|
while (remaining () > 0) |
89 |
|
{ |
90 |
|
put (copied, get ()); |
91 |
|
copied++; |
92 |
|
} |
93 |
|
|
94 |
|
position (copied); |
95 |
return this; |
return this; |
96 |
} |
} |
97 |
|
|
98 |
public boolean isDirect() |
public boolean isDirect () |
99 |
{ |
{ |
100 |
return false; |
return false; |
101 |
} |
} |
102 |
|
|
103 |
final public int get() |
/** |
104 |
|
* Relative get method. Reads the next <code>int</code> from the buffer. |
105 |
|
*/ |
106 |
|
final public int get () |
107 |
{ |
{ |
108 |
int e = backing_buffer[position()]; |
int result = backing_buffer [position ()]; |
109 |
position(position()+1); |
position (position () + 1); |
110 |
return e; |
return result; |
111 |
} |
} |
112 |
|
|
113 |
final public IntBuffer put(int b) |
/** |
114 |
|
* Relative put method. Writes <code>value</code> to the next position |
115 |
|
* in the buffer. |
116 |
|
* |
117 |
|
* @exception ReadOnlyBufferException If this buffer is read-only. |
118 |
|
*/ |
119 |
|
final public IntBuffer put (int value) |
120 |
{ |
{ |
121 |
if (readOnly) |
if (readOnly) |
122 |
throw new ReadOnlyBufferException (); |
throw new ReadOnlyBufferException (); |
123 |
|
|
124 |
backing_buffer[position()] = b; |
backing_buffer [position ()] = value; |
125 |
position(position()+1); |
position (position () + 1); |
126 |
return this; |
return this; |
127 |
} |
} |
128 |
|
|
129 |
final public int get(int index) |
/** |
130 |
|
* Absolute get method. Reads the <code>int</code> at position |
131 |
|
* <code>index</code>. |
132 |
|
* |
133 |
|
* @exception IndexOutOfBoundsException If index is negative or not smaller |
134 |
|
* than the buffer's limit. |
135 |
|
*/ |
136 |
|
final public int get (int index) |
137 |
{ |
{ |
138 |
return backing_buffer[index]; |
return backing_buffer [index]; |
139 |
} |
} |
140 |
|
|
141 |
final public IntBuffer put(int index, int b) |
/** |
142 |
|
* Absolute put method. Writes <code>value</value> to position |
143 |
|
* <code>index</code> in the buffer. |
144 |
|
* |
145 |
|
* @exception IndexOutOfBoundsException If index is negative or not smaller |
146 |
|
* than the buffer's limit. |
147 |
|
* @exception ReadOnlyBufferException If this buffer is read-only. |
148 |
|
*/ |
149 |
|
final public IntBuffer put (int index, int value) |
150 |
{ |
{ |
151 |
if (readOnly) |
if (readOnly) |
152 |
throw new ReadOnlyBufferException (); |
throw new ReadOnlyBufferException (); |
153 |
|
|
154 |
backing_buffer[index] = b; |
backing_buffer [index] = value; |
155 |
return this; |
return this; |
156 |
} |
} |
157 |
|
|
158 |
final public ByteOrder order () |
final public ByteOrder order () |
159 |
{ |
{ |
160 |
return ByteOrder.BIG_ENDIAN; |
return ByteOrder.nativeOrder (); |
161 |
} |
} |
162 |
} |
} |