Class: Embulk::FileInput

Inherits:
Object
  • Object
show all
Defined in:
embulk-core/src/main/ruby/embulk/file_input.rb

Instance Method Summary collapse

Constructor Details

#initialize(java_file_input) ⇒ FileInput

Returns a new instance of FileInput



6
7
8
9
# File 'embulk-core/src/main/ruby/embulk/file_input.rb', line 6

def initialize(java_file_input)
  @java_file_input = java_file_input
  @buffer = nil
end

Instance Method Details

#closeObject



79
80
81
# File 'embulk-core/src/main/ruby/embulk/file_input.rb', line 79

def close
  @java_file_input.close
end

#each(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'embulk-core/src/main/ruby/embulk/file_input.rb', line 19

def each(&block)
  if @buffer
    yield @buffer
    @buffer = nil
  end

  while java_buffer = @java_file_input.poll
    buffer = Buffer.from_java(java_buffer)
    java_buffer.release
    yield buffer
  end
end

#next_fileObject



11
12
13
14
15
16
17
# File 'embulk-core/src/main/ruby/embulk/file_input.rb', line 11

def next_file
  if @java_file_input.nextFile
    return self
  else
    return nil
  end
end

#read(count = nil, dest = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'embulk-core/src/main/ruby/embulk/file_input.rb', line 32

def read(count=nil, dest=nil)
  if count == nil
    @buffer ||= Buffer.new
    while java_buffer = @java_file_input.poll
      @buffer << Buffer.from_java(java_buffer)
      java_buffer.release
    end

    return nil if @buffer.empty? && count != 0

    if dest
      dest.replace(@buffer)
    else
      dest = @buffer
    end
    @buffer = nil

  else
    @buffer ||= Buffer.new
    until @buffer.size >= count
      java_buffer = @java_file_input.poll
      break unless java_buffer
      @buffer << Buffer.from_java(java_buffer)
      java_buffer.release
    end

    return nil if @buffer.empty? && count != 0

    if @buffer.size <= count
      if dest
        dest.replace(@buffer)
      else
        dest = @buffer
      end
      @buffer = nil
    else
      data = @buffer.slice!(0, count)
      if dest
        dest.replace(data)
      else
        dest = data
      end
    end
  end
  return dest
end

#to_javaObject



83
84
85
# File 'embulk-core/src/main/ruby/embulk/file_input.rb', line 83

def to_java
  @java_file_input
end