File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed
src/main/java/io/josemmo/bukkit/plugin/storage Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -114,6 +114,7 @@ public int getDelay() {
114
114
private void load () {
115
115
// Try to load maps from disk
116
116
if (exists () && getLastModified () > imageFile .getLastModified ()) {
117
+ LOGGER .fine ("Found warm cache file \" " + path + "\" " );
117
118
try {
118
119
loadFromDisk ();
119
120
return ;
@@ -125,6 +126,7 @@ private void load() {
125
126
}
126
127
127
128
// Generate maps from image file
129
+ LOGGER .fine ("Missed cache file \" " + path + "\" " );
128
130
try {
129
131
generateFromImage ();
130
132
tryToWriteToDisk ();
Original file line number Diff line number Diff line change 4
4
import org .jetbrains .annotations .NotNull ;
5
5
import java .io .IOException ;
6
6
import java .io .RandomAccessFile ;
7
+ import java .nio .channels .FileChannel ;
8
+ import java .nio .channels .FileLock ;
9
+ import java .nio .channels .OverlappingFileLockException ;
7
10
import java .nio .file .Path ;
8
11
9
12
/**
@@ -17,10 +20,29 @@ public class SynchronizedFileStream extends RandomAccessFile {
17
20
* @param readOnly Whether to open stream in read-only mode
18
21
* @throws IOException if failed to acquire lock
19
22
*/
20
- @ Blocking
21
23
public SynchronizedFileStream (@ NotNull Path path , boolean readOnly ) throws IOException {
22
24
super (path .toFile (), readOnly ? "r" : "rw" );
23
- //noinspection ResultOfMethodCallIgnored
24
- getChannel ().lock (0L , Long .MAX_VALUE , readOnly );
25
+ waitForLock (readOnly );
26
+ }
27
+
28
+ /**
29
+ * Wait for lock to be released
30
+ * @param readOnly Whether to acquire read-only (shared) lock
31
+ */
32
+ @ Blocking
33
+ private void waitForLock (boolean readOnly ) throws IOException {
34
+ FileChannel channel = getChannel ();
35
+ FileLock lock = null ;
36
+ while (lock == null ) {
37
+ try {
38
+ lock = channel .lock (0L , Long .MAX_VALUE , readOnly );
39
+ } catch (OverlappingFileLockException e ) {
40
+ try {
41
+ Thread .sleep (20 );
42
+ } catch (InterruptedException __ ) {
43
+ throw e ;
44
+ }
45
+ }
46
+ }
25
47
}
26
48
}
You can’t perform that action at this time.
0 commit comments