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 implements AutoCloseable 049{ 050 private final FileChannel channel; 051 private final long position; 052 private final long size; 053 private final boolean shared; 054 055 /** 056 * Initializes the file lock. 057 * 058 * @exception IllegalArgumentException If the preconditions on the parameters do not hold 059 */ 060 protected FileLock(FileChannel channel, long position, long size, 061 boolean shared) 062 { 063 if (position < 0 || size < 0) 064 throw new IllegalArgumentException(); 065 066 this.channel = channel; 067 this.position = position; 068 this.size = size; 069 this.shared = shared; 070 } 071 072 /** 073 * Tells whether or not this lock is valid. 074 */ 075 public abstract boolean isValid(); 076 077 /** 078 * Releases this lock. 079 * 080 * @exception IOException If an error occurs 081 * @exception ClosedChannelException If the locked channel is no longer open. 082 */ 083 public abstract void release() throws IOException; 084 085 /** 086 * Returns the file channel upon whose file this lock is held. 087 */ 088 public final FileChannel channel() 089 { 090 return channel; 091 } 092 093 /** 094 * Tells whether this lock is shared. 095 */ 096 public final boolean isShared() 097 { 098 return shared; 099 } 100 101 /** 102 * Tells whether or not this lock overlaps the given lock range. 103 */ 104 public final boolean overlaps(long position, long size) 105 { 106 if (position > this.position + this.size) 107 return false; 108 109 if (position + size < this.position) 110 return false; 111 112 return true; 113 } 114 115 /** 116 * Returns the position within the file of the first byte of the 117 * locked region. 118 */ 119 public final long position() 120 { 121 return position; 122 } 123 124 /** 125 * Returns the size of the locked region in bytes. 126 */ 127 public final long size() 128 { 129 return size; 130 } 131 132 /** 133 * Returns a string describing the range, type, and validity of this lock. 134 */ 135 public final String toString() 136 { 137 CPStringBuilder buf = new CPStringBuilder(getClass().getName()); 138 buf.append("["); 139 buf.append(position); 140 buf.append(":"); 141 buf.append(size); 142 if (shared) 143 buf.append(" shared"); 144 else 145 buf.append(" exclusive"); 146 if (isValid()) 147 buf.append(" valid]"); 148 else 149 buf.append(" invalid]"); 150 return buf.toString(); 151 } 152}