 fs = require("fs")
 isBinaryFile = require("isbinaryfile")
 {Readable} = require 'stream'
+{StringDecoder} = require 'string_decoder'
 
 lastIndexOf = (buffer, length, char) ->
   i = length

      @constructor.chunkedBuffer ?= new Buffer(chunkSize)
      chunkedBuffer = @constructor.chunkedBuffer
      bytesRead = fs.readSync(fd, chunkedBuffer, 0, chunkSize, 0)

      while bytesRead
        # Scary looking. Uses very few new objects


        if index < 0
          # no newlines here, the whole thing is a remainder
          newRemainder = decoder.write(chunkedBuffer.slice(0, bytesRead))
          str = null
        else if index > -1 and index == bytesRead - 1
          # the last char is a newline
          newRemainder = ''
          str = decoder.write(chunkedBuffer.slice(0, bytesRead))
        else
          str = decoder.write(chunkedBuffer.slice(0, index+1))
          newRemainder = decoder.write(chunkedBuffer.slice(index+1, bytesRead))

        if str
          str = remainder + str if remainder
