类 IO::Buffer
IO::Buffer
是用于输入/输出的高效零拷贝缓冲区。典型用例有
-
使用
::new
创建一个空缓冲区,使用copy
或set_value
、set_string
填充缓冲区,使用get_string
获取缓冲区或直接使用write
将其写入某个文件。 -
使用
::for
创建映射到某个字符串的缓冲区,然后可以使用get_string
或get_value
进行读取,也可以进行写入(写入也会更改源字符串)。 -
使用
::map
创建映射到某个文件的缓冲区,然后可以使用它来读取和写入底层文件。
与字符串和文件内存的交互通过高效的底层 C 机制(如“memcpy`”)执行。
该类旨在成为一种实用工具,用于实现更高级别的机制,如 Fiber::Scheduler#io_read
和 Fiber::Scheduler#io_write
以及解析二进制协议。
用法示例¶ ↑
空缓冲区
buffer = IO::Buffer.new(8) # create empty 8-byte buffer # => # #<IO::Buffer 0x0000555f5d1a5c50+8 INTERNAL> # ... buffer # => # <IO::Buffer 0x0000555f5d156ab0+8 INTERNAL> # 0x00000000 00 00 00 00 00 00 00 00 buffer.set_string('test', 2) # put there bytes of the "test" string, starting from offset 2 # => 4 buffer.get_string # get the result # => "\x00\x00test\x00\x00"
字符串缓冲区
string = 'buffer' buffer = IO::Buffer.for(string) # => # #<IO::Buffer 0x00007f3f02be9b18+4 SLICE> # ... buffer # => # #<IO::Buffer 0x00007f3f02be9b18+4 SLICE> # 0x00000000 64 61 74 61 buffer buffer.get_string(2) # read content starting from offset 2 # => "ta" buffer.set_string('---', 1) # write content, starting from offset 1 # => 3 buffer # => # #<IO::Buffer 0x00007f3f02be9b18+4 SLICE> # 0x00000000 64 2d 2d 2d d--- string # original string changed, too # => "d---"
文件缓冲区
File.write('test.txt', 'test buffer') # => 9 buffer = IO::Buffer.map(File.open('test.txt')) # => # #<IO::Buffer 0x00007f3f0768c000+9 MAPPED IMMUTABLE> # ... buffer.get_string(5, 2) # read 2 bytes, starting from offset 5 # => "da" buffer.set_string('---', 1) # attempt to write # in `set_string': Buffer is not writable! (IO::Buffer::AccessError) # To create writable file-mapped buffer # Open file for read-write, pass size, offset, and flags=0 buffer = IO::Buffer.map(File.open('test.txt', 'r+'), 9, 0, 0) buffer.set_string('---', 1) # => 3 -- bytes written File.read('test.txt') # => "t--- buffer"
该类为实验性类,其接口可能会发生变化,这尤其适用于文件映射,它在将来可能会被完全移除。
常量
- BIG_ENDIAN
指大端字节序,其中最高有效字节首先存储。有关更多详细信息,请参见
get_value
。- DEFAULT_SIZE
默认缓冲区大小,通常是
PAGE_SIZE
的(小)倍数。可以通过设置 RUBY_IO_BUFFER_DEFAULT_SIZE 环境变量来明确指定。- EXTERNAL
表示缓冲区中的内存由其他人所有。有关更多详细信息,请参见
external?
。- HOST_ENDIAN
指主机机器的字节序。有关更多详细信息,请参见
get_value
。- INTERNAL
表示缓冲区中的内存由缓冲区所有。有关更多详细信息,请参见
internal?
。- LITTLE_ENDIAN
指小端字节序,其中最低有效字节首先存储。有关更多详细信息,请参见
get_value
。- LOCKED
- MAPPED
表示缓冲区中的内存已由操作系统映射。有关更多详细信息,请参见
mapped?
。- NETWORK_ENDIAN
指网络字节序,它与大端字节序相同。有关更多详细信息,请参见
get_value
。- PAGE_SIZE
操作系统页面大小。用于高效的页面对齐内存分配。
- PRIVATE
表示缓冲区中的内存已私有映射,并且更改不会复制到基础文件。有关更多详细信息,请参见
private?
。- READONLY
表示缓冲区中的内存为只读,并且尝试修改它将失败。有关更多详细信息,请参见
readonly?
。- SHARED
表示缓冲区中的内存也已映射,以便可以与其他进程共享。有关更多详细信息,请参见
shared?
。
公共类方法
从给定字符串的内存中创建一个零拷贝IO::Buffer
。如果没有块,则会高效地创建一个字符串的冻结内部副本并将其用作缓冲区源。当提供一个块时,缓冲区将直接与字符串的内部缓冲区关联,并且更新缓冲区将更新字符串。
直到free
在缓冲区上被调用(显式地或通过垃圾收集器),源字符串将被锁定且无法修改。
如果字符串被冻结,它将创建一个只读缓冲区,该缓冲区无法被修改。如果字符串被共享,它可能会在使用块形式时触发写时复制。
string = 'test' buffer = IO::Buffer.for(string) buffer.external? #=> true buffer.get_string(0, 1) # => "t" string # => "best" buffer.resize(100) # in `resize': Cannot resize external buffer! (IO::Buffer::AccessError) IO::Buffer.for(string) do |buffer| buffer.set_string("T") string # => "Test" end
VALUE rb_io_buffer_type_for(VALUE klass, VALUE string) { StringValue(string); // If the string is frozen, both code paths are okay. // If the string is not frozen, if a block is not given, it must be frozen. if (rb_block_given_p()) { struct io_buffer_for_yield_instance_arguments arguments = { .klass = klass, .string = string, .instance = Qnil, .flags = 0, }; return rb_ensure(io_buffer_for_yield_instance, (VALUE)&arguments, io_buffer_for_yield_instance_ensure, (VALUE)&arguments); } else { // This internally returns the source string if it's already frozen. string = rb_str_tmp_frozen_acquire(string); return io_buffer_for_make_instance(klass, string, RB_IO_BUFFER_READONLY); } }
通过内存映射文件为从file
中读取创建一个IO::Buffer
。file_io
应该是一个File
实例,打开以供读取。
可以指定可选的映射size
和offset
。
默认情况下,缓冲区将是不可变的(只读);要创建一个可写映射,您需要以读写模式打开一个文件,并显式传递不带IO::Buffer::IMMUTABLE的flags
参数。
File.write('test.txt', 'test') buffer = IO::Buffer.map(File.open('test.txt'), nil, 0, IO::Buffer::READONLY) # => #<IO::Buffer 0x00000001014a0000+4 MAPPED READONLY> buffer.readonly? # => true buffer.get_string # => "test" buffer.set_string('b', 0) # `set_string': Buffer is not writable! (IO::Buffer::AccessError) # create read/write mapping: length 4 bytes, offset 0, flags 0 buffer = IO::Buffer.map(File.open('test.txt', 'r+'), 4, 0) buffer.set_string('b', 0) # => 1 # Check it File.read('test.txt') # => "best"
请注意,某些操作系统可能在映射缓冲区和文件读取之间没有缓存一致性。
static VALUE io_buffer_map(int argc, VALUE *argv, VALUE klass) { rb_check_arity(argc, 1, 4); // We might like to handle a string path? VALUE io = argv[0]; size_t size; if (argc >= 2 && !RB_NIL_P(argv[1])) { size = io_buffer_extract_size(argv[1]); } else { rb_off_t file_size = rb_file_size(io); // Compiler can confirm that we handled file_size < 0 case: if (file_size < 0) { rb_raise(rb_eArgError, "Invalid negative file size!"); } // Here, we assume that file_size is positive: else if ((uintmax_t)file_size > SIZE_MAX) { rb_raise(rb_eArgError, "File larger than address space!"); } else { // This conversion should be safe: size = (size_t)file_size; } } // This is the file offset, not the buffer offset: rb_off_t offset = 0; if (argc >= 3) { offset = NUM2OFFT(argv[2]); } enum rb_io_buffer_flags flags = 0; if (argc >= 4) { flags = io_buffer_extract_flags(argv[3]); } return rb_io_buffer_map(io, size, offset, flags); }
创建一个新的零填充IO::Buffer
,大小为size
字节。默认情况下,缓冲区将是内部:直接分配的内存块。但是,如果请求的size
大于特定于操作系统的IO::Buffer::PAGE_SIZE
,则将使用虚拟内存机制(Unix 上的匿名mmap
,Windows 上的VirtualAlloc
)分配缓冲区。可以通过将IO::Buffer::MAPPED
作为第二个参数传递来强制执行此行为。
buffer = IO::Buffer.new(4) # => # #<IO::Buffer 0x000055b34497ea10+4 INTERNAL> # 0x00000000 00 00 00 00 .... buffer.get_string(0, 1) # => "\x00" buffer.set_string("test") buffer # => # #<IO::Buffer 0x000055b34497ea10+4 INTERNAL> # 0x00000000 74 65 73 74 test
VALUE rb_io_buffer_initialize(int argc, VALUE *argv, VALUE self) { io_buffer_experimental(); rb_check_arity(argc, 0, 2); struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); size_t size; if (argc > 0) { size = io_buffer_extract_size(argv[0]); } else { size = RUBY_IO_BUFFER_DEFAULT_SIZE; } enum rb_io_buffer_flags flags = 0; if (argc >= 2) { flags = io_buffer_extract_flags(argv[1]); } else { flags |= io_flags_for_size(size); } io_buffer_initialize(self, buffer, NULL, size, flags, Qnil); return self; }
以字节为单位返回给定缓冲区类型的大小。
IO::Buffer.size_of(:u32) # => 4 IO::Buffer.size_of([:u32, :u32]) # => 8
static VALUE io_buffer_size_of(VALUE klass, VALUE buffer_type) { if (RB_TYPE_P(buffer_type, T_ARRAY)) { size_t total = 0; for (long i = 0; i < RARRAY_LEN(buffer_type); i++) { total += io_buffer_buffer_type_size(RB_SYM2ID(RARRAY_AREF(buffer_type, i))); } return SIZET2NUM(total); } else { return SIZET2NUM(io_buffer_buffer_type_size(RB_SYM2ID(buffer_type))); } }
创建一个给定长度的新字符串,并将一个零拷贝IO::Buffer
实例传递给将字符串用作源的块。该块预计会写入缓冲区,并且将返回该字符串。
IO::Buffer.string(4) do |buffer| buffer.set_string("Ruby") end # => "Ruby"
VALUE rb_io_buffer_type_string(VALUE klass, VALUE length) { VALUE string = rb_str_new(NULL, RB_NUM2LONG(length)); struct io_buffer_for_yield_instance_arguments arguments = { .klass = klass, .string = string, .instance = Qnil, }; rb_ensure(io_buffer_for_yield_instance, (VALUE)&arguments, io_buffer_for_yield_instance_ensure, (VALUE)&arguments); return string; }
公共实例方法
通过对源应用二进制 AND 运算来生成一个与源大小相同的新缓冲区,使用掩码,必要时重复。
IO::Buffer.for("1234567890") & IO::Buffer.for("\xFF\x00\x00\xFF") # => # #<IO::Buffer 0x00005589b2758480+4 INTERNAL> # 0x00000000 31 00 00 34 35 00 00 38 39 00 1..45..89.
static VALUE io_buffer_and(VALUE self, VALUE mask) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); struct rb_io_buffer *mask_buffer = NULL; TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer); io_buffer_check_mask(mask_buffer); VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size)); struct rb_io_buffer *output_buffer = NULL; TypedData_Get_Struct(output, struct rb_io_buffer, &rb_io_buffer_type, output_buffer); memory_and(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size); return output; }
缓冲区通过使用 memcmp
比较大小和它们引用的内存的确切内容。
static VALUE rb_io_buffer_compare(VALUE self, VALUE other) { const void *ptr1, *ptr2; size_t size1, size2; rb_io_buffer_get_bytes_for_reading(self, &ptr1, &size1); rb_io_buffer_get_bytes_for_reading(other, &ptr2, &size2); if (size1 < size2) { return RB_INT2NUM(-1); } if (size1 > size2) { return RB_INT2NUM(1); } return RB_INT2NUM(memcmp(ptr1, ptr2, size1)); }
通过对源应用二进制 XOR 操作生成一个与源大小相同的新缓冲区,使用掩码,必要时重复。
IO::Buffer.for("1234567890") ^ IO::Buffer.for("\xFF\x00\x00\xFF") # => # #<IO::Buffer 0x000055a2d5d10480+10 INTERNAL> # 0x00000000 ce 32 33 cb ca 36 37 c7 c6 30 .23..67..0
static VALUE io_buffer_xor(VALUE self, VALUE mask) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); struct rb_io_buffer *mask_buffer = NULL; TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer); io_buffer_check_mask(mask_buffer); VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size)); struct rb_io_buffer *output_buffer = NULL; TypedData_Get_Struct(output, struct rb_io_buffer, &rb_io_buffer_type, output_buffer); memory_xor(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size); return output; }
通过对源应用二进制 AND 操作修改源缓冲区,使用掩码,必要时重复。
source = IO::Buffer.for("1234567890").dup # Make a read/write copy. # => # #<IO::Buffer 0x000056307a0d0c20+10 INTERNAL> # 0x00000000 31 32 33 34 35 36 37 38 39 30 1234567890 source.and!(IO::Buffer.for("\xFF\x00\x00\xFF")) # => # #<IO::Buffer 0x000056307a0d0c20+10 INTERNAL> # 0x00000000 31 00 00 34 35 00 00 38 39 00 1..45..89.
static VALUE io_buffer_and_inplace(VALUE self, VALUE mask) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); struct rb_io_buffer *mask_buffer = NULL; TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer); io_buffer_check_mask(mask_buffer); io_buffer_check_overlaps(buffer, mask_buffer); void *base; size_t size; io_buffer_get_bytes_for_writing(buffer, &base, &size); memory_and_inplace(base, size, mask_buffer->base, mask_buffer->size); return self; }
使用 value
填充缓冲区,从 offset
开始,持续 length
字节。
buffer = IO::Buffer.for('test') # => # <IO::Buffer 0x00007fca40087c38+4 SLICE> # 0x00000000 74 65 73 74 test buffer.clear # => # <IO::Buffer 0x00007fca40087c38+4 SLICE> # 0x00000000 00 00 00 00 .... buf.clear(1) # fill with 1 # => # <IO::Buffer 0x00007fca40087c38+4 SLICE> # 0x00000000 01 01 01 01 .... buffer.clear(2, 1, 2) # fill with 2, starting from offset 1, for 2 bytes # => # <IO::Buffer 0x00007fca40087c38+4 SLICE> # 0x00000000 01 02 02 01 .... buffer.clear(2, 1) # fill with 2, starting from offset 1 # => # <IO::Buffer 0x00007fca40087c38+4 SLICE> # 0x00000000 01 02 02 02 ....
static VALUE io_buffer_clear(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 0, 3); uint8_t value = 0; if (argc >= 1) { value = NUM2UINT(argv[0]); } size_t offset, length; io_buffer_extract_offset_length(self, argc-1, argv+1, &offset, &length); rb_io_buffer_clear(self, value, offset, length); return self; }
从源 IO::Buffer
高效地复制到缓冲区,在 offset
处使用 memcpy
。要复制 String
实例,请参见 set_string
。
buffer = IO::Buffer.new(32) # => # #<IO::Buffer 0x0000555f5ca22520+32 INTERNAL> # 0x00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ # 0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ * buffer.copy(IO::Buffer.for("test"), 8) # => 4 -- size of buffer copied buffer # => # #<IO::Buffer 0x0000555f5cf8fe40+32 INTERNAL> # 0x00000000 00 00 00 00 00 00 00 00 74 65 73 74 00 00 00 00 ........test.... # 0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ *
copy
可用于将缓冲区放入与缓冲区关联的字符串中
string= "buffer: " # => "buffer: " buffer = IO::Buffer.for(string) buffer.copy(IO::Buffer.for("test"), 5) # => 4 string # => "buffer:test"
尝试复制到只读缓冲区将失败
File.write('test.txt', 'test') buffer = IO::Buffer.map(File.open('test.txt'), nil, 0, IO::Buffer::READONLY) buffer.copy(IO::Buffer.for("test"), 8) # in `copy': Buffer is not writable! (IO::Buffer::AccessError)
有关创建可变文件映射的详细信息,请参见 ::map
,这将起作用
buffer = IO::Buffer.map(File.open('test.txt', 'r+')) buffer.copy(IO::Buffer.for("boom"), 0) # => 4 File.read('test.txt') # => "boom"
尝试复制缓冲区,该缓冲区需要放置在缓冲区边界之外,将失败
buffer = IO::Buffer.new(2) buffer.copy(IO::Buffer.for('test'), 0) # in `copy': Specified offset+length is bigger than the buffer size! (ArgumentError)
static VALUE io_buffer_copy(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 1, 4); struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); VALUE source = argv[0]; const void *source_base; size_t source_size; rb_io_buffer_get_bytes_for_reading(source, &source_base, &source_size); return io_buffer_copy_from(buffer, source_base, source_size, argc-1, argv+1); }
遍历缓冲区,从 offset
开始生成每个 value
的 buffer_type
。
如果给定 count
,则只生成 count
个值。
IO::Buffer.for("Hello World").each(:U8, 2, 2) do |offset, value| puts "#{offset}: #{value}" end # 2: 108 # 3: 108
static VALUE io_buffer_each(int argc, VALUE *argv, VALUE self) { RETURN_ENUMERATOR_KW(self, argc, argv, RB_NO_KEYWORDS); const void *base; size_t size; rb_io_buffer_get_bytes_for_reading(self, &base, &size); ID buffer_type; if (argc >= 1) { buffer_type = RB_SYM2ID(argv[0]); } else { buffer_type = RB_IO_BUFFER_DATA_TYPE_U8; } size_t offset, count; io_buffer_extract_offset_count(buffer_type, size, argc-1, argv+1, &offset, &count); for (size_t i = 0; i < count; i++) { size_t current_offset = offset; VALUE value = rb_io_buffer_get_value(base, size, buffer_type, &offset); rb_yield_values(2, SIZET2NUM(current_offset), value); } return self; }
遍历缓冲区,从 offset
开始生成每个字节。
如果给定 count
,则只生成 count
个字节。
IO::Buffer.for("Hello World").each_byte(2, 2) do |offset, byte| puts "#{offset}: #{byte}" end # 2: 108 # 3: 108
static VALUE io_buffer_each_byte(int argc, VALUE *argv, VALUE self) { RETURN_ENUMERATOR_KW(self, argc, argv, RB_NO_KEYWORDS); const void *base; size_t size; rb_io_buffer_get_bytes_for_reading(self, &base, &size); size_t offset, count; io_buffer_extract_offset_count(RB_IO_BUFFER_DATA_TYPE_U8, size, argc-1, argv+1, &offset, &count); for (size_t i = 0; i < count; i++) { unsigned char *value = (unsigned char *)base + i + offset; rb_yield(RB_INT2FIX(*value)); } return self; }
如果缓冲区引用未由缓冲区本身分配或映射的内存,则缓冲区为外部。
使用 ::for
创建的缓冲区对字符串的内存具有外部引用。
外部缓冲区无法调整大小。
static VALUE rb_io_buffer_external_p(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); return RBOOL(buffer->flags & RB_IO_BUFFER_EXTERNAL); }
如果缓冲区引用内存,则将其释放回操作系统。
-
对于映射缓冲区(例如,来自文件):取消映射。
-
对于从头开始创建的缓冲区:释放内存。
-
对于从字符串创建的缓冲区:取消关联。
释放缓冲区后,无法对其执行进一步的操作。
你可以调整已释放缓冲区的大小以重新分配它。
buffer = IO::Buffer.for('test') buffer.free # => #<IO::Buffer 0x0000000000000000+0 NULL> buffer.get_value(:U8, 0) # in `get_value': The buffer is not allocated! (IO::Buffer::AllocationError) buffer.get_string # in `get_string': The buffer is not allocated! (IO::Buffer::AllocationError) buffer.null? # => true
VALUE rb_io_buffer_free(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); if (buffer->flags & RB_IO_BUFFER_LOCKED) { rb_raise(rb_eIOBufferLockedError, "Buffer is locked!"); } io_buffer_free(buffer); return self; }
以指定的 encoding
将缓冲区的一部分或全部读入字符串。如果没有提供编码,则使用 Encoding::BINARY
。
buffer = IO::Buffer.for('test') buffer.get_string # => "test" buffer.get_string(2) # => "st" buffer.get_string(2, 1) # => "s"
static VALUE io_buffer_get_string(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 0, 3); size_t offset, length; struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length); const void *base; size_t size; io_buffer_get_bytes_for_reading(buffer, &base, &size); rb_encoding *encoding; if (argc >= 3) { encoding = rb_find_encoding(argv[2]); } else { encoding = rb_ascii8bit_encoding(); } io_buffer_validate_range(buffer, offset, length); return rb_enc_str_new((const char*)base + offset, length, encoding); }
从缓冲区读取 offset
处的 type
值。buffer_type
应为以下符号之一
-
:U8
:无符号整数,1 字节 -
:S8
:有符号整数,1 字节 -
:u16
:无符号整数,2 字节,小端 -
:U16
:无符号整数,2 字节,大端 -
:s16
:有符号整数,2 字节,小端 -
:S16
:有符号整数,2 字节,大端 -
:u32
:无符号整数,4 字节,小端 -
:U32
:无符号整数,4 字节,大端 -
:s32
:有符号整数,4 字节,小端 -
:S32
:有符号整数,4 字节,大端 -
:u64
:无符号整数,8 字节,小端 -
:U64
:无符号整数,8 字节,大端 -
:s64
:有符号整数,8 字节,小端 -
:S64
:有符号整数,8 字节,大端 -
:f32
:浮点数,4 字节,小端 -
:F32
:浮点数,4 字节,大端 -
:f64
:双精度浮点数,8 字节,小端 -
:F64
:双精度浮点数,8 字节,大端
缓冲区类型具体指存储在缓冲区中的二进制缓冲区的类型。例如,:u32
缓冲区类型是小端格式的 32 位无符号整数。
string = [1.5].pack('f') # => "\x00\x00\xC0?" IO::Buffer.for(string).get_value(:f32, 0) # => 1.5
static VALUE io_buffer_get_value(VALUE self, VALUE type, VALUE _offset) { const void *base; size_t size; size_t offset = io_buffer_extract_offset(_offset); rb_io_buffer_get_bytes_for_reading(self, &base, &size); return rb_io_buffer_get_value(base, size, RB_SYM2ID(type), &offset); }
类似于 get_value
,但它可以处理多个缓冲区类型并返回一个值数组。
string = [1.5, 2.5].pack('ff') IO::Buffer.for(string).get_values([:f32, :f32], 0) # => [1.5, 2.5]
static VALUE io_buffer_get_values(VALUE self, VALUE buffer_types, VALUE _offset) { size_t offset = io_buffer_extract_offset(_offset); const void *base; size_t size; rb_io_buffer_get_bytes_for_reading(self, &base, &size); if (!RB_TYPE_P(buffer_types, T_ARRAY)) { rb_raise(rb_eArgError, "Argument buffer_types should be an array!"); } VALUE array = rb_ary_new_capa(RARRAY_LEN(buffer_types)); for (long i = 0; i < RARRAY_LEN(buffer_types); i++) { VALUE type = rb_ary_entry(buffer_types, i); VALUE value = rb_io_buffer_get_value(base, size, RB_SYM2ID(type), &offset); rb_ary_push(array, value); } return array; }
返回缓冲区的可读字符串表示形式。确切格式可能会更改。
buffer = IO::Buffer.for("Hello World") puts buffer.hexdump # 0x00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello World
由于缓冲区通常相当大,因此您可能希望通过指定偏移量和长度来限制输出
puts buffer.hexdump(6, 5) # 0x00000006 57 6f 72 6c 64 World
static VALUE rb_io_buffer_hexdump(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 0, 3); size_t offset, length; struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length); size_t width = RB_IO_BUFFER_HEXDUMP_DEFAULT_WIDTH; if (argc >= 3) { width = io_buffer_extract_width(argv[2], 1); } // This may raise an exception if the offset/length is invalid: io_buffer_validate_range(buffer, offset, length); VALUE result = Qnil; if (io_buffer_validate(buffer) && buffer->base) { result = rb_str_buf_new(io_buffer_hexdump_output_size(width, length, 1)); io_buffer_hexdump(result, width, buffer->base, offset+length, offset, 1); } return result; }
创建源缓冲区的内部副本。对副本的更新不会影响源缓冲区。
source = IO::Buffer.for("Hello World") # => # #<IO::Buffer 0x00007fd598466830+11 EXTERNAL READONLY SLICE> # 0x00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello World buffer = source.dup # => # #<IO::Buffer 0x0000558cbec03320+11 INTERNAL> # 0x00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello World
static VALUE rb_io_buffer_initialize_copy(VALUE self, VALUE source) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); const void *source_base; size_t source_size; rb_io_buffer_get_bytes_for_reading(source, &source_base, &source_size); io_buffer_initialize(self, buffer, NULL, source_size, io_flags_for_size(source_size), Qnil); return io_buffer_copy_from(buffer, source_base, source_size, 0, NULL); }
检查缓冲区并报告有关其内部状态的有用信息。缓冲区中只有一小部分将以十六进制转储样式格式显示。
buffer = IO::Buffer.for("Hello World") puts buffer.inspect # #<IO::Buffer 0x000000010198ccd8+11 EXTERNAL READONLY SLICE> # 0x00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello World
VALUE rb_io_buffer_inspect(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); VALUE result = rb_io_buffer_to_s(self); if (io_buffer_validate(buffer)) { // Limit the maximum size generated by inspect: size_t size = buffer->size; int clamped = 0; if (size > RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE) { size = RB_IO_BUFFER_INSPECT_HEXDUMP_MAXIMUM_SIZE; clamped = 1; } io_buffer_hexdump(result, RB_IO_BUFFER_INSPECT_HEXDUMP_WIDTH, buffer->base, size, 0, 0); if (clamped) { rb_str_catf(result, "\n(and %" PRIuSIZE " more bytes not printed)", buffer->size - size); } } return result; }
如果缓冲区是内部缓冲区,则表示它引用缓冲区本身分配的内存。
内部缓冲区与任何外部内存(例如字符串)或文件映射无关。
使用 ::new
创建内部缓冲区,并且当请求的大小小于 IO::Buffer::PAGE_SIZE
且在创建时未请求映射时,它是默认值。
内部缓冲区可以调整大小,此类操作通常会使所有切片无效,但并非总是如此。
static VALUE rb_io_buffer_internal_p(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); return RBOOL(buffer->flags & RB_IO_BUFFER_INTERNAL); }
允许以独占方式处理缓冲区,以确保并发安全性。在执行块时,缓冲区被视为已锁定,并且没有其他代码可以进入锁定。此外,锁定缓冲区不能使用 resize
或 free
进行更改。
锁定不是线程安全的。它被设计为非阻塞系统调用的安全网。您只能通过适当的同步技术在各个线程之间共享缓冲区。
buffer = IO::Buffer.new(4) buffer.locked? #=> false Fiber.schedule do buffer.locked do buffer.write(io) # theoretical system call interface end end Fiber.schedule do # in `locked': Buffer already locked! (IO::Buffer::LockedError) buffer.locked do buffer.set_string("test", 0) end end
VALUE rb_io_buffer_locked(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); if (buffer->flags & RB_IO_BUFFER_LOCKED) { rb_raise(rb_eIOBufferLockedError, "Buffer already locked!"); } buffer->flags |= RB_IO_BUFFER_LOCKED; VALUE result = rb_yield(self); buffer->flags &= ~RB_IO_BUFFER_LOCKED; return result; }
如果缓冲区已锁定,表示它位于 locked
块执行中。已锁定的缓冲区无法调整大小或释放,并且无法对其获取另一个锁。
锁定不是线程安全的,但它是一种语义,用于确保在系统调用使用缓冲区时缓冲区不会移动。
buffer.locked do buffer.write(io) # theoretical system call interface end
static VALUE rb_io_buffer_locked_p(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); return RBOOL(buffer->flags & RB_IO_BUFFER_LOCKED); }
如果缓冲区已映射,表示它引用了由缓冲区映射的内存。
映射缓冲区要么是匿名的(如果通过 ::new
使用 IO::Buffer::MAPPED
标志创建,或者如果大小至少为 IO::Buffer::PAGE_SIZE
),要么由文件支持(如果通过 ::map
创建)。
映射缓冲区通常可以调整大小,此操作通常会使所有切片无效,但并非总是如此。
static VALUE rb_io_buffer_mapped_p(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); return RBOOL(buffer->flags & RB_IO_BUFFER_MAPPED); }
通过对源应用二进制 NOT 操作就地修改源缓冲区。
source = IO::Buffer.for("1234567890").dup # Make a read/write copy. # => # #<IO::Buffer 0x000056307a33a450+10 INTERNAL> # 0x00000000 31 32 33 34 35 36 37 38 39 30 1234567890 source.not! # => # #<IO::Buffer 0x000056307a33a450+10 INTERNAL> # 0x00000000 ce cd cc cb ca c9 c8 c7 c6 cf ..........
static VALUE io_buffer_not_inplace(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); void *base; size_t size; io_buffer_get_bytes_for_writing(buffer, &base, &size); memory_not_inplace(base, size); return self; }
如果缓冲区已通过 free
释放,通过 transfer
传输,或者根本从未分配过。
buffer = IO::Buffer.new(0) buffer.null? #=> true buffer = IO::Buffer.new(4) buffer.null? #=> false buffer.free buffer.null? #=> true
static VALUE rb_io_buffer_null_p(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); return RBOOL(buffer->base == NULL); }
通过使用掩码对源应用二进制 OR 操作就地修改源缓冲区,必要时重复。
source = IO::Buffer.for("1234567890").dup # Make a read/write copy. # => # #<IO::Buffer 0x000056307a272350+10 INTERNAL> # 0x00000000 31 32 33 34 35 36 37 38 39 30 1234567890 source.or!(IO::Buffer.for("\xFF\x00\x00\xFF")) # => # #<IO::Buffer 0x000056307a272350+10 INTERNAL> # 0x00000000 ff 32 33 ff ff 36 37 ff ff 30 .23..67..0
static VALUE io_buffer_or_inplace(VALUE self, VALUE mask) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); struct rb_io_buffer *mask_buffer = NULL; TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer); io_buffer_check_mask(mask_buffer); io_buffer_check_overlaps(buffer, mask_buffer); void *base; size_t size; io_buffer_get_bytes_for_writing(buffer, &base, &size); memory_or_inplace(base, size, mask_buffer->base, mask_buffer->size); return self; }
从指定位置 from
开始,从 io
中读取至少 length
字节,写入从 offset
开始的缓冲区。如果发生错误,则返回 -errno
。
如果未指定 length
或 length
为 nil
,则其默认为缓冲区大小减去偏移量,即整个缓冲区。
如果 length
为零,则会执行一次 pread
操作。
如果未指定 offset
,则其默认为零,即缓冲区的开头。
IO::Buffer.for('test') do |buffer| p buffer # => # <IO::Buffer 0x00007fca40087c38+4 SLICE> # 0x00000000 74 65 73 74 test # take 2 bytes from the beginning of urandom, # put them in buffer starting from position 2 buffer.pread(File.open('/dev/urandom', 'rb'), 0, 2, 2) p buffer # => # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE> # 0x00000000 05 35 73 74 te.5 end
static VALUE io_buffer_pread(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 2, 4); VALUE io = argv[0]; rb_off_t from = NUM2OFFT(argv[1]); size_t length, offset; io_buffer_extract_length_offset(self, argc-2, argv+2, &length, &offset); return rb_io_buffer_pread(self, io, from, length, offset); }
如果缓冲区是私有的,则表示对缓冲区的修改不会复制到底层文件映射。
# Create a test file: File.write('test.txt', 'test') # Create a private mapping from the given file. Note that the file here # is opened in read-only mode, but it doesn't matter due to the private # mapping: buffer = IO::Buffer.map(File.open('test.txt'), nil, 0, IO::Buffer::PRIVATE) # => #<IO::Buffer 0x00007fce63f11000+4 MAPPED PRIVATE> # Write to the buffer (invoking CoW of the underlying file buffer): buffer.set_string('b', 0) # => 1 # The file itself is not modified: File.read('test.txt') # => "test"
static VALUE rb_io_buffer_private_p(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); return RBOOL(buffer->flags & RB_IO_BUFFER_PRIVATE); }
从缓冲区写入至少 length
字节,从 offset
开始,到 io
,从指定的 from
位置开始。如果出现错误,则返回 -errno
。
如果未指定 length
或 length
为 nil
,则其默认为缓冲区大小减去偏移量,即整个缓冲区。
如果 length
为零,则会执行一个 pwrite
操作。
如果未指定 offset
,则其默认为零,即缓冲区的开头。
如果 from
位置超出了文件末尾,则会用空值 (0 值) 字节填充该间隙。
out = File.open('output.txt', File::RDWR) # open for read/write, no truncation IO::Buffer.for('1234567').pwrite(out, 2, 3, 1)
这会导致 234
(3 个字节,从位置 1 开始) 写入到 output.txt
中,从文件位置 2 开始。
static VALUE io_buffer_pwrite(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 2, 4); VALUE io = argv[0]; rb_off_t from = NUM2OFFT(argv[1]); size_t length, offset; io_buffer_extract_length_offset(self, argc-2, argv+2, &length, &offset); return rb_io_buffer_pwrite(self, io, from, length, offset); }
从 io
中读取至少 length
字节,到从 offset
开始的缓冲区中。如果出现错误,则返回 -errno
。
如果未指定 length
或 length
为 nil
,则其默认为缓冲区大小减去偏移量,即整个缓冲区。
如果 length
为零,则会执行一个 read
操作。
如果未指定 offset
,则其默认为零,即缓冲区的开头。
IO::Buffer.for('test') do |buffer| p buffer # => # <IO::Buffer 0x00007fca40087c38+4 SLICE> # 0x00000000 74 65 73 74 test buffer.read(File.open('/dev/urandom', 'rb'), 2) p buffer # => # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE> # 0x00000000 05 35 73 74 .5st end
static VALUE io_buffer_read(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 1, 3); VALUE io = argv[0]; size_t length, offset; io_buffer_extract_length_offset(self, argc-1, argv+1, &length, &offset); return rb_io_buffer_read(self, io, length, offset); }
如果缓冲区是只读的,则表示无法使用 set_value
、set_string
或 copy
等方法修改缓冲区。
冻结的字符串和只读文件会创建只读缓冲区。
static VALUE io_buffer_readonly_p(VALUE self) { return RBOOL(rb_io_buffer_readonly_p(self)); }
将缓冲区调整为 new_size
字节,同时保留其内容。根据旧大小和新大小,与缓冲区关联的内存区域可能会扩展,或在内容被复制的情况下重新分配到不同的地址。
buffer = IO::Buffer.new(4) buffer.set_string("test", 0) buffer.resize(8) # resize to 8 bytes # => # #<IO::Buffer 0x0000555f5d1a1630+8 INTERNAL> # 0x00000000 74 65 73 74 00 00 00 00 test....
外部缓冲区(使用 ::for
创建)和锁定缓冲区无法调整大小。
static VALUE io_buffer_resize(VALUE self, VALUE size) { rb_io_buffer_resize(self, io_buffer_extract_size(size)); return self; }
使用 memcpy
,高效地从源 String
复制到缓冲区,在 offset
处。
buf = IO::Buffer.new(8) # => # #<IO::Buffer 0x0000557412714a20+8 INTERNAL> # 0x00000000 00 00 00 00 00 00 00 00 ........ # set buffer starting from offset 1, take 2 bytes starting from string's # second buf.set_string('test', 1, 2, 1) # => 2 buf # => # #<IO::Buffer 0x0000557412714a20+8 INTERNAL> # 0x00000000 00 65 73 00 00 00 00 00 .es.....
另请参阅 copy
,了解有关如何使用缓冲区写入来更改关联的字符串和文件的示例。
static VALUE io_buffer_set_string(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 1, 4); struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); VALUE string = rb_str_to_str(argv[0]); const void *source_base = RSTRING_PTR(string); size_t source_size = RSTRING_LEN(string); return io_buffer_copy_from(buffer, source_base, source_size, argc-1, argv+1); }
在offset
处将type
的value
写入缓冲区。type
应为get_value
中描述的符号之一。
buffer = IO::Buffer.new(8) # => # #<IO::Buffer 0x0000555f5c9a2d50+8 INTERNAL> # 0x00000000 00 00 00 00 00 00 00 00 buffer.set_value(:U8, 1, 111) # => 1 buffer # => # #<IO::Buffer 0x0000555f5c9a2d50+8 INTERNAL> # 0x00000000 00 6f 00 00 00 00 00 00 .o......
请注意,如果type
为整数且value
为Float
,则会执行隐式截断
buffer = IO::Buffer.new(8) buffer.set_value(:U32, 0, 2.5) buffer # => # #<IO::Buffer 0x0000555f5c9a2d50+8 INTERNAL> # 0x00000000 00 00 00 02 00 00 00 00 # ^^ the same as if we'd pass just integer 2
static VALUE io_buffer_set_value(VALUE self, VALUE type, VALUE _offset, VALUE value) { void *base; size_t size; size_t offset = io_buffer_extract_offset(_offset); rb_io_buffer_get_bytes_for_writing(self, &base, &size); rb_io_buffer_set_value(base, size, RB_SYM2ID(type), &offset, value); return SIZET2NUM(offset); }
在offset
处将buffer_types
的values
写入缓冲区。buffer_types
应为get_value
中描述的符号数组。values
应为要写入的值数组。
buffer = IO::Buffer.new(8) buffer.set_values([:U8, :U16], 0, [1, 2]) buffer # => # #<IO::Buffer 0x696f717561746978+8 INTERNAL> # 0x00000000 01 00 02 00 00 00 00 00 ........
static VALUE io_buffer_set_values(VALUE self, VALUE buffer_types, VALUE _offset, VALUE values) { if (!RB_TYPE_P(buffer_types, T_ARRAY)) { rb_raise(rb_eArgError, "Argument buffer_types should be an array!"); } if (!RB_TYPE_P(values, T_ARRAY)) { rb_raise(rb_eArgError, "Argument values should be an array!"); } if (RARRAY_LEN(buffer_types) != RARRAY_LEN(values)) { rb_raise(rb_eArgError, "Argument buffer_types and values should have the same length!"); } size_t offset = io_buffer_extract_offset(_offset); void *base; size_t size; rb_io_buffer_get_bytes_for_writing(self, &base, &size); for (long i = 0; i < RARRAY_LEN(buffer_types); i++) { VALUE type = rb_ary_entry(buffer_types, i); VALUE value = rb_ary_entry(values, i); rb_io_buffer_set_value(base, size, RB_SYM2ID(type), &offset, value); } return SIZET2NUM(offset); }
生成另一个IO::Buffer
,它是一个切片(或对当前切片的视图),从offset
字节开始,持续length
字节。
切片发生时不会复制内存,并且切片会继续与原始缓冲区的源(字符串或文件,如果存在)相关联。
如果未给定偏移量,则为零。如果偏移量为负,则会引发ArgumentError
。
如果未给定长度,则切片将与原始缓冲区一样长,减去指定的偏移量。如果长度为负,则会引发ArgumentError
。
如果offset+length
超出当前缓冲区的边界,则引发RuntimeError
。
string = 'test' buffer = IO::Buffer.for(string) slice = buffer.slice # => # #<IO::Buffer 0x0000000108338e68+4 SLICE> # 0x00000000 74 65 73 74 test buffer.slice(2) # => # #<IO::Buffer 0x0000000108338e6a+2 SLICE> # 0x00000000 73 74 st slice = buffer.slice(1, 2) # => # #<IO::Buffer 0x00007fc3d34ebc49+2 SLICE> # 0x00000000 65 73 es # Put "o" into 0s position of the slice slice.set_string('o', 0) slice # => # #<IO::Buffer 0x00007fc3d34ebc49+2 SLICE> # 0x00000000 6f 73 os # it is also visible at position 1 of the original buffer buffer # => # #<IO::Buffer 0x00007fc3d31e2d80+4 SLICE> # 0x00000000 74 6f 73 74 tost # ...and original string string # => tost
static VALUE io_buffer_slice(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 0, 2); size_t offset, length; struct rb_io_buffer *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length); return rb_io_buffer_slice(buffer, self, offset, length); }
缓冲区的简短表示形式。它包括地址、大小和符号标志。此格式可能会更改。
puts IO::Buffer.new(4) # uses to_s internally # #<IO::Buffer 0x000055769f41b1a0+4 INTERNAL>
VALUE rb_io_buffer_to_s(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); VALUE result = rb_str_new_cstr("#<"); rb_str_append(result, rb_class_name(CLASS_OF(self))); rb_str_catf(result, " %p+%"PRIdSIZE, buffer->base, buffer->size); if (buffer->base == NULL) { rb_str_cat2(result, " NULL"); } if (buffer->flags & RB_IO_BUFFER_EXTERNAL) { rb_str_cat2(result, " EXTERNAL"); } if (buffer->flags & RB_IO_BUFFER_INTERNAL) { rb_str_cat2(result, " INTERNAL"); } if (buffer->flags & RB_IO_BUFFER_MAPPED) { rb_str_cat2(result, " MAPPED"); } if (buffer->flags & RB_IO_BUFFER_FILE) { rb_str_cat2(result, " FILE"); } if (buffer->flags & RB_IO_BUFFER_SHARED) { rb_str_cat2(result, " SHARED"); } if (buffer->flags & RB_IO_BUFFER_LOCKED) { rb_str_cat2(result, " LOCKED"); } if (buffer->flags & RB_IO_BUFFER_PRIVATE) { rb_str_cat2(result, " PRIVATE"); } if (buffer->flags & RB_IO_BUFFER_READONLY) { rb_str_cat2(result, " READONLY"); } if (buffer->source != Qnil) { rb_str_cat2(result, " SLICE"); } if (!io_buffer_validate(buffer)) { rb_str_cat2(result, " INVALID"); } return rb_str_cat2(result, ">"); }
将底层内存的所有权传输到新缓冲区,导致当前缓冲区未初始化。
buffer = IO::Buffer.new('test') other = buffer.transfer other # => # #<IO::Buffer 0x00007f136a15f7b0+4 SLICE> # 0x00000000 74 65 73 74 test buffer # => # #<IO::Buffer 0x0000000000000000+0 NULL> buffer.null? # => true
VALUE rb_io_buffer_transfer(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); if (buffer->flags & RB_IO_BUFFER_LOCKED) { rb_raise(rb_eIOBufferLockedError, "Cannot transfer ownership of locked buffer!"); } VALUE instance = rb_io_buffer_type_allocate(rb_class_of(self)); struct rb_io_buffer *transferred; TypedData_Get_Struct(instance, struct rb_io_buffer, &rb_io_buffer_type, transferred); *transferred = *buffer; io_buffer_zero(buffer); return instance; }
返回缓冲区 buffer 是否可访问。
如果缓冲区是已释放或在不同地址重新分配的另一个缓冲区(或字符串)的切片,则该缓冲区将变为无效。
static VALUE rb_io_buffer_valid_p(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); return RBOOL(io_buffer_validate(buffer)); }
从 offset
开始返回 buffer_type
值的数组。
如果给定 count
,则仅返回 count
个值。
IO::Buffer.for("Hello World").values(:U8, 2, 2) # => [108, 108]
static VALUE io_buffer_values(int argc, VALUE *argv, VALUE self) { const void *base; size_t size; rb_io_buffer_get_bytes_for_reading(self, &base, &size); ID buffer_type; if (argc >= 1) { buffer_type = RB_SYM2ID(argv[0]); } else { buffer_type = RB_IO_BUFFER_DATA_TYPE_U8; } size_t offset, count; io_buffer_extract_offset_count(buffer_type, size, argc-1, argv+1, &offset, &count); VALUE array = rb_ary_new_capa(count); for (size_t i = 0; i < count; i++) { VALUE value = rb_io_buffer_get_value(base, size, buffer_type, &offset); rb_ary_push(array, value); } return array; }
从 offset
开始,从缓冲区写入至少 length
字节到 io
中。如果发生错误,则返回 -errno
。
如果未指定 length
或 length
为 nil
,则其默认为缓冲区大小减去偏移量,即整个缓冲区。
如果 length
为零,则将执行一个 write
操作。
如果未指定 offset
,则其默认为零,即缓冲区的开头。
out = File.open('output.txt', 'wb') IO::Buffer.for('1234567').write(out, 3)
这会导致 123
被写入 output.txt
static VALUE io_buffer_write(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 1, 3); VALUE io = argv[0]; size_t length, offset; io_buffer_extract_length_offset(self, argc-1, argv+1, &length, &offset); return rb_io_buffer_write(self, io, length, offset); }
通过对源应用二进制 XOR 操作(使用掩码,根据需要重复),就地修改源缓冲区。
source = IO::Buffer.for("1234567890").dup # Make a read/write copy. # => # #<IO::Buffer 0x000056307a25b3e0+10 INTERNAL> # 0x00000000 31 32 33 34 35 36 37 38 39 30 1234567890 source.xor!(IO::Buffer.for("\xFF\x00\x00\xFF")) # => # #<IO::Buffer 0x000056307a25b3e0+10 INTERNAL> # 0x00000000 ce 32 33 cb ca 36 37 c7 c6 30 .23..67..0
static VALUE io_buffer_xor_inplace(VALUE self, VALUE mask) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); struct rb_io_buffer *mask_buffer = NULL; TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer); io_buffer_check_mask(mask_buffer); io_buffer_check_overlaps(buffer, mask_buffer); void *base; size_t size; io_buffer_get_bytes_for_writing(buffer, &base, &size); memory_xor_inplace(base, size, mask_buffer->base, mask_buffer->size); return self; }
通过对源应用二进制 OR 操作(使用掩码,根据需要重复),生成与源大小相同的新缓冲区。
IO::Buffer.for("1234567890") | IO::Buffer.for("\xFF\x00\x00\xFF") # => # #<IO::Buffer 0x0000561785ae3480+10 INTERNAL> # 0x00000000 ff 32 33 ff ff 36 37 ff ff 30 .23..67..0
static VALUE io_buffer_or(VALUE self, VALUE mask) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); struct rb_io_buffer *mask_buffer = NULL; TypedData_Get_Struct(mask, struct rb_io_buffer, &rb_io_buffer_type, mask_buffer); io_buffer_check_mask(mask_buffer); VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size)); struct rb_io_buffer *output_buffer = NULL; TypedData_Get_Struct(output, struct rb_io_buffer, &rb_io_buffer_type, output_buffer); memory_or(output_buffer->base, buffer->base, buffer->size, mask_buffer->base, mask_buffer->size); return output; }
通过对源应用二进制 NOT 操作,生成与源大小相同的新缓冲区。
~IO::Buffer.for("1234567890") # => # #<IO::Buffer 0x000055a5ac42f120+10 INTERNAL> # 0x00000000 ce cd cc cb ca c9 c8 c7 c6 cf ..........
static VALUE io_buffer_not(VALUE self) { struct rb_io_buffer *buffer = NULL; TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, buffer); VALUE output = rb_io_buffer_new(NULL, buffer->size, io_flags_for_size(buffer->size)); struct rb_io_buffer *output_buffer = NULL; TypedData_Get_Struct(output, struct rb_io_buffer, &rb_io_buffer_type, output_buffer); memory_not(output_buffer->base, buffer->base, buffer->size); return output; }