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.

Parameters:

  • buffered_io (LikeHelpers::BufferedIO)

    a readable stream that always blocks

  • encoding (Encoding, nil) (defaults to: nil)

    the encoding to apply to the content provided by this stream



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.

Returns:

  • (nil)


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.

Parameters:

  • length (Integer, nil)

    the number of bytes to consume

Returns:

  • (nil)

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.

Returns:

  • (String)

    the bytes of the buffer

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`.

Returns:

  • (Boolean)


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.

Returns:

  • (Encoding)

    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.

Parameters:

  • many (Boolean) (defaults to: true)

    ignored in this implementation; see ConverterReader#refill

Returns:

  • (nil)

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.

Parameters:

  • buffer (String)

    the bytes to insert into the read buffer

  • length (Integer) (defaults to: buffer.bytesize)

    the number of bytes from the beginning of ‘buffer` to insert into the read buffer

Returns:

  • (nil)

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