Search in sources :

Example 36 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project mapdb by jankotek.

the class ByteBufferMemoryVol method close.

@Override
public void close() {
    if (!closed.compareAndSet(false, true))
        return;
    growLock.lock();
    try {
        if (cleanerHackEnabled) {
            for (ByteBuffer b : slices) {
                if (b != null && (b instanceof MappedByteBuffer)) {
                    unmap((MappedByteBuffer) b);
                }
            }
        }
        Arrays.fill(slices, null);
        slices = null;
    } finally {
        growLock.unlock();
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 37 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project mapdb by jankotek.

the class MappedFileVol method truncate.

@Override
public void truncate(long size) {
    final int maxSize = 1 + (int) (size >>> sliceShift);
    if (maxSize == slices.length)
        return;
    if (maxSize > slices.length) {
        ensureAvailable(size);
        return;
    }
    growLock.lock();
    try {
        if (maxSize >= slices.length)
            return;
        ByteBuffer[] old = slices;
        slices = Arrays.copyOf(slices, maxSize);
        //unmap remaining buffers
        for (int i = maxSize; i < old.length; i++) {
            if (cleanerHackEnabled) {
                unmap((MappedByteBuffer) old[i]);
            }
            old[i] = null;
        }
        if (ByteBufferVol.windowsWorkaround) {
            for (int i = 0; i < maxSize; i++) {
                if (cleanerHackEnabled) {
                    unmap((MappedByteBuffer) old[i]);
                }
                old[i] = null;
            }
        }
        try {
            fileChannel.truncate(1L * sliceSize * maxSize);
        } catch (IOException e) {
            throw new DBException.VolumeIOError(e);
        }
        if (ByteBufferVol.windowsWorkaround) {
            for (int pos = 0; pos < maxSize; pos++) {
                ByteBuffer b = fileChannel.map(mapMode, 1L * sliceSize * pos, sliceSize);
                if (CC.ASSERT && b.order() != ByteOrder.BIG_ENDIAN)
                    throw new AssertionError("Little-endian");
                slices[pos] = b;
            }
        }
    } catch (IOException e) {
        throw new DBException.VolumeIOError(e);
    } finally {
        growLock.unlock();
    }
}
Also used : DBException(org.mapdb.DBException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 38 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project mapdb by jankotek.

the class MappedFileVol method sync.

@Override
public void sync() {
    if (readOnly)
        return;
    growLock.lock();
    try {
        ByteBuffer[] slices = this.slices;
        if (slices == null)
            return;
        // and it increases chance to detect file corruption.
        for (int i = slices.length - 1; i >= 0; i--) {
            ByteBuffer b = slices[i];
            if (b != null && (b instanceof MappedByteBuffer)) {
                MappedByteBuffer bb = ((MappedByteBuffer) b);
                bb.force();
            }
        }
    } finally {
        growLock.unlock();
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 39 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project kafka by apache.

the class Utils method readFileAsString.

/**
     * Attempt to read a file as a string
     * @throws IOException
     */
public static String readFileAsString(String path, Charset charset) throws IOException {
    if (charset == null)
        charset = Charset.defaultCharset();
    try (FileInputStream stream = new FileInputStream(new File(path))) {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return charset.decode(bb).toString();
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 40 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project hadoop by apache.

the class ShortCircuitReplica method munmap.

/**
   * Free the mmap associated with this replica.
   *
   * Must be called with the cache lock held.
   */
void munmap() {
    MappedByteBuffer mmap = (MappedByteBuffer) mmapData;
    NativeIO.POSIX.munmap(mmap);
    mmapData = null;
}
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