use of java.nio.MappedByteBuffer in project asterixdb by apache.
the class NetworkingUtil method sendFileNIO.
//unused
public static void sendFileNIO(FileChannel fileChannel, SocketChannel socketChannel) throws IOException {
long fileSize = fileChannel.size();
MappedByteBuffer bb = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
socketChannel.write(bb);
}
use of java.nio.MappedByteBuffer in project lucene-solr by apache.
the class MMapDirectory method map.
/** Maps a file into a set of buffers */
final ByteBuffer[] map(String resourceDescription, FileChannel fc, long offset, long length) throws IOException {
if ((length >>> chunkSizePower) >= Integer.MAX_VALUE)
throw new IllegalArgumentException("RandomAccessFile too big for chunk size: " + resourceDescription);
final long chunkSize = 1L << chunkSizePower;
// we always allocate one more buffer, the last one may be a 0 byte one
final int nrBuffers = (int) (length >>> chunkSizePower) + 1;
ByteBuffer[] buffers = new ByteBuffer[nrBuffers];
long bufferStart = 0L;
for (int bufNr = 0; bufNr < nrBuffers; bufNr++) {
int bufSize = (int) ((length > (bufferStart + chunkSize)) ? chunkSize : (length - bufferStart));
MappedByteBuffer buffer;
try {
buffer = fc.map(MapMode.READ_ONLY, offset + bufferStart, bufSize);
} catch (IOException ioe) {
throw convertMapFailedIOException(ioe, resourceDescription, bufSize);
}
if (preload) {
buffer.load();
}
buffers[bufNr] = buffer;
bufferStart += bufSize;
}
return buffers;
}
use of java.nio.MappedByteBuffer in project jena by apache.
the class BlockAccessMapped method allocSegment.
// Even for MultipleReader this needs to be sync'ed.
private MappedByteBuffer allocSegment(int seg) {
if (seg < 0) {
getLog().error("Segment negative: " + seg);
throw new FileException("Negative segment: " + seg);
}
while (seg >= segments.length) {
// More space needed.
MappedByteBuffer[] segments2 = new MappedByteBuffer[GrowthFactor * segments.length];
System.arraycopy(segments, 0, segments2, 0, segments.length);
boolean[] segmentDirty2 = new boolean[GrowthFactor * segmentDirty.length];
System.arraycopy(segmentDirty, 0, segmentDirty2, 0, segmentDirty.length);
segmentDirty = segmentDirty2;
segments = segments2;
}
long offset = fileLocation(seg);
if (offset < 0) {
getLog().error("Segment offset gone negative: " + seg);
throw new FileException("Negative segment offset: " + seg);
}
MappedByteBuffer segBuffer = segments[seg];
if (segBuffer == null) {
try {
segBuffer = file.channel().map(MapMode.READ_WRITE, offset, SegmentSize);
if (getLog().isDebugEnabled())
getLog().debug(format("Segment: %d", seg));
segments[seg] = segBuffer;
} catch (IOException ex) {
if (ex.getCause() instanceof java.lang.OutOfMemoryError)
throw new FileException("BlockMgrMapped.segmentAllocate: Segment = " + seg + " : Offset = " + offset);
throw new FileException("BlockMgrMapped.segmentAllocate: Segment = " + seg, ex);
}
}
return segBuffer;
}
use of java.nio.MappedByteBuffer in project robovm by robovm.
the class MappedByteBufferTest method test_force.
/**
* @tests {@link java.nio.MappedByteBuffer#force()}
*/
public void test_force() throws IOException {
// buffer was not mapped in read/write mode
FileInputStream fileInputStream = new FileInputStream(tmpFile);
FileChannel fileChannelRead = fileInputStream.getChannel();
MappedByteBuffer mmbRead = fileChannelRead.map(MapMode.READ_ONLY, 0, fileChannelRead.size());
mmbRead.force();
FileInputStream inputStream = new FileInputStream(tmpFile);
FileChannel fileChannelR = inputStream.getChannel();
MappedByteBuffer resultRead = fileChannelR.map(MapMode.READ_ONLY, 0, fileChannelR.size());
//If this buffer was not mapped in read/write mode, then invoking this method has no effect.
assertEquals("Invoking force() should have no effect when this buffer was not mapped in read/write mode", mmbRead, resultRead);
// Buffer was mapped in read/write mode
RandomAccessFile randomFile = new RandomAccessFile(tmpFile, "rw");
FileChannel fileChannelReadWrite = randomFile.getChannel();
MappedByteBuffer mmbReadWrite = fileChannelReadWrite.map(FileChannel.MapMode.READ_WRITE, 0, fileChannelReadWrite.size());
mmbReadWrite.put((byte) 'o');
mmbReadWrite.force();
RandomAccessFile random = new RandomAccessFile(tmpFile, "rw");
FileChannel fileChannelRW = random.getChannel();
MappedByteBuffer resultReadWrite = fileChannelRW.map(FileChannel.MapMode.READ_WRITE, 0, fileChannelRW.size());
// Invoking force() will change the buffer
assertFalse(mmbReadWrite.equals(resultReadWrite));
fileChannelRead.close();
fileChannelR.close();
fileChannelReadWrite.close();
fileChannelRW.close();
}
use of java.nio.MappedByteBuffer in project robovm by robovm.
the class FileChannelTest method test_map_Private_NonZeroPosition.
/**
* @tests java.nio.channels.FileChannel#map(MapMode,long,long)
*/
public void test_map_Private_NonZeroPosition() throws IOException {
MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 10, CONTENT_LENGTH - 10);
assertEquals(CONTENT_LENGTH - 10, mapped.limit());
assertEquals(CONTENT_LENGTH - 10, mapped.capacity());
assertEquals(0, mapped.position());
}
Aggregations