lib/madeleine/zmarshal.rb in madeleine-0.7.3 vs lib/madeleine/zmarshal.rb in madeleine-0.8.0.pre

- old
+ new

@@ -1,8 +1,8 @@ # # Author:: Anders Bengtsson <ndrsbngtssn@yahoo.se> -# Copyright:: Copyright (c) 2004 +# Copyright:: Copyright (c) 2004-2012 # require 'zlib' module Madeleine @@ -12,38 +12,32 @@ # Compresses the snapshots created by another marshaller. Uses either # Marshal (the default) or another supplied marshaller. # # Uses <tt>zlib</tt> to do on-the-fly compression/decompression. # - # ZMarshal works with Ruby's own Marshal and YAML, but not with SOAP - # marshalling. + # ZMarshal works with Ruby's own Marshal and YAML # # Usage: # # require 'madeleine' # require 'madeleine/zmarshal' # # marshaller = Madeleine::ZMarshal.new(YAML) # madeleine = SnapshotMadeleine.new("my_example_storage", marshaller) { - # SomeExampleApplication.new() + # SomeExampleApplication.new # } # class ZMarshal def initialize(marshaller=Marshal) @marshaller = marshaller end def load(stream) - zstream = Zlib::GzipReader.new(stream) + zstream = WorkaroundGzipReader.new(stream) begin - # Buffer into a string first, since GzipReader can't handle - # Marshal's 0-sized reads and SOAP can't handle streams at all. - # In a bright future we can revert to reading directly from the - # stream again. - buffer = zstream.read - return @marshaller.load(buffer) + return @marshaller.load(zstream) ensure zstream.finish end end @@ -53,8 +47,21 @@ @marshaller.dump(system, zstream) ensure zstream.finish end nil + end + + private + + class WorkaroundGzipReader < Zlib::GzipReader + # The 'psych' YAML parser, default since Ruby 1.9.3, + # assumes that its input IO has an external_encoding() + # method. + unless defined? external_encoding + def external_encoding + nil + end + end end end end