Skip to content

Commit 38908c1

Browse files
committed
Modifying import to apply to all platforms.
1 parent fe685b4 commit 38908c1

File tree

5 files changed

+178
-10
lines changed

5 files changed

+178
-10
lines changed

CoreFoundation/Base.subproj/ForFoundationOnly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ CF_EXPORT Boolean _CFRunLoopFinished(CFRunLoopRef rl, CFStringRef mode);
564564
CF_EXPORT CFTypeRef _CFRunLoopGet2(CFRunLoopRef rl);
565565

566566
CF_EXPORT CFIndex _CFStreamInstanceSize(void);
567+
CF_EXPORT CFReadStreamRef CFReadStreamCreateWithData(CFAllocatorRef alloc, CFDataRef data);
567568

568569
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
569570
typedef struct {

Foundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0383A1751D2E558A0052E5D1 /* TestNSStream.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0383A1741D2E558A0052E5D1 /* TestNSStream.swift */; };
1011
294E3C1D1CC5E19300E4F44C /* TestNSAttributedString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */; };
1112
2EBE67A51C77BF0E006583D5 /* TestNSDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBE67A31C77BF05006583D5 /* TestNSDateFormatter.swift */; };
1213
528776141BF2629700CB0090 /* FoundationErrors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 522C253A1BF16E1600804FC6 /* FoundationErrors.swift */; };
@@ -419,6 +420,7 @@
419420
/* End PBXCopyFilesBuildPhase section */
420421

421422
/* Begin PBXFileReference section */
423+
0383A1741D2E558A0052E5D1 /* TestNSStream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSStream.swift; sourceTree = "<group>"; };
422424
22B9C1E01C165D7A00DECFF9 /* TestNSDate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSDate.swift; sourceTree = "<group>"; };
423425
294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSAttributedString.swift; sourceTree = "<group>"; };
424426
2EBE67A31C77BF05006583D5 /* TestNSDateFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSDateFormatter.swift; sourceTree = "<group>"; };
@@ -1276,6 +1278,7 @@
12761278
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
12771279
CC5249BF1D341D23007CB54D /* TestUnitConverter.swift */,
12781280
5B6F17961C48631C00935030 /* TestUtils.swift */,
1281+
0383A1741D2E558A0052E5D1 /* TestNSStream.swift */,
12791282
);
12801283
name = Tests;
12811284
sourceTree = "<group>";
@@ -2086,6 +2089,7 @@
20862089
5B13B34E1C582D4C00651CE2 /* TestNSXMLDocument.swift in Sources */,
20872090
5B13B32B1C582D4C00651CE2 /* TestNSData.swift in Sources */,
20882091
5B13B34C1C582D4C00651CE2 /* TestNSURLResponse.swift in Sources */,
2092+
0383A1751D2E558A0052E5D1 /* TestNSStream.swift in Sources */,
20892093
5B13B3481C582D4C00651CE2 /* TestNSTimer.swift in Sources */,
20902094
5B13B32D1C582D4C00651CE2 /* TestNSDictionary.swift in Sources */,
20912095
5B13B3261C582D4C00651CE2 /* TestNSAffineTransform.swift in Sources */,

