37 |
|
|
38 |
package java.nio.channels; |
package java.nio.channels; |
39 |
|
|
40 |
|
/** |
41 |
|
* @since 1.4 |
42 |
|
*/ |
43 |
public abstract class FileLock |
public abstract class FileLock |
44 |
{ |
{ |
45 |
FileChannel channel; |
FileChannel channel; |
46 |
long position; |
long position; |
47 |
long size; |
long size; |
48 |
boolean shared; |
boolean shared; |
49 |
|
|
50 |
protected FileLock(FileChannel channel, |
/** |
51 |
long position, |
* Initializes the file lock. |
52 |
long size, |
* |
53 |
boolean shared) |
* @exception IllegalArgumentException If the preconditions on the parameters do not hold |
54 |
{ |
*/ |
55 |
this.channel = channel; |
protected FileLock (FileChannel channel, long position, long size, |
56 |
this.position = position; |
boolean shared) |
57 |
this.size = size; |
{ |
58 |
this.shared = shared; |
if (position < 0 || |
59 |
} |
size < 0) |
60 |
|
throw new IllegalArgumentException (); |
61 |
|
|
62 |
|
this.channel = channel; |
63 |
|
this.position = position; |
64 |
|
this.size = size; |
65 |
|
this.shared = shared; |
66 |
|
} |
67 |
|
|
68 |
public abstract boolean isValid(); |
/** |
69 |
public abstract void release(); |
* Tells whether or not this lock is valid. |
70 |
|
*/ |
71 |
|
public abstract boolean isValid(); |
72 |
public FileChannel channel() |
|
73 |
{ |
/** |
74 |
return channel; |
* Releases this lock. |
75 |
} |
* |
76 |
|
* @exception IOException If an error occurs |
77 |
public boolean isShared() |
* @exception ClosedChannelException If the locked channel is no longer open. |
78 |
{ |
*/ |
79 |
return shared; |
public abstract void release (); |
80 |
} |
|
81 |
|
/** |
82 |
public boolean overlaps(long position, long size) |
* Returns the file channel upon whose file this lock is held. |
83 |
{ |
*/ |
84 |
if (position > this.position+this.size) |
public final FileChannel channel () |
85 |
return false; |
{ |
86 |
|
return channel; |
87 |
if (position+size < this.position) |
} |
88 |
return false; |
|
89 |
|
/** |
90 |
return true; |
* Tells whether this lock is shared. |
91 |
} |
*/ |
92 |
|
public final boolean isShared () |
93 |
public long position() |
{ |
94 |
{ |
return shared; |
95 |
return position; |
} |
96 |
} |
|
97 |
|
/** |
98 |
|
* Tells whether or not this lock overlaps the given lock range. |
99 |
|
*/ |
100 |
|
public final boolean overlaps (long position, long size) |
101 |
|
{ |
102 |
|
if (position > this.position +this.size) |
103 |
|
return false; |
104 |
|
|
105 |
|
if (position + size < this.position) |
106 |
|
return false; |
107 |
|
|
108 |
|
return true; |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
|
* Returns the position within the file of the first byte of the |
113 |
|
* locked region. |
114 |
|
*/ |
115 |
|
public final long position () |
116 |
|
{ |
117 |
|
return position; |
118 |
|
} |
119 |
|
|
120 |
public long size() |
/** |
121 |
{ |
* Returns the size of the locked region in bytes. |
122 |
return size; |
*/ |
123 |
} |
public final long size () |
124 |
|
{ |
125 |
public String toString() |
return size; |
126 |
{ |
} |
127 |
return "file-lock:pos="+position+"size="+size; |
|
128 |
} |
/** |
129 |
|
* Returns a string describing the range, type, and validity of this lock. |
130 |
|
*/ |
131 |
|
public final String toString () |
132 |
|
{ |
133 |
|
return "file-lock:pos=" + position + "size=" + size; |
134 |
|
} |
135 |
} |
} |