     }
 
     open func write(_ data: Data) {
-#if os(Windows)
-      precondition(_handle != INVALID_HANDLE_VALUE, "invalid file handle")
+        _checkFileHandle()
+#if os(Windows)
       data.enumerateBytes() { (bytes, range, stop) in
         do {
           try NSData.write(toHandle: self._handle, path: nil,

    }
    
    open func readDataToEndOfFile() -> Data {
        return readData(ofLength: Int.max)
    }

    open func readData(ofLength length: Int) -> Data {
        do {
            let readResult = try _readDataOfLength(length, untilEOF: true)
            return readResult.toData()

        }
        return UInt64(liPointer.QuadPart)
#else
        return UInt64(lseek(_fd, 0, SEEK_CUR))
#endif
    }

    @discardableResult
    open func seekToEndOfFile() -> UInt64 {
        _checkFileHandle()
#if os(Windows)
        var liPointer: LARGE_INTEGER = LARGE_INTEGER(QuadPart: 0)
        if SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0),
                            &liPointer, DWORD(FILE_END)) == FALSE {
