@@ -34,6 +34,9 @@ open class FileHandle : NSObject, NSSecureCoding {
34
34
precondition ( _handle != INVALID_HANDLE_VALUE, " Invalid file handle " )
35
35
}
36
36
37
+ private var _isPlatformHandleValid : Bool {
38
+ return _handle != INVALID_HANDLE_VALUE
39
+ }
37
40
#else
38
41
private var _fd : Int32
39
42
@@ -44,6 +47,10 @@ open class FileHandle : NSObject, NSSecureCoding {
44
47
private func _checkFileHandle( ) {
45
48
precondition ( _fd >= 0 , " Bad file descriptor " )
46
49
}
50
+
51
+ private var _isPlatformHandleValid : Bool {
52
+ return fileDescriptor >= 0
53
+ }
47
54
#endif
48
55
49
56
private var _closeOnDealloc : Bool
@@ -281,30 +288,28 @@ open class FileHandle : NSObject, NSSecureCoding {
281
288
return true
282
289
}
283
290
284
- private var _isPlatformHandleValid : Bool {
285
- #if os(Windows)
286
- return _handle != INVALID_HANDLE_VALUE
287
- #else
288
- return fileDescriptor >= 0
289
- #endif
290
- }
291
-
292
291
// MARK: -
293
292
// MARK: New API.
294
293
295
294
@available ( swift 5 . 0 )
296
295
public func readToEnd( ) throws -> Data ? {
296
+ guard self != FileHandle . _nulldeviceFileHandle else { return nil }
297
+
297
298
return try read ( upToCount: Int . max)
298
299
}
299
300
300
301
@available ( swift 5 . 0 )
301
302
public func read( upToCount count: Int ) throws -> Data ? {
303
+ guard self != FileHandle . _nulldeviceFileHandle else { return nil }
304
+
302
305
let result = try _readDataOfLength ( count, untilEOF: true )
303
306
return result. length == 0 ? nil : result. toData ( )
304
307
}
305
308
306
309
@available ( swift 5 . 0 )
307
310
public func write< T: DataProtocol > ( contentsOf data: T ) throws {
311
+ guard self != FileHandle . _nulldeviceFileHandle else { return }
312
+
308
313
guard _isPlatformHandleValid else { throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileWriteUnknown. rawValue) }
309
314
310
315
for region in data. regions {
@@ -322,6 +327,8 @@ open class FileHandle : NSObject, NSSecureCoding {
322
327
323
328
@available ( swift 5 . 0 )
324
329
public func offset( ) throws -> UInt64 {
330
+ guard self != FileHandle . _nulldeviceFileHandle else { return 0 }
331
+
325
332
guard _isPlatformHandleValid else { throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileReadUnknown. rawValue) }
326
333
327
334
#if os(Windows)
@@ -340,6 +347,8 @@ open class FileHandle : NSObject, NSSecureCoding {
340
347
@available ( swift 5 . 0 )
341
348
@discardableResult
342
349
public func seekToEnd( ) throws -> UInt64 {
350
+ guard self != FileHandle . _nulldeviceFileHandle else { return 0 }
351
+
343
352
guard _isPlatformHandleValid else { throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileReadUnknown. rawValue) }
344
353
345
354
#if os(Windows)
@@ -357,6 +366,8 @@ open class FileHandle : NSObject, NSSecureCoding {
357
366
358
367
@available ( swift 5 . 0 )
359
368
public func seek( toOffset offset: UInt64 ) throws {
369
+ guard self != FileHandle . _nulldeviceFileHandle else { return }
370
+
360
371
guard _isPlatformHandleValid else { throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileReadUnknown. rawValue) }
361
372
362
373
#if os(Windows)
@@ -370,6 +381,8 @@ open class FileHandle : NSObject, NSSecureCoding {
370
381
371
382
@available ( swift 5 . 0 )
372
383
public func truncate( toOffset offset: UInt64 ) throws {
384
+ guard self != FileHandle . _nulldeviceFileHandle else { return }
385
+
373
386
guard _isPlatformHandleValid else { throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileWriteUnknown. rawValue) }
374
387
375
388
#if os(Windows)
@@ -388,6 +401,8 @@ open class FileHandle : NSObject, NSSecureCoding {
388
401
389
402
@available ( swift 5 . 0 )
390
403
public func synchronize( ) throws {
404
+ guard self != FileHandle . _nulldeviceFileHandle else { return }
405
+
391
406
#if os(Windows)
392
407
guard FlushFileBuffers ( _handle) != FALSE else {
393
408
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
@@ -399,18 +414,19 @@ open class FileHandle : NSObject, NSSecureCoding {
399
414
400
415
@available ( swift 5 . 0 )
401
416
public func close( ) throws {
417
+ guard self != FileHandle . _nulldeviceFileHandle else { return }
418
+ guard _isPlatformHandleValid else { return }
419
+
402
420
#if os(Windows)
403
- if _handle != INVALID_HANDLE_VALUE {
404
- guard CloseHandle ( _handle) != FALSE else {
405
- throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
406
- }
407
- _handle = INVALID_HANDLE_VALUE
421
+ guard CloseHandle ( _handle) != FALSE else {
422
+ throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
408
423
}
424
+ _handle = INVALID_HANDLE_VALUE
409
425
#else
410
- if _fd >= 0 {
411
- guard _close ( _fd) >= 0 else { throw _NSErrorWithErrno ( errno, reading: true ) }
412
- _fd = - 1
426
+ guard _close ( _fd) >= 0 else {
427
+ throw _NSErrorWithErrno ( errno, reading: true )
413
428
}
429
+ _fd = - 1
414
430
#endif
415
431
}
416
432
0 commit comments