Class: B2::UploadChunker

Inherits:
Object
  • Object
show all
Defined in:
lib/b2/upload_chunker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ UploadChunker

Returns a new instance of UploadChunker.



5
6
7
8
9
10
11
12
13
14
# File 'lib/b2/upload_chunker.rb', line 5

def initialize(data)
  @data = data
  @sha_appended = false
  @digestor = Digest::SHA1.new
  @size = if data.is_a?(String)
    data.bytesize + 40
  else
    data.size + 40
  end
end

Instance Attribute Details

#sha1Object (readonly)

Returns the value of attribute sha1.



3
4
5
# File 'lib/b2/upload_chunker.rb', line 3

def sha1
  @sha1
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/b2/upload_chunker.rb', line 3

def size
  @size
end

Instance Method Details

#read(length = nil, outbuf = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/b2/upload_chunker.rb', line 16

def read(length=nil, outbuf=nil)
  return_value = @data.read(length, outbuf)
  
  if outbuf.nil?
    if return_value.nil? && !@sha_appended
      @sha_appended = true
      @digestor.hexdigest
    else
      @digestor << return_value
      return_value
    end
  else
    if outbuf.empty? && !@sha_appended
      @sha_appended = true
      outbuf.replace(@digestor.hexdigest)
    else
      @digestor << outbuf
    end
    outbuf
  end
end