Skip to content

InputStream/OutputStream implemented delegate and getBuffer #1667

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions CoreFoundation/Stream.subproj/CFConcreteStreams.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,56 @@ CFWriteStreamRef _CFWriteStreamCreateFromFileDescriptor(CFAllocatorRef alloc, in
static const struct _CFStreamCallBacksV1 readDataCallBacks = {1, readDataCreate, readDataFinalize, readDataCopyDescription, readDataOpen, NULL, dataRead, dataGetBuffer, dataCanRead, NULL, NULL, NULL, NULL, NULL, NULL, readDataSchedule, NULL};
static const struct _CFStreamCallBacksV1 writeDataCallBacks = {1, writeDataCreate, writeDataFinalize, writeDataCopyDescription, writeDataOpen, NULL, NULL, NULL, NULL, dataWrite, dataCanWrite, NULL, dataCopyProperty, NULL, NULL, writeDataSchedule, NULL};

#if DEPLOYMENT_RUNTIME_SWIFT

Boolean _CFStreamInitWithFile(struct _CFStream *stream, CFURLRef fileURL, Boolean forReading) {
CFAllocatorRef allocator = kCFAllocatorSystemDefault;
struct _CFStream *result = _CFStreamCreateWithFile(allocator, fileURL, forReading);
if (result == NULL) return false;
__CFCopyStream(stream, result);
CFAllocatorDeallocate(allocator, result);
return true;
}

CF_EXPORT Boolean _CFReadStreamInitWithBytesNoCopy(CFReadStreamRef stream, const UInt8 *bytes, CFIndex length, CFAllocatorRef bytesDeallocator) {
CFAllocatorRef allocator = kCFAllocatorSystemDefault;
CFReadStreamRef result = CFReadStreamCreateWithBytesNoCopy(allocator, bytes, length, bytesDeallocator);
if (result == NULL) return false;
__CFCopyStream((struct _CFStream *)stream, (struct _CFStream *)result);
CFAllocatorDeallocate(allocator, result);
return true;
}

/* This needs to be exported to make it callable from Foundation. */
CF_EXPORT Boolean _CFReadStreamInitWithData(CFReadStreamRef stream, CFDataRef data) {
CFAllocatorRef allocator = kCFAllocatorSystemDefault;
CFReadStreamRef result = CFReadStreamCreateWithData(allocator, data);
if (result == NULL) return false;
__CFCopyStream((struct _CFStream *)stream, (struct _CFStream *)result);
CFAllocatorDeallocate(allocator, result);
return true;
}

Boolean _CFWriteStreamInitWithBuffer(CFWriteStreamRef stream, UInt8 *buffer, CFIndex bufferCapacity) {
CFAllocatorRef allocator = kCFAllocatorSystemDefault;
CFWriteStreamRef result = CFWriteStreamCreateWithBuffer(allocator, buffer, bufferCapacity);
if (result == NULL) return false;
__CFCopyStream((struct _CFStream *)stream, (struct _CFStream *)result);
CFAllocatorDeallocate(allocator, result);
return true;
}

CF_EXPORT Boolean _CFWriteStreamInitWithAllocatedBuffers(CFWriteStreamRef stream, CFAllocatorRef bufferAllocator) {
CFAllocatorRef allocator = kCFAllocatorSystemDefault;
CFWriteStreamRef result = CFWriteStreamCreateWithAllocatedBuffers(allocator, bufferAllocator);
if (result == NULL) return false;
__CFCopyStream((struct _CFStream *)stream, (struct _CFStream *)result);
CFAllocatorDeallocate(allocator, result);
return true;
}

#endif

CF_EXPORT CFReadStreamRef CFReadStreamCreateWithBytesNoCopy(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex length, CFAllocatorRef bytesDeallocator) {
_CFReadDataStreamContext ctxt;
CFReadStreamRef result;
Expand Down
26 changes: 26 additions & 0 deletions CoreFoundation/Stream.subproj/CFStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,32 @@ CF_EXPORT void* _CFStreamGetInfoPointer(struct _CFStream* stream) {
return stream == NULL? NULL : stream->info;
}

#if DEPLOYMENT_RUNTIME_SWIFT
void __CFCopyStream(struct _CFStream *newStream, struct _CFStream *source) {
newStream->_cfBase._cfinfoa = source->_cfBase._cfinfoa;
newStream->flags = source->flags;
newStream->error = source->error;
newStream->client = source->client;
newStream->info = source->info;
newStream->callBacks = source->callBacks;
newStream->streamLock = source->streamLock;
newStream->previousRunloopsAndModes = source->previousRunloopsAndModes;
#if __HAS_DISPATCH__
newStream->queue = source->queue;
#endif
newStream->pendingEventsToDeliver = source->pendingEventsToDeliver;
}

Boolean _CFStreamInitWithConstantCallbacks(struct _CFStream *newStream, void *info, const struct _CFStreamCallBacks *cb, Boolean isReading) {
CFAllocatorRef allocator = kCFAllocatorSystemDefault;
struct _CFStream *result = _CFStreamCreateWithConstantCallbacks(allocator, info, cb, isReading);
if (result == NULL) return false;
__CFCopyStream(newStream, result);
CFAllocatorDeallocate(allocator, result);
return true;
}
#endif

CF_PRIVATE struct _CFStream *_CFStreamCreateWithConstantCallbacks(CFAllocatorRef alloc, void *info, const struct _CFStreamCallBacks *cb, Boolean isReading) {
struct _CFStream *newStream;
if (cb->version != 1) return NULL;
Expand Down
5 changes: 5 additions & 0 deletions CoreFoundation/Stream.subproj/CFStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ Boolean CFWriteStreamSetProperty(CFWriteStreamRef _Null_unspecified stream, CFSt

*/

CF_EXPORT
void *_CFReadStreamGetClient(CFReadStreamRef readStream);
CF_EXPORT
void *_CFWriteStreamGetClient(CFWriteStreamRef writeStream);

CF_EXPORT
Boolean CFReadStreamSetClient(CFReadStreamRef _Null_unspecified stream, CFOptionFlags streamEvents, CFReadStreamClientCallBack _Null_unspecified clientCB, CFStreamClientContext * _Null_unspecified clientContext);
CF_EXPORT
Expand Down
11 changes: 11 additions & 0 deletions CoreFoundation/Stream.subproj/CFStreamPriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ CF_EXPORT void* _CFStreamGetInfoPointer(struct _CFStream* stream);
#define CF_PRIVATE extern __attribute__((__visibility__("hidden")))
#endif

void __CFCopyStream(struct _CFStream *newStream, struct _CFStream *source);

// cb version must be > 0
CF_PRIVATE struct _CFStream *_CFStreamCreateWithConstantCallbacks(CFAllocatorRef alloc, void *info, const struct _CFStreamCallBacks *cb, Boolean isReading);

Expand All @@ -74,6 +76,15 @@ CFReadStreamRef _CFReadStreamCreateFromFileDescriptor(CFAllocatorRef alloc, int
CF_EXPORT
CFWriteStreamRef _CFWriteStreamCreateFromFileDescriptor(CFAllocatorRef alloc, int fd);

#if DEPLOYMENT_RUNTIME_SWIFT
Boolean _CFStreamInitWithConstantCallbacks(struct _CFStream* newStream, void *info, const struct _CFStreamCallBacks *cb, Boolean isReading);
Boolean _CFStreamInitWithFile(struct _CFStream *stream, CFURLRef fileURL, Boolean forReading);

CF_EXPORT Boolean _CFReadStreamInitWithBytesNoCopy(CFReadStreamRef stream, const UInt8 *bytes, CFIndex length, CFAllocatorRef bytesDeallocator);
CF_EXPORT Boolean _CFReadStreamInitWithData(CFReadStreamRef stream, CFDataRef data);
Boolean _CFWriteStreamInitWithBuffer(CFWriteStreamRef stream, UInt8 *buffer, CFIndex bufferCapacity);
CF_EXPORT Boolean _CFWriteStreamInitWithAllocatedBuffers(CFWriteStreamRef stream, CFAllocatorRef bufferAllocator);
#endif


#define SECURITY_NONE (0)
Expand Down
5 changes: 2 additions & 3 deletions Docs/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,8 @@ There is no _Complete_ status for test coverage because there are always additio
| `SocketPort` | Unimplemented | None | |
| `PortMessage` | Unimplemented | None | |
| `RunLoop` | Mostly Complete | Incomplete | `add(_: Port, forMode:)` and `remove(_: Port, forMode:)` remain unimplemented |
| `NSStream` | Mostly Complete | Substantial | |
| `Stream` | Unimplemented | Substantial | Methods which require a concrete implementation remain unimplemented |
| `InputStream` | Mostly Complete | Substantial | `getBuffer(_:length:)` remains unimplemented |
| `Stream` | Complete | Substantial | `getStreamsToHost(withName:port:inputStream:outputStream:)` and `getBoundStreams(withBufferSize:inputStream:outputStream:)` class methods are disabled |
| `InputStream` | Complete | Substantial | |
| `NSOutputStream` | Complete | Substantial | |
| `Timer` | Complete | Substantial | |

Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSSwiftRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ internal func __CFInitializeSwift() {
_CFRuntimeBridgeTypeToClass(CFCharacterSetGetTypeID(), unsafeBitCast(_NSCFCharacterSet.self, to: UnsafeRawPointer.self))
_CFRuntimeBridgeTypeToClass(_CFKeyedArchiverUIDGetTypeID(), unsafeBitCast(_NSKeyedArchiverUID.self, to: UnsafeRawPointer.self))

// _CFRuntimeBridgeTypeToClass(CFErrorGetTypeID(), unsafeBitCast(NSError.self, UnsafeRawPointer.self))
// _CFRuntimeBridgeTypeToClass(CFErrorGetTypeID(), unsafeBitCast(NSError.self, to: UnsafeRawPointer.self))
_CFRuntimeBridgeTypeToClass(CFAttributedStringGetTypeID(), unsafeBitCast(NSMutableAttributedString.self, to: UnsafeRawPointer.self))
// _CFRuntimeBridgeTypeToClass(CFReadStreamGetTypeID(), unsafeBitCast(InputStream.self, UnsafeRawPointer.self))
// _CFRuntimeBridgeTypeToClass(CFWriteStreamGetTypeID(), unsafeBitCast(OutputStream.self, UnsafeRawPointer.self))
_CFRuntimeBridgeTypeToClass(CFRunLoopTimerGetTypeID(), unsafeBitCast(Timer.self, to: UnsafeRawPointer.self))
_CFRuntimeBridgeTypeToClass(CFReadStreamGetTypeID(), unsafeBitCast(InputStream.self, to: UnsafeRawPointer.self))
_CFRuntimeBridgeTypeToClass(CFWriteStreamGetTypeID(), unsafeBitCast(OutputStream.self, to: UnsafeRawPointer.self))
_CFRuntimeBridgeTypeToClass(CFRunLoopTimerGetTypeID(), unsafeBitCast(Timer.self, to: UnsafeRawPointer.self))

__CFSwiftBridge.NSObject.isEqual = _CFSwiftIsEqual
__CFSwiftBridge.NSObject.hash = _CFSwiftGetHash
Expand Down
2 changes: 1 addition & 1 deletion Foundation/PropertyListSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ open class PropertyListSerialization : NSObject {
}

open class func propertyList(with stream: InputStream, options opt: ReadOptions = [], format: UnsafeMutablePointer<PropertyListFormat>?) throws -> Any {
return try propertyList(with: stream._stream, options: opt, format: format)
return try propertyList(with: stream._cfObject, options: opt, format: format)
}
}
Loading