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.

Parameters:

  • path (String)
  • mode (String) (defaults to: "r")

    ‘r’ or ‘w’ or ‘a’



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

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

Parameters:

  • path (String)
  • mode (String) (defaults to: "r")

    ‘r’ or ‘w’ or ‘a’



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hdfs_jruby/file.rb', line 37

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



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

def __getobj__
  @stream
end

#__setobj__(obj) ⇒ Object



79
80
81
# File 'lib/hdfs_jruby/file.rb', line 79

def __setobj__(obj)
  @stream = obj
end

#closeObject



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

def close
  @stream.close
  @fs.close
end

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



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

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

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



55
56
57
58
59
60
61
62
63
# File 'lib/hdfs_jruby/file.rb', line 55

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



50
51
52
53
# File 'lib/hdfs_jruby/file.rb', line 50

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