Sha256: baac3464122ac8f355b58a220ba3f4f7c7115134d423bb77b55e5a8e755bc50e

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module NBTUtils
  class File
    def initialize(path = nil)
      @path = path
    end

    def read(path = @path)
      # Zlib does not provide a way to test a file for compressed, and I'm
      # not going to tool around with magic numbers, so I guess we use
      # exceptions for flow control.
      begin
        Zlib::GzipReader.open(path) do |f|
          @content = StringIO.new(f.read)
        end
        @compressed = true
      rescue Zlib::GzipFile::Error
        ::File.open(path) do |f|
          @content = StringIO.new(f.read)
        end
        @compressed = false
      end


      last_byte = @content.read(1).bytes.first
      klass = NBTUtils::Tag.tag_type_to_class(last_byte)

      @tag = klass.new(@content, true)
    end

    def write(path = @path, tag = @tag, compressed = @compressed)
      if compressed
        Zlib::GzipWriter.open(path) do |gz|
          gz.write tag.to_nbt_string
        end
      else
        ::File.open(path, 'w') do |f|
          f.write tag.to_nbt_string
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nbt_utils-0.0.4 lib/nbt_utils/file.rb