GNU Classpath (0.98) | |
Frames | No Frames |
1: /* FileLock.java -- 2: Copyright (C) 2002, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package java.nio.channels; 39: 40: import gnu.java.lang.CPStringBuilder; 41: 42: import java.io.IOException; 43: 44: /** 45: * @since 1.4 46: */ 47: public abstract class FileLock 48: { 49: private final FileChannel channel; 50: private final long position; 51: private final long size; 52: private final boolean shared; 53: 54: /** 55: * Initializes the file lock. 56: * 57: * @exception IllegalArgumentException If the preconditions on the parameters do not hold 58: */ 59: protected FileLock(FileChannel channel, long position, long size, 60: boolean shared) 61: { 62: if (position < 0 || size < 0) 63: throw new IllegalArgumentException(); 64: 65: this.channel = channel; 66: this.position = position; 67: this.size = size; 68: this.shared = shared; 69: } 70: 71: /** 72: * Tells whether or not this lock is valid. 73: */ 74: public abstract boolean isValid(); 75: 76: /** 77: * Releases this lock. 78: * 79: * @exception IOException If an error occurs 80: * @exception ClosedChannelException If the locked channel is no longer open. 81: */ 82: public abstract void release() throws IOException; 83: 84: /** 85: * Returns the file channel upon whose file this lock is held. 86: */ 87: public final FileChannel channel() 88: { 89: return channel; 90: } 91: 92: /** 93: * Tells whether this lock is shared. 94: */ 95: public final boolean isShared() 96: { 97: return shared; 98: } 99: 100: /** 101: * Tells whether or not this lock overlaps the given lock range. 102: */ 103: public final boolean overlaps(long position, long size) 104: { 105: if (position > this.position + this.size) 106: return false; 107: 108: if (position + size < this.position) 109: return false; 110: 111: return true; 112: } 113: 114: /** 115: * Returns the position within the file of the first byte of the 116: * locked region. 117: */ 118: public final long position() 119: { 120: return position; 121: } 122: 123: /** 124: * Returns the size of the locked region in bytes. 125: */ 126: public final long size() 127: { 128: return size; 129: } 130: 131: /** 132: * Returns a string describing the range, type, and validity of this lock. 133: */ 134: public final String toString() 135: { 136: CPStringBuilder buf = new CPStringBuilder(getClass().getName()); 137: buf.append("["); 138: buf.append(position); 139: buf.append(":"); 140: buf.append(size); 141: if (shared) 142: buf.append(" shared"); 143: else 144: buf.append(" exclusive"); 145: if (isValid()) 146: buf.append(" valid]"); 147: else 148: buf.append(" invalid]"); 149: return buf.toString(); 150: } 151: }
GNU Classpath (0.98) |