OSDN Git Service

2005-01-07 Michael Koch <konqueror@gmx.de>
[pf3gnuchains/gcc-fork.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 gnu.gcj.RawData;
42
43 import java.io.IOException;
44
45 final class MappedByteBufferImpl extends MappedByteBuffer
46 {
47   boolean readOnly;
48
49   /** Posix uses this for the pointer returned by mmap;
50    * Win32 uses it for the pointer returned by MapViewOfFile. */
51   public RawData implPtr;
52   /** Posix uses this for the actual length passed to mmap;
53    * Win32 uses it for the pointer returned by CreateFileMapping. */
54   public long implLen;
55   
56   public MappedByteBufferImpl(RawData address, int size, boolean readOnly)
57     throws IOException
58   {
59     super(size, size, 0, -1);
60     this.address = address;
61     this.readOnly = readOnly;
62   }
63
64   public boolean isReadOnly()
65   {
66     return readOnly;
67   }
68   
69   public byte get()
70   {
71     checkForUnderflow();
72
73     int pos = position();
74     byte result = VMDirectByteBuffer.get(address, pos);
75     position(pos + 1);
76     return result;
77   }
78
79   public ByteBuffer put(byte value)
80   {
81     checkIfReadOnly();
82     checkForOverflow();
83
84     int pos = position();
85     VMDirectByteBuffer.put(address, pos, value);
86     position(pos + 1);
87     return this;
88   }
89
90   public byte get(int index)
91   {
92     checkIndex(index);
93
94     return VMDirectByteBuffer.get(address, index);
95   }
96
97   public ByteBuffer get(byte[] dst, int offset, int length)
98   {
99     checkArraySize(dst.length, offset, length);
100     checkForUnderflow(length);
101
102     int index = position();
103     VMDirectByteBuffer.get(address, index, dst, offset, length);
104     position(index+length);
105
106     return this;
107   }
108
109   public ByteBuffer put(int index, byte value)
110   {
111     checkIfReadOnly();
112     checkIndex(index);
113
114     VMDirectByteBuffer.put(address, index, value);
115     return this;
116   }
117
118   public ByteBuffer compact()
119   {
120     int pos = position();
121     if (pos > 0)
122       {
123         int count = remaining();
124         // Call shiftDown method optimized for direct buffers.
125         VMDirectByteBuffer.shiftDown(address, 0, pos, count);
126         position(count);
127         limit(capacity());
128       }
129     return this;
130   }
131
132   public boolean isDirect()
133   {
134     return true;
135   }
136
137   public ByteBuffer slice()
138   {
139     int rem = remaining();
140     if (isReadOnly())
141         return new DirectByteBufferImpl.ReadOnly
142       (this, VMDirectByteBuffer.adjustAddress(address, position()),
143        rem, rem, 0);
144     else
145         return new DirectByteBufferImpl.ReadWrite
146       (this, VMDirectByteBuffer.adjustAddress(address, position()),
147        rem, rem, 0);
148   }
149
150   private ByteBuffer duplicate(boolean readOnly)
151   {
152     int pos = position();
153     reset();
154     int mark = position();
155     position(pos);
156     DirectByteBufferImpl result;
157     if (readOnly)
158         result = new DirectByteBufferImpl.ReadOnly(this, address, capacity(),
159                                                    limit(), pos);
160     else
161         result = new DirectByteBufferImpl.ReadWrite(this, address, capacity(),
162                                                     limit(), pos);
163
164     if (mark != pos)
165       {
166         result.position(mark);
167         result.mark();
168         result.position(pos);
169       }
170     return result;
171   }
172
173   public ByteBuffer duplicate()
174   {
175     return duplicate(isReadOnly());
176   }
177
178   public ByteBuffer asReadOnlyBuffer()
179   {
180     return duplicate(true);
181   }
182
183   public CharBuffer asCharBuffer()
184   {
185     return new CharViewBufferImpl(this, remaining() >> 1);
186   }
187
188   public ShortBuffer asShortBuffer()
189   {
190     return new ShortViewBufferImpl(this, remaining() >> 1);
191   }
192
193   public IntBuffer asIntBuffer()
194   {
195     return new IntViewBufferImpl(this, remaining() >> 2);
196   }
197
198   public LongBuffer asLongBuffer()
199   {
200     return new LongViewBufferImpl(this, remaining() >> 3);
201   }
202
203   public FloatBuffer asFloatBuffer()
204   {
205     return new FloatViewBufferImpl(this, remaining() >> 2);
206   }
207
208   public DoubleBuffer asDoubleBuffer()
209   {
210     return new DoubleViewBufferImpl(this, remaining() >> 3);
211   }
212
213   public char getChar()
214   {
215     return ByteBufferHelper.getChar(this, order());
216   }
217   
218   public ByteBuffer putChar(char value)
219   {
220     ByteBufferHelper.putChar(this, value, order());
221     return this;
222   }
223   
224   public char getChar(int index)
225   {
226     return ByteBufferHelper.getChar(this, index, order());
227   }
228   
229   public ByteBuffer putChar(int index, char value)
230   {
231     ByteBufferHelper.putChar(this, index, value, order());
232     return this;
233   }
234
235   public short getShort()
236   {
237     return ByteBufferHelper.getShort(this, order());
238   }
239   
240   public ByteBuffer putShort(short value)
241   {
242     ByteBufferHelper.putShort(this, value, order());
243     return this;
244   }
245   
246   public short getShort(int index)
247   {
248     return ByteBufferHelper.getShort(this, index, order());
249   }
250   
251   public ByteBuffer putShort(int index, short value)
252   {
253     ByteBufferHelper.putShort(this, index, value, order());
254     return this;
255   }
256
257   public int getInt()
258   {
259     return ByteBufferHelper.getInt(this, order());
260   }
261   
262   public ByteBuffer putInt(int value)
263   {
264     ByteBufferHelper.putInt(this, value, order());
265     return this;
266   }
267   
268   public int getInt(int index)
269   {
270     return ByteBufferHelper.getInt(this, index, order());
271   }
272   
273   public ByteBuffer putInt(int index, int value)
274   {
275     ByteBufferHelper.putInt(this, index, value, order());
276     return this;
277   }
278
279   public long getLong()
280   {
281     return ByteBufferHelper.getLong(this, order());
282   }
283   
284   public ByteBuffer putLong(long value)
285   {
286     ByteBufferHelper.putLong(this, value, order());
287     return this;
288   }
289   
290   public long getLong(int index)
291   {
292     return ByteBufferHelper.getLong(this, index, order());
293   }
294   
295   public ByteBuffer putLong(int index, long value)
296   {
297     ByteBufferHelper.putLong(this, index, value, order());
298     return this;
299   }
300
301   public float getFloat()
302   {
303     return ByteBufferHelper.getFloat(this, order());
304   }
305   
306   public ByteBuffer putFloat(float value)
307   {
308     ByteBufferHelper.putFloat(this, value, order());
309     return this;
310   }
311   
312   public float getFloat(int index)
313   {
314     return ByteBufferHelper.getFloat(this, index, order());
315   }
316
317   public ByteBuffer putFloat(int index, float value)
318   {
319     ByteBufferHelper.putFloat(this, index, value, order());
320     return this;
321   }
322
323   public double getDouble()
324   {
325     return ByteBufferHelper.getDouble(this, order());
326   }
327
328   public ByteBuffer putDouble(double value)
329   {
330     ByteBufferHelper.putDouble(this, value, order());
331     return this;
332   }
333   
334   public double getDouble(int index)
335   {
336     return ByteBufferHelper.getDouble(this, index, order());
337   }
338   
339   public ByteBuffer putDouble(int index, double value)
340   {
341     ByteBufferHelper.putDouble(this, index, value, order());
342     return this;
343   }
344
345   // NOTE: In libgcj these methods are implemented in natFileChannelXxx.cc,
346   // because they're small, and to put them next to FileChannelImpl::mapImpl.
347   native void unmapImpl();
348   native boolean isLoadedImpl();
349     // FIXME: Try to load all pages into memory.
350   native void loadImpl();
351
352   native void forceImpl();
353 }