FileLock.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* FileLock.java --
  2. Copyright (C) 2002, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.nio.channels;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.io.IOException;
  34. /**
  35. * @since 1.4
  36. */
  37. public abstract class FileLock
  38. implements AutoCloseable
  39. {
  40. private final FileChannel channel;
  41. private final long position;
  42. private final long size;
  43. private final boolean shared;
  44. /**
  45. * Initializes the file lock.
  46. *
  47. * @exception IllegalArgumentException If the preconditions on the parameters do not hold
  48. */
  49. protected FileLock(FileChannel channel, long position, long size,
  50. boolean shared)
  51. {
  52. if (position < 0 || size < 0)
  53. throw new IllegalArgumentException();
  54. this.channel = channel;
  55. this.position = position;
  56. this.size = size;
  57. this.shared = shared;
  58. }
  59. /**
  60. * Tells whether or not this lock is valid.
  61. */
  62. public abstract boolean isValid();
  63. /**
  64. * Releases this lock.
  65. *
  66. * @exception IOException If an error occurs
  67. * @exception ClosedChannelException If the locked channel is no longer open.
  68. */
  69. public abstract void release() throws IOException;
  70. /**
  71. * Returns the file channel upon whose file this lock is held.
  72. */
  73. public final FileChannel channel()
  74. {
  75. return channel;
  76. }
  77. /**
  78. * Tells whether this lock is shared.
  79. */
  80. public final boolean isShared()
  81. {
  82. return shared;
  83. }
  84. /**
  85. * Tells whether or not this lock overlaps the given lock range.
  86. */
  87. public final boolean overlaps(long position, long size)
  88. {
  89. if (position > this.position + this.size)
  90. return false;
  91. if (position + size < this.position)
  92. return false;
  93. return true;
  94. }
  95. /**
  96. * Returns the position within the file of the first byte of the
  97. * locked region.
  98. */
  99. public final long position()
  100. {
  101. return position;
  102. }
  103. /**
  104. * Returns the size of the locked region in bytes.
  105. */
  106. public final long size()
  107. {
  108. return size;
  109. }
  110. /**
  111. * Returns a string describing the range, type, and validity of this lock.
  112. */
  113. public final String toString()
  114. {
  115. CPStringBuilder buf = new CPStringBuilder(getClass().getName());
  116. buf.append("[");
  117. buf.append(position);
  118. buf.append(":");
  119. buf.append(size);
  120. if (shared)
  121. buf.append(" shared");
  122. else
  123. buf.append(" exclusive");
  124. if (isValid())
  125. buf.append(" valid]");
  126. else
  127. buf.append(" invalid]");
  128. return buf.toString();
  129. }
  130. }