Skip to content

Commit

Permalink
Implement AsyncFileWriter.truncate()
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Feb 10, 2024
1 parent 7f06107 commit a30d4d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;

import java.io.IOException;

import java.util.concurrent.CompletableFuture;

import lombok.Getter;
Expand All @@ -36,6 +38,18 @@ public AsyncFileChannelWriter(Path filePath, AsynchronousFileChannel fileChannel
this.fileChannel = fileChannel;
}

@Override
public CompletableFuture<Void> truncate() {
var completableFuture = new CompletableFuture<Void>();
try {
fileChannel.truncate(0);
completableFuture.complete(null);
} catch (IOException e) {
completableFuture.completeExceptionally(e);
}
return completableFuture;
}

@Override
public CompletableFuture<Integer> write(byte[] data, int offset) {
var byteBuffer = ByteBuffer.wrap(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.concurrent.CompletableFuture;

public interface AsyncFileWriter {
CompletableFuture<Void> truncate();

CompletableFuture<Integer> write(byte[] data, int offset);

Path getFilePath();
Expand Down

0 comments on commit a30d4d5

Please sign in to comment.