blob: c8a366075a7e83a9626c473522f7cf4edeeb29cf [file] [log] [blame]
Jens Geyer56e5b9b2015-10-09 22:01:55 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20import Foundation
21
22
23public struct TBinary : TSerializable {
24
25 public static var thriftType : TType { return .STRING }
26
27 private var storage : NSData
28
29 public init() {
30 self.storage = NSData()
31 }
32
33 public init(contentsOfFile file: String, options: NSDataReadingOptions = []) throws {
34 self.storage = try NSData(contentsOfFile: file, options: options)
35 }
36
37 public init(contentsOfURL URL: NSURL, options: NSDataReadingOptions = []) throws {
38 self.storage = try NSData(contentsOfURL: URL, options: options)
39 }
40
41 public init?(base64EncodedData base64Data: NSData, options: NSDataBase64DecodingOptions = []) {
42 guard let storage = NSData(base64EncodedData: base64Data, options: options) else {
43 return nil
44 }
45 self.storage = storage
46 }
47
48 public init(data: NSData) {
49 self.storage = data
50 }
51
52 public var length : Int {
53 return storage.length
54 }
55
56 public var hashValue : Int {
57 return storage.hashValue
58 }
59
60 public var bytes : UnsafePointer<Void> {
61 return storage.bytes
62 }
63
64 public func getBytes(buffer: UnsafeMutablePointer<Void>, length: Int) {
65 storage.getBytes(buffer, length: length)
66 }
67
68 public func getBytes(buffer: UnsafeMutablePointer<Void>, range: Range<Int>) {
69 storage.getBytes(buffer, range: NSRange(range))
70 }
71
72 public func subBinaryWithRange(range: Range<Int>) -> TBinary {
73 return TBinary(data: storage.subdataWithRange(NSRange(range)))
74 }
75
76 public func writeToFile(path: String, options: NSDataWritingOptions = []) throws {
77 try storage.writeToFile(path, options: options)
78 }
79
80 public func writeToURL(url: NSURL, options: NSDataWritingOptions = []) throws {
81 try storage.writeToURL(url, options: options)
82 }
83
84 public func rangeOfData(dataToFind data: NSData, options: NSDataSearchOptions, range: Range<Int>) -> Range<Int>? {
85 return storage.rangeOfData(data, options: options, range: NSRange(range)).toRange()
86 }
87
88 public func enumerateByteRangesUsingBlock(block: (UnsafePointer<Void>, Range<Int>, inout Bool) -> Void) {
89 storage.enumerateByteRangesUsingBlock { bytes, range, stop in
90 var stopTmp = Bool(stop.memory)
91 block(bytes, range.toRange()!, &stopTmp)
92 stop.memory = ObjCBool(stopTmp)
93 }
94 }
95
96 public static func readValueFromProtocol(proto: TProtocol) throws -> TBinary {
97 var data : NSData?
98 try proto.readBinary(&data)
99 return TBinary(data: data!)
100 }
101
102 public static func writeValue(value: TBinary, toProtocol proto: TProtocol) throws {
103 try proto.writeBinary(value.storage)
104 }
105
106}
107
108extension TBinary : CustomStringConvertible, CustomDebugStringConvertible {
109
110 public var description : String {
111 return storage.description
112 }
113
114 public var debugDescription : String {
115 return storage.debugDescription
116 }
117
118}
119
120public func ==(lhs: TBinary, rhs: TBinary) -> Bool {
121 return lhs.storage == rhs.storage
122}