Skip to content

Commit eefc448

Browse files
committed
cleanup
1 parent ff50fba commit eefc448

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.nio.file.attribute.FileTime;
1616
import java.time.Instant;
1717
import java.util.Optional;
18+
import java.util.concurrent.atomic.AtomicInteger;
1819
import java.util.concurrent.atomic.AtomicLong;
1920
import java.util.concurrent.atomic.AtomicReference;
2021

@@ -33,11 +34,11 @@ public class OpenCryptoFile implements Closeable {
3334
private final AtomicLong fileSize;
3435
private final OpenCryptoFileComponent component;
3536

36-
private volatile int openChannelsCount = 0;
37+
private final AtomicInteger openChannelsCount = new AtomicInteger(0);
3738

3839
@Inject
3940
public OpenCryptoFile(FileCloseListener listener, ChunkCache chunkCache, Cryptor cryptor, FileHeaderHolder headerHolder, ChunkIO chunkIO, //
40-
@CurrentOpenFilePath AtomicReference<Path> currentFilePath, @OpenFileSize AtomicLong fileSize,
41+
@CurrentOpenFilePath AtomicReference<Path> currentFilePath, @OpenFileSize AtomicLong fileSize, //
4142
@OpenFileModifiedDate AtomicReference<Instant> lastModified, OpenCryptoFileComponent component) {
4243
this.listener = listener;
4344
this.chunkCache = chunkCache;
@@ -65,7 +66,7 @@ public synchronized FileChannel newFileChannel(EffectiveOpenOptions options, Fil
6566
FileChannel ciphertextFileChannel = null;
6667
CleartextFileChannel cleartextFileChannel = null;
6768

68-
openChannelsCount = openChannelsCount + 1; // synchronized context, hence we can proactively increase the number
69+
openChannelsCount.incrementAndGet(); // synchronized context, hence we can proactively increase the number
6970
try {
7071
ciphertextFileChannel = path.getFileSystem().provider().newFileChannel(path, options.createOpenOptionsForEncryptedFile(), attrs);
7172
initFileHeader(options, ciphertextFileChannel);
@@ -78,7 +79,6 @@ public synchronized FileChannel newFileChannel(EffectiveOpenOptions options, Fil
7879
cleartextFileChannel = component.newChannelComponent() //
7980
.create(ciphertextFileChannel, options, this::cleartextChannelClosed) //
8081
.channel();
81-
chunkIO.registerChannel(ciphertextFileChannel,options.writable());
8282
} finally {
8383
if (cleartextFileChannel == null) { // i.e. something didn't work
8484
cleartextChannelClosed(ciphertextFileChannel);
@@ -87,6 +87,7 @@ public synchronized FileChannel newFileChannel(EffectiveOpenOptions options, Fil
8787
}
8888

8989
assert cleartextFileChannel != null; // otherwise there would have been an exception
90+
chunkIO.registerChannel(ciphertextFileChannel, options.writable());
9091
return cleartextFileChannel;
9192
}
9293

@@ -175,12 +176,10 @@ public void updateCurrentFilePath(Path newFilePath) {
175176
}
176177

177178
private synchronized void cleartextChannelClosed(FileChannel ciphertextFileChannel) {
178-
if( ciphertextFileChannel != null ){
179+
if (ciphertextFileChannel != null) {
179180
chunkIO.unregisterChannel(ciphertextFileChannel);
180181
}
181-
182-
openChannelsCount = openChannelsCount - 1;
183-
if (openChannelsCount == 0) {
182+
if (openChannelsCount.decrementAndGet() == 0) {
184183
close();
185184
}
186185
}

0 commit comments

Comments
 (0)