Class: IO::LikeHelpers::CharacterIO::BasicReader Private

Inherits:
Object
  • Object
show all
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

ConverterReader

Instance Method Summary collapse

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

#clearnil

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.

Raises:

  • (IOError)

    if the stream is not readable



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

#contentString

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.

Raises:

  • (IOError)

    if the stream is not readable



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

#encodingEncoding

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.

Raises:

  • (EOFError)

    when reading at the end of the stream

  • (IOError)

    if the stream is not readable

  • (IOError)

    if the buffer is already full



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.

Raises:

  • (IOError)

    if the remaining space in the internal buffer is insufficient to contain the given data

  • (IOError)

    if the stream is not readable



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