This document provides a variety of cookbook examples demonstrating usage of the Binary Codable library.
import BinaryCodable
struct <#DecodableObject#>: BinaryDecodable {
init(from decoder: BinaryDecoder) throws {
var container = decoder.container(maxLength: <#maxLengthOrNil#>)
<#decoding logic#>
}
}
let fileUrl = URL(fileURLWithPath: <#path#>)
let data = try Data(contentsOf: fileUrl)
let decoder = BinaryDataDecoder()
let <#decodedObject#> = try decoder.decode(<#BinaryDecodableType#>.self, from: data)
import BinaryCodable
struct <#EncodableObject#>: BinaryEncodable {
func encode(to encoder: BinaryEncoder) throws {
var container = encoder.container()
<#encoding logic#>
}
}
let object = <#EncodableObject#>
let encoder = BinaryDataEncoder()
let data = try encoder.encode(object)
import BinaryCodable
import Socket // E.g. from https://github.com/IBM-Swift/BlueSocket.git
let socket = try Socket.create()
try socket.connect(to: host, port: port)
guard socket.isConnected else {
return nil
}
var buffer = Data(capacity: socket.readBufferSize)
var bufferedData = BufferedData(reader: AnyBufferedDataSource(read: { length in
if buffer.count == 0 {
_ = try socket.read(into: &buffer)
}
let pulledData = buffer.prefix(length)
buffer = buffer.dropFirst(length)
return pulledData
}, isAtEnd: {
do {
return try buffer.isEmpty && !socket.isReadableOrWritable(waitForever: false, timeout: 0).readable
} catch {
return true
}
}))
let decoder = BinaryDataDecoder()
let data = try decoder.decode(<#BinaryDecodableType#>.self, from: bufferedData)
import BinaryCodable
struct <#DecodableObject#>: BinaryDecodable {
init(from decoder: BinaryDecoder) throws {
var container = decoder.container(maxLength: <#maxLengthOrNil#>)
let string = try container.decodeString(encoding: .utf8, terminator: 0)
}
}
import BinaryCodable
struct <#DecodableObject#>: BinaryDecodable {
init(from decoder: BinaryDecoder) throws {
var container = decoder.container(maxLength: <#maxLengthOrNil#>)
var stringContainer = container.nestedContainer(maxLength: <#length#>)
let string = try stringContainer.decodeString(encoding: .utf8, terminator: nil)
}
}
import BinaryCodable
struct <#EncodableObject#>: BinaryEncodable {
func encode(to encoder: BinaryEncoder) throws {
var container = encoder.container()
try container.encode("String", encoding: .utf8, terminator: 0)
}
}
import BinaryCodable
struct <#DecodableObject#>: BinaryDecodable {
init(from decoder: BinaryDecoder) throws {
var container = decoder.container(maxLength: nil)
let data = try container.decode(length: <#T##Int#>)
}
}
import BinaryCodable
struct <#EncodableObject#>: BinaryEncodable {
func encode(to encoder: BinaryEncoder) throws {
var container = encoder.container()
try container.encode(sequence: <#T##Sequence#>)
}
}