         if let line = flush() {
             return line
         }
-        while true {
-            let chunk = try reader.read(128)
+        let batch = 128
+        while reading {
+            let chunk = try reader.read(batch)
             if chunk.count == 0 {
-                if buffer.count == 0 {
-                    return nil
-                }
-                return buffer
+                reading = false
+            }
+            else if chunk.count < batch {
+                reading = false
             }
             buffer.appendContentsOf(chunk)
             if let line = flush() {
                 return line
             }
         }
+        if buffer.count > 0 {
+            let line = buffer
+            buffer.removeAll()
+            return line
+        }
+        return nil
     }
 
     func read(maxLength: Int) throws -> [[Int8]] {

            throw ReaderError.GenericError(error: errno)
        }
        var bytes = [Int8]()
        for i in 0..<maxLength {
            bytes.append(buffer[i])
        }
        buffer.dealloc(maxLength)


    var buffer = [Int8]()

    var reading = true

    func flush() -> [Int8]? {
        let size = buffer.count
        for i in 0..<size {
