use of java.nio.MappedByteBuffer in project mapdb by jankotek.
the class MappedFileVol method close.
@Override
public void close() {
if (!closed.compareAndSet(false, true))
return;
growLock.lock();
try {
if (fileLock != null && fileLock.isValid()) {
fileLock.release();
}
fileChannel.close();
raf.close();
if (cleanerHackEnabled) {
for (ByteBuffer b : slices) {
if (b != null && (b instanceof MappedByteBuffer)) {
unmap((MappedByteBuffer) b);
}
}
}
Arrays.fill(slices, null);
slices = null;
} catch (IOException e) {
throw new DBException.VolumeIOError(e);
} finally {
growLock.unlock();
}
}
use of java.nio.MappedByteBuffer in project guava by hceylan.
the class Files method map.
/**
* Maps a file in to memory as per
* {@link FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}
* using the requested {@link MapMode}.
*
* <p>Files are mapped from offset 0 to {@code size}.
*
* <p>If the mode is {@link MapMode#READ_WRITE} and the file does not exist,
* it will be created with the requested {@code size}. Thus this method is
* useful for creating memory mapped files which do not yet exist.
*
* <p>This only works for files <= {@link Integer#MAX_VALUE} bytes.
*
* @param file the file to map
* @param mode the mode to use when mapping {@code file}
* @return a buffer reflecting {@code file}
* @throws IOException if an I/O error occurs
*
* @see FileChannel#map(MapMode, long, long)
* @since 2.0
*/
public static MappedByteBuffer map(File file, MapMode mode, long size) throws FileNotFoundException, IOException {
RandomAccessFile raf = new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw");
boolean threw = true;
try {
MappedByteBuffer mbb = map(raf, mode, size);
threw = false;
return mbb;
} finally {
Closeables.close(raf, threw);
}
}
use of java.nio.MappedByteBuffer in project FastDev4Android by jiangqqlmj.
the class StrUtils method calcMd5.
/**
* @param file
* 文件
* @return 文件转换MD5
*/
public static String calcMd5(File file) {
FileInputStream in = null;
try {
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
in = new FileInputStream(file);
FileChannel ch = in.getChannel();
MappedByteBuffer byteBuffer;
byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
algorithm.update(byteBuffer);
return toHexString(algorithm.digest(), "");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
use of java.nio.MappedByteBuffer in project libgdx by libgdx.
the class ZipResourceFile method addPatchFile.
/*
* Opens the specified file read-only. We memory-map the entire thing and
* close the file before returning.
*/
void addPatchFile(String zipFileName) throws IOException {
File file = new File(zipFileName);
RandomAccessFile f = new RandomAccessFile(file, "r");
long fileLength = f.length();
if (fileLength < kEOCDLen) {
f.close();
throw new java.io.IOException();
}
long readAmount = kMaxEOCDSearch;
if (readAmount > fileLength)
readAmount = fileLength;
/*
* Make sure this is a Zip archive.
*/
f.seek(0);
int header = read4LE(f);
if (header == kEOCDSignature) {
Log.i(LOG_TAG, "Found Zip archive, but it looks empty");
throw new IOException();
} else if (header != kLFHSignature) {
Log.v(LOG_TAG, "Not a Zip archive");
throw new IOException();
}
/*
* Perform the traditional EOCD snipe hunt. We're searching for the End
* of Central Directory magic number, which appears at the start of the
* EOCD block. It's followed by 18 bytes of EOCD stuff and up to 64KB of
* archive comment. We need to read the last part of the file into a
* buffer, dig through it to find the magic number, parse some values
* out, and use those to determine the extent of the CD. We start by
* pulling in the last part of the file.
*/
long searchStart = fileLength - readAmount;
f.seek(searchStart);
ByteBuffer bbuf = ByteBuffer.allocate((int) readAmount);
byte[] buffer = bbuf.array();
f.readFully(buffer);
bbuf.order(ByteOrder.LITTLE_ENDIAN);
/*
* Scan backward for the EOCD magic. In an archive without a trailing
* comment, we'll find it on the first try. (We may want to consider
* doing an initial minimal read; if we don't find it, retry with a
* second read as above.)
*/
// EOCD == 0x50, 0x4b, 0x05, 0x06
int eocdIdx;
for (eocdIdx = buffer.length - kEOCDLen; eocdIdx >= 0; eocdIdx--) {
if (buffer[eocdIdx] == 0x50 && bbuf.getInt(eocdIdx) == kEOCDSignature) {
if (LOGV) {
Log.v(LOG_TAG, "+++ Found EOCD at index: " + eocdIdx);
}
break;
}
}
if (eocdIdx < 0) {
Log.d(LOG_TAG, "Zip: EOCD not found, " + zipFileName + " is not zip");
}
/*
* Grab the CD offset and size, and the number of entries in the
* archive. After that, we can release our EOCD hunt buffer.
*/
int numEntries = bbuf.getShort(eocdIdx + kEOCDNumEntries);
long dirSize = bbuf.getInt(eocdIdx + kEOCDSize) & 0xffffffffL;
long dirOffset = bbuf.getInt(eocdIdx + kEOCDFileOffset) & 0xffffffffL;
// Verify that they look reasonable.
if (dirOffset + dirSize > fileLength) {
Log.w(LOG_TAG, "bad offsets (dir " + dirOffset + ", size " + dirSize + ", eocd " + eocdIdx + ")");
throw new IOException();
}
if (numEntries == 0) {
Log.w(LOG_TAG, "empty archive?");
throw new IOException();
}
if (LOGV) {
Log.v(LOG_TAG, "+++ numEntries=" + numEntries + " dirSize=" + dirSize + " dirOffset=" + dirOffset);
}
MappedByteBuffer directoryMap = f.getChannel().map(FileChannel.MapMode.READ_ONLY, dirOffset, dirSize);
directoryMap.order(ByteOrder.LITTLE_ENDIAN);
byte[] tempBuf = new byte[0xffff];
/*
* Walk through the central directory, adding entries to the hash table.
*/
int currentOffset = 0;
/*
* Allocate the local directory information
*/
ByteBuffer buf = ByteBuffer.allocate(kLFHLen);
buf.order(ByteOrder.LITTLE_ENDIAN);
for (int i = 0; i < numEntries; i++) {
if (directoryMap.getInt(currentOffset) != kCDESignature) {
Log.w(LOG_TAG, "Missed a central dir sig (at " + currentOffset + ")");
throw new IOException();
}
/* useful stuff from the directory entry */
int fileNameLen = directoryMap.getShort(currentOffset + kCDENameLen) & 0xffff;
int extraLen = directoryMap.getShort(currentOffset + kCDEExtraLen) & 0xffff;
int commentLen = directoryMap.getShort(currentOffset + kCDECommentLen) & 0xffff;
/* get the CDE filename */
directoryMap.position(currentOffset + kCDELen);
directoryMap.get(tempBuf, 0, fileNameLen);
directoryMap.position(0);
/* UTF-8 on Android */
String str = new String(tempBuf, 0, fileNameLen);
if (LOGV) {
Log.v(LOG_TAG, "Filename: " + str);
}
ZipEntryRO ze = new ZipEntryRO(zipFileName, file, str);
ze.mMethod = directoryMap.getShort(currentOffset + kCDEMethod) & 0xffff;
ze.mWhenModified = directoryMap.getInt(currentOffset + kCDEModWhen) & 0xffffffffL;
ze.mCRC32 = directoryMap.getLong(currentOffset + kCDECRC) & 0xffffffffL;
ze.mCompressedLength = directoryMap.getLong(currentOffset + kCDECompLen) & 0xffffffffL;
ze.mUncompressedLength = directoryMap.getLong(currentOffset + kCDEUncompLen) & 0xffffffffL;
ze.mLocalHdrOffset = directoryMap.getInt(currentOffset + kCDELocalOffset) & 0xffffffffL;
// set the offsets
buf.clear();
ze.setOffsetFromFile(f, buf);
// put file into hash
mHashMap.put(str, ze);
// go to next directory entry
currentOffset += kCDELen + fileNameLen + extraLen + commentLen;
}
if (LOGV) {
Log.v(LOG_TAG, "+++ zip good scan " + numEntries + " entries");
}
}
use of java.nio.MappedByteBuffer in project pinot by linkedin.
the class MmapUtils method mmapFile.
/**
* Memory maps a file, tracking usage information.
*
* @param randomAccessFile The random access file to mmap
* @param mode The mmap mode
* @param position The byte position to mmap
* @param size The number of bytes to mmap
* @param file The file that is mmap'ed
* @param details Additional details about the allocation
*/
public static MappedByteBuffer mmapFile(RandomAccessFile randomAccessFile, FileChannel.MapMode mode, long position, long size, File file, String details) throws IOException {
final String context;
final AllocationContext allocationContext;
if (file != null) {
context = file.getAbsolutePath() + " (" + details + ")";
allocationContext = new AllocationContext(file, details, AllocationType.MMAP);
} else {
context = "no file (" + details + ")";
allocationContext = new AllocationContext(details, AllocationType.MMAP);
}
MMAP_BUFFER_USAGE.addAndGet(size);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Memory mapping file, mmap size {} with context {}, allocation after operation {}", size, context, getTrackedAllocationStatus());
}
MappedByteBuffer byteBuffer = null;
try {
byteBuffer = randomAccessFile.getChannel().map(mode, position, size);
MMAP_BUFFER_COUNT.incrementAndGet();
} catch (Exception e) {
ALLOCATION_FAILURE_COUNT.incrementAndGet();
LOGGER.error("Failed to mmap file (size {}, context {})", size, context, e);
LOGGER.error("Allocation status {}", getTrackedAllocationStatus());
Utils.rethrowException(e);
}
BUFFER_TO_CONTEXT_MAP.put(byteBuffer, allocationContext);
return byteBuffer;
}
Aggregations