import static java.util.Objects.requireNonNull;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import org.eclipse.jdt.annotation.NonNull;
* Hash of a single file state. {@link #size()} corresponds to {@link BasicFileAttributes#size()}.
*/
record FileState(@NonNull String path, long size, int crc32) {
+ /**
+ * A simple interface describing the intended content of a file.
+ */
+ @FunctionalInterface
+ interface FileContent {
+ /**
+ * Write the content of this file to specified {@link OutputStream}.
+ *
+ * @param out OutputStream to write to, guaranteed to be non-null
+ * @throws IOException if any error occurs
+ */
+ void writeTo(@NonNull OutputStream out) throws IOException;
+ }
+
FileState {
requireNonNull(path);
}
+
+ static @NonNull FileState ofWrittenFile(final File file, final FileContent content) throws IOException {
+ try (var out = new CapturingOutputStream(Files.newOutputStream(file.toPath()))) {
+ content.writeTo(out);
+ return new FileState(file.getPath(), out.size(), out.crc32c());
+ }
+ }
}