Foundation/NSStream.swift

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
import CoreFoundation
11+
1012
extension Stream {
1113
public struct PropertyKey : RawRepresentable, Equatable, Hashable, Comparable {
1214
public private(set) var rawValue: String
@@ -60,17 +62,17 @@ public func <(lhs: Stream.PropertyKey, rhs: Stream.PropertyKey) -> Bool {
6062
// NSStream is an abstract class encapsulating the common API to NSInputStream and NSOutputStream.
6163
// Subclassers of NSInputStream and NSOutputStream must also implement these methods.
6264
public class Stream: NSObject {
63-
65+
6466
public override init() {
65-
67+
6668
}
6769

6870
public func open() {
69-
NSUnimplemented()
71+
NSRequiresConcreteImplementation()
7072
}
7173

7274
public func close() {
73-
NSUnimplemented()
75+
NSRequiresConcreteImplementation()
7476
}
7577

7678
public weak var delegate: StreamDelegate?
@@ -95,7 +97,7 @@ public class Stream: NSObject {
9597
}
9698

9799
public var streamStatus: Status {
98-
NSUnimplemented()
100+
NSRequiresConcreteImplementation()
99101
}
100102

101103
/*@NSCopying */public var streamError: NSError? {
@@ -106,9 +108,12 @@ public class Stream: NSObject {
106108
// NSInputStream is an abstract class representing the base functionality of a read stream.
107109
// Subclassers are required to implement these methods.
108110
public class InputStream: Stream {
111+
112+
private var _stream: CFReadStream!
113+
109114
// reads up to length bytes into the supplied buffer, which must be at least of size len. Returns the actual number of bytes read.
110115
public func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int {
111-
NSUnimplemented()
116+
return CFReadStreamRead(_stream, buffer, CFIndex(len._bridgeToObject()))
112117
}
113118

114119
// returns in O(1) a pointer to the buffer in 'buffer' and by reference in 'len' how many bytes are available. This buffer is only valid until the next stream operation. Subclassers may return NO for this if it is not appropriate for the stream type. This may return NO if the buffer is not available.
@@ -118,19 +123,31 @@ public class InputStream: Stream {
118123

119124
// returns YES if the stream has bytes available or if it impossible to tell without actually doing the read.
120125
public var hasBytesAvailable: Bool {
121-
NSUnimplemented()
126+
return CFReadStreamHasBytesAvailable(_stream)
122127
}
123128

124129
public init(data: Data) {
125-
NSUnimplemented()
130+
_stream = CFReadStreamCreateWithData(kCFAllocatorSystemDefault, data._cfObject)
126131
}
127132

128133
public init?(url: URL) {
129-
NSUnimplemented()
134+
_stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, url._cfObject)
130135
}
131136

132137
public convenience init?(fileAtPath path: String) {
133-
NSUnimplemented()
138+
self.init(url: URL(fileURLWithPath: path))
139+
}
140+
141+
public override func open() {
142+
CFReadStreamOpen(_stream)
143+
}
144+
145+
public override func close() {
146+
CFReadStreamClose(_stream)
147+
}
148+
149+
public override var streamStatus: Status {
150+
return Stream.Status(rawValue: UInt(CFReadStreamGetStatus(_stream)))!
134151
}
135152
}
136153

TestFoundation/TestNSStream.swift

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
11+
import Foundation
12+
import XCTest
13+
#else
14+
import SwiftFoundation
15+
import SwiftXCTest
16+
#endif
17+
18+
class TestNSStream : XCTestCase {
19+
static var allTests: [(String, (TestNSStream) -> () throws -> Void)] {
20+
return [
21+
("test_InputStreamWithData", test_InputStreamWithData),
22+
("test_InputStreamWithUrl", test_InputStreamWithUrl),
23+
("test_InputStreamWithFile", test_InputStreamWithFile),
24+
("test_InputStreamHasBytesAvailable", test_InputStreamHasBytesAvailable),
25+
("test_InputStreamInvalidPath", test_InputStreamInvalidPath),
26+
]
27+
}
28+
29+
func test_InputStreamWithData(){
30+
let message: NSString = "Hello, playground"
31+
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
32+
let dataStream: InputStream = InputStream(data: messageData)
33+
XCTAssertEqual(Stream.Status.notOpen, dataStream.streamStatus)
34+
dataStream.open()
35+
XCTAssertEqual(Stream.Status.open, dataStream.streamStatus)
36+
var buffer = [UInt8](repeating: 0, count: 20)
37+
if dataStream.hasBytesAvailable {
38+
let result: Int = dataStream.read(&buffer, maxLength: buffer.count)
39+
dataStream.close()
40+
XCTAssertEqual(Stream.Status.closed, dataStream.streamStatus)
41+
if(result > 0){
42+
let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)
43+
XCTAssertEqual(message, output!)
44+
}
45+
}
46+
}
47+
48+
func test_InputStreamWithUrl() {
49+
let message: NSString = "Hello, playground"
50+
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
51+
//Initialiser with url
52+
let testFile = createTestFile("testFile_in.txt", _contents: messageData)
53+
if testFile != nil {
54+
let url = URL(fileURLWithPath: testFile!)
55+
let urlStream: InputStream = InputStream(url: url)!
56+
XCTAssertEqual(Stream.Status.notOpen, urlStream.streamStatus)
57+
urlStream.open()
58+
XCTAssertEqual(Stream.Status.open, urlStream.streamStatus)
59+
var buffer = [UInt8](repeating: 0, count: 20)
60+
if urlStream.hasBytesAvailable {
61+
let result :Int = urlStream.read(&buffer, maxLength: buffer.count)
62+
urlStream.close()
63+
XCTAssertEqual(Stream.Status.closed, urlStream.streamStatus)
64+
XCTAssertEqual(messageData.count, result)
65+
if(result > 0) {
66+
let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)
67+
XCTAssertEqual(message, output!)
68+
}
69+
}
70+
removeTestFile(testFile!)
71+
} else {
72+
XCTFail("Unable to create temp file")
73+
}
74+
}
75+
76+
func test_InputStreamWithFile() {
77+
let message: NSString = "Hello, playground"
78+
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
79+
//Initialiser with file
80+
let testFile = createTestFile("testFile_in.txt", _contents: messageData)
81+
if testFile != nil {
82+
let fileStream: InputStream = InputStream(fileAtPath: testFile!)!
83+
XCTAssertEqual(Stream.Status.notOpen, fileStream.streamStatus)
84+
fileStream.open()
85+
XCTAssertEqual(Stream.Status.open, fileStream.streamStatus)
86+
var buffer = [UInt8](repeating: 0, count: 20)
87+
if fileStream.hasBytesAvailable {
88+
let result: Int = fileStream.read(&buffer, maxLength: buffer.count)
89+
fileStream.close()
90+
XCTAssertEqual(Stream.Status.closed, fileStream.streamStatus)
91+
XCTAssertEqual(messageData.count, result)
92+
if(result > 0){
93+
let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)
94+
XCTAssertEqual(message, output!)
95+
}
96+
}
97+
removeTestFile(testFile!)
98+
} else {
99+
XCTFail("Unable to create temp file")
100+
}
101+
}
102+
103+
func test_InputStreamHasBytesAvailable() {
104+
let message: NSString = "Hello, playground"
105+
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
106+
let stream: InputStream = InputStream(data: messageData)
107+
var buffer = [UInt8](repeating: 0, count: 20)
108+
stream.open()
109+
XCTAssertTrue(stream.hasBytesAvailable)
110+
_ = stream.read(&buffer, maxLength: buffer.count)
111+
XCTAssertFalse(stream.hasBytesAvailable)
112+
}
113+
114+
func test_InputStreamInvalidPath() {
115+
let fileStream: InputStream = InputStream(fileAtPath: "/tmp/file.txt")!
116+
XCTAssertEqual(Stream.Status.notOpen, fileStream.streamStatus)
117+
fileStream.open()
118+
XCTAssertEqual(Stream.Status.error, fileStream.streamStatus)
119+
}
120+
121+
private func createTestFile(_ path: String,_contents: Data) -> String? {
122+
let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().UUIDString + "/"
123+
do {
124+
try FileManager.default().createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
125+
if FileManager.default().createFile(atPath: tempDir + "/" + path, contents: _contents, attributes: nil) {
126+
return tempDir + path
127+
} else {
128+
return nil
129+
}
130+
} catch _ {
131+
return nil
132+
}
133+
134+
}
135+
136+
private func removeTestFile(_ location: String) {
137+
do {
138+
try FileManager.default().removeItem(atPath: location)
139+
} catch _ {
140+
141+
}
142+
}
143+
}
144+
145+

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ XCTMain([
5757
testCase(TestNSRunLoop.allTests),
5858
testCase(TestNSScanner.allTests),
5959
testCase(TestNSSet.allTests),
60+
testCase(TestNSStream.allTests),
6061
testCase(TestNSString.allTests),
6162
// testCase(TestNSThread.allTests),
6263
testCase(TestNSTask.allTests),

0 commit comments

Comments
 (0)