@@ -26,6 +26,10 @@ class File
2626
2727 IMAGE_EXT = %w[ .bmp .gif .jpg .jpeg .png .ico ] . freeze
2828
29+ # Constants for file operations
30+ BLOCK_SIZE = 512
31+ TAIL_CHUNK_SIZE = 2 **16 # 64k chunks
32+
2933 # :startdoc:
3034
3135 # Returns whether or not the file is an image. Only JPEG, PNG, BMP,
@@ -232,13 +236,11 @@ def self.head(filename, num_lines = 10)
232236 # to be configured in the future as an optional 3rd argument.
233237 #
234238 def self . tail ( filename , num_lines = 10 , &block )
235- tail_size = 2 **16 # 64k chunks
236-
237239 # MS Windows gets unhappy if you try to seek backwards past the
238240 # end of the file, so we have some extra checks here and later.
239241 file_size = File . size ( filename )
240- read_bytes = file_size % tail_size
241- read_bytes = tail_size if read_bytes == 0
242+ read_bytes = file_size % TAIL_CHUNK_SIZE
243+ read_bytes = TAIL_CHUNK_SIZE if read_bytes == 0
242244
243245 line_sep = File ::ALT_SEPARATOR ? "\r \n " : "\n "
244246
@@ -252,7 +254,7 @@ def self.tail(filename, num_lines = 10, &block)
252254 while buf . scan ( line_sep ) . size <= num_lines and position >= 0
253255 fh . seek ( position , IO ::SEEK_SET )
254256 buf = fh . read ( read_bytes ) + buf
255- read_bytes = tail_size
257+ read_bytes = TAIL_CHUNK_SIZE
256258 position -= read_bytes
257259 end
258260 end
@@ -288,24 +290,23 @@ def self.nl_convert(old_file, new_file = old_file, platform = 'local')
288290 if old_file == new_file
289291 require 'tempfile'
290292 temp_name = Time . new . strftime ( '%Y%m%d%H%M%S' )
291- nf = Tempfile . new ( "ruby_temp_#{ temp_name } " )
292- else
293- nf = File . new ( new_file , 'w' )
294- end
295-
296- begin
297- nf . open if old_file == new_file
298- File . foreach ( old_file ) do |line |
299- line . chomp!
300- nf . print ( "#{ line } #{ format } " )
301- end
302- ensure
303- nf . close if nf && !nf . closed?
304- if old_file == new_file
293+ Tempfile . open ( "ruby_temp_#{ temp_name } " ) do |nf |
294+ File . foreach ( old_file ) do |line |
295+ line . chomp!
296+ nf . print ( "#{ line } #{ format } " )
297+ end
298+ nf . close
305299 require 'fileutils'
306300 File . delete ( old_file )
307301 FileUtils . mv ( nf . path , old_file )
308302 end
303+ else
304+ File . open ( new_file , 'w' ) do |nf |
305+ File . foreach ( old_file ) do |line |
306+ line . chomp!
307+ nf . print ( "#{ line } #{ format } " )
308+ end
309+ end
309310 end
310311
311312 self
@@ -358,13 +359,13 @@ def self.wc(filename, option = 'all')
358359 n
359360 else
360361 bytes , chars , lines , words = 0 , 0 , 0 , 0
361- File . foreach ( filename ) do |line |
362- lines += 1
363- words += line . split . length
364- chars += line . chars . length
365- end
366362 File . open ( filename ) do |f |
367- bytes += 1 while f . getc
363+ while ( line = f . gets )
364+ lines += 1
365+ words += line . split . length
366+ chars += line . chars . length
367+ bytes += line . bytesize
368+ end
368369 end
369370 [ bytes , chars , words , lines ]
370371 end
@@ -382,7 +383,7 @@ def self.wc(filename, option = 'all')
382383 #
383384 def self . sparse? ( file )
384385 stats = File . stat ( file )
385- stats . size > stats . blocks * 512
386+ stats . size > stats . blocks * BLOCK_SIZE
386387 end
387388 end
388389
@@ -429,27 +430,27 @@ def self.bmp?(file)
429430 # Is the file a jpeg file?
430431 #
431432 def self . jpg? ( file )
432- File . read ( file , 10 , nil , :encoding => 'binary' ) == String . new ( " \377 \330 \377 \340 \000 \020 JFIF" ) . force_encoding ( Encoding ::BINARY )
433+ File . read ( file , 10 , nil , :encoding => 'binary' ) == " \xFF \xD8 \xFF \xE0 \x00 \x10 JFIF" . force_encoding ( Encoding ::BINARY )
433434 end
434435
435436 # Is the file a png file?
436437 #
437438 def self . png? ( file )
438- File . read ( file , 4 , nil , :encoding => 'binary' ) == String . new ( " \211 PNG" ) . force_encoding ( Encoding ::BINARY )
439+ File . read ( file , 4 , nil , :encoding => 'binary' ) == " \x89 PNG" . force_encoding ( Encoding ::BINARY )
439440 end
440441
441442 # Is the file a gif?
442443 #
443444 def self . gif? ( file )
444- %w[ GIF89a GIF97a ] . include? ( File . read ( file , 6 ) )
445+ %w[ GIF89a GIF97a ] . include? ( File . read ( file , 6 , nil , :encoding => 'binary' ) )
445446 end
446447
447448 # Is the file a tiff?
448449 #
449450 def self . tiff? ( file )
450451 return false if File . size ( file ) < 12
451452
452- bytes = File . read ( file , 4 )
453+ bytes = File . read ( file , 4 , nil , :encoding => 'binary' )
453454
454455 # II is Intel, MM is Motorola
455456 return false if bytes [ 0 ..1 ] != 'II' && bytes [ 0 ..1 ] != 'MM'
@@ -464,6 +465,6 @@ def self.tiff?(file)
464465 # Is the file an ico file?
465466 #
466467 def self . ico? ( file )
467- [ "\000 \000 \001 \000 " , "\000 \000 \002 \000 " ] . include? ( File . read ( file , 4 , nil , :encoding => 'binary' ) )
468+ [ "\x00 \x00 \x01 \x00 " . force_encoding ( Encoding :: BINARY ) , "\x00 \x00 \x02 \x00 " . force_encoding ( Encoding :: BINARY ) ] . include? ( File . read ( file , 4 , nil , :encoding => 'binary' ) )
468469 end
469470end
0 commit comments