Last active
August 16, 2018 18:43
-
-
Save langford/dac981d8fe1291a593f627faae643594 to your computer and use it in GitHub Desktop.
Chunking data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
struct ChunkedData{ | |
let chunkCount:Int | |
let chunkSize:Int | |
let byteCount:Int | |
let originalData:Data | |
let chunks:[Data] | |
init(data:Data,chunkSizeInBytes:Int){ | |
self.originalData = data | |
self.byteCount = data.count | |
self.chunkSize = chunkSizeInBytes | |
self.chunkCount = byteCount/chunkSize | |
var chunksForData:[Data] = [] | |
//todo | |
self.chunks = chunksForData | |
} | |
} | |
struct ChunkMessage{ | |
let chunk:Data | |
let index:Int | |
} | |
///locate this in your bluetooth communication file | |
extension Data{ | |
//Used to send a specific chunk | |
func sendChunk(to:String,chunkIndexMetadata:Int){ | |
let msg = ChunkMessage(chunk:self, index:chunkIndexMetadata) | |
//implement bluetooth send | |
} | |
} | |
///locate this in hyour bluetooth communication file | |
extension ChunkedData{ | |
func resendChunks(to destination:String,chunkIndicies:[Int]){ | |
for i in chunkIndicies{ | |
chunks[i].sendChunk(to: destination, chunkIndexMetadata: i) | |
} | |
} | |
} | |
let s = "aoisdjofaidsjfsfdijfoaisjfdoafisjdfoiajsfosdijfaosidfjsoafijoifj" | |
let data = s.data(using: .utf8)! | |
let chunks = ChunkedData(data: data, chunkSizeInBytes: 10).chunks | |
chunks.map { chunk in | |
let string = String(decoding: chunk, as: UTF8.self) | |
print("\(string) is a chunk") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment