1 /* ByteBufferImpl.java --
2    Copyright (C) 2002, 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 /**
42  * This is a Heap memory implementation
43  */
44 final class ByteBufferImpl extends ByteBuffer
45 {
46   private boolean readOnly;
47 
ByteBufferImpl(int capacity)48   ByteBufferImpl (int capacity)
49   {
50     this (new byte [capacity], 0, capacity, capacity, 0, -1, false);
51   }
52 
ByteBufferImpl(byte[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly)53   ByteBufferImpl (byte[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly)
54   {
55     super (buffer, offset, capacity, limit, position, mark);
56     this.readOnly = readOnly;
57   }
58 
asCharBuffer()59   public CharBuffer asCharBuffer ()
60   {
61     return new CharViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
62   }
63 
asShortBuffer()64   public ShortBuffer asShortBuffer ()
65   {
66     return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
67   }
68 
asIntBuffer()69   public IntBuffer asIntBuffer ()
70   {
71     return new IntViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
72   }
73 
asLongBuffer()74   public LongBuffer asLongBuffer ()
75   {
76     return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
77   }
78 
asFloatBuffer()79   public FloatBuffer asFloatBuffer ()
80   {
81     return new FloatViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
82   }
83 
asDoubleBuffer()84   public DoubleBuffer asDoubleBuffer ()
85   {
86     return new DoubleViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
87   }
88 
isReadOnly()89   public boolean isReadOnly ()
90   {
91     return readOnly;
92   }
93 
slice()94   public ByteBuffer slice ()
95   {
96     return new ByteBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ());
97   }
98 
duplicate()99   public ByteBuffer duplicate ()
100   {
101     return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ());
102   }
103 
asReadOnlyBuffer()104   public ByteBuffer asReadOnlyBuffer ()
105   {
106     return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true);
107   }
108 
compact()109   public ByteBuffer compact ()
110   {
111     int pos = position();
112     if (pos > 0)
113       {
114 	int count = remaining();
115 	shiftDown(0, pos, count);
116 	position(count);
117 	limit(capacity());
118       }
119     return this;
120   }
121 
isDirect()122   public boolean isDirect ()
123   {
124     return false;
125   }
126 
127   /**
128    * Relative get method. Reads the next <code>byte</code> from the buffer.
129    */
get()130   final public byte get ()
131   {
132     byte result = backing_buffer [position ()];
133     position (position () + 1);
134     return result;
135   }
136 
137   /**
138    * Relative put method. Writes <code>value</code> to the next position
139    * in the buffer.
140    *
141    * @exception ReadOnlyBufferException If this buffer is read-only.
142    */
put(byte value)143   final public ByteBuffer put (byte value)
144   {
145     if (readOnly)
146       throw new ReadOnlyBufferException ();
147 
148     backing_buffer [position ()] = value;
149     position (position () + 1);
150     return this;
151   }
152 
153   /**
154    * Absolute get method. Reads the <code>byte</code> at position
155    * <code>index</code>.
156    *
157    * @exception IndexOutOfBoundsException If index is negative or not smaller
158    * than the buffer's limit.
159    */
get(int index)160   final public byte get (int index)
161   {
162     return backing_buffer [index];
163   }
164 
165   /**
166    * Absolute put method. Writes <code>value</value> to position
167    * <code>index</code> in the buffer.
168    *
169    * @exception IndexOutOfBoundsException If index is negative or not smaller
170    * than the buffer's limit.
171    * @exception ReadOnlyBufferException If this buffer is read-only.
172    */
put(int index, byte value)173   final public ByteBuffer put (int index, byte value)
174   {
175     if (readOnly)
176       throw new ReadOnlyBufferException ();
177 
178     backing_buffer [index] = value;
179     return this;
180   }
181 
getChar()182   final public char getChar ()
183   {
184     return ByteBufferHelper.getChar(this, order());
185   }
186 
putChar(char value)187   final public ByteBuffer putChar (char value)
188   {
189     ByteBufferHelper.putChar(this, value, order());
190     return this;
191   }
192 
getChar(int index)193   final public char getChar (int index)
194   {
195     return ByteBufferHelper.getChar(this, index, order());
196   }
197 
putChar(int index, char value)198   final public ByteBuffer putChar (int index, char value)
199   {
200     ByteBufferHelper.putChar(this, index, value, order());
201     return this;
202   }
203 
getShort()204   final public short getShort ()
205   {
206     return ByteBufferHelper.getShort(this, order());
207   }
208 
putShort(short value)209   final public ByteBuffer putShort (short value)
210   {
211     ByteBufferHelper.putShort(this, value, order());
212     return this;
213   }
214 
getShort(int index)215   final public short getShort (int index)
216   {
217     return ByteBufferHelper.getShort(this, index, order());
218   }
219 
putShort(int index, short value)220   final public ByteBuffer putShort (int index, short value)
221   {
222     ByteBufferHelper.putShort(this, index, value, order());
223     return this;
224   }
225 
getInt()226   final public int getInt ()
227   {
228     return ByteBufferHelper.getInt(this, order());
229   }
230 
putInt(int value)231   final public ByteBuffer putInt (int value)
232   {
233     ByteBufferHelper.putInt(this, value, order());
234     return this;
235   }
236 
getInt(int index)237   final public int getInt (int index)
238   {
239     return ByteBufferHelper.getInt(this, index, order());
240   }
241 
putInt(int index, int value)242   final public ByteBuffer putInt (int index, int value)
243   {
244     ByteBufferHelper.putInt(this, index, value, order());
245     return this;
246   }
247 
getLong()248   final public long getLong ()
249   {
250     return ByteBufferHelper.getLong(this, order());
251   }
252 
putLong(long value)253   final public ByteBuffer putLong (long value)
254   {
255     ByteBufferHelper.putLong (this, value, order());
256     return this;
257   }
258 
getLong(int index)259   final public long getLong (int index)
260   {
261     return ByteBufferHelper.getLong (this, index, order());
262   }
263 
putLong(int index, long value)264   final public ByteBuffer putLong (int index, long value)
265   {
266     ByteBufferHelper.putLong (this, index, value, order());
267     return this;
268   }
269 
getFloat()270   final public float getFloat ()
271   {
272     return ByteBufferHelper.getFloat (this, order());
273   }
274 
putFloat(float value)275   final public ByteBuffer putFloat (float value)
276   {
277     ByteBufferHelper.putFloat (this, value, order());
278     return this;
279   }
280 
getFloat(int index)281   public final float getFloat (int index)
282   {
283     return ByteBufferHelper.getFloat (this, index, order());
284   }
285 
putFloat(int index, float value)286   final public ByteBuffer putFloat (int index, float value)
287   {
288     ByteBufferHelper.putFloat (this, index, value, order());
289     return this;
290   }
291 
getDouble()292   final public double getDouble ()
293   {
294     return ByteBufferHelper.getDouble (this, order());
295   }
296 
putDouble(double value)297   final public ByteBuffer putDouble (double value)
298   {
299     ByteBufferHelper.putDouble (this, value, order());
300     return this;
301   }
302 
getDouble(int index)303   final public double getDouble (int index)
304   {
305     return ByteBufferHelper.getDouble (this, index, order());
306   }
307 
putDouble(int index, double value)308   final public ByteBuffer putDouble (int index, double value)
309   {
310     ByteBufferHelper.putDouble (this, index, value, order());
311     return this;
312   }
313 }
314