23
23
import java .nio .BufferOverflowException ;
24
24
import java .nio .ByteBuffer ;
25
25
import java .nio .channels .FileChannel ;
26
+ import java .nio .channels .SeekableByteChannel ;
26
27
import java .nio .file .Files ;
27
28
import java .nio .file .Path ;
28
29
import java .nio .file .StandardCopyOption ;
42
43
import org .exist .util .ReadOnlyException ;
43
44
import org .exist .util .sanity .SanityCheck ;
44
45
46
+ import static java .nio .file .StandardOpenOption .CREATE_NEW ;
47
+ import static java .nio .file .StandardOpenOption .WRITE ;
45
48
import static org .exist .util .ThreadUtils .newInstanceThread ;
46
49
47
50
/**
@@ -135,8 +138,7 @@ public final class Journal {
135
138
* the current output channel
136
139
* Only valid after switchFiles() was called at least once!
137
140
*/
138
- private FileOutputStream os ;
139
- private FileChannel channel ;
141
+ private SeekableByteChannel channel ;
140
142
141
143
/**
142
144
* Syncing the journal is done by a background thread
@@ -474,12 +476,9 @@ public void switchFiles() throws LogException {
474
476
synchronized (latch ) {
475
477
close ();
476
478
try {
477
- //RandomAccessFile raf = new RandomAccessFile(file, "rw");
478
- os = new FileOutputStream (file .toFile (), true );
479
- channel = os .getChannel ();
480
-
481
- fileSyncRunnable .setChannel (channel );
482
- } catch (final FileNotFoundException e ) {
479
+ channel = Files .newByteChannel (file , CREATE_NEW , WRITE );
480
+ fileSyncRunnable .setChannel ((FileChannel ) channel );
481
+ } catch (final IOException e ) {
483
482
throw new LogException ("Failed to open new journal: " + file .toAbsolutePath ().toString (), e );
484
483
}
485
484
}
@@ -496,13 +495,6 @@ public void close() {
496
495
LOG .warn ("Failed to close journal channel" , e );
497
496
}
498
497
}
499
- if (os != null ) {
500
- try {
501
- os .close ();
502
- } catch (final IOException e ) {
503
- LOG .warn ("Failed to close journal output stream" , e );
504
- }
505
- }
506
498
}
507
499
508
500
private static int journalFileNum (final Path path ) {
@@ -609,10 +601,10 @@ static String getFileName(final int fileNum) {
609
601
}
610
602
611
603
private static class RemoveRunnable implements Runnable {
612
- private final FileChannel channel ;
604
+ private final SeekableByteChannel channel ;
613
605
private final Path path ;
614
606
615
- RemoveRunnable (final FileChannel channel , final Path path ) {
607
+ RemoveRunnable (final SeekableByteChannel channel , final Path path ) {
616
608
this .channel = channel ;
617
609
this .path = path ;
618
610
}
0 commit comments