001/* FileLock.java -- 002 Copyright (C) 2002, 2005 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038package java.nio.channels; 039 040import gnu.java.lang.CPStringBuilder; 041 042import java.io.IOException; 043 044/** 045 * @since 1.4 046 */ 047public abstract class FileLock 048{ 049 private final FileChannel channel; 050 private final long position; 051 private final long size; 052 private final boolean shared; 053 054 /** 055 * Initializes the file lock. 056 * 057 * @exception IllegalArgumentException If the preconditions on the parameters do not hold 058 */ 059 protected FileLock(FileChannel channel, long position, long size, 060 boolean shared) 061 { 062 if (position < 0 || size < 0) 063 throw new IllegalArgumentException(); 064 065 this.channel = channel; 066 this.position = position; 067 this.size = size; 068 this.shared = shared; 069 } 070 071 /** 072 * Tells whether or not this lock is valid. 073 */ 074 public abstract boolean isValid(); 075 076 /** 077 * Releases this lock. 078 * 079 * @exception IOException If an error occurs 080 * @exception ClosedChannelException If the locked channel is no longer open. 081 */ 082 public abstract void release() throws IOException; 083 084 /** 085 * Returns the file channel upon whose file this lock is held. 086 */ 087 public final FileChannel channel() 088 { 089 return channel; 090 } 091 092 /** 093 * Tells whether this lock is shared. 094 */ 095 public final boolean isShared() 096 { 097 return shared; 098 } 099 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}