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