Class: Uplink::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/uplink/download.rb

Instance Method Summary collapse

Constructor Details

#initialize(download_result) ⇒ Download

Returns a new instance of Download.



5
6
7
# File 'lib/uplink/download.rb', line 5

def initialize(download_result)
  @download = download_result[:download]
end

Instance Method Details

#closeObject



35
36
37
38
39
40
# File 'lib/uplink/download.rb', line 35

def close
  error = UplinkLib.uplink_close_download(@download)
  ErrorUtil.handle_error(error)
ensure
  UplinkLib.uplink_free_error(error) if error
end

#infoObject



26
27
28
29
30
31
32
33
# File 'lib/uplink/download.rb', line 26

def info
  result = UplinkLib.uplink_download_info(@download)
  ErrorUtil.handle_result_error(result)

  Object.new(result)
ensure
  UplinkLib.uplink_free_object_result(result) if result
end

#read(bytes, length) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/uplink/download.rb', line 9

def read(bytes, length)
  raise ArgumentError, 'bytes argument is nil' if bytes.nil?

  mem_bytes = FFI::MemoryPointer.new(:uint8, length)
  result = UplinkLib.uplink_download_read(@download, mem_bytes, length)

  error_code = ErrorUtil.handle_result_error(result)
  is_eof = (error_code == EOF)

  bytes_read = result[:bytes_read]
  bytes.concat(mem_bytes.read_array_of_uint8(bytes_read)) if bytes_read.positive?

  [bytes_read, is_eof]
ensure
  UplinkLib.uplink_free_read_result(result) if result
end