/** * Try to open the file. * * @param fileName the file name * @param readOnly whether the file should only be opened in read-only mode, even if the file is * writable * @param encryptionKey the encryption key, or null if encryption is not used */ public void open(String fileName, boolean readOnly, char[] encryptionKey) { if (file != null) { return; } if (fileName != null) { FilePath p = FilePath.get(fileName); // if no explicit scheme was specified, NIO is used if (p instanceof FilePathDisk && !fileName.startsWith(p.getScheme() + ":")) { // ensure the NIO file system is registered FilePathNio.class.getName(); fileName = "nio:" + fileName; } } this.fileName = fileName; FilePath f = FilePath.get(fileName); FilePath parent = f.getParent(); if (parent != null && !parent.exists()) { throw DataUtils.newIllegalArgumentException("Directory does not exist: {0}", parent); } if (f.exists() && !f.canWrite()) { readOnly = true; } this.readOnly = readOnly; try { file = f.open(readOnly ? "r" : "rw"); if (encryptionKey != null) { byte[] key = FilePathEncrypt.getPasswordBytes(encryptionKey); encryptedFile = file; file = new FilePathEncrypt.FileEncrypt(fileName, key, file); } file = FilePathCache.wrap(file); try { if (readOnly) { fileLock = file.tryLock(0, Long.MAX_VALUE, true); } else { fileLock = file.tryLock(); } } catch (OverlappingFileLockException e) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_FILE_LOCKED, "The file is locked: {0}", fileName, e); } if (fileLock == null) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_FILE_LOCKED, "The file is locked: {0}", fileName); } fileSize = file.size(); } catch (IOException e) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_READING_FAILED, "Could not open file {0}", fileName, e); } }
/** Flush all changes. */ public void sync() { try { file.force(true); } catch (IOException e) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_WRITING_FAILED, "Could not sync file {0}", fileName, e); } }
/** * Get the block. * * @param key the key * @return the block */ byte[] getBlock(long key) { byte[] data = map.get(key); if (data == null) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_BLOCK_NOT_FOUND, "Block {0} not found", key); } return data; }
/** * Truncate the file. * * @param size the new file size */ public void truncate(long size) { try { writeCount++; file.truncate(size); fileSize = Math.min(fileSize, size); } catch (IOException e) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_WRITING_FAILED, "Could not truncate file {0} to size {1}", fileName, size, e); } }
/** Close this store. */ public void close() { try { if (fileLock != null) { fileLock.release(); fileLock = null; } file.close(); freeSpace.clear(); } catch (Exception e) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_WRITING_FAILED, "Closing failed for file {0}", fileName, e); } finally { file = null; } }
/** * Check whether the map is open. * * @throws IllegalStateException if the map is closed */ protected void checkOpen() { if (closed) { throw DataUtils.newIllegalStateException("This map is closed"); } }