Source for java.nio.channels.FileLock

   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:   implements AutoCloseable
  49: {
  50:   private final FileChannel channel;
  51:   private final long position;
  52:   private final long size;
  53:   private final boolean shared;
  54: 
  55:   /**
  56:    * Initializes the file lock.
  57:    *
  58:    * @exception IllegalArgumentException If the preconditions on the parameters do not hold
  59:    */
  60:   protected FileLock(FileChannel channel, long position, long size,
  61:                      boolean shared)
  62:   {
  63:     if (position < 0 || size < 0)
  64:       throw new IllegalArgumentException();
  65: 
  66:     this.channel = channel;
  67:     this.position = position;
  68:     this.size = size;
  69:     this.shared = shared;
  70:   }
  71: 
  72:   /**
  73:    * Tells whether or not this lock is valid.
  74:    */
  75:   public abstract boolean isValid();
  76: 
  77:   /**
  78:    * Releases this lock.
  79:    *
  80:    * @exception IOException If an error occurs
  81:    * @exception ClosedChannelException If the locked channel is no longer open.
  82:    */
  83:   public abstract void release() throws IOException;
  84: 
  85:   /**
  86:    * Returns the file channel upon whose file this lock is held.
  87:    */
  88:   public final FileChannel channel()
  89:   {
  90:     return channel;
  91:   }
  92: 
  93:   /**
  94:    * Tells whether this lock is shared.
  95:    */
  96:   public final boolean isShared()
  97:   {
  98:     return shared;
  99:   }
 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: }