Skip to content

Commit b959e42

Browse files
committed
Add sig IO#pread and IO#pwrite
1 parent 3c2c906 commit b959e42

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

core/io.rbs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,61 @@ class IO < Object
14431443
#
14441444
def puts: (*untyped objects) -> nil
14451445

1446+
# <!--
1447+
# rdoc-file=io.c
1448+
# - pread(maxlen, offset) -> string
1449+
# - pread(maxlen, offset, out_string) -> string
1450+
# -->
1451+
# Behaves like IO#readpartial, except that it:
1452+
#
1453+
# * Reads at the given `offset` (in bytes).
1454+
# * Disregards, and does not modify, the stream's position (see
1455+
# [Position](rdoc-ref:IO@Position)).
1456+
# * Bypasses any user space buffering in the stream.
1457+
#
1458+
# Because this method does not disturb the stream's state (its position, in
1459+
# particular), `pread` allows multiple threads and processes to use the same IO
1460+
# object for reading at various offsets.
1461+
#
1462+
# f = File.open('t.txt')
1463+
# f.read # => "First line\nSecond line\n\nFourth line\nFifth line\n"
1464+
# f.pos # => 52
1465+
# # Read 12 bytes at offset 0.
1466+
# f.pread(12, 0) # => "First line\n"
1467+
# # Read 9 bytes at offset 8.
1468+
# f.pread(9, 8) # => "ne\nSecon"
1469+
# f.close
1470+
#
1471+
# Not available on some platforms.
1472+
#
1473+
def pread: (Integer maxlen, Integer offset, ?String out_string) -> String
1474+
1475+
# <!--
1476+
# rdoc-file=io.c
1477+
# - pwrite(object, offset) -> integer
1478+
# -->
1479+
# Behaves like IO#write, except that it:
1480+
#
1481+
# * Writes at the given `offset` (in bytes).
1482+
# * Disregards, and does not modify, the stream's position (see
1483+
# [Position](rdoc-ref:IO@Position)).
1484+
# * Bypasses any user space buffering in the stream.
1485+
#
1486+
# Because this method does not disturb the stream's state (its position, in
1487+
# particular), `pwrite` allows multiple threads and processes to use the same IO
1488+
# object for writing at various offsets.
1489+
#
1490+
# f = File.open('t.tmp', 'w+')
1491+
# # Write 6 bytes at offset 3.
1492+
# f.pwrite('ABCDEF', 3) # => 6
1493+
# f.rewind
1494+
# f.read # => "\u0000\u0000\u0000ABCDEF"
1495+
# f.close
1496+
#
1497+
# Not available on some platforms.
1498+
#
1499+
def pwrite: (_ToS object, Integer offset) -> Integer
1500+
14461501
# <!--
14471502
# rdoc-file=io.c
14481503
# - read(maxlen = nil, out_string = nil) -> new_string, out_string, or nil

test/stdlib/IO_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,28 @@ def test_readline
484484
io, :readline, "\n", 100, chomp: true
485485
end
486486
end
487+
488+
def test_pwrite
489+
Dir.mktmpdir do |dir|
490+
File.open(File.join(dir, "io-pwrite"), "w") do |io|
491+
assert_send_type "(String, Integer) -> Integer",
492+
io, :pwrite, "hello", 0
493+
end
494+
end
495+
end
496+
497+
def test_pread
498+
IO.open(IO.sysopen(File.expand_path(__FILE__))) do |io|
499+
assert_send_type(
500+
"(Integer, Integer) -> String",
501+
io, :pread, 10, 0
502+
)
503+
assert_send_type(
504+
"(Integer, Integer, String) -> String",
505+
io, :pread, 10, 0, +"buffer"
506+
)
507+
end
508+
end
487509
end
488510

489511
class IOWaitTest < Test::Unit::TestCase

0 commit comments

Comments
 (0)