use of java.nio.DirectByteBuffer in project j2objc by google.
the class FileChannelImpl method map.
public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException {
ensureOpen();
if (mode == null)
throw new NullPointerException("Mode is null");
if (position < 0L)
throw new IllegalArgumentException("Negative position");
if (size < 0L)
throw new IllegalArgumentException("Negative size");
if (position + size < 0)
throw new IllegalArgumentException("Position + size overflow");
if (size > Integer.MAX_VALUE)
throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
int imode = -1;
if (mode == MapMode.READ_ONLY)
imode = MAP_RO;
else if (mode == MapMode.READ_WRITE)
imode = MAP_RW;
else if (mode == MapMode.PRIVATE)
imode = MAP_PV;
assert (imode >= 0);
if ((mode != MapMode.READ_ONLY) && !writable)
throw new NonWritableChannelException();
if (!readable)
throw new NonReadableChannelException();
long addr = -1;
int ti = -1;
try {
begin();
ti = threads.add();
if (!isOpen())
return null;
long filesize;
do {
filesize = nd.size(fd);
} while ((filesize == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
if (filesize < position + size) {
// Extend file size
if (!writable) {
throw new IOException("Channel not open for writing " + "- cannot extend file to required size");
}
int rv;
do {
rv = nd.truncate(fd, position + size);
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
}
if (size == 0) {
addr = 0;
// a valid file descriptor is not required
FileDescriptor dummy = new FileDescriptor();
/*
if ((!writable) || (imode == MAP_RO))
return Util.newMappedByteBufferR(0, 0, dummy, null);
else
return Util.newMappedByteBuffer(0, 0, dummy, null);
*/
return new DirectByteBuffer(0, 0, dummy, null, (!writable) || (imode == MAP_RO));
}
int pagePosition = (int) (position % allocationGranularity);
long mapPosition = position - pagePosition;
long mapSize = size + pagePosition;
try {
// Android-added: BlockGuard support.
BlockGuard.getThreadPolicy().onReadFromDisk();
// If no exception was thrown from map0, the address is valid
addr = map0(imode, mapPosition, mapSize);
} catch (OutOfMemoryError x) {
// An OutOfMemoryError may indicate that we've exhausted memory
// so force gc and re-attempt map
System.gc();
try {
Thread.sleep(100);
} catch (InterruptedException y) {
Thread.currentThread().interrupt();
}
try {
addr = map0(imode, mapPosition, mapSize);
} catch (OutOfMemoryError y) {
// After a second OOME, fail
throw new IOException("Map failed", y);
}
}
// On Windows, and potentially other platforms, we need an open
// file descriptor for some mapping operations.
FileDescriptor mfd;
try {
mfd = nd.duplicateForMapping(fd);
} catch (IOException ioe) {
unmap0(addr, mapSize);
throw ioe;
}
assert (IOStatus.checkAll(addr));
assert (addr % allocationGranularity == 0);
int isize = (int) size;
Unmapper um = new Unmapper(addr, mapSize, isize, mfd);
/*
if ((!writable) || (imode == MAP_RO)) {
return Util.newMappedByteBufferR(isize,
addr + pagePosition,
mfd,
um);
} else {
return Util.newMappedByteBuffer(isize,
addr + pagePosition,
mfd,
um);
}
*/
return new DirectByteBuffer(isize, addr + pagePosition, mfd, um, (!writable) || (imode == MAP_RO));
} finally {
threads.remove(ti);
end(IOStatus.checkAll(addr));
}
}
use of java.nio.DirectByteBuffer in project j2objc by google.
the class DirectDoubleBufferTest method testJNIAccessByAddress.
// http://b/28964300
public void testJNIAccessByAddress() throws Exception {
DirectByteBuffer directByteBuffer = (DirectByteBuffer) ByteBuffer.allocateDirect(10);
directByteBuffer.put((byte) 'a');
DoubleBuffer doubleBuffer = directByteBuffer.asDoubleBuffer();
long byteBufferBasePointer = NIOAccess.getBasePointer(directByteBuffer);
long doubleBufferBasePointer = NIOAccess.getBasePointer(doubleBuffer);
assertEquals(byteBufferBasePointer, doubleBufferBasePointer);
// Check if the NIOAccess method adds up the current position value.
doubleBuffer.put(1.0);
assertEquals(doubleBufferBasePointer + Double.BYTES, NIOAccess.getBasePointer(doubleBuffer));
}
use of java.nio.DirectByteBuffer in project j2objc by google.
the class DirectIntBufferTest method testJNIAccessByAddress.
// http://b/28964300
public void testJNIAccessByAddress() throws Exception {
DirectByteBuffer directByteBuffer = (DirectByteBuffer) ByteBuffer.allocateDirect(10);
directByteBuffer.put((byte) 'a');
IntBuffer intBuffer = directByteBuffer.asIntBuffer();
long byteBufferBasePointer = NIOAccess.getBasePointer(directByteBuffer);
long intBufferBasePointer = NIOAccess.getBasePointer(intBuffer);
assertEquals(byteBufferBasePointer, intBufferBasePointer);
// Check if the NIOAccess method adds up the current position value.
intBuffer.put(1);
assertEquals(intBufferBasePointer + Integer.BYTES, NIOAccess.getBasePointer(intBuffer));
}
use of java.nio.DirectByteBuffer in project j2objc by google.
the class DirectShortBufferTest method testJNIAccessByAddress.
// http://b/28964300
public void testJNIAccessByAddress() {
DirectByteBuffer directByteBuffer = (DirectByteBuffer) ByteBuffer.allocateDirect(10);
directByteBuffer.put((byte) 'a');
ShortBuffer shortBuffer = directByteBuffer.asShortBuffer();
long byteBufferBasePointer = NIOAccess.getBasePointer(directByteBuffer);
long shortBufferBasePointer = NIOAccess.getBasePointer(shortBuffer);
assertEquals(byteBufferBasePointer, shortBufferBasePointer);
// Check if the NIOAccess method adds up the current position value.
shortBuffer.put((short) 1);
assertEquals(shortBufferBasePointer + Short.BYTES, NIOAccess.getBasePointer(shortBuffer));
}
use of java.nio.DirectByteBuffer in project j2objc by google.
the class DirectCharBufferTest method testJNIAccessByAddress.
// http://b/28964300
public void testJNIAccessByAddress() throws Exception {
DirectByteBuffer directByteBuffer = (DirectByteBuffer) ByteBuffer.allocateDirect(10);
directByteBuffer.put((byte) 'a');
CharBuffer charBuffer = directByteBuffer.asCharBuffer();
long byteBufferBasePointer = NIOAccess.getBasePointer(directByteBuffer);
long charBufferBasePointer = NIOAccess.getBasePointer(charBuffer);
assertEquals(byteBufferBasePointer, charBufferBasePointer);
// Check if the NIOAccess method adds up the current position value.
charBuffer.put('b');
assertEquals(charBufferBasePointer + Character.BYTES, NIOAccess.getBasePointer(charBuffer));
}
Aggregations