<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,3 +1,13 @@
+Fri Oct 16 03:15:52 2009  NARUSE, Yui  &lt;naruse@ruby-lang.org&gt;
+
+	* lib/csv.rb (CSV#read_to_char): set encoding and verify data
+	  which read from io before encode it to @encoding.
+
+	* lib/csv.rb (CSV#raw_encoding): add to get @io's encoding.
+
+	* lib/csv.rb (CSV#read_io): add to read string and set @io's
+	  encoding.
+
 Thu Oct 15 18:26:12 2009  Nobuyoshi Nakada  &lt;nobu@ruby-lang.org&gt;
 
 	* parse.y (rb_intern3): check symbol table overflow before generate</diff>
      <filename>ChangeLog</filename>
    </modified>
    <modified>
      <diff>@@ -1550,12 +1550,7 @@ class CSV
     # create the IO object we will read from
     @io       =   if data.is_a? String then StringIO.new(data) else data end
     # honor the IO encoding if we can, otherwise default to ASCII-8BIT
-    @encoding =   if @io.respond_to? :internal_encoding
-                    @io.internal_encoding || @io.external_encoding
-                  elsif @io.is_a? StringIO
-                    @io.string.encoding
-                  end
-    @encoding ||= Encoding.default_internal || Encoding.default_external
+    @encoding = raw_encoding || Encoding.default_internal || Encoding.default_external
     #
     # prepare for building safe regular expressions in the target encoding,
     # if we can transcode the needed characters
@@ -1989,7 +1984,6 @@ class CSV
             sample =  read_to_char(1024)
             sample += read_to_char(1) if sample[-1..-1] == encode_str(&quot;\r&quot;) and
                                          not @io.eof?
-
             # try to find a standard separator
             if sample =~ encode_re(&quot;\r\n?|\n&quot;)
               @row_sep = $&amp;
@@ -2272,8 +2266,9 @@ class CSV
   #
   def read_to_char(bytes)
     return &quot;&quot; if @io.eof?
-    data = @io.read(bytes)
+    data = read_io(bytes)
     begin
+      raise unless data.valid_encoding?
       encoded = encode_str(data)
       raise unless encoded.valid_encoding?
       return encoded
@@ -2281,11 +2276,26 @@ class CSV
       if @io.eof? or data.size &gt;= bytes + 10
         return data
       else
-        data += @io.read(1)
+        data += read_io(1)
         retry
       end
     end
   end
+
+  private
+  def raw_encoding
+    if @io.respond_to? :internal_encoding
+      @io.internal_encoding || @io.external_encoding
+    elsif @io.is_a? StringIO
+      @io.string.encoding
+    else
+      @io.encoding
+    end
+  end
+
+  def read_io(bytes)
+    @io.read(bytes).force_encoding(raw_encoding)
+  end
 end
 
 # Another name for CSV::instance().</diff>
      <filename>lib/csv.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>99664f9ec38d8ec55e0199ce54f587aebb030b96</id>
    </parent>
  </parents>
  <author>
    <name>naruse</name>
    <email>naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e</email>
  </author>
  <url>http://github.com/skaes/matzruby/commit/7bc663eecf536e39ca8abf1f556ad2b4aabc1997</url>
  <id>7bc663eecf536e39ca8abf1f556ad2b4aabc1997</id>
  <committed-date>2009-10-15T11:19:15-07:00</committed-date>
  <authored-date>2009-10-15T11:19:15-07:00</authored-date>
  <message>* lib/csv.rb (CSV#read_to_char): set encoding and verify data
  which read from io before encode it to @encoding.

* lib/csv.rb (CSV#raw_encoding): add to get @io's encoding.

* lib/csv.rb (CSV#read_io): add to read string and set @io's
  encoding.

git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@25353 b2dd03c8-39d4-4d8f-98ff-823fe69b080e</message>
  <tree>b73fc8d1a8752bce06b64f658a556edb408bf603</tree>
  <committer>
    <name>naruse</name>
    <email>naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e</email>
  </committer>
</commit>
