The BinaryHandler is a flexible and powerful Swift library designed to handle binary data streams efficiently. It provides classes and protocols for reading, writing, and manipulating binary data with support for seeking, file I/O, and more.
Create your own binary data structure by using BinaryReader and BinaryWriter from a given Readable or Writable, or accomplish both with BinaryStream, which uses a given Streamable.
For convenience, the library comes with the following Readables and Writables:
FileReader(Readable)FileWriter(Writable)FileStream(Streamable)MemoryStream(Streamable)
letmemoryData=MemoryStream(withData:Data())letstream=BinaryStream(source: memoryData, byteOrder:.littleEndian)do{try stream.write(UInt8(1))try stream.write(Int16(1234))try stream.seekTo(position:0)letbyte=try stream.readUInt8()print(byte) // Output: 1
letint16=try stream.readInt16()print(int16) // Output: 1234
}catch{print("Error handling stream: \(error)")}iflet fileStream =FileStream(filePath:"path/to/file"){do{
// Write data to file
letdataToWrite:[UInt8]=[0x01,0x02,0x03]try fileStream.writeBytes(dataToWrite)
// Seek back to the beginning of the file
try fileStream.seekTo(position:0)
// Read data from file
letbytes=try fileStream.readBytes(count:3)print(bytes) // Output: [1, 2, 3]
}catch{print("Error handling file stream: \(error)")}}You can also use FileReader or FileWriter for specific cases.
.package(url:"https://github.com/brustolin/BinaryHandler-Swift.git", from:"2.0.0")