-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add NSInputStream.source() and BufferedSource.inputStream() functions for Apple's NSInputStream #1123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jeffdgr8
wants to merge
29
commits into
square:master
Choose a base branch
from
jeffdgr8:nsinputstream
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add NSInputStream.source() and BufferedSource.inputStream() functions for Apple's NSInputStream #1123
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
e0548f2
NSInputStream.source() and BufferedSource.inputStream(): NSInputStream
jeffdgr8 5578fd8
Set streamError and return -1 on read failure
jeffdgr8 b95de57
Implement NSInputStream.getBuffer()
jeffdgr8 ddb66c3
convert() native types
jeffdgr8 a6c3e16
Test NSInputStream.close()
jeffdgr8 7a8c292
Additional test assertions
jeffdgr8 b8d1ba9
appleTest TestUtil
jeffdgr8 c319152
Add doc comments
jeffdgr8 506fa4f
Resolve checks
jeffdgr8 aa0b6d3
Avoid data copy & read source to buffer
jeffdgr8 9ac4e4d
Resolve checks
jeffdgr8 7e9cbfa
Merge branch 'master' into nsinputstream
jeffdgr8 72efc44
Override open() as no-op
jeffdgr8 0f74ac7
Rename variable to avoid ambiguity
jeffdgr8 3191c97
Move private functions in class
jeffdgr8 031a3a1
Code review feedback
jeffdgr8 3571012
Replace additional RealBufferedSource.commonExhausted() logic
jeffdgr8 41c38a5
Remove variable
jeffdgr8 c59ea79
Keep buffer pinned for getBuffer() caller
jeffdgr8 fb883f9
null pinnedBuffer after unpin()
jeffdgr8 e554a72
Merge branch 'master' into nsinputstream
jeffdgr8 f917c2e
Merge remote-tracking branch 'upstream/master' into nsinputstream
jeffdgr8 a40e1c7
Fix lint errors
jeffdgr8 97192bb
Merge remote-tracking branch 'upstream/master' into nsinputstream
jeffdgr8 bbddc69
Add NSOutputStream extensions
jeffdgr8 8b3fe7e
Fix from https://github.com/Kotlin/kotlinx-io/issues/215
jeffdgr8 f5d63c0
Remove comment
jeffdgr8 4b7600f
Merge branch 'master' into nsinputstream
jeffdgr8 089f548
Merge branch 'master' into nsinputstream
jeffdgr8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,16 @@ | ||
package okio | ||
|
||
import kotlinx.cinterop.UnsafeNumber | ||
import platform.Foundation.NSError | ||
import platform.Foundation.NSLocalizedDescriptionKey | ||
import platform.Foundation.NSUnderlyingErrorKey | ||
|
||
@OptIn(UnsafeNumber::class) | ||
internal fun Exception.toNSError() = NSError( | ||
domain = "Kotlin", | ||
code = 0, | ||
userInfo = mapOf( | ||
NSLocalizedDescriptionKey to message, | ||
NSUnderlyingErrorKey to this, | ||
), | ||
) |
This file contains hidden or 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,172 @@ | ||
/* | ||
* Copyright (C) 2020 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package okio | ||
|
||
import kotlin.experimental.ExperimentalNativeApi | ||
import kotlin.native.ref.WeakReference | ||
import kotlinx.cinterop.CPointer | ||
import kotlinx.cinterop.UnsafeNumber | ||
import kotlinx.cinterop.convert | ||
import platform.Foundation.NSError | ||
import platform.Foundation.NSOutputStream | ||
import platform.Foundation.NSRunLoop | ||
import platform.Foundation.NSRunLoopMode | ||
import platform.Foundation.NSStream | ||
import platform.Foundation.NSStreamDataWrittenToMemoryStreamKey | ||
import platform.Foundation.NSStreamDelegateProtocol | ||
import platform.Foundation.NSStreamEvent | ||
import platform.Foundation.NSStreamEventErrorOccurred | ||
import platform.Foundation.NSStreamEventHasSpaceAvailable | ||
import platform.Foundation.NSStreamEventOpenCompleted | ||
import platform.Foundation.NSStreamPropertyKey | ||
import platform.Foundation.NSStreamStatusClosed | ||
import platform.Foundation.NSStreamStatusError | ||
import platform.Foundation.NSStreamStatusNotOpen | ||
import platform.Foundation.NSStreamStatusOpen | ||
import platform.Foundation.NSStreamStatusOpening | ||
import platform.Foundation.NSStreamStatusWriting | ||
import platform.Foundation.performInModes | ||
import platform.darwin.NSInteger | ||
import platform.darwin.NSUInteger | ||
import platform.posix.uint8_tVar | ||
|
||
/** | ||
* Returns an output stream that writes to this sink. Closing the stream will also close this sink. | ||
*/ | ||
fun BufferedSink.outputStream(): NSOutputStream = BufferedSinkNSOutputStream(this) | ||
|
||
@OptIn(UnsafeNumber::class, ExperimentalNativeApi::class) | ||
private class BufferedSinkNSOutputStream( | ||
private val sink: BufferedSink, | ||
) : NSOutputStream(toMemory = Unit), NSStreamDelegateProtocol { | ||
|
||
private val isClosed: () -> Boolean = when (sink) { | ||
is RealBufferedSink -> sink::closed | ||
is Buffer -> { | ||
{ false } | ||
} | ||
} | ||
|
||
private var status = NSStreamStatusNotOpen | ||
private var error: NSError? = null | ||
set(value) { | ||
status = NSStreamStatusError | ||
field = value | ||
postEvent(NSStreamEventErrorOccurred) | ||
sink.close() | ||
} | ||
|
||
override fun streamStatus() = if (status != NSStreamStatusError && isClosed()) NSStreamStatusClosed else status | ||
|
||
override fun streamError() = error | ||
|
||
override fun open() { | ||
if (status == NSStreamStatusNotOpen) { | ||
status = NSStreamStatusOpening | ||
status = NSStreamStatusOpen | ||
postEvent(NSStreamEventOpenCompleted) | ||
postEvent(NSStreamEventHasSpaceAvailable) | ||
} | ||
} | ||
|
||
override fun close() { | ||
if (status == NSStreamStatusError || status == NSStreamStatusNotOpen) return | ||
status = NSStreamStatusClosed | ||
runLoop = null | ||
runLoopModes = listOf() | ||
sink.close() | ||
} | ||
|
||
override fun write(buffer: CPointer<uint8_tVar>?, maxLength: NSUInteger): NSInteger { | ||
if (streamStatus != NSStreamStatusOpen || buffer == null) return -1 | ||
status = NSStreamStatusWriting | ||
val toWrite = minOf(maxLength, Int.MAX_VALUE.convert()).toInt() | ||
return try { | ||
sink.buffer.write(buffer, toWrite) | ||
sink.emitCompleteSegments() | ||
status = NSStreamStatusOpen | ||
toWrite.convert() | ||
} catch (e: Exception) { | ||
error = e.toNSError() | ||
-1 | ||
} | ||
} | ||
|
||
override fun hasSpaceAvailable() = !isFinished | ||
|
||
private val isFinished | ||
get() = when (streamStatus) { | ||
NSStreamStatusClosed, NSStreamStatusError -> true | ||
else -> false | ||
} | ||
|
||
override fun propertyForKey(key: NSStreamPropertyKey): Any? = when (key) { | ||
NSStreamDataWrittenToMemoryStreamKey -> sink.buffer.snapshotAsNSData() | ||
else -> null | ||
} | ||
|
||
override fun setProperty(property: Any?, forKey: NSStreamPropertyKey) = false | ||
|
||
// WeakReference as delegate should not be retained | ||
// https://developer.apple.com/documentation/foundation/nsstream/1418423-delegate | ||
private var _delegate: WeakReference<NSStreamDelegateProtocol>? = null | ||
private var runLoop: NSRunLoop? = null | ||
private var runLoopModes = listOf<NSRunLoopMode>() | ||
|
||
private fun postEvent(event: NSStreamEvent) { | ||
val runLoop = runLoop ?: return | ||
runLoop.performInModes(runLoopModes) { | ||
if (runLoop == this.runLoop) { | ||
delegateOrSelf.stream(this, event) | ||
} | ||
} | ||
} | ||
|
||
override fun delegate() = _delegate?.value | ||
|
||
private val delegateOrSelf get() = delegate ?: this | ||
|
||
override fun setDelegate(delegate: NSStreamDelegateProtocol?) { | ||
_delegate = delegate?.let { WeakReference(it) } | ||
} | ||
|
||
override fun stream(aStream: NSStream, handleEvent: NSStreamEvent) { | ||
// no-op | ||
} | ||
|
||
override fun scheduleInRunLoop(aRunLoop: NSRunLoop, forMode: NSRunLoopMode) { | ||
if (runLoop == null) { | ||
runLoop = aRunLoop | ||
} | ||
if (runLoop == aRunLoop) { | ||
runLoopModes += forMode | ||
} | ||
if (status == NSStreamStatusOpen) { | ||
postEvent(NSStreamEventHasSpaceAvailable) | ||
} | ||
} | ||
|
||
override fun removeFromRunLoop(aRunLoop: NSRunLoop, forMode: NSRunLoopMode) { | ||
if (aRunLoop == runLoop) { | ||
runLoopModes -= forMode | ||
if (runLoopModes.isEmpty()) { | ||
runLoop = null | ||
} | ||
} | ||
} | ||
|
||
override fun description() = "$sink.outputStream()" | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.