@Override public String toString() { return data.getName(); }
@Override public long size() { return data.length(); }
@Override public synchronized FileLock tryLock(long position, long size, boolean shared) throws IOException { if (shared) { if (!data.lockShared()) { return null; } } else { if (!data.lockExclusive()) { return null; } } // cast to FileChannel to avoid JDK 1.7 ambiguity FileLock lock = new FileLock((FileChannel) null, position, size, shared) { @Override public boolean isValid() { return true; } @Override public void release() throws IOException { data.unlock(); } }; return lock; }
/** * Truncate the file. * * @param newLength the new length */ void truncate(long newLength) { changeLength(newLength); long end = MathUtils.roundUpLong(newLength, BLOCK_SIZE); if (end != newLength) { int lastPage = (int) (newLength >>> BLOCK_SIZE_SHIFT); expand(data, lastPage); ByteBuffer d = data[lastPage]; for (int i = (int) (newLength & BLOCK_SIZE_MASK); i < BLOCK_SIZE; i++) { d.put(i, (byte) 0); } if (compress) { compressLater(data, lastPage); } } }
private FileNioMemData getMemoryFile() { synchronized (MEMORY_FILES) { FileNioMemData m = MEMORY_FILES.get(name); if (m == null) { m = new FileNioMemData(name, compressed()); MEMORY_FILES.put(name, m); } return m; } }
@Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { if (size() < size) { return false; } CompressItem c = (CompressItem) eldest.getKey(); compress(c.data, c.page); return true; } }
@Override public boolean canWrite() { return getMemoryFile().canWrite(); }
@Override public long lastModified() { return getMemoryFile().getLastModified(); }
if (end > length) { if (write) { changeLength(end); } else { len = (int) (length - pos); int l = (int) Math.min(len, BLOCK_SIZE - (pos & BLOCK_SIZE_MASK)); int page = (int) (pos >>> BLOCK_SIZE_SHIFT); expand(data, page); ByteBuffer block = data[page]; int blockOffset = (int) (pos & BLOCK_SIZE_MASK); compressLater(data, page);
@Override public long size() { return getMemoryFile().length(); }