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@19676 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
sgjesse@google.com 2013-03-08 10:06:28 +00:00
parent a61e898b01
commit b6096848b1
65 changed files with 531 additions and 333 deletions

View file

@ -78,30 +78,20 @@ List<String> split1(String toSplit, String pattern) {
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.

View file

@ -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 @@ Future startServer() {
}
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 @@ Future startServer() {
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();
});
});

View file

@ -763,20 +763,25 @@ class _Socket extends Stream<List<int>> implements Socket {
unsubscribeOnError: unsubscribeOnError);
}
Encoding get encoding => _sink.encoding;
void set encoding(Encoding value) => _sink.encoding = value;
void write(Object obj) => _sink.write(obj);
void writeln(Object obj) => _sink.writeln(obj);
void writeCharCode(int charCode) => _sink.writeCharCode(charCode);
void writeAll(Iterable objects) => _sink.writeAll(objects);
void writeBytes(List<int> bytes) => _sink.writeBytes(bytes);
Future<Socket> consume(Stream<List<int>> stream) {
return _sink.consume(stream);
}
Future<Socket> addStream(Stream<List<int>> stream) {
return _sink.addStream(stream);
}
void add(List<int> data) {
return _sink.add(data);
}
void addString(String string, [Encoding encoding = Encoding.UTF_8]) {
return _sink.addString(string, encoding);
Future<Socket> writeStream(Stream<List<int>> stream) {
return _sink.writeStream(stream);
}
close() => _sink.close();

View file

@ -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.
@ -293,7 +293,7 @@ void compile(List<String> argv) {
if (sourceMapFileName != null) {
String sourceMapTag = '//@ sourceMappingURL=$sourceMapFileName\n';
sink.count += sourceMapTag.length;
output.addString(sourceMapTag);
output.write(sourceMapTag);
}
output.close();
if (isPrimaryOutput) {
@ -302,7 +302,7 @@ void compile(List<String> argv) {
}
var controller = new StreamController<String>();
controller.stream.listen(output.addString, onDone: onDone);
controller.stream.listen(output.write, onDone: onDone);
sink = new CountingSink(controller);
return sink;
}

View file

@ -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.
@ -174,8 +174,14 @@ abstract class File extends FileSystemEntity {
* * [FileMode.WRITE]: truncates the file to length zero.
* * [FileMode.APPEND]: sets the initial write position to the end
* of the file.
*
* When writing strings through the returned [IOSink] the encoding
* specified using [encoding] will be used. The returned [IOSink]
* has an [:encoding:] property which can be changed after the
* [IOSink] has been created.
*/
IOSink<File> openWrite([FileMode mode = FileMode.WRITE]);
IOSink<File> openWrite({FileMode mode: FileMode.WRITE,
Encoding encoding: Encoding.UTF_8});
/**
* Read the entire file contents as a list of bytes. Returns a

View file

@ -468,14 +468,15 @@ class _File extends _FileBase implements File {
return new _FileStream(_path);
}
IOSink<File> openWrite([FileMode mode = FileMode.WRITE]) {
IOSink<File> openWrite({FileMode mode: FileMode.WRITE,
Encoding encoding: Encoding.UTF_8}) {
if (mode != FileMode.WRITE &&
mode != FileMode.APPEND) {
throw new FileIOException(
"Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND");
}
var consumer = new _FileStreamConsumer(this, mode);
return new IOSink<File>(consumer);
return new IOSink<File>(consumer, encoding: encoding);
}
Future<List<int>> readAsBytes() {
@ -547,23 +548,14 @@ class _File extends _FileBase implements File {
Future<File> writeAsBytes(List<int> bytes,
[FileMode mode = FileMode.WRITE]) {
Completer<File> completer = new Completer<File>();
try {
var stream = openWrite(mode);
stream.add(bytes);
stream.close();
stream.done
.then((_) {
completer.complete(this);
})
.catchError((e) {
completer.completeError(e);
});
IOSink<File> sink = openWrite(mode: mode);
sink.writeBytes(bytes);
sink.close();
return sink.done.then((_) => this);;
} catch (e) {
Timer.run(() => completer.completeError(e));
return completer.future;
return new Future.immediateError(e);
}
return completer.future;
}
void writeAsBytesSync(List<int> bytes, [FileMode mode = FileMode.WRITE]) {

View file

@ -676,6 +676,32 @@ abstract class HttpRequest implements Stream<List<int>> {
/**
* HTTP response to be send back to the client.
*
* This object has a number of properties for setting up the HTTP
* header of the response. When the header has been set up the methods
* from the [IOSink] can be used to write the actual body of the HTTP
* response. When one of the [IOSink] methods is used for the
* first time the request header is send. Calling any methods that
* will change the header after it is sent will throw an exception.
*
* When writing string data through the [IOSink] the encoding used
* will be determined from the "charset" parameter of the
* "Content-Type" header.
*
* HttpResponse response = ...
* response.headers.contentType
* = new ContentType("application", "json", charset: "utf-8");
* response.write(...); // Strings written will be UTF-8 encoded.
*
* If no charset is provided the default of ISO-8859-1 (Latin 1) will
* be used.
*
* HttpResponse response = ...
* response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain");
* response.write(...); // Strings written will be ISO-8859-1 encoded.
*
* If an unsupported encoding is used an exception will be thrown if
* using one of the write methods taking a string.
*/
abstract class HttpResponse implements IOSink<HttpResponse> {
// TODO(ajohnsen): Add documentation of how to pipe a file to the response.
@ -860,13 +886,31 @@ abstract class HttpClient {
/**
* HTTP request for a client connection.
*
* The request is an [IOSink], used to write the request data. When
* all request data has been written, close the stream to indicate the end of
* the request.
* This object has a number of properties for setting up the HTTP
* header of the request. When the header has been set up the methods
* from the [IOSink] can be used to write the actual body of the HTTP
* request. When one of the [IOSink] methods is used for the first
* time the request header is send. Calling any methods that will
* change the header after it is sent will throw an exception.
*
* When this is accessed for the first time the request header is
* send. Calling any methods that will change the header after
* having retrieved the output stream will throw an exception.
* When writing string data through the [IOSink] the
* encoding used will be determined from the "charset" parameter of
* the "Content-Type" header.
*
* HttpClientRequest request = ...
* request.headers.contentType
* = new ContentType("application", "json", charset: "utf-8");
* request.write(...); // Strings written will be UTF-8 encoded.
*
* If no charset is provided the default of ISO-8859-1 (Latin 1) will
* be used.
*
* HttpClientRequest request = ...
* request.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain");
* request.write(...); // Strings written will be ISO-8859-1 encoded.
*
* If an unsupported encoding is used an exception will be thrown if
* using one of the write methods taking a string.
*/
abstract class HttpClientRequest
implements IOSink<HttpClientRequest> {

View file

@ -350,7 +350,7 @@ class _HttpHeaders implements HttpHeaders {
var bufferPos = 0;
void writeBuffer() {
sink.add(buffer.getRange(0, bufferPos));
sink.writeBytes(buffer.getRange(0, bufferPos));
bufferPos = 0;
}
@ -621,6 +621,9 @@ class _HeaderValue implements HeaderValue {
class _ContentType extends _HeaderValue implements ContentType {
String _primaryType = "";
String _subType = "";
_ContentType(String primaryType,
String subType,
String charset,
@ -639,7 +642,7 @@ class _ContentType extends _HeaderValue implements ContentType {
}
}
_ContentType.fromString(String value) : super.fromString(value) {
_ContentType.fromString(String value) : super.fromString(value) {
int index = _value.indexOf("/");
if (index == -1 || index == (_value.length - 1)) {
_primaryType = _value.trim().toLowerCase();
@ -655,9 +658,6 @@ class _ContentType extends _HeaderValue implements ContentType {
String get subType => _subType;
String get charset => parameters["charset"];
String _primaryType = "";
String _subType = "";
}

View file

@ -332,7 +332,7 @@ abstract class _HttpOutboundMessage<T> implements IOSink {
_HttpOutboundMessage(String protocolVersion, _HttpOutgoing outgoing)
: _outgoing = outgoing,
_ioSink = new IOSink(outgoing),
_ioSink = new IOSink(outgoing, encoding: Encoding.ASCII),
headers = new _HttpHeaders(protocolVersion);
int get contentLength => headers.contentLength;
@ -345,6 +345,67 @@ abstract class _HttpOutboundMessage<T> implements IOSink {
headers.persistentConnection = p;
}
Encoding get encoding {
var charset;
if (headers.contentType != null && headers.contentType.charset != null) {
charset = headers.contentType.charset;
} else {
charset = "iso-8859-1";
}
return Encoding.fromName(charset);
}
void set encoding(Encoding value) {
throw new StateError("IOSink encoding is not mutable");
}
void write(Object obj) {
_writeHeaders();
if (_ignoreBody) return;
// This comment is copied from runtime/lib/string_buffer_patch.dart.
// TODO(srdjan): The following four lines could be replaced by
// '$obj', but apparently this is too slow on the Dart VM.
String string;
if (obj is String) {
string = obj;
} else {
string = obj.toString();
if (string is! String) {
throw new ArgumentError('toString() did not return a string');
}
}
if (string.isEmpty) return;
if (_chunked) {
_ChunkedTransformer._addChunk(_encodeString(string, encoding),
_ioSink.writeBytes);
} else {
_ioSink.write(string);
}
}
void writeAll(Iterable objects) {
for (Object obj in objects) write(obj);
}
void writeln(Object obj) {
write(obj);
write("\n");
}
void writeCharCode(int charCode) {
write(new String.fromCharCode(charCode));
}
void writeBytes(List<int> data) {
_writeHeaders();
if (_ignoreBody || data.length == 0) return;
if (_chunked) {
_ChunkedTransformer._addChunk(data, _ioSink.writeBytes);
} else {
_ioSink.writeBytes(data);
}
}
Future<T> consume(Stream<List<int>> stream) {
_writeHeaders();
if (_ignoreBody) return new Future.immediate(this);
@ -355,28 +416,14 @@ abstract class _HttpOutboundMessage<T> implements IOSink {
return _ioSink.consume(stream).then((_) => this);
}
void add(List<int> data) {
_writeHeaders();
if (_ignoreBody || data.length == 0) return;
if (_chunked) {
_ChunkedTransformer._addChunk(data, _ioSink.add);
} else {
_ioSink.add(data);
}
}
void addString(String string, [Encoding encoding = Encoding.UTF_8]) {
add(_encodeString(string, encoding));
}
Future<T> addStream(Stream<List<int>> stream) {
Future<T> writeStream(Stream<List<int>> stream) {
_writeHeaders();
if (_ignoreBody) return new Future.immediate(this);
if (_chunked) {
// Transform when chunked.
stream = stream.transform(new _ChunkedTransformer(writeEnd: false));
}
return _ioSink.addStream(stream).then((_) => this);
return _ioSink.writeStream(stream).then((_) => this);
}
void close() {
@ -388,7 +435,7 @@ abstract class _HttpOutboundMessage<T> implements IOSink {
_writeHeaders();
if (!_ignoreBody) {
if (_chunked) {
_ChunkedTransformer._addChunk([], _ioSink.add);
_ChunkedTransformer._addChunk([], _ioSink.writeBytes);
}
}
_ioSink.close();
@ -399,7 +446,9 @@ abstract class _HttpOutboundMessage<T> implements IOSink {
void _writeHeaders() {
if (_headersWritten) return;
_headersWritten = true;
_ioSink.encoding = Encoding.ASCII;
_writeHeader();
_ioSink.encoding = encoding;
if (_ignoreBody) {
_ioSink.close();
return;
@ -452,19 +501,19 @@ class _HttpResponse extends _HttpOutboundMessage<HttpResponse>
HttpConnectionInfo get connectionInfo => _httpRequest.connectionInfo;
void _writeHeader() {
writeSP() => _ioSink.add([_CharCode.SP]);
writeCRLF() => _ioSink.add([_CharCode.CR, _CharCode.LF]);
writeSP() => _ioSink.writeBytes([_CharCode.SP]);
writeCRLF() => _ioSink.writeBytes([_CharCode.CR, _CharCode.LF]);
// Write status line.
if (headers.protocolVersion == "1.1") {
_ioSink.add(_Const.HTTP11);
_ioSink.writeBytes(_Const.HTTP11);
} else {
_ioSink.add(_Const.HTTP10);
_ioSink.writeBytes(_Const.HTTP10);
}
writeSP();
_ioSink.addString(statusCode.toString());
_ioSink.write(statusCode.toString());
writeSP();
_ioSink.addString(reasonPhrase);
_ioSink.write(reasonPhrase);
writeCRLF();
var session = _httpRequest._session;
@ -645,10 +694,10 @@ class _HttpClientRequest extends _HttpOutboundMessage<HttpClientRequest>
}
void _writeHeader() {
writeSP() => _ioSink.add([_CharCode.SP]);
writeCRLF() => _ioSink.add([_CharCode.CR, _CharCode.LF]);
writeSP() => _ioSink.writeBytes([_CharCode.SP]);
writeCRLF() => _ioSink.writeBytes([_CharCode.CR, _CharCode.LF]);
_ioSink.addString(method);
_ioSink.write(method);
writeSP();
// Send the path for direct connections and the whole URL for
// proxy connections.
@ -662,12 +711,12 @@ class _HttpClientRequest extends _HttpOutboundMessage<HttpClientRequest>
path = "${path}?${uri.query}";
}
}
_ioSink.addString(path);
_ioSink.write(path);
} else {
_ioSink.addString(uri.toString());
_ioSink.write(uri.toString());
}
writeSP();
_ioSink.add(_Const.HTTP11);
_ioSink.writeBytes(_Const.HTTP11);
writeCRLF();
// Add the cookies to the headers.
@ -775,7 +824,8 @@ class _DataValidatorTransformer
_controller.signalError(new HttpException(
"Content size exceeds specified contentLength. "
"$_bytesWritten bytes written while expected "
"$expectedTransferLength."));
"$expectedTransferLength. "
"[${new String.fromCharCodes(data)}]"));
_controller.close();
return;
}
@ -901,7 +951,7 @@ class _HttpClientConnection {
// Sending request, set up response completer.
_nextResponseCompleter = new Completer();
var requestFuture = _socket.addStream(stream)
var requestFuture = _socket.writeStream(stream)
.catchError((e) {
destroy();
throw e;
@ -1228,7 +1278,7 @@ class _HttpConnection {
outgoing);
var request = new _HttpRequest(response, incoming, _httpServer, this);
outgoing.onStream((stream) {
return _streamFuture = _socket.addStream(stream)
return _streamFuture = _socket.writeStream(stream)
.then((_) {
if (_state == _DETACHED) return;
if (response.persistentConnection &&
@ -1509,24 +1559,40 @@ class _DetachedSocket extends Stream<List<int>> implements Socket {
unsubscribeOnError: unsubscribeOnError);
}
Encoding get encoding => _socket.encoding;
void set encoding(Encoding value) {
_socket.encoding = value;
}
void write(Object obj) => _socket.write(obj);
void writeln(Object obj) => _socket.writeln(obj);
void writeCharCode(int charCode) => _socket.writeCharCode(charCode);
void writeAll(Iterable objects) => _socket.writeAll(objects);
void writeBytes(List<int> bytes) => _socket.writeBytes(bytes);
Future<Socket> consume(Stream<List<int>> stream) {
return _socket.consume(stream);
}
Future<Socket> addStream(Stream<List<int>> stream) {
return _socket.addStream(stream);
}
void addString(String string, [Encoding encoding = Encoding.UTF_8]) {
return _socket.addString(string, encoding);
Future<Socket> writeStream(Stream<List<int>> stream) {
return _socket.writeStream(stream);
}
void destroy() => _socket.destroy();
void add(List<int> data) => _socket.add(data);
void close() => _socket.close();
Future<Socket> get done => _socket.done;
int get port => _socket.port;
String get remoteHost => _socket.remoteHost;
int get remotePort => _socket.remotePort;
}

View file

@ -5,18 +5,31 @@
part of dart.io;
/**
* Helper class to wrap a [StreamConsumer<List<int>, T>] and provide utility
* functions for writing to the StreamConsumer directly. The [IOSink]
* buffers the input given by [add] and [addString] and will delay a [consume]
* or [addStream] until the buffer is flushed.
* Helper class to wrap a [StreamConsumer<List<int>, T>] and provide
* utility functions for writing to the StreamConsumer directly. The
* [IOSink] buffers the input given by [write], [writeAll], [writeln],
* [writeCharCode] and [writeBytes] and will delay a [consume] or
* [writeStream] until the buffer is flushed.
*
* When the [IOSink] is bound to a stream (through either [consume]
* or [addStream]) any call to the [IOSink] will throw a
* or [writeStream]) any call to the [IOSink] will throw a
* [StateError].
*/
abstract class IOSink<T> implements StreamConsumer<List<int>, T> {
factory IOSink(StreamConsumer<List<int>, T> target)
=> new _IOSinkImpl(target);
abstract class IOSink<T> implements StreamConsumer<List<int>, T>, StringSink {
factory IOSink(StreamConsumer<List<int>, T> target,
{Encoding encoding: Encoding.UTF_8})
=> new _IOSinkImpl(target, encoding);
/**
* The [Encoding] used when writing strings. Depending on the
* underlying consumer this property might be mutable.
*/
Encoding encoding;
/**
* Writes the bytes uninterpreted to the consumer.
*/
void writeBytes(List<int> data);
/**
* Provide functionality for piping to the [IOSink].
@ -26,17 +39,7 @@ abstract class IOSink<T> implements StreamConsumer<List<int>, T> {
/**
* Like [consume], but will not close the target when done.
*/
Future<T> addStream(Stream<List<int>> stream);
/**
* Write a list of bytes to the target.
*/
void add(List<int> data);
/**
* Write a String to the target.
*/
void addString(String string, [Encoding encoding = Encoding.UTF_8]);
Future<T> writeStream(Stream<List<int>> stream);
/**
* Close the target.
@ -59,8 +62,56 @@ class _IOSinkImpl<T> implements IOSink<T> {
Future<T> _pipeFuture;
StreamSubscription<List<int>> _bindSubscription;
bool _paused = true;
bool _encodingMutable = true;
_IOSinkImpl(StreamConsumer<List<int>, T> target) : _target = target;
_IOSinkImpl(StreamConsumer<List<int>, T> this._target, this._encoding);
Encoding _encoding;
Encoding get encoding => _encoding;
void set encoding(Encoding value) {
if (!_encodingMutable) {
throw new StateError("IOSink encoding is not mutable");
}
_encoding = value;
}
void write(Object obj) {
// This comment is copied from runtime/lib/string_buffer_patch.dart.
// TODO(srdjan): The following four lines could be replaced by
// '$obj', but apparently this is too slow on the Dart VM.
String string;
if (obj is String) {
string = obj;
} else {
string = obj.toString();
if (string is! String) {
throw new ArgumentError('toString() did not return a string');
}
}
if (string.isEmpty) return;
writeBytes(_encodeString(string, _encoding));
}
void writeAll(Iterable objects) {
for (Object obj in objects) write(obj);
}
void writeln(Object obj) {
write(obj);
write("\n");
}
void writeCharCode(int charCode) {
write(new String.fromCharCode(charCode));
}
void writeBytes(List<int> data) {
if (_isBound) {
throw new StateError("IOSink is already bound to a stream");
}
_controller.add(data);
}
Future<T> consume(Stream<List<int>> stream) {
if (_isBound) {
@ -69,24 +120,13 @@ class _IOSinkImpl<T> implements IOSink<T> {
return _fillFromStream(stream);
}
Future<T> addStream(Stream<List<int>> stream) {
Future<T> writeStream(Stream<List<int>> stream) {
if (_isBound) {
throw new StateError("IOSink is already bound to a stream");
}
return _fillFromStream(stream, unbind: true);
}
void add(List<int> data) {
if (_isBound) {
throw new StateError("IOSink is already bound to a stream");
}
_controller.add(data);
}
void addString(String string, [Encoding encoding = Encoding.UTF_8]) {
add(_encodeString(string, encoding));
}
void close() {
if (_isBound) {
throw new StateError("IOSink is already bound to a stream");

View file

@ -12,6 +12,54 @@ class Encoding {
static const Encoding ISO_8859_1 = const Encoding._internal("iso-8859-1");
static const Encoding ASCII = const Encoding._internal("us-ascii");
// All aliasses (in lowercase) of supported encoding from
// http://www.iana.org/assignments/character-sets/character-sets.xml.
static Map<String, Encoding> _nameToEncoding = <String, Encoding> {
// ISO_8859-1:1987.
"iso_8859-1:1987": ISO_8859_1,
"iso-ir-100": ISO_8859_1,
"iso_8859-1": ISO_8859_1,
"iso-8859-1": ISO_8859_1,
"latin1": ISO_8859_1,
"l1": ISO_8859_1,
"ibm819": ISO_8859_1,
"cp819": ISO_8859_1,
"csisolatin1": ISO_8859_1,
// US-ASCII.
"iso-ir-6": ASCII,
"ansi_x3.4-1968": ASCII,
"ansi_x3.4-1986": ASCII,
"iso_646.irv:1991": ASCII,
"iso646-us": ASCII,
"us-ascii": ASCII,
"us": ASCII,
"ibm367": ASCII,
"cp367": ASCII,
"csascii": ASCII,
"ascii": ASCII, // This is not in the IANA official names.
// UTF-8.
"csutf8": UTF_8,
"utf-8": UTF_8
};
/**
* Gets an [Encoding] object from the name of the character set
* name. The names used are the IANA official names for the
* character set (see
* http://www.iana.org/assignments/character-sets/character-sets.xml).
*
* The [name] passed is case insensitive.
*
* If character set is not supported [:null:] is returned.
*/
static Encoding fromName(String name) {
if (name == null) return null;
name = name.toLowerCase();
return _nameToEncoding[name];
}
/**
* Name of the encoding. This will be the lower-case version of one of the
* IANA official names for the character set (see

View file

@ -659,9 +659,9 @@ class _WebSocketImpl extends Stream implements WebSocket {
}
assert(index == headerSize);
try {
_socket.add(header);
_socket.writeBytes(header);
if (data != null) {
_socket.add(data);
_socket.writeBytes(data);
}
} catch (_) {
// The socket can be closed before _socket.done have a chance

View file

@ -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.
@ -460,7 +460,7 @@ class Debugger {
void sendMessage(Map<String,dynamic> msg) {
String jsonMsg = JSON.stringify(msg);
if (verboseWire) print("SEND: $jsonMsg");
socket.addString(jsonMsg);
socket.write(jsonMsg);
}
bool get errorsDetected => errors.length > 0;

View file

@ -63,7 +63,7 @@ class EchoServerGame {
_socket.listen(onData,
onError: errorHandler,
onDone: onClosed);
_socket.add(_buffer);
_socket.writeBytes(_buffer);
_socket.close();
data = new List<int>(MSGSIZE);
}
@ -120,7 +120,7 @@ class EchoServer extends TestingServer {
Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
}
if (offset == MSGSIZE) {
connection.add(buffer);
connection.writeBytes(buffer);
connection.close();
}
}

View file

@ -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.
@ -28,7 +28,7 @@ fuzzSyncMethods() {
doItSync(f.readAsLinesSync);
typeMapping.forEach((k2, v2) {
doItSync(() => f.openSync(v2));
doItSync(() => f.openWrite(v2));
doItSync(() => f.openWrite(mode: v2));
doItSync(() => f.readAsStringSync(v2));
doItSync(() => f.readAsLinesSync(v2));
});

View file

@ -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.
// Testing file input stream, VM-only, standalone test.
@ -21,7 +21,7 @@ void testOpenOutputStreamSync() {
file.createSync();
IOSink x = file.openWrite();
var data = [65, 66, 67];
x.add(data);
x.writeBytes(data);
x.close();
x.done.then((_) {
Expect.listEquals(file.readAsBytesSync(), data);

View file

@ -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.
@ -104,8 +104,8 @@ testFileWriteRead() {
new File(x).createSync();
createLink(x, y, true, () {
var data = "asdf".codeUnits;
var output = new File(y).openWrite(FileMode.WRITE);
output.add(data);
var output = new File(y).openWrite(mode: FileMode.WRITE);
output.writeBytes(data);
output.close();
output.done.then((_) {
var read = new File(y).readAsBytesSync();

View file

@ -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.
//
@ -91,7 +91,7 @@ class FileTest {
tempDirectory.path.concat("/out_read_write_stream");
var file2 = new File(outFilename);
var output = file2.openWrite();
output.add(buffer);
output.writeBytes(buffer);
output.close();
output.done.then((_) {
// Now read the contents of the file just written.
@ -129,8 +129,8 @@ class FileTest {
tempDirectory.path.concat("/out_read_write_stream_large_file");
File file = new File(filename);
IOSink output = file.openWrite();
output.add(buffer);
output.add(buffer);
output.writeBytes(buffer);
output.writeBytes(buffer);
output.close();
output.done.then((_) {
Stream input = file.openRead();
@ -344,12 +344,12 @@ class FileTest {
file.createSync();
List<int> buffer = content.codeUnits;
var output = file.openWrite();
output.add(buffer);
output.writeBytes(buffer);
output.close();
output.done.then((_) {
File file2 = new File(filename);
var appendingOutput = file2.openWrite(FileMode.APPEND);
appendingOutput.add(buffer);
var appendingOutput = file2.openWrite(mode: FileMode.APPEND);
appendingOutput.writeBytes(buffer);
appendingOutput.close();
appendingOutput.done.then((_) {
File file3 = new File(filename);
@ -376,11 +376,15 @@ class FileTest {
file.createSync();
List<int> buffer = content.codeUnits;
var output = file.openWrite();
output.addString("abcdABCD");
output.addString("abcdABCD", Encoding.UTF_8);
output.addString("abcdABCD", Encoding.ISO_8859_1);
output.addString("abcdABCD", Encoding.ASCII);
output.addString("æøå", Encoding.UTF_8);
output.write("abcdABCD");
output.encoding = Encoding.UTF_8;
output.write("abcdABCD");
output.encoding = Encoding.ISO_8859_1;
output.write("abcdABCD");
output.encoding = Encoding.ASCII;
output.write("abcdABCD");
output.encoding = Encoding.UTF_8;
output.write("æøå");
output.close();
output.done.then((_) {
RandomAccessFile raf = file.openSync();
@ -761,7 +765,7 @@ class FileTest {
file.createSync();
var output = file.openWrite();
output.close();
Expect.throws(() => output.add(buffer));
Expect.throws(() => output.writeBytes(buffer));
output.done.then((_) {
file.deleteSync();
asyncTestDone("testCloseExceptionStream");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// (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.
@ -25,10 +25,10 @@ void testHttp10NoKeepAlive() {
response.done
.then((_) => Expect.fail("Unexpected response completion"))
.catchError((e) => Expect.isTrue(e.error is HttpException));
response.addString("Z");
response.addString("Z");
response.write("Z");
response.write("Z");
response.close();
Expect.throws(() => response.addString("x"),
Expect.throws(() => response.write("x"),
(e) => e is StateError);
},
onError: (e) => Expect.fail("Unexpected error $e"));
@ -36,7 +36,7 @@ void testHttp10NoKeepAlive() {
int count = 0;
makeRequest() {
Socket.connect("127.0.0.1", server.port).then((socket) {
socket.addString("GET / HTTP/1.0\r\n\r\n");
socket.write("GET / HTTP/1.0\r\n\r\n");
List<int> response = [];
socket.listen(
@ -72,7 +72,7 @@ void testHttp10ServerClose() {
Expect.equals(-1, request.contentLength);
var response = request.response;
Expect.equals("1.0", request.protocolVersion);
response.addString("Z");
response.write("Z");
response.close();
},
onError: (e) => Expect.fail("Unexpected error $e"));
@ -80,8 +80,8 @@ void testHttp10ServerClose() {
int count = 0;
makeRequest() {
Socket.connect("127.0.0.1", server.port).then((socket) {
socket.addString("GET / HTTP/1.0\r\n\r\n");
socket.addString("Connection: Keep-Alive\r\n\r\n");
socket.write("GET / HTTP/1.0\r\n\r\n");
socket.write("Connection: Keep-Alive\r\n\r\n");
List<int> response = [];
socket.listen(
@ -119,15 +119,15 @@ void testHttp10KeepAlive() {
var response = request.response;
response.contentLength = 1;
Expect.equals("1.0", request.protocolVersion);
response.addString("Z");
response.write("Z");
response.close();
},
onError: (e) => Expect.fail("Unexpected error $e"));
Socket.connect("127.0.0.1", server.port).then((socket) {
void sendRequest() {
socket.addString("GET / HTTP/1.0\r\n");
socket.addString("Connection: Keep-Alive\r\n\r\n");
socket.write("GET / HTTP/1.0\r\n");
socket.write("Connection: Keep-Alive\r\n\r\n");
}
List<int> response = [];
@ -169,7 +169,7 @@ void testHttp10KeepAliveServerCloses() {
Expect.equals(-1, request.contentLength);
var response = request.response;
Expect.equals("1.0", request.protocolVersion);
response.addString("Z");
response.write("Z");
response.close();
},
onError: (e) => Expect.fail("Unexpected error $e"));
@ -177,8 +177,8 @@ void testHttp10KeepAliveServerCloses() {
int count = 0;
makeRequest() {
Socket.connect("127.0.0.1", server.port).then((socket) {
socket.addString("GET / HTTP/1.0\r\n");
socket.addString("Connection: Keep-Alive\r\n\r\n");
socket.write("GET / HTTP/1.0\r\n");
socket.write("Connection: Keep-Alive\r\n\r\n");
List<int> response = [];
socket.listen(

View file

@ -110,7 +110,7 @@ class TestServer {
var response = request.response;
Expect.equals("GET", request.method);
request.listen((_) {}, onDone: () {
response.addString("01234567890");
response.write("01234567890");
response.close();
});
}
@ -120,7 +120,7 @@ class TestServer {
var response = request.response;
response.statusCode = HttpStatus.NOT_FOUND;
response.headers.set("Content-Type", "text/html; charset=UTF-8");
response.addString("Page not found");
response.write("Page not found");
response.close();
}
@ -228,11 +228,11 @@ void testPOST(bool chunkedEncoding) {
httpClient.post("127.0.0.1", port, "/echo")
.then((request) {
if (chunkedEncoding) {
request.addString(data.substring(0, 10));
request.addString(data.substring(10, data.length));
request.write(data.substring(0, 10));
request.write(data.substring(10, data.length));
} else {
request.contentLength = data.length;
request.addString(data);
request.write(data);
}
return request.close();
})

View file

@ -32,7 +32,7 @@ void testGetDataRequest() {
HttpServer.bind().then((server) {
var data = "lalala".codeUnits;
server.listen((request) {
request.response.add(data);
request.response.writeBytes(data);
request.pipe(request.response);
});
@ -84,8 +84,8 @@ void testGetDataServerClose() {
HttpServer.bind().then((server) {
server.listen((request) {
request.response.contentLength = 100;
request.response.addString("data");
request.response.addString("more data");
request.response.write("data");
request.response.write("more data");
completer.future.then((_) => server.close());
});

View file

@ -46,7 +46,7 @@ void testBadResponseAdd() {
testClientRequest((request) {
var port = new ReceivePort();
request.contentLength = 0;
request.add([0]);
request.writeBytes([0]);
request.done.catchError((error) {
port.close();
}, test: (e) => e is HttpException);
@ -56,8 +56,8 @@ void testBadResponseAdd() {
testClientRequest((request) {
var port = new ReceivePort();
request.contentLength = 5;
request.add([0, 0, 0]);
request.add([0, 0, 0]);
request.writeBytes([0, 0, 0]);
request.writeBytes([0, 0, 0]);
request.done.catchError((error) {
port.close();
}, test: (e) => e is HttpException);
@ -67,9 +67,9 @@ void testBadResponseAdd() {
testClientRequest((request) {
var port = new ReceivePort();
request.contentLength = 0;
request.add(new Uint8List(64 * 1024));
request.add(new Uint8List(64 * 1024));
request.add(new Uint8List(64 * 1024));
request.writeBytes(new Uint8List(64 * 1024));
request.writeBytes(new Uint8List(64 * 1024));
request.writeBytes(new Uint8List(64 * 1024));
request.done.catchError((error) {
port.close();
}, test: (e) => e is HttpException);
@ -91,7 +91,7 @@ void testBadResponseClose() {
testClientRequest((request) {
var port = new ReceivePort();
request.contentLength = 5;
request.add([0]);
request.writeBytes([0]);
request.close();
request.done.catchError((error) {
port.close();

View file

@ -91,7 +91,7 @@ void testClientCloseDelayed(int connections) {
client.post("localhost", server.port, "/")
.then((request) {
req = request;
request.add(new Uint8List(1024));
request.writeBytes(new Uint8List(1024));
return request.response;
})
.then((response) {
@ -124,7 +124,7 @@ void testClientCloseSendingResponse(int connections) {
}
server.listen((request) {
var timer = new Timer.repeating(const Duration(milliseconds: 20), (_) {
request.response.add(new Uint8List(16 * 1024));
request.response.writeBytes(new Uint8List(16 * 1024));
});
request.response.done
.catchError((_) {})

View file

@ -15,7 +15,7 @@ void testHttp10Close(bool closeRequest) {
Socket.connect("127.0.0.1", server.port)
.then((socket) {
socket.addString("GET / HTTP/1.0\r\n\r\n");
socket.write("GET / HTTP/1.0\r\n\r\n");
socket.listen(
(data) {},
onDone: () {
@ -36,7 +36,7 @@ void testHttp11Close(bool closeRequest) {
Socket.connect("127.0.0.1", server.port)
.then((socket) {
List<int> buffer = new List<int>(1024);
socket.addString("GET / HTTP/1.1\r\nConnection: close\r\n\r\n");
socket.write("GET / HTTP/1.1\r\nConnection: close\r\n\r\n");
socket.listen(
(data) {},
onDone: () {
@ -53,7 +53,7 @@ void testStreamResponse() {
server.listen((request) {
// TODO(ajohnsen): Use timer (see old version).
for (int i = 0; i < 10; i++) {
request.response.addString(
request.response.write(
'data:${new DateTime.now().millisecondsSinceEpoch}\n\n');
}
});

View file

@ -21,16 +21,16 @@ void testNoBody(int totalConnections, bool explicitContentLength) {
.catchError((e) {
Expect.isTrue(e.error is HttpException);
});
// addString with content length 0 closes the connection and
// write with content length 0 closes the connection and
// reports an error.
response.addString("x");
// Subsequent addString are ignored as there is already an
response.write("x");
// Subsequent write are ignored as there is already an
// error.
response.addString("x");
// After an explicit close, addString becomes a state error
response.write("x");
// After an explicit close, write becomes a state error
// because we have said we will not add more.
response.close();
Expect.throws(() => response.addString("x"),
Expect.throws(() => response.write("x"),
(e) => e is StateError);
},
onError: (e) {
@ -82,11 +82,11 @@ void testBody(int totalConnections, bool useHeader) {
request.listen(
(d) {},
onDone: () {
response.addString("x");
response.write("x");
Expect.throws(() => response.contentLength = 3,
(e) => e is HttpException);
response.addString("x");
response.addString("x");
response.write("x");
response.write("x");
response.done
.then((_) {
Expect.fail("Unexpected successful response completion");
@ -98,7 +98,7 @@ void testBody(int totalConnections, bool useHeader) {
}
});
response.close();
Expect.throws(() => response.addString("x"),
Expect.throws(() => response.write("x"),
(e) => e is StateError);
});
},
@ -115,10 +115,10 @@ void testBody(int totalConnections, bool useHeader) {
request.headers.add(HttpHeaders.CONTENT_LENGTH, "7");
request.headers.add(HttpHeaders.CONTENT_LENGTH, "2");
}
request.addString("x");
request.write("x");
Expect.throws(() => request.contentLength = 3,
(e) => e is HttpException);
request.addString("x");
request.write("x");
return request.close();
})
.then((response) {
@ -153,14 +153,14 @@ void testBodyChunked(int totalConnections, bool useHeader) {
request.listen(
(d) {},
onDone: () {
response.addString("x");
response.write("x");
Expect.throws(
() => response.headers.chunkedTransferEncoding = false,
(e) => e is HttpException);
response.addString("x");
response.addString("x");
response.write("x");
response.write("x");
response.close();
Expect.throws(() => response.addString("x"),
Expect.throws(() => response.write("x"),
(e) => e is StateError);
});
},
@ -178,11 +178,11 @@ void testBodyChunked(int totalConnections, bool useHeader) {
request.headers.add(HttpHeaders.CONTENT_LENGTH, "2");
request.headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
}
request.addString("x");
request.write("x");
Expect.throws(() => request.headers.chunkedTransferEncoding = false,
(e) => e is HttpException);
request.addString("x");
request.addString("x");
request.write("x");
request.write("x");
return request.close();
})
.then((response) {
@ -212,7 +212,7 @@ void testSetContentLength() {
response.headers.set("content-length", 3);
Expect.equals("3", response.headers.value('content-length'));
Expect.equals(3, response.contentLength);
response.addString("xxx");
response.write("xxx");
response.close();
});

View file

@ -21,14 +21,14 @@ void testServerDetachSocket() {
socket.listen(
(data) => body.add(new String.fromCharCodes(data)),
onDone: () => Expect.equals("Some data", body.toString()));
socket.addString("Test!");
socket.write("Test!");
socket.close();
});
server.close();
});
Socket.connect("127.0.0.1", server.port).then((socket) {
socket.addString("GET / HTTP/1.1\r\n"
socket.write("GET / HTTP/1.1\r\n"
"content-length: 0\r\n\r\n"
"Some data");
var body = new StringBuffer();
@ -57,7 +57,7 @@ void testBadServerDetachSocket() {
});
Socket.connect("127.0.0.1", server.port).then((socket) {
socket.addString("GET / HTTP/1.1\r\n"
socket.write("GET / HTTP/1.1\r\n"
"content-length: 0\r\n\r\n");
socket.listen((_) {}, onDone: () {
socket.close();
@ -69,7 +69,7 @@ void testBadServerDetachSocket() {
void testClientDetachSocket() {
ServerSocket.bind().then((server) {
server.listen((socket) {
socket.addString("HTTP/1.1 200 OK\r\n"
socket.write("HTTP/1.1 200 OK\r\n"
"\r\n"
"Test!");
var body = new StringBuffer();
@ -99,7 +99,7 @@ void testClientDetachSocket() {
body.toString());
client.close();
});
socket.addString("Some data");
socket.write("Some data");
socket.close();
});
});

View file

@ -14,15 +14,15 @@ void testHEAD(int totalConnections) {
} else if (request.uri.path == "/test200") {
response.contentLength = 200;
List<int> data = new List<int>.filled(200, 0);
response.add(data);
response.writeBytes(data);
response.close();
} else if (request.uri.path == "/testChunked100") {
List<int> data = new List<int>.filled(100, 0);
response.add(data);
response.writeBytes(data);
response.close();
} else if (request.uri.path == "/testChunked200") {
List<int> data = new List<int>.filled(200, 0);
response.add(data);
response.writeBytes(data);
response.close();
} else {
assert(false);

View file

@ -19,7 +19,7 @@ void test(int totalConnections, [String body]) {
// Can still mutate response headers as long as no data has been sent.
response.headers.add("X-Response-Header", "value");
if (body != null) {
response.addString(body);
response.write(body);
// Cannot mutate response headers when data has been sent.
Expect.throws(() => request.headers.add("X-Request-Header", "value2"),
(e) => e is HttpException);
@ -42,7 +42,7 @@ void test(int totalConnections, [String body]) {
// Can still mutate request headers as long as no data has been sent.
request.headers.add("X-Request-Header", "value");
if (body != null) {
request.addString(body);
request.write(body);
// Cannot mutate request headers when data has been sent.
Expect.throws(
() => request.headers.add("X-Request-Header", "value2"),

View file

@ -28,7 +28,7 @@ Future<HttpServer> startServer() {
int length = int.parse(request.queryParameters["length"]);
var buffer = new List<int>.filled(length, 0);
if (!chunked) request.response.contentLength = length;
request.response.add(buffer);
request.response.writeBytes(buffer);
request.response.close();
});
return server;

View file

@ -49,7 +49,7 @@ class Server {
String path = request.uri.path.substring(1);
String content = "$path$path$path";
Expect.equals(content, body.toString());
response.addString(request.uri.path);
response.write(request.uri.path);
response.close();
});
});
@ -165,7 +165,7 @@ void testDirectProxy() {
.then((HttpClientRequest clientRequest) {
String content = "$i$i$i";
clientRequest.contentLength = content.length;
clientRequest.addString(content);
clientRequest.write(content);
return clientRequest.close();
})
.then((HttpClientResponse response) {
@ -224,7 +224,7 @@ void testProxy() {
client.postUrl(Uri.parse(url))
.then((HttpClientRequest clientRequest) {
String content = "$i$i$i";
clientRequest.addString(content);
clientRequest.write(content);
return clientRequest.close();
})
.then((HttpClientResponse response) {
@ -290,7 +290,7 @@ void testProxyChain() {
.then((HttpClientRequest clientRequest) {
String content = "$i$i$i";
clientRequest.contentLength = content.length;
clientRequest.addString(content);
clientRequest.write(content);
return clientRequest.close();
})
.then((HttpClientResponse response) {
@ -333,7 +333,7 @@ void testRealProxy() {
.then((HttpClientRequest clientRequest) {
String content = "$i$i$i";
clientRequest.contentLength = content.length;
clientRequest.addString(content);
clientRequest.write(content);
return clientRequest.close();
})
.then((HttpClientResponse response) {

View file

@ -111,7 +111,7 @@ class TestServer {
var response = request.response;
response.statusCode = HttpStatus.NOT_FOUND;
response.headers.set("Content-Type", "text/html; charset=UTF-8");
response.addString("Page not found");
response.write("Page not found");
response.close();
}
@ -170,11 +170,11 @@ void testRead(bool chunkedEncoding) {
httpClient.post("127.0.0.1", port, "/echo")
.then((request) {
if (chunkedEncoding) {
request.addString(data.substring(0, 10));
request.addString(data.substring(10, data.length));
request.write(data.substring(0, 10));
request.write(data.substring(10, data.length));
} else {
request.contentLength = data.length;
request.add(data.codeUnits);
request.writeBytes(data.codeUnits);
}
return request.close();
})

View file

@ -16,7 +16,7 @@ void main() {
HttpServer.bind().then((server) {
server.listen((HttpRequest request) {
count++;
request.response.addString(request.uri.path);
request.response.write(request.uri.path);
request.response.close();
if (request.uri.path == "/done") {
request.response.done.then((_) {
@ -28,9 +28,9 @@ void main() {
Socket.connect("127.0.0.1", server.port).then((s) {
s.listen((data) { });
for (int i = 0; i < REQUEST_COUNT; i++) {
s.addString("GET /$i HTTP/1.1\r\nX-Header-1: 111\r\n\r\n");
s.write("GET /$i HTTP/1.1\r\nX-Header-1: 111\r\n\r\n");
}
s.addString("GET /done HTTP/1.1\r\nConnection: close\r\n\r\n");
s.write("GET /done HTTP/1.1\r\nConnection: close\r\n\r\n");
s.close();
});
});

View file

@ -11,7 +11,7 @@ void sendData(List<int> data, int port) {
socket.listen((data) {
Expect.fail("No data response was expected");
});
socket.add(data);
socket.writeBytes(data);
socket.close();
socket.done.then((_) {
socket.destroy();

View file

@ -55,7 +55,7 @@ void testResponseAddStream() {
int bytes = new File(new Options().script).lengthSync();
testServerRequest((server, request) {
request.response.addStream(new File(new Options().script).openRead())
request.response.writeStream(new File(new Options().script).openRead())
.then((response) {
response.close();
response.done.then((_) => server.close());
@ -63,9 +63,9 @@ void testResponseAddStream() {
}, bytes: bytes);
testServerRequest((server, request) {
request.response.addStream(new File(new Options().script).openRead())
request.response.writeStream(new File(new Options().script).openRead())
.then((response) {
request.response.addStream(new File(new Options().script).openRead())
request.response.writeStream(new File(new Options().script).openRead())
.then((response) {
response.close();
response.done.then((_) => server.close());
@ -75,7 +75,7 @@ void testResponseAddStream() {
testServerRequest((server, request) {
var controller = new StreamController();
request.response.addStream(controller.stream)
request.response.writeStream(controller.stream)
.then((response) {
response.close();
response.done.then((_) => server.close());
@ -87,7 +87,7 @@ void testResponseAddStream() {
void testBadResponseAdd() {
testServerRequest((server, request) {
request.response.contentLength = 0;
request.response.add([0]);
request.response.writeBytes([0]);
request.response.done.catchError((error) {
server.close();
}, test: (e) => e is HttpException);
@ -95,8 +95,8 @@ void testBadResponseAdd() {
testServerRequest((server, request) {
request.response.contentLength = 5;
request.response.add([0, 0, 0]);
request.response.add([0, 0, 0]);
request.response.writeBytes([0, 0, 0]);
request.response.writeBytes([0, 0, 0]);
request.response.done.catchError((error) {
server.close();
}, test: (e) => e is HttpException);
@ -104,9 +104,9 @@ void testBadResponseAdd() {
testServerRequest((server, request) {
request.response.contentLength = 0;
request.response.add(new Uint8List(64 * 1024));
request.response.add(new Uint8List(64 * 1024));
request.response.add(new Uint8List(64 * 1024));
request.response.writeBytes(new Uint8List(64 * 1024));
request.response.writeBytes(new Uint8List(64 * 1024));
request.response.writeBytes(new Uint8List(64 * 1024));
request.response.done.catchError((error) {
server.close();
}, test: (e) => e is HttpException);
@ -124,7 +124,7 @@ void testBadResponseClose() {
testServerRequest((server, request) {
request.response.contentLength = 5;
request.response.add([0]);
request.response.writeBytes([0]);
request.response.close();
request.response.done.catchError((error) {
server.close();

View file

@ -40,7 +40,7 @@ void test2(int totalConnections, int outputStreamWrites) {
// Server which responds without waiting for request body.
HttpServer.bind().then((server) {
server.listen((HttpRequest request) {
request.response.addString("!dlrow ,olleH");
request.response.write("!dlrow ,olleH");
request.response.close();
});
@ -51,7 +51,7 @@ void test2(int totalConnections, int outputStreamWrites) {
.then((HttpClientRequest request) {
request.contentLength = -1;
for (int i = 0; i < outputStreamWrites; i++) {
request.addString("Hello, world!");
request.write("Hello, world!");
}
request.done.catchError((_) {});
return request.close();
@ -86,7 +86,7 @@ void test3(int totalConnections) {
server.listen((HttpRequest request) {
request.listen((_) {}, onDone: () {
request.response.addString("!dlrow ,olleH");
request.response.write("!dlrow ,olleH");
request.response.close();
});
});
@ -97,7 +97,7 @@ void test3(int totalConnections) {
client.get("127.0.0.1", server.port, "/")
.then((HttpClientRequest request) {
request.contentLength = -1;
request.addString("Hello, world!");
request.write("Hello, world!");
return request.close();
})
.then((HttpClientResponse response) {
@ -164,7 +164,7 @@ void test5(int totalConnections) {
for (int i = 0; i < totalConnections; i++) {
client.post("127.0.0.1", server.port, "/")
.then((request) {
request.add([0]);
request.writeBytes([0]);
// TODO(sgjesse): Make this test work with
//request.response instead of request.close() return
//return request.response;

View file

@ -29,7 +29,7 @@ main() {
serverOnClosed = true;
checkDone();
});
request.response.addString("hello!");
request.response.write("hello!");
request.response.close();
});
});
@ -41,7 +41,7 @@ main() {
clientOnClosed = true;
checkDone();
});
request.addString("hello!");
request.write("hello!");
return request.close();
})
.then((response) {

View file

@ -21,7 +21,7 @@ Function test() {
server.listen((HttpRequest request) {
Expect.isNotNull(request.certificate);
Expect.equals('CN=localhost', request.certificate.subject);
request.response.addString("Hello");
request.response.write("Hello");
request.response.close();
});

View file

@ -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.
//
@ -24,7 +24,7 @@ main() {
// Write to the stdin after the process is terminated to test
// writing to a broken pipe.
process.exitCode.then((code) {
process.stdin.add([0]);
process.stdin.writeBytes([0]);
});
});
}

View file

@ -1,11 +1,11 @@
// 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.
import "dart:io";
main() {
stdout.addString("standard out");
stderr.addString("standard error");
stdout.write("standard out");
stderr.write("standard error");
exitCode = 25;
}

View file

@ -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.
//
@ -15,8 +15,8 @@ main() {
stdin.pipe(stderr);
} else if (options.arguments[0] == "2") {
stdin.listen((data) {
stdout.add(data);
stderr.add(data);
stdout.writeBytes(data);
stderr.writeBytes(data);
});
}
}

View file

@ -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.
//
@ -9,9 +9,11 @@ import "dart:io";
writeData(data, encoding, stream) {
if (stream == "stdout") {
stdout.addString(data, encoding);
stdout.encoding = encoding;
stdout.write(data);
} else if (stream == "stderr") {
stderr.addString(data, encoding);
stderr.encoding = encoding;
stderr.write(data);
}
}

View file

@ -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.
//
@ -46,7 +46,7 @@ void test(Future<Process> future, int expectedExitCode) {
}
process.stdout.listen((_) {});
process.stdin.add(data);
process.stdin.writeBytes(data);
process.stdin.close();
process.stderr.listen(readData);
});

View file

@ -23,7 +23,7 @@ void test(Future<Process> future, int expectedExitCode) {
process.stdout.listen((_) {});
process.stderr.listen((_) {});
process.stdin.addString("Line1\n");
process.stdin.writeln("Line1");
});
}

View file

@ -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.
//
@ -46,7 +46,7 @@ void test(Future<Process> future, int expectedExitCode) {
}
process.stderr.listen((_) {});
process.stdin.add(data);
process.stdin.writeBytes(data);
process.stdin.close();
process.stdout.listen(readData);
});

View file

@ -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.
@ -17,6 +17,6 @@ String getProcessTestFileName() {
var executable = new Options().executable;
var dirIndex = executable.lastIndexOf('dart$extension');
var buffer = new StringBuffer(executable.substring(0, dirIndex));
buffer.add('process_test$extension');
buffer.write('process_test$extension');
return buffer.toString();
}

View file

@ -23,7 +23,7 @@ Future<HttpServer> startServer() {
onDone: () {
request.response.contentLength = 100;
for (int i = 0; i < 10; i++) {
request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
request.response.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
request.response.close();
});

View file

@ -23,7 +23,7 @@ Future<HttpServer> startServer() {
onDone: () {
request.response.contentLength = 100;
for (int i = 0; i < 10; i++) {
request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
request.response.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
request.response.close();
});

View file

@ -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.
//
@ -22,7 +22,7 @@ void main() {
.then((request) {
// Keep a reference to the client request object.
clientRequest = request;
request.add([0]);
request.writeBytes([0]);
return request.response;
})
.then((response) {

View file

@ -19,9 +19,9 @@ main() {
var scriptDir = new Path(options.script).directoryPath;
var script = scriptDir.append('regress_7191_script.dart').toNativePath();
Process.start(executable, [script]).then((process) {
process.stdin.add([0]);
process.stdin.writeBytes([0]);
process.stdout.listen((_) { },
onDone: () { process.stdin.add([0]); });
onDone: () { process.stdin.writeBytes([0]); });
process.stderr.listen((_) { });
process.exitCode.then((exitCode) => port.close());
});

View file

@ -13,9 +13,9 @@ void main() {
HttpServer.bind().then((server) {
server.listen((request) {
request.response
..addString("first line\n")
..addString("")
..addString("second line\n")
..writeln("first line")
..write("")
..writeln("second line")
..close();
});

View file

@ -61,7 +61,7 @@ Future testClient(server) {
Completer success = new Completer();
List<String> chunks = <String>[];
SecureSocket.connect(HOST_NAME, server.port).then((socket) {
socket.add("Hello server.".codeUnits);
socket.write("Hello server.");
socket.close();
socket.listen(
(List<int> data) {

View file

@ -22,7 +22,7 @@ Future<SecureServerSocket> startEchoServer() {
server.listen((SecureSocket client) {
client.reduce(<int>[], (message, data) => message..addAll(data))
.then((message) {
client.add(message);
client.writeBytes(message);
client.close();
});
});
@ -32,7 +32,7 @@ Future<SecureServerSocket> startEchoServer() {
Future testClient(server) {
return SecureSocket.connect(HOST_NAME, server.port).then((socket) {
socket.add("Hello server.".codeUnits);
socket.write("Hello server.");
socket.close();
return socket.reduce(<int>[], (message, data) => message..addAll(data))
.then((message) {

View file

@ -25,7 +25,7 @@ Future<SecureServerSocket> startServer() {
String received = new String.fromCharCodes(message);
Expect.isTrue(received.contains("Hello from client "));
String name = received.substring(received.indexOf("client ") + 7);
client.add("Welcome, client $name".codeUnits);
client.writeBytes("Welcome, client $name".codeUnits);
client.close();
});
});
@ -35,7 +35,7 @@ Future<SecureServerSocket> startServer() {
Future testClient(server, name) {
return SecureSocket.connect(HOST_NAME, server.port).then((socket) {
socket.add("Hello from client $name".codeUnits);
socket.writeBytes("Hello from client $name".codeUnits);
socket.close();
return socket.reduce(<int>[], (message, data) => message..addAll(data))
.then((message) {

View file

@ -170,7 +170,7 @@ void testSimpleReadWrite() {
bytesRead += buffer.length;
if (bytesRead == data.length) {
verifyTestData(data);
client.add(data);
client.writeBytes(data);
client.close();
}
},
@ -184,7 +184,7 @@ void testSimpleReadWrite() {
int bytesWritten = 0;
List<int> dataSent = createTestData();
List<int> dataReceived = new List<int>(dataSent.length);
socket.add(dataSent);
socket.writeBytes(dataSent);
socket.close(); // Can also be delayed.
socket.listen(
(List<int> buffer) {

View file

@ -34,7 +34,7 @@ Future<SecureServerSocket> startServer() {
String received = new String.fromCharCodes(message);
Expect.isTrue(received.contains("Hello from client "));
String name = received.substring(received.indexOf("client ") + 7);
client.add("Welcome, client $name".codeUnits);
client.write("Welcome, client $name");
client.close();
});
});
@ -44,7 +44,7 @@ Future<SecureServerSocket> startServer() {
Future testClient(server, name) {
return SecureSocket.connect(HOST_NAME, server.port).then((socket) {
socket.add("Hello from client $name".codeUnits);
socket.write("Hello from client $name");
socket.close();
return socket.reduce(<int>[], (message, data) => message..addAll(data))
.then((message) {

View file

@ -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.
//
@ -49,7 +49,7 @@ Future testCertificateCallback({String host, bool acceptCertificate}) {
onBadCertificate: badCertificateCallback)
.then((socket) {
Expect.isTrue(acceptCertificate);
socket.add("GET / HTTP/1.0\r\nHost: $host\r\n\r\n".codeUnits);
socket.write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
socket.close();
return socket.reduce(<int>[], (message, data) => message..addAll(data))
.then((message) {

View file

@ -22,7 +22,7 @@ Future<HttpServer> startServer() {
onDone: () {
request.response.contentLength = 100;
for (int i = 0; i < 10; i++) {
request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
request.response.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
request.response.close();
});
@ -43,7 +43,7 @@ void main() {
List<int> body = <int>[];
startServer().then((server) {
SecureSocket.connect("localhost", server.port).then((socket) {
socket.add("GET / HTTP/1.0\r\nHost: localhost\r\n\r\n".codeUnits);
socket.write("GET / HTTP/1.0\r\nHost: localhost\r\n\r\n");
socket.close();
socket.listen(
(List<int> data) {

View file

@ -101,7 +101,7 @@ class SocketClose {
onError: (error) => errorHandler(socket));
void writeHello() {
socket.add("Hello".codeUnits);
socket.write("Hello");
}
_iterations++;
@ -224,7 +224,7 @@ class SocketCloseServer {
}
void writeHello() {
connection.add("Hello".codeUnits);
connection.write("Hello");
}
void dataHandler(bytes) {

View file

@ -88,7 +88,7 @@ class SocketExceptionTest {
Expect.isFalse(wrongExceptionCaught);
try {
List<int> buffer = new List<int>(10);
client.add(buffer);
client.writeBytes(buffer);
} on StateError catch (ex) {
exceptionCaught = true;
} catch (ex) {
@ -121,7 +121,7 @@ class SocketExceptionTest {
});
Socket.connect("127.0.0.1", server.port).then((client) {
client.listen((data) {}, onDone: server.close);
client.add(new List.filled(1024 * 1024, 0));
client.writeBytes(new List.filled(1024 * 1024, 0));
client.destroy();
});
});
@ -143,7 +143,7 @@ class SocketExceptionTest {
Expect.equals(SIZE, count);
server.close();
});
client.add(new List.filled(SIZE, 0));
client.writeBytes(new List.filled(SIZE, 0));
client.close();
// Start piping now.
completer.complete(null);
@ -174,7 +174,7 @@ class SocketExceptionTest {
Expect.isTrue(errors <= 1);
server.close();
});
client.add(new List.filled(SIZE, 0));
client.writeBytes(new List.filled(SIZE, 0));
// Destroy other socket now.
completer.complete(null);
var port = new ReceivePort();
@ -199,7 +199,7 @@ class SocketExceptionTest {
Socket.connect("127.0.0.1", server.port).then((client) {
const int SIZE = 1024 * 1024;
int errors = 0;
client.add(new List.filled(SIZE, 0));
client.writeBytes(new List.filled(SIZE, 0));
client.close();
client.done.catchError((error) {
server.close();

View file

@ -39,7 +39,7 @@ testAdd(buffer) {
socket.destroy();
server.close();
});
socket.add(buffer);
socket.writeBytes(buffer);
});
});
}

View file

@ -113,7 +113,7 @@ void testConnectConsumerWriteClose() {
ServerSocket.bind().then((server) {
server.listen((_) { });
Socket.connect("127.0.0.1", server.port).then((socket) {
socket.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
socket.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
socket.close();
socket.done.then((_) {
socket.destroy();
@ -155,7 +155,7 @@ void testConnectStreamDataClose(bool useDestroy) {
ServerSocket.bind().then((server) {
server.listen(
(client) {
client.add(sendData);
client.writeBytes(sendData);
if (useDestroy) {
client.destroy();
} else {
@ -171,7 +171,7 @@ void testConnectStreamDataClose(bool useDestroy) {
Expect.isFalse(onDoneCalled);
onDoneCalled = true;
if (!useDestroy) Expect.listEquals(sendData, data);
socket.add([0]);
socket.writeBytes([0]);
socket.close();
server.close();
port.close();
@ -186,7 +186,7 @@ void testConnectStreamDataCloseCancel(bool useDestroy) {
ServerSocket.bind().then((server) {
server.listen(
(client) {
client.add(sendData);
client.writeBytes(sendData);
if (useDestroy) {
client.destroy();
} else {

View file

@ -1,11 +1,11 @@
// 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.
import "dart:io";
main() {
stdout.add("Hello\n");
stdout.writeBytes("Hello\n");
stdout.done.catchError((e) {
exit(0);
});

View file

@ -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.
//
@ -99,8 +99,8 @@ testFileToFilePipe2() {
var dstFile = new File(dstFileName);
dstFile.createSync();
var output = dstFile.openWrite();
output.addStream(srcStream).then((_) {
output.add([32]);
output.writeStream(srcStream).then((_) {
output.writeBytes([32]);
output.close();
output.done.then((_) {
var src = srcFile.openSync();
@ -142,9 +142,9 @@ testFileToFilePipe3() {
var dstFile = new File(dstFileName);
dstFile.createSync();
var output = dstFile.openWrite();
output.addStream(srcStream).then((_) {
output.writeStream(srcStream).then((_) {
var srcStream2 = srcFile.openRead();
output.addStream(srcStream2).then((_) {
output.writeStream(srcStream2).then((_) {
output.close();
output.done.then((_) {
var src = srcFile.openSync();

View file

@ -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.
@ -58,7 +58,7 @@ main() {
expect(new String.fromCharCodes(bodyBytes), equals('email=email'));
response.headers.contentType = new ContentType("application", "json");
response.addString(json.stringify({
response.write(json.stringify({
'success': {'message': 'Good job!'}
}));
response.close();
@ -77,7 +77,7 @@ main() {
server.handle('DELETE', '/packages/pkg/uploaders/email.json',
(request, response) {
response.headers.contentType = new ContentType("application", "json");
response.addString(json.stringify({
response.write(json.stringify({
'success': {'message': 'Good job!'}
}));
response.close();
@ -97,7 +97,7 @@ main() {
server.handle('POST', '/packages/test_pkg/uploaders.json',
(request, response) {
response.headers.contentType = new ContentType("application", "json");
response.addString(json.stringify({
response.write(json.stringify({
'success': {'message': 'Good job!'}
}));
response.close();
@ -115,7 +115,7 @@ main() {
server.handle('POST', '/packages/pkg/uploaders.json', (request, response) {
response.statusCode = 400;
response.headers.contentType = new ContentType("application", "json");
response.addString(json.stringify({
response.write(json.stringify({
'error': {'message': 'Bad job!'}
}));
response.close();
@ -135,7 +135,7 @@ main() {
(request, response) {
response.statusCode = 400;
response.headers.contentType = new ContentType("application", "json");
response.addString(json.stringify({
response.write(json.stringify({
'error': {'message': 'Bad job!'}
}));
response.close();
@ -151,7 +151,7 @@ main() {
var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']);
server.handle('POST', '/packages/pkg/uploaders.json', (request, response) {
response.addString("{not json");
response.write("{not json");
response.close();
});
@ -167,7 +167,7 @@ main() {
server.handle('DELETE', '/packages/pkg/uploaders/email.json',
(request, response) {
response.addString("{not json");
response.write("{not json");
response.close();
});

View file

@ -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.
@ -121,7 +121,7 @@ void serve([List<Descriptor> contents]) {
stream.toBytes().then((data) {
response.statusCode = 200;
response.contentLength = data.length;
response.add(data);
response.writeBytes(data);
response.close();
}).catchError((e) {
print("Exception while handling ${request.uri}: $e");
@ -1439,7 +1439,7 @@ class ScheduledProcess {
/// Writes [line] to the process as stdin.
void writeLine(String line) {
_schedule((_) => _processFuture.then(
(p) => p.stdin.add('$line\n'.codeUnits)));
(p) => p.stdin.writeln('$line')));
}
/// Kills the process, and waits until it's dead.