Class: IO::Stream::Buffered

Inherits:
Generic
  • Object
show all
Defined in:
lib/io/stream/buffered.rb

Overview

A buffered stream implementation that wraps an underlying IO object to provide efficient buffered reading and writing.

Constant Summary

Constants included from Writable

Writable::ASYNC_SAFE

Constants included from Readable

Readable::ASYNC_SAFE

Instance Attribute Summary collapse

Attributes included from Writable

#minimum_write_size

Attributes included from Readable

#minimum_read_size

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

async_safe?, #close

Methods included from Writable

#<<, async_safe?, #flush, #puts, #write

Methods included from Readable

async_safe?, #block_size, #block_size=, #discard_until, #finish!, #finished?, #gets, #peek, #read, #read_exactly, #read_partial, #read_until, #readpartial

Constructor Details

#initialize(io) ⇒ Buffered

Initialize a new buffered stream.



53
54
55
56
57
58
59
60
61
62
# File 'lib/io/stream/buffered.rb', line 53

def initialize(io, ...)
	super(...)
	
	@io = io
	if io.respond_to?(:timeout)
		@timeout = io.timeout
	else
		@timeout = nil
	end
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



65
66
67
# File 'lib/io/stream/buffered.rb', line 65

def io
  @io
end

Class Method Details

.open(path, mode = "r+", **options) ⇒ Object

Open a file and wrap it in a buffered stream.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/io/stream/buffered.rb', line 17

def self.open(path, mode = "r+", **options)
	stream = self.new(::File.open(path, mode), **options)
	
	return stream unless block_given?
	
	begin
		yield stream
	ensure
		stream.close
	end
end

.wrap(io, **options) ⇒ Object

Wrap an existing IO object in a buffered stream.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/io/stream/buffered.rb', line 33

def self.wrap(io, **options)
	if io.respond_to?(:buffered=)
		io.buffered = false
	elsif io.respond_to?(:sync=)
		io.sync = true
	end
	
	stream = self.new(io, **options)
	
	return stream unless block_given?
	
	begin
		yield stream
	ensure
		stream.close
	end
end

Instance Method Details

#close_readObject

Close the read end of the stream.



80
81
82
# File 'lib/io/stream/buffered.rb', line 80

def close_read
	@io.close_read
end

#close_writeObject

Close the write end of the stream.



85
86
87
88
89
# File 'lib/io/stream/buffered.rb', line 85

def close_write
	super
ensure
	@io.close_write
end

#closed?Boolean

Check if the stream is closed.

Returns:

  • (Boolean)


75
76
77
# File 'lib/io/stream/buffered.rb', line 75

def closed?
	@io.closed?
end

#readable?Boolean

Check if the stream is readable.

Returns:

  • (Boolean)


93
94
95
# File 'lib/io/stream/buffered.rb', line 93

def readable?
	super && @io.readable?
end

#The wrapped IO object.=(wrappedIOobject. = (value)) ⇒ Object



65
# File 'lib/io/stream/buffered.rb', line 65

attr :io

#to_ioObject

Get the underlying IO object.



69
70
71
# File 'lib/io/stream/buffered.rb', line 69

def to_io
	@io.to_io
end