Class: Fluent::Plugin::TailInput::TailWatcher::FIFO

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/in_tail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_encoding, encoding) ⇒ FIFO

Returns a new instance of FIFO.



578
579
580
581
582
583
# File 'lib/fluent/plugin/in_tail.rb', line 578

def initialize(from_encoding, encoding)
  @from_encoding = from_encoding
  @encoding = encoding
  @buffer = ''.force_encoding(from_encoding)
  @eol = "\n".encode(from_encoding).freeze
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



585
586
587
# File 'lib/fluent/plugin/in_tail.rb', line 585

def buffer
  @buffer
end

#encodingObject (readonly)

Returns the value of attribute encoding.



585
586
587
# File 'lib/fluent/plugin/in_tail.rb', line 585

def encoding
  @encoding
end

#from_encodingObject (readonly)

Returns the value of attribute from_encoding.



585
586
587
# File 'lib/fluent/plugin/in_tail.rb', line 585

def from_encoding
  @from_encoding
end

Instance Method Details

#<<(chunk) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/fluent/plugin/in_tail.rb', line 587

def <<(chunk)
  # Although "chunk" is most likely transient besides String#force_encoding itself
  # won't affect the actual content of it, it is also probable that "chunk" is
  # a reused buffer and changing its encoding causes some problems on the caller side.
  #
  # Actually, the caller here is specific and "chunk" comes from IO#partial with
  # the second argument, which the function always returns as a return value.
  #
  # Feeding a string that has its encoding attribute set to any double-byte or
  # quad-byte encoding to IO#readpartial as the second arguments results in an
  # assertion failure on Ruby < 2.4.0 for unknown reasons.
  orig_encoding = chunk.encoding
  chunk.force_encoding(from_encoding)
  @buffer << chunk
  # Thus the encoding needs to be reverted back here
  chunk.force_encoding(orig_encoding)
end

#bytesizeObject



618
619
620
# File 'lib/fluent/plugin/in_tail.rb', line 618

def bytesize
  @buffer.bytesize
end

#convert(s) ⇒ Object



605
606
607
608
609
610
611
# File 'lib/fluent/plugin/in_tail.rb', line 605

def convert(s)
  if @from_encoding == @encoding
    s
  else
    s.encode(@encoding, @from_encoding)
  end
end

#next_lineObject



613
614
615
616
# File 'lib/fluent/plugin/in_tail.rb', line 613

def next_line
  idx = @buffer.index(@eol)
  convert(@buffer.slice!(0, idx + 1)) unless idx.nil?
end