Skip to content

Commit

Permalink
feat: ChunkReader
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Dec 4, 2024
1 parent b0f5bec commit 29dec5f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
30 changes: 6 additions & 24 deletions Sources/InfomaniakCore/Chunking/ChunkProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,24 @@ public protocol ChunkProvidable: IteratorProtocol {
public final class ChunkProvider: ChunkProvidable {
public typealias Element = Data

let fileHandle: FileHandlable
let chunkReader: ChunkReader

var ranges: [DataRange]

deinit {
do {
// For the sake of consistency
try fileHandle.close()
} catch {}
}

public init?(fileURL: URL, ranges: [DataRange]) {
self.ranges = ranges

do {
fileHandle = try FileHandle(forReadingFrom: fileURL)
} catch {
guard let chunkReader = ChunkReader(fileURL: fileURL) else {
return nil
}

self.chunkReader = chunkReader
}

/// Internal testing method
init(mockedHandlable: FileHandlable, ranges: [DataRange]) {
self.ranges = ranges
fileHandle = mockedHandlable
chunkReader = ChunkReader(mockedHandlable: mockedHandlable)
}

/// Will provide chunks one by one, using the IteratorProtocol
Expand All @@ -69,23 +62,12 @@ public final class ChunkProvider: ChunkProvidable {
let range = ranges.removeFirst()

do {
let chunk = try readChunk(range: range)
let chunk = try chunkReader.readChunk(range: range)
return chunk
} catch {
return nil
}
}

// MARK: Internal

func readChunk(range: DataRange) throws -> Data? {
let offset = range.lowerBound
try fileHandle.seek(toOffset: offset)

let byteCount = Int(range.upperBound - range.lowerBound) + 1
let chunk = try fileHandle.read(upToCount: byteCount)
return chunk
}
}

/// Print the FileHandle shows the current offset
Expand Down
53 changes: 53 additions & 0 deletions Sources/InfomaniakCore/Chunking/ChunkReader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Infomaniak kDrive - iOS App
Copyright (C) 2024 Infomaniak Network SA

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import Foundation

@available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *)
public final class ChunkReader {
let fileHandle: FileHandlable

deinit {
do {
// For the sake of consistency
try fileHandle.close()
} catch {}
}

public init?(fileURL: URL) {
do {
fileHandle = try FileHandle(forReadingFrom: fileURL)
} catch {
return nil
}
}

/// Internal testing method
init(mockedHandlable: FileHandlable) {
fileHandle = mockedHandlable
}

public func readChunk(range: DataRange) throws -> Data? {
let offset = range.lowerBound
try fileHandle.seek(toOffset: offset)

let byteCount = Int(range.upperBound - range.lowerBound) + 1
let chunk = try fileHandle.read(upToCount: byteCount)
return chunk
}
}
6 changes: 3 additions & 3 deletions Tests/InfomaniakCoreTests/Chunking/UTChunkProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ final class UTChunkProvider: XCTestCase {
XCTAssertEqual(index, range.lowerBound)
}

let chunkProvider = ChunkProvider(mockedHandlable: mckFileHandle, ranges: [])
let chunkProvider = ChunkReader(mockedHandlable: mckFileHandle)

// WHEN
do {
Expand All @@ -141,7 +141,7 @@ final class UTChunkProvider: XCTestCase {
mckFileHandle.readUpToCountClosure = { _ in Data() }
mckFileHandle.seekToOffsetError = NSError(domain: "k", code: 1337)

let chunkProvider = ChunkProvider(mockedHandlable: mckFileHandle, ranges: [])
let chunkProvider = ChunkReader(mockedHandlable: mckFileHandle)

// WHEN
do {
Expand All @@ -168,7 +168,7 @@ final class UTChunkProvider: XCTestCase {
mckFileHandle.readUpToCountClosure = { _ in Data() }
mckFileHandle.readUpToCountError = NSError(domain: "k", code: 1337)

let chunkProvider = ChunkProvider(mockedHandlable: mckFileHandle, ranges: [])
let chunkProvider = ChunkReader(mockedHandlable: mckFileHandle)

// WHEN
do {
Expand Down

0 comments on commit 29dec5f

Please sign in to comment.