Search in sources :

Example 61 with MappedByteBuffer

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);
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer)

Example 62 with MappedByteBuffer

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;
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 63 with MappedByteBuffer

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;
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) IOException(java.io.IOException)

Example 64 with MappedByteBuffer

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();
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileInputStream(java.io.FileInputStream)

Example 65 with MappedByteBuffer

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());
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer)

Aggregations

MappedByteBuffer (java.nio.MappedByteBuffer)154 FileChannel (java.nio.channels.FileChannel)75 IOException (java.io.IOException)44 File (java.io.File)38 RandomAccessFile (java.io.RandomAccessFile)36 FileInputStream (java.io.FileInputStream)29 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.Test)18 Path (java.nio.file.Path)11 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9 Elf (com.facebook.buck.cxx.elf.Elf)8 FileOutputStream (java.io.FileOutputStream)8 ElfSection (com.facebook.buck.cxx.elf.ElfSection)6 FileNotFoundException (java.io.FileNotFoundException)5 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)4 Pair (com.facebook.buck.model.Pair)3 Date (java.util.Date)3 NulTerminatedCharsetDecoder (com.facebook.buck.charset.NulTerminatedCharsetDecoder)2 ElfDynamicSection (com.facebook.buck.cxx.elf.ElfDynamicSection)2