Class: IO::LikeHelpers::CharacterIO::BasicReader Private
- Inherits:
-
Object
- Object
- IO::LikeHelpers::CharacterIO::BasicReader
- Defined in:
- lib/io/like_helpers/character_io/basic_reader.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
This class exists mostly to provide an interface that is compatible with that of ConverterReader. It is otherwise a thin wrapper around the BufferedIO instance provided to it as a data source.
Direct Known Subclasses
Instance Method Summary collapse
-
#clear ⇒ nil
private
Clears the state of this reader.
-
#consume(length) ⇒ nil
private
Consumes bytes from the front of the buffer.
-
#content ⇒ String
private
Returns the bytes of the buffer as a binary encoded String.
-
#empty? ⇒ Boolean
private
Returns ‘true` if the read buffer is empty and `false` otherwise.
-
#encoding ⇒ Encoding
private
The encoding to apply on byte strings from #content.
-
#initialize(buffered_io, encoding: nil) ⇒ BasicReader
constructor
private
Creates a new intance of this class.
-
#refill(many = true) ⇒ nil
private
Refills the buffer from the stream.
-
#unread(buffer, length: buffer.bytesize) ⇒ nil
private
Places bytes at the beginning of the read buffer.
Constructor Details
#initialize(buffered_io, encoding: nil) ⇒ BasicReader
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new intance of this class.
When ‘encoding` is `nil`, #encoding will return the current value of Encoding.default_external when called.
22 23 24 25 26 27 28 |
# File 'lib/io/like_helpers/character_io/basic_reader.rb', line 22 def initialize( buffered_io, encoding: nil ) @buffered_io = buffered_io @encoding = encoding ? Encoding.find(encoding) : nil end |
Instance Method Details
#clear ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clears the state of this reader.
34 35 36 37 |
# File 'lib/io/like_helpers/character_io/basic_reader.rb', line 34 def clear buffered_io.flush nil end |
#consume(length) ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Consumes bytes from the front of the buffer.
63 64 65 66 |
# File 'lib/io/like_helpers/character_io/basic_reader.rb', line 63 def consume(length) buffered_io.skip(length) nil end |
#content ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the bytes of the buffer as a binary encoded String.
The returned bytes should be encoded using the value of #encoding and ‘String#force_encoding`. Bytes are returned rather than characters because CharacterIO#read_line works on bytes for compatibility with the MRI implementation and working with characters would be inefficient in that case.
51 52 53 |
# File 'lib/io/like_helpers/character_io/basic_reader.rb', line 51 def content buffered_io.peek end |
#empty? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns ‘true` if the read buffer is empty and `false` otherwise.
This implementation does not have its own a buffer, so this method always returns ‘true`.
75 76 77 |
# File 'lib/io/like_helpers/character_io/basic_reader.rb', line 75 def empty? true end |
#encoding ⇒ Encoding
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the encoding to apply on byte strings from #content.
81 82 83 |
# File 'lib/io/like_helpers/character_io/basic_reader.rb', line 81 def encoding @encoding || Encoding.default_external end |
#refill(many = true) ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Refills the buffer from the stream.
96 97 98 99 100 |
# File 'lib/io/like_helpers/character_io/basic_reader.rb', line 96 def refill(many = true) bytes_added = buffered_io.refill raise IOError, 'no bytes read' if bytes_added < 1 nil end |
#unread(buffer, length: buffer.bytesize) ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Places bytes at the beginning of the read buffer.
114 115 116 |
# File 'lib/io/like_helpers/character_io/basic_reader.rb', line 114 def unread(buffer, length: buffer.bytesize) return buffered_io.unread(buffer, length: length) end |