@@ -1282,6 +1282,53 @@ function Path:tail(lines)
1282
1282
return (table.concat (data ):gsub (" [\r\n ]$" , " " ))
1283
1283
end
1284
1284
1285
+ --- @param offset integer
1286
+ --- @param length integer
1287
+ --- @return string
1288
+ function Path :readbyterange (offset , length )
1289
+ vim .validate {
1290
+ offset = { offset , " n" },
1291
+ length = { length , " n" },
1292
+ }
1293
+
1294
+ local stat = self :_get_readable_stat ()
1295
+ local fd , err = uv .fs_open (self :absolute (), " r" , 438 )
1296
+ if fd == nil then
1297
+ error (err )
1298
+ end
1299
+
1300
+ if offset < 0 then
1301
+ offset = stat .size + offset
1302
+ -- Windows fails if offset is < 0 even though offset is defined as signed
1303
+ -- http://docs.libuv.org/en/v1.x/fs.html#c.uv_fs_read
1304
+ if offset < 0 then
1305
+ offset = 0
1306
+ end
1307
+ end
1308
+
1309
+ local data = " "
1310
+ local read_chunk
1311
+ while # data < length do
1312
+ -- local read_chunk = assert(uv.fs_read(fd, length - #data, offset))
1313
+ read_chunk , err = uv .fs_read (fd , length - # data , offset )
1314
+ if read_chunk == nil then
1315
+ error (err )
1316
+ end
1317
+ if # read_chunk == 0 then
1318
+ break
1319
+ end
1320
+ data = data .. read_chunk
1321
+ offset = offset + # read_chunk
1322
+ end
1323
+
1324
+ _ , err = uv .fs_close (fd )
1325
+ if err ~= nil then
1326
+ error (err )
1327
+ end
1328
+
1329
+ return data
1330
+ end
1331
+
1285
1332
--- write to file
1286
1333
--- @param data string | string[] data to write
1287
1334
--- @param flags uv.aliases.fs_access_flags | integer flag used to open file (eg. " w" or " a" )
0 commit comments