diff options
| author | Lena <lena@omega> | 2026-01-01 00:00:00 +0000 |
|---|---|---|
| committer | Lena <lena@omega> | 2026-06-24 22:14:50 +0300 |
| commit | eb0c8951196c637e44daf3c0617b131b997d5d2c (patch) | |
| tree | 2f83b6a41cee745467e53fc401942b82cea286ea /app/src/main/java/invalid/lena/scrcpy/AtomicFiles.java | |
| download | scrcpy-android-0.1.tar.gz | |
scrcpy-android: mirror an Android device over wireless ADB0.1
Native Java app for Android 12+ that mirrors another Android
device over wireless ADB, forwarding video, audio, touch input,
and clipboard. Bundles a pinned scrcpy-server.jar and the
vendored libadb-android stack. Supports h264/h265/av1 video and
raw/opus audio with in-app codec selection. No NDK, no Kotlin.
Includes JVM unit tests and a Docker-based emulator e2e rig.
Diffstat (limited to 'app/src/main/java/invalid/lena/scrcpy/AtomicFiles.java')
| -rw-r--r-- | app/src/main/java/invalid/lena/scrcpy/AtomicFiles.java | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/app/src/main/java/invalid/lena/scrcpy/AtomicFiles.java b/app/src/main/java/invalid/lena/scrcpy/AtomicFiles.java new file mode 100644 index 0000000..6f755b6 --- /dev/null +++ b/app/src/main/java/invalid/lena/scrcpy/AtomicFiles.java @@ -0,0 +1,40 @@ +package invalid.lena.scrcpy; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +// Crash-atomic whole-file write: stage the bytes into a sibling temp file, +// fsync it, then rename it over the destination. The rename is the only +// mutation a concurrent reader can observe, so a reader sees either the old +// file or the new file in full, never a truncated mix. A crash mid-write +// leaves at most a stale ".tmp", never a damaged destination. +// +// Deliberately no fsync of the parent directory: the rename itself may be +// lost on power failure (the old content survives intact). Callers store +// re-creatable state, so atomicity matters here and durability does not. +// +// android-free (java.io/java.nio only) so it is unit-testable on the JVM. +final class AtomicFiles { + + private AtomicFiles() {} + + static void write(File dest, byte[] data) throws IOException { + File parent = dest.getAbsoluteFile().getParentFile(); + File tmp = new File(parent, dest.getName() + ".tmp"); + try (FileOutputStream os = new FileOutputStream(tmp)) { + os.write(data); + os.flush(); + os.getFD().sync(); + } + try { + Files.move(tmp.toPath(), dest.toPath(), + StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + tmp.delete(); + throw e; + } + } +} |