Class: Async::IO::Stream

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, block_size: 1024*8, sync: true) ⇒ Stream

Returns a new instance of Stream.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/async/io/stream.rb', line 27

def initialize(io, block_size: 1024*8, sync: true)
	@io = io
	@eof = false
	
	# We don't want Ruby to do any IO buffering.
	@io.sync = sync
	
	@block_size = block_size
	
	@read_buffer = BinaryString.new
	@write_buffer = BinaryString.new
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



40
41
42
# File 'lib/async/io/stream.rb', line 40

def io
  @io
end

Instance Method Details

#<<(string) ⇒ Object

Writes ‘string` to the stream and returns self.



68
69
70
71
72
# File 'lib/async/io/stream.rb', line 68

def <<(string)
	write(string)
	
	return self
end

#closeObject

Closes the stream and flushes any unwritten data.



95
96
97
98
99
# File 'lib/async/io/stream.rb', line 95

def close
	flush rescue nil
	
	@io.close
end

#eof?Boolean Also known as: eof

Returns true if the stream is at file which means there is no more data to be read.

Returns:

  • (Boolean)


102
103
104
105
106
# File 'lib/async/io/stream.rb', line 102

def eof?
	fill_read_buffer if !@eof && @read_buffer.empty?
	
	return @eof && @read_buffer.empty?
end

#flushObject

Flushes buffered data to the stream.



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

def flush
	syswrite(@write_buffer)
	@write_buffer.clear
end

#gets(separator = $/) ⇒ Object



80
81
82
83
84
# File 'lib/async/io/stream.rb', line 80

def gets(separator = $/)
	flush
	
	read_until(separator)
end

#peekObject



132
133
134
135
136
# File 'lib/async/io/stream.rb', line 132

def peek
	until yield(@read_buffer) || @eof
		fill_read_buffer
	end
end

#puts(*args, separator: $/) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/async/io/stream.rb', line 86

def puts(*args, separator: $/)
	args.each do |arg|
		@write_buffer << arg << separator
	end
	
	flush
end

#read(size = nil) ⇒ Object

Reads ‘size` bytes from the stream. If size is not specified, read until end of file.



43
44
45
46
47
48
49
50
51
# File 'lib/async/io/stream.rb', line 43

def read(size = nil)
	return "" if size == 0
	
	until @eof || (size && size <= @read_buffer.size)
		fill_read_buffer
	end

	return consume_read_buffer(size)
end

#read_until(pattern) ⇒ String

Efficiently read data from the stream until encountering pattern.

Parameters:

  • pattern (String)

    The pattern to match.

Returns:

  • (String)

    The contents of the stream up until the pattern, which is consumed but not returned.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/async/io/stream.rb', line 113

def read_until(pattern)
	index = @read_buffer.index(pattern)
	
	until index
		offset = @read_buffer.size

		fill_read_buffer
		
		return if @eof

		index = @read_buffer.index(pattern, offset)
	end
	
	matched = @read_buffer.slice!(0, index)
	@read_buffer.slice!(0, pattern.bytesize)
	
	return matched
end

#write(string) ⇒ Object

Writes ‘string` to the buffer. When the buffer is full or #sync is true the buffer is flushed to the underlying `io`.

Parameters:

  • string

    the string to write to the buffer.

Returns:

  • the number of bytes appended to the buffer.



57
58
59
60
61
62
63
64
65
# File 'lib/async/io/stream.rb', line 57

def write(string)
	@write_buffer << string
	
	if @write_buffer.size > @block_size
		flush
	end
	
	return string.bytesize
end