如何从Ruby 1.9中的文件中读取字节?

时间:2011-11-26 19:53:56

标签: ruby

在Ruby 1.9中,文件和IO库已更改 - 它们现在似乎总是将数据解释为编码字符串(例如UTF-8),并且返回的值似乎总是字符串。

我需要逐字节读取Ruby中的文件,而不需要对数据进行任何修改或解释。 我想读取字节序列,而不是编码字符串。

有关如何做到这一点的任何提示?

2 个答案:

答案 0 :(得分:3)

我在写的一个宝石中遇到过类似的问题。这是相关的代码: (您不需要require语句)

# ==============================================================================
# Loading Libraries and Stuff needed for Ruby 1.9 vs 1.8 Compatibility
# ==============================================================================
# the idea here is to define a couple of go-between methods for different classes
# which are differently defined depending on which Ruby version it is -- thereby
# abstracting from the particular Ruby version's API of those classes

if RUBY_VERSION >= "1.9.0"
  require "digest/md5"
  require "digest/sha1"
  include Digest

  require 'fileutils'        # replaces ftools
  include FileUtils::Verbose

  class File
    def read_bytes(n)  # returns a string containing bytes
          self.bytes.take(n)
    end
    def write_bytes(bytes)
      self.syswrite(bytes)
    end
    def get_byte
      self.getbyte     # returns a number 0..255
    end
  end

  ZEROBYTE = "\x00".force_encoding(Encoding::BINARY) unless defined? ZEROBYTE

else # older Ruby versions:
  require 'rubygems'

  require "md5"
  require "sha1"

  require 'ftools'
  def move(a,b)
    File.move(a,b)
  end

  class String
    def getbyte(x)   # when accessing a string and selecting x-th byte to do calculations , as defined in Ruby 1.9
      self[x]  # returns an integer
    end
  end

  class File
    def read_bytes(n)
      self.read(n)   # should use sysread here as well?
    end
    def write_bytes(bytes)
      self.write(bytes)   # should use syswrite here as well?
    end
    def get_byte     # in older Ruby versions <1.9 getc returned a byte, e.g. a number 0..255
      self.getc   # returns a number 0..255
    end
  end

  ZEROBYTE = "\0" unless defined? ZEROBYTE
end

答案 1 :(得分:1)

IO具有 binmode 方法(调用它正在设置它),这会禁用换行和编码转换。 File类继承了这个方法。