Make IOSink implement StringSink

Besides adding the StringSink methods I also added writeBytes and
deprecated both add and addString.

To handle the encoding of strings the IOSike has an encoding
property. This property is mutable in situation when it makes sense to
change encoding of what is written. The exception here is for HTTP
where the encoding is determined from the header and the encoding
cannot be changed.

R=ajohnsen@google.com, ager@google.com, nweiz@google.com

BUG=

Review URL: https://codereview.chromium.org//12504006

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/http@19676 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index 65c4799..b626b1a 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -78,30 +78,20 @@
 Encoding encodingForCharset(
     String charset, [Encoding fallback = Encoding.ISO_8859_1]) {
   if (charset == null) return fallback;
-  var encoding = _encodingForCharset(charset);
+  var encoding = Encoding.fromName(charset);
   return encoding == null ? fallback : encoding;
 }
 
+
 /// Returns the [Encoding] that corresponds to [charset]. Throws a
 /// [FormatException] if no [Encoding] was found that corresponds to [charset].
 /// [charset] may not be null.
 Encoding requiredEncodingForCharset(String charset) {
-  var encoding = _encodingForCharset(charset);
+  var encoding = Encoding.fromName(charset);
   if (encoding != null) return encoding;
   throw new FormatException('Unsupported encoding "$charset".');
 }
 
-/// Returns the [Encoding] that corresponds to [charset]. Returns null if no
-/// [Encoding] was found that corresponds to [charset]. [charset] may not be
-/// null.
-Encoding _encodingForCharset(String charset) {
-  charset = charset.toLowerCase();
-  if (charset == 'ascii' || charset == 'us-ascii') return Encoding.ASCII;
-  if (charset == 'utf-8') return Encoding.UTF_8;
-  if (charset == 'iso-8859-1') return Encoding.ISO_8859_1;
-  return null;
-}
-
 /// Converts [bytes] into a [String] according to [encoding].
 String decodeString(List<int> bytes, Encoding encoding) {
   // TODO(nweiz): implement this once issue 6284 is fixed.
diff --git a/test/utils.dart b/test/utils.dart
index 8b404a5..a396097 100644
--- a/test/utils.dart
+++ b/test/utils.dart
@@ -1,4 +1,4 @@
-// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
+// Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
@@ -57,9 +57,18 @@
       }
 
       new ByteStream(request).toBytes().then((requestBodyBytes) {
-        response.statusCode = 200;
-        response.headers.contentType = new ContentType("application", "json");
-      response.headers.set('single', 'value');
+        var outputEncoding;
+        var encodingName = request.queryParameters['response-encoding'];
+        if (encodingName != null) {
+          outputEncoding = requiredEncodingForCharset(encodingName);
+        } else {
+          outputEncoding = Encoding.ASCII;
+        }
+
+        response.headers.contentType =
+            new ContentType(
+                "application", "json", charset: outputEncoding.name);
+        response.headers.set('single', 'value');
 
         var requestBody;
         if (requestBodyBytes.isEmpty) {
@@ -86,17 +95,9 @@
           content['headers'][name] = values;
         });
 
-        var outputEncoding;
-        var encodingName = request.queryParameters['response-encoding'];
-        if (encodingName != null) {
-          outputEncoding = requiredEncodingForCharset(encodingName);
-        } else {
-          outputEncoding = Encoding.ASCII;
-        }
-
         var body = json.stringify(content);
         response.contentLength = body.length;
-        response.addString(body, outputEncoding);
+        response.write(body);
         response.close();
       });
     });