001 /* FileLock.java -- 002 Copyright (C) 2002, 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package java.nio.channels; 039 040 import java.io.IOException; 041 042 043 /** 044 * @since 1.4 045 */ 046 public abstract class FileLock 047 { 048 private final FileChannel channel; 049 private final long position; 050 private final long size; 051 private final boolean shared; 052 053 /** 054 * Initializes the file lock. 055 * 056 * @exception IllegalArgumentException If the preconditions on the parameters do not hold 057 */ 058 protected FileLock(FileChannel channel, long position, long size, 059 boolean shared) 060 { 061 if (position < 0 || size < 0) 062 throw new IllegalArgumentException(); 063 064 this.channel = channel; 065 this.position = position; 066 this.size = size; 067 this.shared = shared; 068 } 069 070 /** 071 * Tells whether or not this lock is valid. 072 */ 073 public abstract boolean isValid(); 074 075 /** 076 * Releases this lock. 077 * 078 * @exception IOException If an error occurs 079 * @exception ClosedChannelException If the locked channel is no longer open. 080 */ 081 public abstract void release() throws IOException; 082 083 /** 084 * Returns the file channel upon whose file this lock is held. 085 */ 086 public final FileChannel channel() 087 { 088 return channel; 089 } 090 091 /** 092 * Tells whether this lock is shared. 093 */ 094 public final boolean isShared() 095 { 096 return shared; 097 } 098 099 /** 100 * Tells whether or not this lock overlaps the given lock range. 101 */ 102 public final boolean overlaps(long position, long size) 103 { 104 if (position > this.position + this.size) 105 return false; 106 107 if (position + size < this.position) 108 return false; 109 110 return true; 111 } 112 113 /** 114 * Returns the position within the file of the first byte of the 115 * locked region. 116 */ 117 public final long position() 118 { 119 return position; 120 } 121 122 /** 123 * Returns the size of the locked region in bytes. 124 */ 125 public final long size() 126 { 127 return size; 128 } 129 130 /** 131 * Returns a string describing the range, type, and validity of this lock. 132 */ 133 public final String toString() 134 { 135 StringBuffer buf = new StringBuffer(getClass().getName()); 136 buf.append("["); 137 buf.append(position); 138 buf.append(":"); 139 buf.append(size); 140 if (shared) 141 buf.append(" shared"); 142 else 143 buf.append(" exclusive"); 144 if (isValid()) 145 buf.append(" valid]"); 146 else 147 buf.append(" invalid]"); 148 return buf.toString(); 149 } 150 }