-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement request using InpuStream on iOS
- Loading branch information
Showing
6 changed files
with
223 additions
and
19 deletions.
There are no files selected for viewing
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
112 changes: 112 additions & 0 deletions
112
ios/Bridge/GomobileIPFS/Sources/InputStreamFromGo.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// | ||
// InputStreamFromGo.swift | ||
// GomobileIPFS | ||
// | ||
// Created by Antoine Eddi on 19/01/2020. | ||
// | ||
|
||
import Foundation | ||
import Core | ||
|
||
public class InputStreamFromGoError: IPFSError { | ||
private static var code: Int = 6 | ||
private static var subdomain: String = "InputStreamFromGo" | ||
|
||
required init(_ description: String, _ optCause: NSError? = nil) { | ||
super.init(description, optCause, InputStreamFromGoError.subdomain, InputStreamFromGoError.code) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
} | ||
} | ||
|
||
public class InputStreamFromGo: InputStream { | ||
private var _streamStatus: Stream.Status | ||
private var _streamError: Error? | ||
private weak var _delegate: StreamDelegate? | ||
private var readCloser: CoreReadCloser | ||
|
||
init(_ readCloser: CoreReadCloser) { | ||
self._streamStatus = .notOpen | ||
self._streamError = nil | ||
self.readCloser = readCloser | ||
super.init(data: Data()) | ||
} | ||
|
||
override public var hasBytesAvailable: Bool { | ||
if self.streamStatus == .atEnd || self.streamStatus == .closed { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
override public var streamStatus: Stream.Status { | ||
return _streamStatus | ||
} | ||
|
||
override public var streamError: Error? { | ||
return _streamError | ||
} | ||
|
||
override public var delegate: StreamDelegate? { | ||
get { | ||
return _delegate | ||
} | ||
set { | ||
_delegate = newValue | ||
} | ||
} | ||
|
||
override public func open() { | ||
if self._streamStatus == .notOpen { | ||
self._streamStatus = .open | ||
} | ||
} | ||
|
||
override public func close() { | ||
if self._streamStatus != .closed { | ||
self._streamStatus = .closed | ||
try? self.readCloser.close() | ||
} | ||
} | ||
|
||
override public func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int { | ||
if self._streamStatus != .open { | ||
self._streamError = InputStreamFromGoError("stream not opened (\(self._streamStatus))") | ||
return -1 | ||
} | ||
|
||
self._streamStatus = .reading | ||
|
||
// swiftlint:disable todo | ||
// TODO: Use the buffer argument directly without making a copy. | ||
// swiftlint:enable todo | ||
let tmp: NSData? = NSMutableData(length: len) | ||
var read: Int = 0 | ||
|
||
do { | ||
try self.readCloser.read(tmp as Data?, n: &read) | ||
} catch let err { | ||
if err.localizedDescription == "EOF" { | ||
self._streamStatus = .atEnd | ||
return 0 | ||
} | ||
|
||
self._streamStatus = .error | ||
self._streamError = err | ||
return -1 | ||
} | ||
|
||
tmp!.getBytes(buffer, length: read) | ||
self._streamStatus = .open | ||
|
||
return read | ||
} | ||
|
||
override public func getBuffer( | ||
_ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>, | ||
length len: UnsafeMutablePointer<Int>) -> Bool { | ||
return false | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// InputStreamToGo.swift | ||
// GomobileIPFS | ||
// | ||
// Created by CI Agent on 20/01/2020. | ||
// | ||
|
||
import Foundation | ||
import Core | ||
|
||
public class InputStreamToGo: NSObject, CoreReaderProtocol { | ||
private var inputStream: InputStream | ||
|
||
init(_ inputStream: InputStream) { | ||
self.inputStream = inputStream | ||
self.inputStream.open() | ||
} | ||
|
||
public func read(_ buffer: Data?, n len: UnsafeMutablePointer<Int>?) throws { | ||
var read: Int | ||
|
||
let bytes = UnsafeMutablePointer<UInt8>(OpaquePointer((buffer! as NSData).bytes)) | ||
read = self.inputStream.read(bytes, maxLength: buffer!.count) | ||
len?.initialize(to: read) | ||
|
||
if read == 0 && self.inputStream.streamStatus == .atEnd { | ||
self.inputStream.close() | ||
throw NSError(domain: "", code: 0, userInfo: ["NSLocalizedDescription": "EOF"]) | ||
} | ||
} | ||
} |
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
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
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