Implements the read operation, which reads data from an open file handle. This also implements the common case of reading to the end of the file. In that case, the callback is guaranteed to receive the contents of the entire file in one chunk.
DEFAULT_CHUNK_SIZE | = | 64 * 1024 |
The default maximum amount of data to read at once when reading an entire file. |
Invoked when a data packet is received from the server. If the original request was for an entire file, this will send another read request, offset to the end of the data that has been read so far. Otherwise, the callback will be invoked directly.
[ show source ]
# File lib/net/sftp/operations/read.rb, line 51 51: def do_data( data ) 52: @log.debug "[#{@id}] got #{data.length} bytes" if @log.debug? 53: 54: @data << data 55: @progress_callback[@data] if @progress_callback 56: 57: if @length < 0 || @data.length < @length 58: if @length < 0 59: length = @chunk_size 60: else 61: length = @length - @data.length 62: length = length > @chunk_size ? @chunk_size : length 63: end 64: 65: @log.debug "[#{@id}] requesting #{length} more bytes" if @log.debug? 66: @driver.read @id, @handle, @offset + @data.length, length 67: @session.register( @id, self ) 68: else 69: @callback[ OK, @data ] 70: end 71: end
Invoked when a status code is received from the server. If the code is FX_EOF, then no data could be read because the end of the file was reached. In this case, the callback is invoked with the data that has been read so far. Other status codes are handled by the superclass.
[ show source ]
# File lib/net/sftp/operations/read.rb, line 77 77: def do_status( code, message, language ) 78: if code == FX_EOF 79: @log.debug "[#{@id}] got EOF" if @log.debug? 80: @callback[ OK, @data ] 81: else 82: super 83: end 84: end
Perform the operation. If length is less than 0 (the default), then the entire file (from the given offset) will be read and returned in "one fell swoop". Otherwise, the given length of data will be requested.
[ show source ]
# File lib/net/sftp/operations/read.rb, line 34 34: def perform( handle, options={} ) 35: @length = options[:length] || -1 36: @handle = handle 37: @offset = options[:offset] || 0 38: @chunk_size = options[:chunk_size] || DEFAULT_CHUNK_SIZE 39: @progress_callback = options[:progress_callback] 40: @data = "" 41: 42: real_length = ( @length >= 0 && @length < @chunk_size ? 43: @length : @chunk_size ) 44: @driver.read( nil, @handle, @offset, real_length ) 45: end