Created
June 7, 2014 20:27
-
-
Save samdmarshall/e1049f9d3c6d5998ef76 to your computer and use it in GitHub Desktop.
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
class DataBuffer { | |
var internalData: NSData; | |
init(fromData: NSData) { | |
self.internalData = NSData.dataWithData(fromData) as NSData; | |
} | |
init(fromFilePath: String) { | |
self.internalData = NSData.dataWithContentsOfFile(fromFilePath, options: .DataReadingMappedIfSafe, error: nil) as NSData; | |
} | |
// subscripting syntax to access raw pointer to bytes from offset ORIGIN | |
// e.g.: var pointer = myData[<offset from origin>] | |
subscript(origin: Int) -> UnsafePointer<UInt8> { | |
get { | |
var result: UnsafePointer<UInt8> = nil; | |
var dataLength = self.internalData.length(); | |
if (origin < dataLength) { | |
var newLength = dataLength - origin; | |
var tempData = self.internalData.subdataWithRange(NSMakeRange(origin, newLength)); | |
result = UnsafePointer<UInt8>(tempData.bytes()); | |
} | |
return result; | |
} | |
} | |
// subscripting syntax to access SIZE number of bytes from offset ORIGIN | |
// e.g.: var value = myData[<offset from origin>, <number of bytes>] | |
subscript(origin: Int, size: Int) -> CChar[] { | |
get { | |
var result: CChar[] = []; | |
var dataLength = self.internalData.length(); | |
if (origin < dataLength) { | |
var tempData = self.internalData.subdataWithRange(NSMakeRange(origin, size)); | |
for index in 0...(tempData.length()-1) { | |
var byte = UnsafePointer<CChar>(tempData.bytes())[index]; | |
result.append(byte); | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment