Class: Hdfs::File

Inherits:
Delegator
  • Object
show all
Defined in:
lib/hdfs_jruby/file.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode = "r") ⇒ File

Returns a new instance of File.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hdfs_jruby/file.rb', line 10

def initialize(path, mode = "r")
  @conf = Hdfs::Configuration.new()
  @fs = Hdfs::FileSystem.get(@conf)

  @mode = mode
  if mode == "w"
    @stream = @fs.create(Hdfs::Path.new(path), false)
  elsif mode == "r"
    @stream = @fs.open(Hdfs::Path.new(path))
  elsif mode == "a"
    p = Hdfs::Path.new(path)
    if !@fs.exists(p)
      @stream = @fs.create(Hdfs::Path.new(path), false)
    else
      if ! @fs.isFile(p)
        raise "path: #{path} is not file"
      end
      @stream = @fs.append(Hdfs::Path.new(path))
    end
  end
end

Class Method Details

.open(path, mode = "r") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hdfs_jruby/file.rb', line 32

def self.open(path, mode = "r")
  if block_given?
    io = File.new(path, mode).to_io
    begin
      yield(io)
    ensure
      io.close
    end
  else
    return File.new(path, mode).to_io
  end
end

Instance Method Details

#__getobj__Object



70
71
72
# File 'lib/hdfs_jruby/file.rb', line 70

def __getobj__
  @stream
end

#__setobj__(obj) ⇒ Object



74
75
76
# File 'lib/hdfs_jruby/file.rb', line 74

def __setobj__(obj)
  @stream = obj
end

#closeObject



65
66
67
68
# File 'lib/hdfs_jruby/file.rb', line 65

def close
  @stream.close
  @fs.close
end

#seek(offset, whence = IO::SEEK_SET) ⇒ Object



60
61
62
63
# File 'lib/hdfs_jruby/file.rb', line 60

def seek(offset, whence = IO::SEEK_SET)
  @stream.seek(offset)
  0
end

#sysread(length, outbuf = "") ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/hdfs_jruby/file.rb', line 50

def sysread(length, outbuf = "")
  buf = Java::byte[length].new

  n = @stream.read(buf)
  if n < 0
    return nil
  end
  outbuf << java.lang.String.new(buf, 0, n).to_s
end

#syswrite(str) ⇒ Object



45
46
47
48
# File 'lib/hdfs_jruby/file.rb', line 45

def syswrite(str)
  n = @stream.write(str.to_java_bytes)
  return n.to_i
end