Skip to content

Commit

Permalink
archive-zip patch for Ruby1.9.2 (part2).
Browse files Browse the repository at this point in the history
  • Loading branch information
ongaeshi committed Aug 3, 2011
1 parent 86f7e45 commit ec8475b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
37 changes: 2 additions & 35 deletions lib/milkode/common/archive-zip.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
# -*- coding: utf-8 -*-
#
# @file
# @brief archive-zipがRuby1.9.2に対応するまでのパッチ、readbytesが無ければ実装する
# @brief archive-zipがRuby1.9.2に対応するまでのパッチ
# @author ongaeshi
# @date 2011/08/04

begin
require 'readbytes'
rescue LoadError
# for Ruby 1.9.2
class TruncatedDataError<IOError
def initialize(mesg, data) # :nodoc:
@data = data
super(mesg)
end

# The read portion of an IO#readbytes attempt.
attr_reader :data
end

class IO
# Reads exactly +n+ bytes.
#
# If the data read is nil an EOFError is raised.
#
# If the data read is too short a TruncatedDataError is raised and the read
# data is obtainable via its #data method.
def readbytes(n)
str = read(n)
if str == nil
raise EOFError, "End of file reached"
end
if str.size < n
raise TruncatedDataError.new("data truncated", str)
end
str
end
end
end

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../../../vendor')
require 'archive/zip'
53 changes: 53 additions & 0 deletions vendor/archive/support/io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
#
# @file
# @brief archive-zip/lib/archive/support/io.rb patch, removed in the future.
# @author ongaeshi
# @date 2011/08/04

begin
require 'readbytes'
rescue LoadError
# for Ruby 1.9.2
class TruncatedDataError<IOError
def initialize(mesg, data) # :nodoc:
@data = data
super(mesg)
end

# The read portion of an IO#readbytes attempt.
attr_reader :data
end

class IO
# Reads exactly +n+ bytes.
#
# If the data read is nil an EOFError is raised.
#
# If the data read is too short a TruncatedDataError is raised and the read
# data is obtainable via its #data method.
def readbytes(n)
str = read(n)
if str == nil
raise EOFError, "End of file reached"
end
if str.size < n
raise TruncatedDataError.new("data truncated", str)
end
str
end
end
end

class IO
# Returns +true+ if the seek method of this IO instance would succeed, +false+
# otherwise.
def seekable?
begin
pos
true
rescue SystemCallError
false
end
end
end

0 comments on commit ec8475b

Please sign in to comment.