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 gnu.java.lang.CPStringBuilder;
041
042 import java.io.IOException;
043
044 /**
045 * @since 1.4
046 */
047 public 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 }