Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(365)

Side by Side Diff: pkg/barback/lib/src/asset.dart

Issue 18478004: Make Asset.readAsString() async. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/barback/test/utils.dart » ('j') | pkg/barback/test/utils.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library barback.asset; 5 library barback.asset;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 9
10 /// A blob of content. 10 /// A blob of content.
(...skipping 18 matching lines...) Expand all
29 factory Asset.deserialize(data) { 29 factory Asset.deserialize(data) {
30 // TODO(rnystrom): Handle errors. 30 // TODO(rnystrom): Handle errors.
31 switch (data[0]) { 31 switch (data[0]) {
32 case "file": return new _FileAsset(new File(data[1])); break; 32 case "file": return new _FileAsset(new File(data[1])); break;
33 case "string": return new _StringAsset(data[1]); break; 33 case "string": return new _StringAsset(data[1]); break;
34 } 34 }
35 } 35 }
36 36
37 /// Returns the contents of the asset as a string. 37 /// Returns the contents of the asset as a string.
38 // TODO(rnystrom): Figure out how binary assets should be handled. 38 // TODO(rnystrom): Figure out how binary assets should be handled.
39 String readAsString(); 39 Future<String> readAsString();
40 40
41 /// Streams the contents of the asset. 41 /// Streams the contents of the asset.
42 Stream<List<int>> read(); 42 Stream<List<int>> read();
43 43
44 /// Serializes this [Asset] to an object that can be sent across isolates 44 /// Serializes this [Asset] to an object that can be sent across isolates
45 /// and passed to [deserialize]. 45 /// and passed to [deserialize].
46 Object serialize(); 46 Object serialize();
47 } 47 }
48 48
49 /// An asset backed by a file on the local file system. 49 /// An asset backed by a file on the local file system.
50 class _FileAsset implements Asset { 50 class _FileAsset implements Asset {
51 final File _file; 51 final File _file;
52 _FileAsset(this._file); 52 _FileAsset(this._file);
53 53
54 String readAsString() => _file.readAsStringSync(); 54 Future<String> readAsString() => _file.readAsString();
55 Stream<List<int>> read() => _file.openRead(); 55 Stream<List<int>> read() => _file.openRead();
56 56
57 String toString() => 'File "${_file.path}"'; 57 String toString() => 'File "${_file.path}"';
58 58
59 Object serialize() => ["file", _file.path]; 59 Object serialize() => ["file", _file.path];
60 } 60 }
61 61
62 /// An asset whose data is stored in a string. 62 /// An asset whose data is stored in a string.
63 // TODO(rnystrom): Have something similar for in-memory binary assets. 63 // TODO(rnystrom): Have something similar for in-memory binary assets.
64 class _StringAsset implements Asset { 64 class _StringAsset implements Asset {
65 final String _contents; 65 final String _contents;
66 66
67 _StringAsset(this._contents); 67 _StringAsset(this._contents);
68 68
69 String readAsString() => _contents; 69 Future<String> readAsString() => new Future.value(_contents);
70 70
71 // TODO(rnystrom): Implement this and handle encoding. 71 // TODO(rnystrom): Implement this and handle encoding.
72 Stream<List<int>> read() => throw new UnimplementedError(); 72 Stream<List<int>> read() => throw new UnimplementedError();
73 73
74 String toString() { 74 String toString() {
75 // Don't show the whole string if it's long. 75 // Don't show the whole string if it's long.
76 var contents = _contents; 76 var contents = _contents;
77 if (contents.length > 40) { 77 if (contents.length > 40) {
78 contents = contents.substring(0, 20) + " ... " + 78 contents = contents.substring(0, 20) + " ... " +
79 contents.substring(contents.length - 20); 79 contents.substring(contents.length - 20);
80 } 80 }
81 81
82 contents = _escape(contents); 82 contents = _escape(contents);
83 return 'String "$contents"'; 83 return 'String "$contents"';
84 } 84 }
85 85
86 Object serialize() => ["string", _contents]; 86 Object serialize() => ["string", _contents];
87 87
88 String _escape(String string) { 88 String _escape(String string) {
89 return string 89 return string
90 .replaceAll("\"", r'\"') 90 .replaceAll("\"", r'\"')
91 .replaceAll("\n", r"\n") 91 .replaceAll("\n", r"\n")
92 .replaceAll("\r", r"\r") 92 .replaceAll("\r", r"\r")
93 .replaceAll("\t", r"\t"); 93 .replaceAll("\t", r"\t");
94 } 94 }
95 } 95 }
OLDNEW
« no previous file with comments | « no previous file | pkg/barback/test/utils.dart » ('j') | pkg/barback/test/utils.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698