Skip to content

Commit

Permalink
test for immutable collection before attempting to remove CREATE_NEW …
Browse files Browse the repository at this point in the history
…option. (#264)
  • Loading branch information
markjschreiber authored Nov 3, 2023
1 parent 003182d commit 558d2ae
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package software.amazon.nio.spi.s3;

import org.slf4j.Logger;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
Expand All @@ -13,7 +14,12 @@
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.*;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand All @@ -31,6 +37,8 @@ class S3WritableByteChannel implements WritableByteChannel {

private boolean open;

Logger logger = org.slf4j.LoggerFactory.getLogger(S3WritableByteChannel.class);

S3WritableByteChannel(S3Path path, S3AsyncClient client, Set<? extends OpenOption> options, Long timeout, TimeUnit timeUnit) throws IOException {
Objects.requireNonNull(path);
Objects.requireNonNull(client);
Expand Down Expand Up @@ -69,7 +77,16 @@ class S3WritableByteChannel implements WritableByteChannel {
}
}

options.remove(StandardOpenOption.CREATE_NEW);
try {
options.remove(StandardOpenOption.CREATE_NEW);
} catch (UnsupportedOperationException e) {
if (options.isEmpty() || !options.contains(StandardOpenOption.CREATE_NEW)) {
// options is immutable but it doesn't matter because the value isn't there anyway.
logger.debug("Could not remove CREATE_NEW option as the operation is unsupported on options", e);
} else {
throw e;
}
}
channel = Files.newByteChannel(this.tempFile, options);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand Down

0 comments on commit 558d2ae

Please sign in to comment.