- 
      
- 
        Save nhatlee/d9b4fb7c6d9b0d9ec32a30f14df1c7d3 to your computer and use it in GitHub Desktop. 
Revisions
- 
        sooop renamed this gist Jan 24, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewingFile renamed without changes.
- 
        sooop revised this gist Jan 24, 2017 . 1 changed file with 5 additions and 11 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,19 +5,17 @@ class StreamReader { let chunkSize: Int let fileHandle: FileHandle var buffer: Data let delimPattern : Data var isAtEOF: Bool = false init?(url: URL, delimeter: String = "\n", encoding: String.Encoding = .utf8, chunkSize: Int = 4096) { guard let fileHandle = try? FileHandle(forReadingFrom: url) else { return nil } self.fileHandle = fileHandle self.chunkSize = chunkSize self.encoding = encoding buffer = Data(capacity: chunkSize) delimPattern = delimeter.data(using: .utf8)! } deinit { @@ -34,20 +32,16 @@ class StreamReader { if isAtEOF { return nil } repeat { if let range = buffer.range(of: delimPattern, options: [], in: buffer.startIndex..<buffer.endIndex) { let subData = buffer.subdata(in: buffer.startIndex..<range.lowerBound) let line = String(data: subData, encoding: encoding) buffer.replaceSubrange(buffer.startIndex..<range.upperBound, with: []) return line } else { let tempData = fileHandle.readData(ofLength: chunkSize) if tempData.count == 0 { isAtEOF = true return (buffer.count > 0) ? String(data: buffer, encoding: encoding) : nil } buffer.append(tempData) } 
- 
        sooop revised this gist Jan 23, 2017 . No changes.There are no files selected for viewing
- 
        sooop revised this gist Jan 23, 2017 . 1 changed file with 6 additions and 31 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,3 @@ import Foundation class StreamReader { @@ -20,15 +12,12 @@ class StreamReader { encoding: String.Encoding = .utf8, chunkSize: Int = 4096) { guard let fileHandle = try? FileHandle(forReadingFrom: url) else { return nil } self.fileHandle = fileHandle self.chunkSize = chunkSize self.encoding = encoding buffer = Data(capacity: chunkSize) delimData = delimeter.data(using: .utf8)! } deinit { @@ -42,13 +31,10 @@ class StreamReader { } func nextLine() -> String? { if isAtEOF { return nil } repeat { if let range = buffer.range(of: delimData, options: [], in: buffer.startIndex..<buffer.endIndex) { let subData = buffer.subdata(in: buffer.startIndex..<range.lowerBound) let line = String(data: subData, encoding: encoding) buffer.replaceSubrange(buffer.startIndex..<range.upperBound, with: []) @@ -59,23 +45,12 @@ class StreamReader { if tempData.count == 0 { isAtEOF = true if buffer.count > 0 { return = String(data: buffer, encoding: encoding) } return nil } buffer.append(tempData) } } while true } } 
- 
        sooop created this gist Jan 23, 2017 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ // // main.swift // LineReader // // Created by BONGSOO KWON on 2017. 1. 23.. // Copyright © 2017년 BONGSOO KWON. All rights reserved. // import Foundation class StreamReader { let encoding: String.Encoding let chunkSize: Int let fileHandle: FileHandle var buffer: Data let delimData : Data var isAtEOF: Bool = false init?(url: URL, delimeter: String = "\n", encoding: String.Encoding = .utf8, chunkSize: Int = 4096) { guard let fileHandle = try? FileHandle(forReadingFrom: url) else { return nil } self.fileHandle = fileHandle self.chunkSize = chunkSize self.encoding = encoding buffer = Data(capacity: chunkSize) self.buffer = Data(capacity: chunkSize) self.delimData = delimeter.data(using: .utf8)! } deinit { fileHandle.closeFile() } func rewind() { fileHandle.seek(toFileOffset: 0) buffer.removeAll(keepingCapacity: true) isAtEOF = false } func nextLine() -> String? { if isAtEOF { return nil } repeat { if let range = buffer.range(of: delimData, options: [], in: buffer.startIndex..<buffer.endIndex) { let subData = buffer.subdata(in: buffer.startIndex..<range.lowerBound) let line = String(data: subData, encoding: encoding) buffer.replaceSubrange(buffer.startIndex..<range.upperBound, with: []) return line } else { let tempData = fileHandle.readData(ofLength: chunkSize) if tempData.count == 0 { isAtEOF = true if buffer.count > 0 { let line = String(data: buffer, encoding: encoding) return line } return nil } buffer.append(tempData) } } while true } } let pathURL = URL(fileURLWithPath: (NSString(string:"~/Downloads/words.txt").expandingTildeInPath )) if FileManager.default.fileExists(atPath: pathURL.path) { print(1) } let s = StreamReader(url: pathURL) for _ in 1...10 { if let line = s?.nextLine() { print(line) } }