@@ -44,7 +44,45 @@ class ChunkedFile {
44
44
guard fileHandle != nil else {
45
45
return Result . failure ( ChunkedFileError . invalidState ( " readNextChunk() called but the file was not open " ) )
46
46
}
47
- return try Result . success ( doReadNextChunk ( ) )
47
+
48
+ SDKLogger . logger? . info ( " --doReadNextChunk " )
49
+ guard let fileHandle = fileHandle, let fileURL = fileURL else {
50
+ throw ChunkedFileError . invalidState ( " doReadNextChunk called without file handle. Did you call open()? " )
51
+ }
52
+ var data : Data ?
53
+ try autoreleasepool {
54
+ data = try fileHandle. read ( upToCount: chunkSize)
55
+ }
56
+
57
+ let fileSize = try fileManager. fileSizeOfItem (
58
+ atPath: fileURL. path
59
+ )
60
+
61
+ guard let data = data else {
62
+ // Called while already at the end of the file. We read zero bytes, "ending" at the end of the file
63
+ return . success(
64
+ FileChunk (
65
+ startByte: fileSize,
66
+ endByte: fileSize,
67
+ totalFileSize: fileSize,
68
+ chunkData: Data ( capacity: 0 )
69
+ )
70
+ )
71
+ }
72
+
73
+ let chunkLength = data. count
74
+ let updatedFilePosition = filePos + UInt64( chunkLength)
75
+
76
+ let chunk = FileChunk (
77
+ startByte: self . filePos,
78
+ endByte: updatedFilePosition,
79
+ totalFileSize: fileSize,
80
+ chunkData: data
81
+ )
82
+
83
+ state? . filePosition = updatedFilePosition
84
+
85
+ return . success( chunk)
48
86
} catch {
49
87
return Result . failure ( ChunkedFileError . fileHandle ( error) )
50
88
}
@@ -86,40 +124,6 @@ class ChunkedFile {
86
124
state? . filePosition = byte
87
125
}
88
126
89
- private func doReadNextChunk( ) throws -> FileChunk {
90
- SDKLogger . logger? . info ( " --doReadNextChunk " )
91
- guard let fileHandle = fileHandle, let fileURL = fileURL else {
92
- throw ChunkedFileError . invalidState ( " doReadNextChunk called without file handle. Did you call open()? " )
93
- }
94
- var data : Data ?
95
- try autoreleasepool {
96
- data = try fileHandle. read ( upToCount: chunkSize)
97
- }
98
-
99
- let fileSize = try fileManager. fileSizeOfItem (
100
- atPath: fileURL. path
101
- )
102
-
103
- guard let data = data else {
104
- // Called while already at the end of the file. We read zero bytes, "ending" at the end of the file
105
- return FileChunk ( startByte: fileSize, endByte: fileSize, totalFileSize: fileSize, chunkData: Data ( capacity: 0 ) )
106
- }
107
-
108
- let nsData = NSData ( data: data)
109
- let readLen = nsData. length
110
- let newFilePos = filePos + UInt64( readLen)
111
- let chunk = FileChunk (
112
- startByte: self . filePos,
113
- endByte: newFilePos,
114
- totalFileSize: fileSize,
115
- chunkData: data
116
- )
117
-
118
- state? . filePosition = newFilePos
119
-
120
- return chunk
121
- }
122
-
123
127
/// Creates a ``ChunkedFile`` that wraps the file given by the URL. The file will be opened after calling ``openFile()``
124
128
init ( chunkSize: Int ) {
125
129
self . chunkSize = chunkSize
0 commit comments