Class: Ftpd::Stream

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

Constant Summary collapse

CHUNK_SIZE =

100kb

1024 * 100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, data_type) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • io (IO)

    The stream to read from or write to

  • data_type (String)

    The FTP data type of the stream


13
14
15
16
# File 'lib/ftpd/stream.rb', line 13

def initialize(io, data_type)
  @io, @data_type = io, data_type
  @byte_count = 0
end

Instance Attribute Details

#byte_countObject (readonly)

Returns the value of attribute byte_count


8
9
10
# File 'lib/ftpd/stream.rb', line 8

def byte_count
  @byte_count
end

#data_typeObject (readonly)

Returns the value of attribute data_type


7
8
9
# File 'lib/ftpd/stream.rb', line 7

def data_type
  @data_type
end

Instance Method Details

#readString, NilClass

Read and convert a chunk of up to CHUNK_SIZE from the stream

Returns:

  • (String)

    if any bytes remain to read from the stream

  • (NilClass)

    if no bytes remain


22
23
24
25
26
27
28
# File 'lib/ftpd/stream.rb', line 22

def read
  chunk = converted_chunk(@io)
  return unless chunk
  chunk = nvt_ascii_to_unix(chunk) if data_type == 'A'
  record_bytes(chunk)
  chunk
end

#write(io) ⇒ Object

Convert and write a chunk of up to CHUNK_SIZE to the stream from the provided IO object

Parameters:

  • io (IO)

    The data to be written to the stream


35
36
37
38
39
40
41
42
# File 'lib/ftpd/stream.rb', line 35

def write(io)
  while chunk = converted_chunk(io)
    chunk = unix_to_nvt_ascii(chunk) if data_type == 'A'
    result = @io.write(chunk)
    record_bytes(chunk)
    result
  end
end