diff --git a/SQLite/Core/Blob.swift b/SQLite/Core/Blob.swift index 1b30ffa1..034f2481 100644 --- a/SQLite/Core/Blob.swift +++ b/SQLite/Core/Blob.swift @@ -22,26 +22,58 @@ // THE SOFTWARE. // -public struct Blob { +import Foundation - public let bytes: [UInt8] - public init(bytes: [UInt8]) { - self.bytes = bytes +public final class Blob { + + public let data: NSData + public var bytes: UnsafePointer { + return self.data.bytes } - - public init(bytes: UnsafePointer, length: Int) { - self.init(bytes: [UInt8](UnsafeBufferPointer( - start: UnsafePointer(bytes), count: length - ))) + public var length: Int { + return self.data.length + } + + public convenience init(bytes: [UInt8]) { + let buffer = UnsafeMutablePointer.alloc(bytes.count) + for idx in 0..(buffer), + length: bytes.count, + freeWhenDone: true + ) + self.init(data: data) + } + + public convenience init(bytes: UnsafePointer, length: Int) { + self.init(data: NSData(bytes: bytes, length: length)) } + + public init(data: NSData) { + self.data = data + } + +} +extension Blob { + public func toHex() -> String { - return bytes.map { - ($0 < 16 ? "0" : "") + String($0, radix: 16, uppercase: false) - }.joinWithSeparator("") + let bytes = UnsafePointer(self.bytes) + + var hex = "" + for idx in 0.. NSData { - return NSData(bytes: dataValue.bytes, length: dataValue.bytes.count) + return dataValue.data } - + public var datatypeValue: Blob { - return Blob(bytes: bytes, length: length) + return Blob(data: self) } } + extension NSDate : Value { public class var declaredDatatype: String {