MappedByteBuffer.java: (forceImpl...

* java/nio/MappedByteBuffer.java: (forceImpl, isLoadedImpl, loadImpl,
	unmapImpl):  New dummy methods, to be overridden by subclass.
	(finalize, isLoaded, load, force):  New methods.
	* java/nio/MappedByteBufferImpl.java:  More-or-less rewrite.
	Now works, at least for read mapping.

From-SVN: r78662
This commit is contained in:
Per Bothner 2004-02-29 11:12:45 -08:00
parent ef3916ef8e
commit d199feb748
2 changed files with 103 additions and 47 deletions

View file

@ -44,28 +44,46 @@ package java.nio;
*/ */
public abstract class MappedByteBuffer extends ByteBuffer public abstract class MappedByteBuffer extends ByteBuffer
{ {
private boolean loaded = false;
MappedByteBuffer (int capacity, int limit, int position, int mark) MappedByteBuffer (int capacity, int limit, int position, int mark)
{ {
super (capacity, limit, position, mark); super (capacity, limit, position, mark);
} }
void forceImpl()
{
}
public final MappedByteBuffer force () public final MappedByteBuffer force ()
{ {
// FIXME: Flush to disk here. forceImpl();
return this; return this;
} }
boolean isLoadedImpl()
{
load();
return true;
}
public final boolean isLoaded () public final boolean isLoaded ()
{ {
return loaded; return isLoadedImpl();
}
void loadImpl()
{
} }
public final MappedByteBuffer load () public final MappedByteBuffer load ()
{ {
// FIXME: Try to load all pages into memory. loadImpl();
loaded = true;
return this; return this;
} }
void unmapImpl ()
{
forceImpl();
}
public void finalize () { unmapImpl(); }
} }

View file

@ -39,31 +39,26 @@ exception statement from your version. */
package java.nio; package java.nio;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.FileChannelImpl; import gnu.java.nio.channels.FileChannelImpl;
import gnu.gcj.RawData; import gnu.gcj.RawData;
public class MappedByteBufferImpl extends MappedByteBuffer class MappedByteBufferImpl extends MappedByteBuffer
{ {
boolean readOnly; boolean readOnly;
RawData map_address; RawData address;
public FileChannelImpl ch;
public MappedByteBufferImpl (FileChannelImpl ch) throws IOException /** Posix uses this for the pointer returned by mmap;
* Win32 uses it for the pointer returned by MapViewOfFile. */
public RawData implPtr;
/** Posix uses this for the actual length passed to mmap;
* Win32 uses it for the pointer returned by CreateFileMapping. */
public long implLen;
public MappedByteBufferImpl (RawData address, int size, boolean readOnly)
throws IOException
{ {
super ((int) ch.size (), (int) ch.size (), 0, -1); super(size, size, 0, -1);
this.address = address;
this.ch = ch;
map_address = ch.map_address;
long si = ch.size () / 1;
limit ((int) si);
}
public MappedByteBufferImpl (FileChannelImpl ch, int offset, int capacity, int limit, int position, int mark, boolean readOnly)
{
super (capacity, limit, position, mark);
this.ch = ch;
this.array_offset = offset;
this.readOnly = readOnly; this.readOnly = readOnly;
} }
@ -72,40 +67,52 @@ public class MappedByteBufferImpl extends MappedByteBuffer
return readOnly; return readOnly;
} }
public static byte getImpl (FileChannelImpl ch, int index,
int limit, RawData map_address)
{
throw new Error ("Not implemented");
}
public static void putImpl (FileChannelImpl ch, int index,
int limit, byte value, RawData map_address)
{
throw new Error ("Not implemented");
}
public byte get () public byte get ()
{ {
byte result = get (position()); int pos = position();
position (position() + 1); if (pos >= limit())
throw new BufferUnderflowException();
byte result = DirectByteBufferImpl.getImpl(address, pos);
position (pos + 1);
return result; return result;
} }
public ByteBuffer put (byte value) public ByteBuffer put (byte value)
{ {
put (position(), value); int pos = position();
position (position() + 1); if (pos >= limit())
throw new BufferUnderflowException();
DirectByteBufferImpl.putImpl(address, pos, value);
position(pos + 1);
return this; return this;
} }
public byte get (int index) public byte get (int index)
{ {
return getImpl (ch, index, limit (), map_address); if (index >= limit())
throw new BufferUnderflowException();
return DirectByteBufferImpl.getImpl(address, index);
}
public ByteBuffer get (byte[] dst, int offset, int length)
{
if (offset < 0 || length < 0 || offset + length > dst.length)
throw new IndexOutOfBoundsException ();
if (length > remaining())
throw new BufferUnderflowException();
int index = position();
DirectByteBufferImpl.getImpl(address, index, dst, offset, length);
position(index+length);
return this;
} }
public ByteBuffer put (int index, byte value) public ByteBuffer put (int index, byte value)
{ {
putImpl (ch, index, limit (), value, map_address); if (index >= limit())
throw new BufferUnderflowException();
DirectByteBufferImpl.putImpl(address, index, value);
return this; return this;
} }
@ -129,17 +136,39 @@ public class MappedByteBufferImpl extends MappedByteBuffer
public ByteBuffer slice () public ByteBuffer slice ()
{ {
throw new Error ("Not implemented"); int rem = remaining();
return new DirectByteBufferImpl (this,
DirectByteBufferImpl
.adjustAddress(address, position()),
rem, rem, 0, isReadOnly ());
}
private ByteBuffer duplicate (boolean readOnly)
{
int pos = position();
reset();
int mark = position();
position(pos);
DirectByteBufferImpl result
= new DirectByteBufferImpl (this, address, capacity (), limit (),
pos, readOnly);
if (mark != pos)
{
result.position(mark);
result.mark();
result.position(pos);
}
return result;
} }
public ByteBuffer duplicate () public ByteBuffer duplicate ()
{ {
throw new Error ("Not implemented"); return duplicate(isReadOnly());
} }
public ByteBuffer asReadOnlyBuffer () public ByteBuffer asReadOnlyBuffer ()
{ {
throw new Error ("Not implemented"); return duplicate(true);
} }
public CharBuffer asCharBuffer () public CharBuffer asCharBuffer ()
@ -303,4 +332,13 @@ public class MappedByteBufferImpl extends MappedByteBuffer
ByteBufferHelper.putDouble (this, index, value, order()); ByteBufferHelper.putDouble (this, index, value, order());
return this; return this;
} }
// NOTE: In libgcj these methods are implemented in natFileChannelXxx.cc,
// because they're small, and to put them next to FileChannelImpl::mapImpl.
native void unmapImpl ();
native boolean isLoadedImpl ();
// FIXME: Try to load all pages into memory.
native void loadImpl();
native void forceImpl();
} }