liveBook · Manning

Atomic query

As explained in 100 Java Mistakes and How to Avoid Them

An atomic query is a retrieval operation whose observed results are obtained as one indivisible filesystem action.

An atomic query obtains related filesystem observations through one indivisible action. This prevents a path from changing between separate checks, such as testing whether it is a regular file and then retrieving its size. section 9.11

Separate filesystem requests can observe different states: another process may replace a regular file with a directory, or modify the file after its size is checked. Therefore, a size check does not guarantee how much data a later read will consume. section 9.11

Prefer these defensive patterns:

  • Avoid assuming that Files.size(), File.length(), or similar results still match file content when reading later. section 9.11

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains a race, demonstrates a robust attribute query, and defers filesystem-content guarantees to bounded reading.
Examples
Uses regular-file size retrieval, replacement by a directory, and a one-megabyte guarded file read.
What it assumes
Assumes familiarity with Java filesystem operations, paths, streams, and exception-throwing methods.
What you can do after
Apply it via Java Files APIs to obtain consistent attributes and limit reads without trusting stale file sizes.
How it connects
Connects filesystem metadata, race conditions, file attributes, and bounded input streams.

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.