Common Lisp Package: BINARY-FILE

BINARY-FILE The binary file package contains utilities to read and write binary files. The utilities support writing bytes of size 8 bits (octets) and currently only two's complement signed encoding is supported. The package has also in-memory binary stream which can be used as a file stream.

README:

FUNCTION

Public

BINARY-STREAM-LENGTH (STREAM)

Returns length of stream.

MAKE-BINARY-ARRAY-INPUT-STREAM (&OPTIONAL INITIAL-CONTENTS)

Returns new binary array input stream. The initial-contents is sequence of bytes and if given the stream contains bytes in initial-contents.

MAKE-BINARY-ARRAY-IO-STREAM (&OPTIONAL INITIAL-CONTENTS)

Returns new binary array input/output stream. The initial-contents is sequence of bytes and if given the stream contains bytes in initial-contents.

MAKE-BINARY-ARRAY-OUTPUT-STREAM (&OPTIONAL INITIAL-CONTENTS)

Returns new binary array output stream. The initial-contents is sequence of bytes and if given the stream contains bytes in initial-contents.

OPEN-BINARY-STREAM (FILESPEC &KEY (IF-EXISTS OVERWRITE) (IF-DOES-NOT-EXIST ERROR))

Opens and returns binary stream. If filespec is a string or pathname, the designed file is opened. If filespec is stream, filespec is returned.

READ-ARRAY (STREAM SIZE &OPTIONAL (ELEMENT-READER #'READ-BYTE))

Reads and returns array of values. Each value is read with element-reader.

READ-BYTES (STREAM SIZE &OPTIONAL OFFSET)

Reads and returns size bytes from stream. If offset is given, stream is positioned to that offset before reading.

READ-INTEGER (STREAM &KEY (BYTES 1) (SIGNED NIL) (ENDIANNESS LITTLE-ENDIAN))

Reads and returns integer from stream. Integer size is (8 * bytes) bits. If signed is t integer is interpreted as signed, otherwise unsigned. Byte order is defined by endianness.

READ-INTEGERS (STREAM SIZE &KEY (BYTES 1) (SIGNED NIL) (ENDIANNESS LITTLE-ENDIAN))

Reads and returns array of integers of size bytes.

WRITE-INTEGER (VALUE STREAM &KEY (BYTES 1) (ENDIANNESS LITTLE-ENDIAN))

Writes integer to stream as unsigned bytes. Byte order is defined by endianness.

WRITE-INTEGERS (SEQ STREAM &KEY (BYTES 1) (ENDIANNESS LITTLE-ENDIAN))

Write sequence of integers to binary stream.

Private

ENSURE-BYTE-ARRAY (INPUT-BYTE-SEQ)

Returns byte array suitable for binary array stream. input-byte-seq is sequence of bytes which is converted to suitable byte array if necessary.

INT-BYTES (INT BYTES &AUX (BYTES-1 (1- BYTES)))

Returns bytes from given (positive) integer least significant byte first.

TWOS-COMPLEMENT (VALUE BYTES)

Returns two's complement of given value. Bytes is size of value in bytes.

TWOS-COMPLEMENT-SIGNED (VALUE BYTES &AUX (BITS (* BYTES 8)) (SIGN-BIT (1- BITS)))

Returns signed integer. Value is in two's complement format and result has bytes * 8 bits.

MACRO

Public

WITH-INPUT-FROM-BINARY-ARRAY ((VAR ARRAY &KEY (INDEX 0 INDEX-P) START END) &BODY BODY)

Binds var to binary array input stream with given initial contents and executes body with var available for reading.

WITH-OUTPUT-TO-BINARY-ARRAY ((STREAM &OPTIONAL ARRAY-FORM) &BODY BODY)

Returns byte array after executing the body from binary array stream.

GENERIC-FUNCTION

Public

BINARY-ARRAY-STREAM-P (OBJ)

Returns t, if obj is binary-array-stream, nil otherwise.

SLOT-ACCESSOR

Public

Undocumented

BINARY-ARRAY (OBJECT)

Private

Undocumented

BINARY-ARRAY-POSITION (OBJECT)

SETFBINARY-ARRAY-POSITION (NEW-VALUE OBJECT)

CLASS

Public

BINARY-ARRAY-INPUT-STREAM

The binary array input stream for reading data from binary stream.

BINARY-ARRAY-IO-STREAM

The binary array input/output stream for both reading and writng data from and to binary stream.

BINARY-ARRAY-OUTPUT-STREAM

The binary array output stream for writing data to binary stream.

BINARY-ARRAY-STREAM

The base class for binary array streams. The stream is implemented with gray streams. The stream can used to store both characters and bytes. The class has adjustable array for storing stream contents.