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
31
# 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))
    @buf = java.nio.ByteBuffer.allocate(65536)
  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



33
34
35
36
37
38
39
# File 'lib/hdfs_jruby/file.rb', line 33

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

Instance Method Details

#__getobj__Object



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

def __getobj__
  @stream
end

#__setobj__(obj) ⇒ Object



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

def __setobj__(obj)
  @stream = obj
end

#closeObject



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

def close
  @stream.close
  @fs.close
end

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



56
57
58
59
# File 'lib/hdfs_jruby/file.rb', line 56

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

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



46
47
48
49
50
51
52
53
54
# File 'lib/hdfs_jruby/file.rb', line 46

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



41
42
43
44
# File 'lib/hdfs_jruby/file.rb', line 41

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