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();
}
}
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();
}
}
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();
}
}
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();
}
}
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;
}
Aggregations