Fix bug in readAsString when charset is LATIN-1 and content-length is set.

BUG= https://github.com/dart-lang/resource/issues/21
R=floitsch@google.com

Review-Url: https://codereview.chromium.org//2671243002 .
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b257ba5..110d5b7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,8 @@
 # Changelog
 
+## 2.1.2
+- Fix bug in `readAsString` when charset is LATIN-1 and content-length is set.
+
 ## 2.1.1
 - Reduce max concurrent connections to the same host to 6 when using `dart:io`.
   That's the same limit that many browsers use.
diff --git a/lib/src/io_io.dart b/lib/src/io_io.dart
index 619dfed..0450651 100644
--- a/lib/src/io_io.dart
+++ b/lib/src/io_io.dart
@@ -76,7 +76,8 @@
       // Special case LATIN-1 since it is common and doesn't need decoding.
       int length = response.contentLength;
       if (length < 0) length = 0;
-      var buffer = new Uint8Buffer(length);
+      // Create empty buffer with capacity matching contentLength.
+      var buffer = new Uint8Buffer(length)..length = 0;
       await for (var bytes in response) {
         buffer.addAll(bytes);
       }
diff --git a/pubspec.yaml b/pubspec.yaml
index b15729e..2829a08 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
 name: resource
-version: 2.1.1
+version: 2.1.2
 description: Reading resource data from (package and other) files.
 author: Dart Team <misc@dartlang.org>
 homepage: https://github.com/dart-lang/resource
diff --git a/test/loader_http_test.dart b/test/loader_http_test.dart
index 7795f58..155613e 100644
--- a/test/loader_http_test.dart
+++ b/test/loader_http_test.dart
@@ -30,6 +30,9 @@
       var encoding = parseAcceptCharset(encodings);
       request.response.headers.contentType =
           new ContentType("text", "plain", charset: encoding.name);
+      if (request.uri.query.contains("length")) {
+        request.response.headers.contentLength = content.length;
+      }
       request.response..write(content)
                       ..close();
     }).catchError(print);
@@ -47,6 +50,14 @@
     expect(string, content);
   });
 
+  test("Latin-1 encoding w/ length", () async {
+    // Regression test for issue #21.
+    var loader = ResourceLoader.defaultLoader;
+    var newUri = uri.replace(query: "length");  // Request length set.
+    String string = await loader.readAsString(newUri, encoding: LATIN1);
+    expect(string, content);
+  });
+
   test("UTF-8 encoding", () async {
     var loader = ResourceLoader.defaultLoader;
     String string = await loader.readAsString(uri, encoding: UTF8);