         }
     }
     
+    deinit{
+        close()
+    }
 }
+
 
 
 

    var fileSize:UInt64? {
        if  fileHandle != nil{
            let prevOffset = fileHandle.offsetInFile
            fileHandle.seekToEndOfFile()
            let size = fileHandle.offsetInFile
            fileHandle.seek(toFileOffset: prevOffset)
            return size
        }

            return nil
        }
        
        self.buffer = Data(capacity: chunkSize)
        
        
        if let delimData = delimiter.data(using: encoding) {
            self.delimData = delimData
        }else{
            return nil
        }
    }
    
    fileprivate func nextLine() -> String? {
        
        if atEOF {
            return nil
        }
        
        // Read data chunks from file until a line delimiter is found:
        var range = buffer.range(of: delimData, options: .init(rawValue: 0), in: 0..<buffer.count)
        while range == nil {
            var tmpData = fileHandle.readData(ofLength: chunkSize)
            if tmpData.count == 0 {
                // EOF or read error.
                atEOF = true
                if buffer.count > 0 {
                    // Buffer contains last line in file (not terminated by delimiter).
                    
                    let line = String(bytes: buffer, encoding: encoding)
                    buffer.count = 0
                    return line
                }
                // No more lines.
                return nil
            }
            buffer.append(tmpData)
            range = buffer.range(of: delimData , options: .init(rawValue: 0), in: 0..<buffer.count)
        }
        
        // Convert complete line (excluding the delimiter) to a string:
        let line = String(bytes: buffer.subdata(in: 0..<range!.lowerBound), encoding: encoding)
        
        // Remove line (and the delimiter) from the buffer:
        buffer.resetBytes(in: 0..<(range!.lowerBound + range!.count))
        
        return line
    }
    
    func rewind() {
        fileHandle.seek(toFileOffset: 0)
        buffer.count = 0
        atEOF = false
    }
    
