Class: Viking::File

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ File

Returns a new instance of File.



94
95
96
97
98
99
# File 'lib/viking/file.rb', line 94

def initialize(file_name)
  @path     = file_name
  @reading  = false
  @writing = false
  @closed   = false
end

Class Method Details

.absolute_path(file_name, dir_string = Viking.client.get_working_directory) ⇒ Object



4
5
6
7
# File 'lib/viking/file.rb', line 4

def self.absolute_path(file_name, dir_string=Viking.client.get_working_directory)
  dir_string = dir_string.to_s.sub(/[^\/]*:\/*\//, '/')
  ::File.absolute_path(file_name, dir_string)
end

.basename(file_name, suffix = nil) ⇒ Object



9
10
11
12
# File 'lib/viking/file.rb', line 9

def self.basename(file_name, suffix = nil)
  base = file_name.split("/").last
  suffix.nil? ? base : base.sub(suffix, '')
end

.chmod(mode_int, *file_names) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/viking/file.rb', line 14

def self.chmod(mode_int, *file_names)
  permission = FsPermission.new(mode_int)
  file_names.each do |file_name|
    Viking.client.set_permission(Path.new(file_name), permission)
  end
  file_names.size
end

.chown(owner_name, group_name, *file_names) ⇒ Object



22
23
24
25
26
27
# File 'lib/viking/file.rb', line 22

def self.chown(owner_name, group_name, *file_names)
  file_names.each do |file_name|
    Viking.client.set_owner(Path.new(file_name), owner_name, group_name)
  end
  file_names.size
end

.delete(*file_names) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/viking/file.rb', line 29

def self.delete(*file_names)
  file_names.each do |file_name|
    path = Path.new(file_name)
    if file?(file_name)
      Viking.client.delete(Path.new(file_name), false)
    else
      raise IOError, "File::delete can only delete files. Attempted to delete directory #{file_name}."
    end
  end
  file_names.size
end

.directory?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.directory?(file_name)
  Viking.client.is_directory(Path.new(file_name))
end

.exist?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.exist?(file_name)
  exists?(file_name)
end

.exists?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/viking/file.rb', line 49

def self.exists?(file_name)
  Viking.client.exists(Path.new(file_name))
end

.file?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/viking/file.rb', line 53

def self.file?(file_name)
  Viking.client.is_file(Path.new(file_name))
end

.ftype(file_name) ⇒ Object



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

def self.ftype(file_name)
  directory?(file_name) ? 'directory' : 'file'
end

.move(old_name, new_name) ⇒ Object



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

def self.move(old_name, new_name)
  rename(old_name, new_name)
end

.open(file_name, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/viking/file.rb', line 65

def self.open(file_name, &block)
  file = Viking::File.new(file_name)
  if block
    value = yield file
    file.close unless file.closed?
    value
  else
    file
  end
end

.rename(old_name, new_name) ⇒ Object



76
77
78
# File 'lib/viking/file.rb', line 76

def self.rename(old_name, new_name)
  Viking.client.rename(Path.new(old_name), Path.new(new_name))
end

.size(file_name) ⇒ Object



80
81
82
# File 'lib/viking/file.rb', line 80

def self.size(file_name)
  Viking.client.get_file_status(Path.new(file_name)).get_len
end

.size?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/viking/file.rb', line 84

def self.size?(file_name)
  exists?(file_name) ? size(file_name) : nil
end

.split(file_name) ⇒ Object



88
89
90
91
92
# File 'lib/viking/file.rb', line 88

def self.split(file_name)
  name = basename(file_name)
  path = file_name.sub(/\/#{name}$/,'')
  [path, name]
end

Instance Method Details

#closeObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/viking/file.rb', line 109

def close
  unless closed?
    reader.close  if @reading
    writer.close if @writing

    @reading  = false
    @writing = false

    @closed = true
  end
end

#closed?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/viking/file.rb', line 121

def closed?
  @closed
end

#each_charObject



132
133
134
135
136
137
# File 'lib/viking/file.rb', line 132

def each_char
  while(char = getc) do
    yield char
  end
  close
end

#each_line(sep = "\n", limit = nil) ⇒ Object



125
126
127
128
129
130
# File 'lib/viking/file.rb', line 125

def each_line(sep="\n", limit=nil)
  while((line = gets(sep, limit)) != nil) do
    yield line
  end
  close
end

#eofObject



139
140
141
# File 'lib/viking/file.rb', line 139

def eof
  eof?
end

#eof?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/viking/file.rb', line 143

def eof?
  current_byte.nil?
end

#getcObject



147
148
149
150
151
# File 'lib/viking/file.rb', line 147

def getc
  byte = current_byte
  next_byte
  byte
end

#gets(sep = nil, limit = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/viking/file.rb', line 153

def gets(sep=nil, limit=nil)
  sep_bytes = sep.bytes.to_a if sep
  bytes     = []
  while(b = getc) do
    bytes << b
    break if sep   && bytes.last(sep_bytes.size) == sep_bytes
    break if limit && bytes.size == limit
  end
  bytes.empty? ? nil : bytes.pack("C*")
end

#pathObject



101
102
103
# File 'lib/viking/file.rb', line 101

def path
  @path
end

#putc(obj) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/viking/file.rb', line 164

def putc(obj)
  if obj.is_a? Numeric
    writer.java_send :write, [Java::int], obj
  else
    byte = obj.to_s.bytes.first
    writer.write(byte)
  end
end

#puts(*objs) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/viking/file.rb', line 173

def puts(*objs)
  separator = "\n"
  string    = objs.reduce("") do |acc, obj|
    if obj.end_with?(separator)
      acc << obj
    else
      acc << obj
      acc << separator
    end
  end
  write(string)
end

#read(limit = nil) ⇒ Object



186
187
188
# File 'lib/viking/file.rb', line 186

def read(limit=nil)
  gets(nil, limit)
end

#sizeObject



105
106
107
# File 'lib/viking/file.rb', line 105

def size
  Viking.client.get_file_status(Path.new(path)).get_len
end

#write(string) ⇒ Object



190
191
192
# File 'lib/viking/file.rb', line 190

def write(string)
  writer.write(string.to_java_bytes)
end