The scenario: an unknown document size reaches the phone
A document picker can return a readable URI without giving the application a size it should trust. Reading the entire stream before checking its length can consume unnecessary memory or block progress on oversized input. The client needs an actual byte budget at the read boundary, before it hands a complete byte array to the upload service.
The pinned Movo-X Android episode-document path uses a bounded InputStream helper. Two existing and three supplemental JVM cases passed with synthetic streams, including an endless source and an injected read failure. The native picker, upload adapter and selected server route were inspected separately. No real PDF or patient attachment was read.
Enforce the budget from bytes consumed
The helper requires a nonnegative maximum, accumulates accepted bytes and limits each bulk-read request to the remaining budget plus one. The extra byte distinguishes a stream ending exactly at the limit from one that exceeds it. It refuses overflow before copying that over-budget chunk into its output.
The existing fixture preserves an exact-limit byte array and an empty stream at a zero budget. Another supplies an endless stream under a 32-byte budget and observes 33 bytes consumed before refusal. This is a focused demonstration of stopping after the overflow indicator, not a benchmark of every provider or a claim that reading can never block.
Handle a zero bulk read without treating it as successful EOF
A stream can return zero from a bulk-read attempt in a selected edge case. The source then tries a single-byte read, checks whether that byte fits and continues rather than interpreting zero as the end of the document.
A supplemental fixture returns zero for its first bulk call and then supplies three synthetic bytes. The complete result is preserved. This tests the fallback path using a controlled implementation; it does not establish how every Android document provider behaves, or make a blocking single-byte read cancellable by itself. Provider errors and time/cancellation policy need separate acceptance.
Do not turn a read failure into a partial document
A second supplemental stream supplies an initial chunk and then throws an IOException. The helper propagates that selected exception instead of returning its accumulated prefix as if it were the complete document. Another case refuses a negative budget before any input is read.
These local cases give the caller a clear distinction between a completed bounded read and a failed one. They do not validate the content of a successful result. A short file can still be an invalid PDF, an unsuitable document or the wrong attachment for the selected person. Successful byte acquisition is only one stage of the workflow.
Let the owner of the stream close it
The helper accepts an existing stream and does not close it. Its supplemental fixture checks that closing remains under caller control. In the inspected native picker, openInputStream is wrapped in use while the bounded read runs on the I/O dispatcher. That placement gives the opening code responsibility for resource release.
The JVM fixture uses an in-memory stream with a close flag, not an actual ContentResolver descriptor. Review denied/revoked URI permission, provider exceptions, close failures and cancellation on the intended Android build. A source-level use block is not proof of physical descriptor behaviour under every provider or process interruption.
Trace the selected eight-MiB document path
The inspected episode picker requests application/pdf and calls the helper with an eight-MiB raw-byte limit. After a successful read, its service passes those bytes, the selected person/episode path and request identifier to the raw-body upload adapter. The caller marks the selected request-key slot done only after that service returns. These native integration branches were not executed here.
The helper still builds a complete accepted byte array in memory before upload. It is a bounded read, not an end-to-end streaming upload or a complete application memory ceiling. Buffer copies, transport implementation, decoded structures and concurrent work have separate costs. Do not infer a device memory benchmark from one input-size guard.
Retain independent server validation and persistence decisions
The selected server branch reads its own bounded upload, checks PDF shape/screening and page count, then asks the authoritative API to reserve an attachment before its storage write and completion call. Its named raw-byte limit matches the selected client path, while page and format decisions remain server responsibilities.
Those server branches were source-reviewed without PDF execution or a database transaction in this batch. A picker MIME filter and a client limit are not authoritative permission or content validation. Likewise, the inspected PDF checks do not newly establish malware isolation, clinical accuracy, durable attachment linkage or physical storage acceptance.
Keep an unreadable input separate from an uncertain upload
The native caller can know that local acquisition failed before it sent bytes. A later transport interruption has a different uncertainty: a server may have received, reserved or stored data even when the client cannot confirm the response. The existing upload-completion chapter treats those phases separately.
A release package should retain stable retry identity for the intended payload and reconcile server state before making a new completion claim. The five bounded-read cases do not execute transport retries or verify that a URI still contains the same bytes on a later attempt. Byte identity, request identity and durable completion require their own explicit contract.
A practical native attachment acceptance package
Keep the five local stream cases and the pinned caller/service path. Add controlled provider fixtures for missing metadata, exact/oversized input, temporary permission loss, read/close failures and cancellation. Exercise actual upload refusals and response loss with generated nonpatient documents in approved isolated environments.
For a CTO building a medical application, this creates a useful division: local acquisition enforces a byte budget, the upload service transports an intended payload, the server evaluates permission/content and persistence establishes an attachment. The selected local cases support the first boundary. They do not turn a completed read into a verified clinical document or a successfully stored record.
Compare the reviewed boundaries
A document passes several independent boundaries
On smaller screens, scroll across to compare every column. Keyboard users can focus the table and use the arrow keys.
| Boundary | Selected evidence | Separate acceptance |
|---|---|---|
| Local acquisition | Five synthetic stream cases | Real provider, permission and cancellation |
| Resource ownership | Close-flag case and caller use source | Actual descriptor/process behaviour |
| Transport | Raw-body adapter source only | Payload identity, retry and response loss |
| Server acceptance | Selected PDF/reserve/store source | Installed authorization, parser and durable linkage |
Illustrative control flow
Pseudocode summarizing the decision boundary. It is not copied production code or a deployment instruction.
stream = open_selected_document_uri()
with caller_owned_close(stream):
bytes = read_with_actual_byte_budget(stream, maximum)
upload_raw_bytes_with_stable_request_identity(bytes)
# Completed local acquisition is separate from server content, access and storage acceptance.About this engineering series
Written by Omer Muneer Qazi from a review of Movo-X implementation and dated engineering records. This article explains source behaviour and historical observations; current production, clinical outcomes and store availability require their own verification. Examples are synthetic and proposed test cases are identified as such.
Explore the Movo-X engineering series → · Related implementation experience →
Discuss a similar engineering problem
Send the workflow, current platform and the decision your team needs to make. I can help define an implementation or review scope with concrete acceptance evidence.
Discuss your project on WhatsApp ↗