4 Binary Reader
(require binaryio/reader) | package: binaryio-lib |
Added in version 1.1 of package binaryio-lib.
procedure
(make-binary-reader in [ #:limit limit #:error-handler error-handler]) β binary-reader? in : input-port? limit : (or/c exact-nonnegative-integer? #f) = #f error-handler : (binary-reader-error-handler? #f) = #f
Convenience functions for reading binary encodings of integers and floating-point numbers of different lengths, endianness, etc.
The error-handler hook for customizing error message. See make-binary-reader-error-handler for details.
Automatic handling of short reads. If in returns eof or fewer bytes than requested in a read operation on the binary reader, the error-handler is used to raise an error. Thus, for example, the caller of (b-read-bytes br len) can rely on receiving a bytestring of exactly len bytes.
A stack of limits, maintained with b-push-limit and b-pop-limit. On every read operation, the limits are decremented by the number of bytes read. If a read operation requests more bytes than the current limit, the error-handler is used to raise an error.
Binary readers are not thread-safe. Be careful when interleaving uses of a binary reader with direct uses of its input port. For example, direct reads from in do not count against limits imposed on the binary reader.
procedure
(binary-reader? v) β boolean?
v : any/c
procedure
(make-binary-reader-error-handler [ #:error error-callback #:show-data? show-data?-callback]) β binary-reader-error-handler?
error-callback : (or/c #f (->* [binary-reader? symbol? string?] [] #:rest list? none/c)) = #f
show-data?-callback : (or/c #f (-> binary-reader? symbol? boolean?)) = #f
When an error occurs, the error-callback is called with the binary reader, the name of the function that raised the error, and a format string and arguments for the error message. If error-callback is #f, the error procedure is called instead (omitting the binary reader argument). The error-callback must escape, typically by throwing an exception; if it returns an exception is raised.
When a short read occurs, the show-data?-callback determines whether the error message contains the data actually read.
procedure
(binary-reader-error-handler? v) β boolean?
v : any/c
procedure
(b-get-limit br) β (or/c exact-nonnegative-integer? #f)
br : binary-reader?
procedure
(b-at-limit? br) β boolean?
br : binary-reader?
procedure
(b-at-limit/eof? br) β boolean?
br : binary-reader?
procedure
(b-push-limit br limit) β void?
br : binary-reader? limit : exact-nonnegative-integer?
procedure
(b-pop-limit br) β void?
br : binary-reader?
procedure
(b-call/save-limit br proc) β any
br : binary-reader? proc : (-> any)
Added in version 1.2 of package binaryio-lib.
procedure
(b-check-exhausted br what) β void?
br : binary-reader? what : (or/c string? #f)
procedure
(b-read-bytes br len) β bytes?
br : binary-reader? len : exact-nonnegative-integer?
procedure
(b-read-bytes! br bs [start end]) β exact-nonnegative-integer?
br : binary-reader? bs : bytes? start : exact-nonnegative-integer? = 0 end : exact-nonnegative-integer? = (bytes-length bs)
procedure
(b-read-byte br) β byte?
br : binary-reader?
procedure
(b-read-integer br size signed? [big-endian?]) β exact-integer?
br : binary-reader? size : exact-positive-integer? signed? : boolean? big-endian? : boolean? = #t
procedure
(b-read-float br size [big-endian?]) β real?
br : binary-reader? size : (or/c 4 8) big-endian? : boolean? = #t
procedure
(b-read-be-int br size) β exact-integer?
br : binary-reader? size : exact-positive-integer?
procedure
(b-read-be-uint br size) β exact-nonnegative-integer?
br : binary-reader? size : exact-positive-integer?
procedure
(b-read-le-int br size) β exact-integer?
br : binary-reader? size : exact-positive-integer?
procedure
(b-read-le-uint br size) β exact-integer?
br : binary-reader? size : exact-positive-integer?
procedure
(b-read-nul-terminated-bytes br) β bytes?
br : binary-reader?
procedure
(b-read-bytes-line+eol br eol-mode) β
bytes? bytes? br : binary-reader? eol-mode : (or/c 'linefeed 'return 'return-linefeed 'any 'any-one)
Added in version 1.2 of package binaryio-lib.
procedure
(b-read-bytes-line br eol-mode) β bytes?
br : binary-reader? eol-mode : (or/c 'linefeed 'return 'return-linefeed 'any 'any-one)
Added in version 1.2 of package binaryio-lib.