1 7 8 package java.nio.channels; 9 10 import java.io.IOException ; 11 12 13 100 101 public abstract class FileLock { 102 103 private final FileChannel channel; 104 private final long position; 105 private final long size; 106 private final boolean shared; 107 108 129 protected FileLock(FileChannel channel, 130 long position, long size, boolean shared) 131 { 132 if (position < 0) 133 throw new IllegalArgumentException ("Negative position"); 134 if (size < 0) 135 throw new IllegalArgumentException ("Negative size"); 136 if (position + size < 0) 137 throw new IllegalArgumentException ("Negative position + size"); 138 this.channel = channel; 139 this.position = position; 140 this.size = size; 141 this.shared = shared; 142 } 143 144 149 public final FileChannel channel() { 150 return channel; 151 } 152 153 163 public final long position() { 164 return position; 165 } 166 167 176 public final long size() { 177 return size; 178 } 179 180 186 public final boolean isShared() { 187 return shared; 188 } 189 190 196 public final boolean overlaps(long position, long size) { 197 if (position + size <= this.position) 198 return false; if (this.position + this.size <= position) 200 return false; return true; 202 } 203 204 212 public abstract boolean isValid(); 213 214 228 public abstract void release() throws IOException ; 229 230 235 public final String toString() { 236 return (this.getClass().getName() 237 + "[" + position 238 + ":" + size 239 + " " + (shared ? "shared" : "exclusive") 240 + " " + (isValid() ? "valid" : "invalid") 241 + "]"); 242 } 243 244 } 245 | Popular Tags |