Author: martinkl
Date: Thu Jul  3 15:10:26 2014
New Revision: 1607667

URL: http://svn.apache.org/r1607667
Log:
AVRO-1499. Ruby: Fix corruption of data files under Ruby 2.0+. Contributed by 
Willem van Bergen.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/ruby/lib/avro/data_file.rb

Modified: avro/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1607667&r1=1607666&r2=1607667&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Thu Jul  3 15:10:26 2014
@@ -107,6 +107,9 @@ Trunk (not yet released)
 
     AVRO-1525. Java: ReflectData cannot resolve union with fixed. (tomwhite)
 
+    AVRO-1499. Ruby: Fix encoding issue that caused corrupted data files
+    to be written under Ruby 2.0+. (Willem van Bergen and martinkl)
+
 Avro 1.7.6 (15 January 2014)
 
   NEW FEATURES

Modified: avro/trunk/lang/ruby/lib/avro/data_file.rb
URL: 
http://svn.apache.org/viewvc/avro/trunk/lang/ruby/lib/avro/data_file.rb?rev=1607667&r1=1607666&r2=1607667&view=diff
==============================================================================
--- avro/trunk/lang/ruby/lib/avro/data_file.rb (original)
+++ avro/trunk/lang/ruby/lib/avro/data_file.rb Thu Jul  3 15:10:26 2014
@@ -20,7 +20,8 @@ module Avro
   module DataFile
     VERSION = 1
     MAGIC = "Obj" + [VERSION].pack('c')
-    MAGIC_SIZE = MAGIC.size
+    MAGIC.force_encoding('BINARY') if MAGIC.respond_to?(:force_encoding)
+    MAGIC_SIZE = MAGIC.respond_to?(:bytesize) ? MAGIC.bytesize : MAGIC.size
     SYNC_SIZE = 16
     SYNC_INTERVAL = 4000 * SYNC_SIZE
     META_SCHEMA = Schema.parse('{"type": "map", "values": "bytes"}')
@@ -98,6 +99,7 @@ module Avro
         @encoder = IO::BinaryEncoder.new(@writer)
         @datum_writer = datum_writer
         @buffer_writer = StringIO.new('', 'w')
+        @buffer_writer.set_encoding('BINARY') if 
@buffer_writer.respond_to?(:set_encoding)
         @buffer_encoder = IO::BinaryEncoder.new(@buffer_writer)
         @block_count = 0
 
@@ -181,7 +183,7 @@ module Avro
           # write number of items in block and block size in bytes
           encoder.write_long(block_count)
           to_write = codec.compress(buffer_writer.string)
-          encoder.write_long(to_write.size)
+          encoder.write_long(to_write.respond_to?(:bytesize) ? 
to_write.bytesize : to_write.size)
 
           # write block contents
           writer.write(to_write)


Reply via email to