Class: EM::File

Inherits:
Object
  • Object
show all
Defined in:
lib/em-files.rb

Overview

Sequenced file reader and writer.

Constant Summary collapse

RWSIZE =

Holds the default size of block operated during one tick.

65536

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, mode = "r", rwsize = self.class::RWSIZE) ⇒ File

Constructor. If IO object is given instead of filepath, uses it as native one and mode argument is ignored.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/em-files.rb', line 139

def initialize(filepath, mode = "r", rwsize = self.class::RWSIZE)
    @mode = mode
    @rw_len = rwsize
    
    rwsize = self::RWSIZE if rwsize.nil?
    
    # If filepath is directly IO, uses it
    if filepath.kind_of? IO
        @native = filepath
    else
        @native = ::File::open(filepath, mode)
    end
    
end

Instance Attribute Details

#modeString (readonly)

Holds mode of the object.



127
128
129
# File 'lib/em-files.rb', line 127

def mode
  @mode
end

#nativeIO

Holds file object.



111
112
113
# File 'lib/em-files.rb', line 111

def native
  @native
end

#rw_lenInteger

Indicates block size for operate with in one tick.



119
120
121
# File 'lib/em-files.rb', line 119

def rw_len
  @rw_len
end

Class Method Details

.open(filepath, mode = "r", rwsize = self::RWSIZE, &block) {|File| ... } ⇒ File

Opens the file.

In opposite to appropriate Ruby method, “block syntax” is only syntactic sugar, file isn’t closed after return from block because processing is asynchronous so it doesn’t know when is convenient to close the file.

Yields:

  • (File)

    file access object



42
43
44
45
46
47
48
49
50
51
# File 'lib/em-files.rb', line 42

def self.open(filepath, mode = "r", rwsize = self::RWSIZE, &block)   # 64 kilobytes
    rwsize = self::RWSIZE if rwsize.nil?
    
    file = self::new(filepath, mode, rwsize)
    if not block.nil?
        yield file
    end
    
    return file
end

.read(filepath, rwsize = self::RWSIZE, filter = nil, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/em-files.rb', line 67

def self.read(filepath, rwsize = self::RWSIZE, filter = nil, &block)
    rwsize = self::RWSIZE if rwsize.nil?
    self::open(filepath, "rb", rwsize) do |io|
        io.read(nil, filter) do |out|
            io.close()
            yield out
        end
    end
end

.write(filepath, data = "", rwsize = self::RWSIZE, filter = nil, &block) {|Integer| ... } ⇒ Object

Writes data to file and closes it. Writes them in binary mode. If IO object is given instead of filepath, uses it as native one and mode argument is ignored.

Yields:

  • (Integer)

    really written data length



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/em-files.rb', line 92

def self.write(filepath, data = "", rwsize = self::RWSIZE, filter = nil, &block)
    rwsize = self::RWSIZE if rwsize.nil?
    self::open(filepath, "wb", rwsize) do |io|
        io.write(data, filter) do |length|
            io.close()
            if not block.nil?
                yield length
            end
        end
    end
end

Instance Method Details

#closeObject

Closes the file.



302
303
304
# File 'lib/em-files.rb', line 302

def close
    @native.close
end

#read(length, &block) {|String| ... } ⇒ Object #read(&block) {|String| ... } ⇒ Object

Reads data from file.

It will reopen the file if EBADF: Bad file descriptor of File class IO object will occur.

Overloads:

  • #read(length, &block) {|String| ... } ⇒ Object

    Reads specified amount of data from file.

    Yields:

    • (String)

      read data

  • #read(&block) {|String| ... } ⇒ Object

    Reads whole content of file.

    Yields:

    • (String)

      read data



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/em-files.rb', line 174

def read(length = nil, filter = nil, &block)
    buffer = ""
    pos = 0
    
    # Arguments
    if length.kind_of? Proc
        filter = length
    end
    
    
    worker = Proc::new do
    
        # Sets length for read
        if not length.nil?
            rlen = length - buffer.length
            if rlen > @rw_len
                rlen = @rw_len
            end
        else
            rlen = @rw_len
        end
        
        # Reads
        begin
            chunk = @native.read(rlen)
            if not filter.nil?
                chunk = filter.call(chunk)
            end
            buffer << chunk
        rescue Errno::EBADF
            if @native.kind_of? ::File
                self.reopen!
                @native.seek(pos)
                redo
            else
                raise
            end
        end
        
        pos = @native.pos
        
        # Returns or continues work
        if @native.eof? or (buffer.length == length)
            if not block.nil?
                yield buffer              # returns result
            end
        else
            EM::next_tick { worker.call() }     # continues work
        end
        
    end
    
    worker.call()
end

#reopen!Object

Reopens the file with the original mode.



233
234
235
# File 'lib/em-files.rb', line 233

def reopen!
    @native = ::File.open(@native.path, @mode)
end

#write(data, filter = nil, &block) {|Integer| ... } ⇒ Object

Writes data to file. Supports writing both strings or copying from another IO object. Returns length of written data to callback if filename given or current position of output string if IO used.

It will reopen the file if EBADF: Bad file descriptor of File class IO object will occur on File object.

Yields:

  • (Integer)

    length of really written data



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/em-files.rb', line 254

def write(data, filter = nil, &block)
    pos = 0
    
    if data.kind_of? IO
        io = data
    else
        io = StringIO::new(data)
    end
    
    worker = Proc::new do
    
        # Writes
        begin
            chunk = io.read(@rw_len)
            if not filter.nil?
                chunk = filter.call(chunk)
            end
            @native.write(chunk)
        rescue Errno::EBADF
            if @native.kind_of? File
                self.reopen!
                @native.seek(pos)
                redo
            else
                raise
            end
        end
    
        pos = @native.pos
        
        # Returns or continues work
        if io.eof?
            if not block.nil?
                yield pos                 # returns result
            end
        else
            EM::next_tick { worker.call() }     # continues work
        end
        
    end
    
    worker.call()
end