コード例 #1
0
  @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;
  }
コード例 #2
0
 @Override
 public void moveTo(FilePath newName) {
   synchronized (MEMORY_FILES) {
     FileNioMemData f = getMemoryFile();
     f.setName(newName.name);
     MEMORY_FILES.remove(name);
     MEMORY_FILES.put(newName.name, f);
   }
 }
コード例 #3
0
 @Override
 public FileChannel truncate(long newLength) throws IOException {
   if (newLength < size()) {
     data.touch(readOnly);
     pos = Math.min(pos, newLength);
     data.truncate(newLength);
   }
   return this;
 }
コード例 #4
0
 @Override
 public int write(ByteBuffer src) throws IOException {
   int len = src.remaining();
   if (len == 0) {
     return 0;
   }
   data.touch(readOnly);
   pos = data.readWrite(pos, src, 0 /*because we start writing from src.position()*/, len, true);
   src.position(src.position() + len);
   return len;
 }
コード例 #5
0
 @Override
 public int read(ByteBuffer dst) throws IOException {
   int len = dst.remaining();
   if (len == 0) {
     return 0;
   }
   long newPos = data.readWrite(pos, dst, dst.position(), len, false);
   len = (int) (newPos - pos);
   if (len <= 0) {
     return -1;
   }
   dst.position(dst.position() + len);
   pos = newPos;
   return len;
 }
コード例 #6
0
 @Override
 public String toString() {
   return data.getName();
 }
コード例 #7
0
 @Override
 public long size() {
   return data.length();
 }