Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. //Copyright 2019 Soroush Khanlou
  2. //
  3. //Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4. //
  5. //The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  6. //
  7. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  8. //
  9.  
  10. import Foundation
  11.  
  12. class SerialInputStream: InputStream {
  13.  
  14. let inputStreams: [InputStream]
  15.  
  16. private var currentIndex: Int
  17. private var _streamStatus: Stream.Status
  18. private var _streamError: Error?
  19. private var _delegate: StreamDelegate?
  20.  
  21. init(inputStreams: [InputStream]) {
  22. self.inputStreams = inputStreams
  23. self.currentIndex = 0
  24. self._streamStatus = .notOpen
  25. self._streamError = nil
  26. super.init(data: Data()) //required because `init()` is not marked as a designated initializer
  27. }
  28.  
  29. override var streamStatus: Stream.Status {
  30. return _streamStatus
  31. }
  32.  
  33. override var streamError: Error? {
  34. return _streamError
  35. }
  36.  
  37. override var delegate: StreamDelegate? {
  38. get {
  39. return _delegate
  40. }
  41. set {
  42. _delegate = newValue
  43. }
  44. }
  45.  
  46. override func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength: Int) -> Int {
  47. if _streamStatus == .closed{
  48. return 0
  49. }
  50.  
  51. var totalNumberOfBytesRead = 0
  52.  
  53. while totalNumberOfBytesRead < maxLength {
  54. if currentIndex == inputStreams.count {
  55. self.close()
  56. break
  57. }
  58.  
  59. let currentInputStream = inputStreams[currentIndex]
  60.  
  61. if currentInputStream.streamStatus != .open {
  62. currentInputStream.open()
  63. }
  64.  
  65. if !currentInputStream.hasBytesAvailable {
  66. self.currentIndex += 1
  67. continue
  68. }
  69.  
  70. let remainingLength = maxLength - totalNumberOfBytesRead
  71.  
  72. let numberOfBytesRead = currentInputStream.read(&buffer[totalNumberOfBytesRead], maxLength: remainingLength)
  73.  
  74. if numberOfBytesRead == 0 {
  75. self.currentIndex += 1
  76. continue
  77. }
  78.  
  79. if numberOfBytesRead == -1 {
  80. self._streamError = currentInputStream.streamError
  81. self._streamStatus = .error
  82. return -1
  83. }
  84.  
  85. totalNumberOfBytesRead += numberOfBytesRead
  86. }
  87.  
  88. return totalNumberOfBytesRead
  89. }
  90.  
  91. override func getBuffer(_ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>, length len: UnsafeMutablePointer<Int>) -> Bool {
  92. return false
  93. }
  94.  
  95. override var hasBytesAvailable: Bool {
  96. return true
  97. }
  98.  
  99. override func open() {
  100. guard self._streamStatus == .open else {
  101. return
  102. }
  103. self._streamStatus = .open
  104. }
  105.  
  106. override func close() {
  107. self._streamStatus = .closed
  108. }
  109.  
  110. override func property(forKey key: Stream.PropertyKey) -> Any? {
  111. return nil
  112. }
  113.  
  114. override func setProperty(_ property: Any?, forKey key: Stream.PropertyKey) -> Bool {
  115. return false
  116. }
  117.  
  118. override func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
  119.  
  120. }
  121.  
  122. override func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
  123.  
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement