Skip to content

Commit fb3fb59

Browse files
fixes #59
1 parent cbbd510 commit fb3fb59

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/main/java/org/cryptomator/cryptofs/attr/CryptoBasicFileAttributes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public CryptoBasicFileAttributes(BasicFileAttributes delegate, CiphertextFileTyp
5454
}
5555

5656
private static long getPlaintextFileSize(Path ciphertextPath, long size, Optional<OpenCryptoFile> openCryptoFile, Cryptor cryptor) {
57-
return openCryptoFile.map(OpenCryptoFile::size).orElseGet(() -> calculatePlaintextFileSize(ciphertextPath, size, cryptor));
57+
return openCryptoFile.flatMap(OpenCryptoFile::size).orElseGet(() -> calculatePlaintextFileSize(ciphertextPath, size, cryptor));
5858
}
5959

6060
private static long calculatePlaintextFileSize(Path ciphertextPath, long size, Cryptor cryptor) {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.file.Path;
2626
import java.nio.file.attribute.FileTime;
2727
import java.time.Instant;
28+
import java.util.Optional;
2829
import java.util.concurrent.ConcurrentHashMap;
2930
import java.util.concurrent.ConcurrentMap;
3031
import java.util.concurrent.atomic.AtomicLong;
@@ -146,12 +147,15 @@ private void initFileSize(FileChannel ciphertextFileChannel) throws IOException
146147
}
147148

148149
/**
149-
* @return The size of the opened file
150-
* @throws IllegalStateException If the OpenCryptoFile {@link OpenCryptoFiles#getOrCreate(Path) has been created} without {@link #newFileChannel(EffectiveOpenOptions) creating a file channel} next.
150+
* @return The size of the opened file. Note that the filesize is unknown until a {@link #newFileChannel(EffectiveOpenOptions) file channel is opened}. In this case this method returns an empty optional.
151151
*/
152-
public long size() {
153-
Preconditions.checkState(fileSize.get() != -1l, "size must only be called after a FileChannel is created for this OpenCryptoFile");
154-
return fileSize.get();
152+
public Optional<Long> size() {
153+
long val = fileSize.get();
154+
if (val == -1l) {
155+
return Optional.empty();
156+
} else {
157+
return Optional.of(val);
158+
}
155159
}
156160

157161
public FileTime getLastModifiedTime() {

src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ public void setup() throws IOException {
124124
@Order(0)
125125
@DisplayName("getting size fails before creating first file channel")
126126
public void testGetSizeBeforeCreatingFileChannel() {
127-
Assertions.assertThrows(IllegalStateException.class, () -> {
128-
openCryptoFile.size();
129-
});
127+
Assertions.assertFalse(openCryptoFile.size().isPresent());
130128
}
131129

132130
@Test
@@ -143,7 +141,7 @@ public void createFileChannel() throws IOException {
143141
@Order(11)
144142
@DisplayName("getting size succeeds after creating first file channel")
145143
public void testGetSizeAfterCreatingFirstFileChannel() {
146-
Assertions.assertEquals(0l, openCryptoFile.size());
144+
Assertions.assertEquals(0l, openCryptoFile.size().get());
147145
}
148146

149147
// related to https://github.com/cryptomator/cryptofs/issues/51
@@ -166,7 +164,7 @@ public void errorDuringCreationOfSecondChannel() {
166164
@Order(13)
167165
@DisplayName("getting size succeeds after creating second file channel")
168166
public void testGetSizeAfterCreatingSecondFileChannel() {
169-
Assertions.assertEquals(0l, openCryptoFile.size());
167+
Assertions.assertEquals(0l, openCryptoFile.size().get());
170168
}
171169

172170

0 commit comments

Comments
 (0)