Things turn much complicated here when taking multi-byte characters into account.  Hadoop [`LineRecordReader`](https://github.com/apache/hadoop-common/blob/release-1.0.4/src/mapred/org/apache/hadoop/mapred/LineRecordReader.java#L127) could be a good reference for this scenario.