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

Side by Side Diff: runtime/bin/string_stream.dart

Issue 8639004: Fix excess memory usage problem in StringInputStream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years 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 | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 // Interface for decoders decoding binary data into objects of type T. 5 // Interface for decoders decoding binary data into objects of type T.
6 interface _Decoder<T> { 6 interface _Decoder<T> {
7 // Add more binary data to be decoded. The ownership of the buffer 7 // Add more binary data to be decoded. The ownership of the buffer
8 // is transfered to the decoder and the caller most not modify it any more. 8 // is transfered to the decoder and the caller most not modify it any more.
9 int write(List<int> buffer); 9 int write(List<int> buffer);
10 10
(...skipping 10 matching lines...) Expand all
21 String toString() => "DecoderException: $message"; 21 String toString() => "DecoderException: $message";
22 final String message; 22 final String message;
23 } 23 }
24 24
25 25
26 // Utility class for decoding UTF-8 from data delivered as a stream of 26 // Utility class for decoding UTF-8 from data delivered as a stream of
27 // bytes. 27 // bytes.
28 class _StringDecoderBase implements _Decoder<String> { 28 class _StringDecoderBase implements _Decoder<String> {
29 _StringDecoderBase() 29 _StringDecoderBase()
30 : _bufferList = new _BufferList(), 30 : _bufferList = new _BufferList(),
31 _result = new StringBuffer(); 31 _result = new List<int>();
32 32
33 // Add UTF-8 encoded data. 33 // Add UTF-8 encoded data.
34 int write(List<int> buffer) { 34 int write(List<int> buffer) {
35 _bufferList.add(buffer); 35 _bufferList.add(buffer);
36 // Decode as many bytes into characters as possible. 36 // Decode as many bytes into characters as possible.
37 while (_bufferList.length > 0) { 37 while (_bufferList.length > 0) {
38 if (!_processNext()) { 38 if (!_processNext()) {
39 break; 39 break;
40 } 40 }
41 } 41 }
42 return buffer.length; 42 return buffer.length;
43 } 43 }
44 44
45 // Check if any characters have been decoded since the last call to decode. 45 // Check if any characters have been decoded since the last call to decode.
46 bool isEmpty() { 46 bool isEmpty() {
47 return _result.isEmpty(); 47 return _result.isEmpty();
48 } 48 }
49 49
50 // Return the string decoded since the last call to decode. 50 // Return the string decoded since the last call to decode.
51 String get decoded() { 51 String get decoded() {
52 if (isEmpty()) { 52 if (isEmpty()) {
53 return null; 53 return null;
54 } else { 54 } else {
55 String result = _result.toString(); 55 String result = new String.fromCharCodes(_result);
56 _result = new StringBuffer(); 56 _result = new List<int>();
57 return result; 57 return result;
58 } 58 }
59 } 59 }
60 60
61 abstract bool _processNext(); 61 abstract bool _processNext();
62 62
63 _BufferList _bufferList; 63 _BufferList _bufferList;
64 StringBuffer _result; 64 List<int> _result;
65 } 65 }
66 66
67 67
68 // Utility class for decoding ascii data delivered as a stream of 68 // Utility class for decoding ascii data delivered as a stream of
69 // bytes. 69 // bytes.
70 class _AsciiDecoder extends _StringDecoderBase { 70 class _AsciiDecoder extends _StringDecoderBase {
71 // Process the next ascii encoded character. 71 // Process the next ascii encoded character.
72 bool _processNext() { 72 bool _processNext() {
73 while (_bufferList.length > 0) { 73 while (_bufferList.length > 0) {
74 int byte = _bufferList.next(); 74 int byte = _bufferList.next();
75 if (byte > 127) { 75 if (byte > 127) {
76 throw new DecoderException("Illegal ASCII character $byte"); 76 throw new DecoderException("Illegal ASCII character $byte");
77 } 77 }
78 _result.addCharCode(byte); 78 _result.add(byte);
79 } 79 }
80 return true; 80 return true;
81 } 81 }
82 } 82 }
83 83
84 84
85 // Utility class for decoding Latin-1 data delivered as a stream of 85 // Utility class for decoding Latin-1 data delivered as a stream of
86 // bytes. 86 // bytes.
87 class _Latin1Decoder extends _StringDecoderBase { 87 class _Latin1Decoder extends _StringDecoderBase {
88 // Process the next Latin-1 encoded character. 88 // Process the next Latin-1 encoded character.
89 bool _processNext() { 89 bool _processNext() {
90 while (_bufferList.length > 0) { 90 while (_bufferList.length > 0) {
91 int byte = _bufferList.next(); 91 int byte = _bufferList.next();
92 _result.addCharCode(byte); 92 _result.add(byte);
93 } 93 }
94 return true; 94 return true;
95 } 95 }
96 } 96 }
97 97
98 98
99 // Utility class for decoding UTF-8 from data delivered as a stream of 99 // Utility class for decoding UTF-8 from data delivered as a stream of
100 // bytes. 100 // bytes.
101 class _UTF8Decoder extends _StringDecoderBase { 101 class _UTF8Decoder extends _StringDecoderBase {
102 // Process the next UTF-8 encoded character. 102 // Process the next UTF-8 encoded character.
(...skipping 21 matching lines...) Expand all
124 // Remove the value peeked from the buffer list. 124 // Remove the value peeked from the buffer list.
125 _bufferList.next(); 125 _bufferList.next();
126 for (int i = 0; i < additionalBytes; i++) { 126 for (int i = 0; i < additionalBytes; i++) {
127 int byte = _bufferList.next(); 127 int byte = _bufferList.next();
128 value = value << 6 | (byte & 0x3F); 128 value = value << 6 | (byte & 0x3F);
129 } 129 }
130 } else { 130 } else {
131 // Remove the value peeked from the buffer list. 131 // Remove the value peeked from the buffer list.
132 _bufferList.next(); 132 _bufferList.next();
133 } 133 }
134 _result.addCharCode(value); 134 _result.add(value);
135 return true; 135 return true;
136 } 136 }
137 } 137 }
138 138
139 139
140 class _StringInputStream implements StringInputStream { 140 class _StringInputStream implements StringInputStream {
141 _StringInputStream(InputStream this._input, [String this._encoding]) { 141 _StringInputStream(InputStream this._input, [String this._encoding]) {
142 if (_encoding === null) { 142 if (_encoding === null) {
143 _encoding = "UTF-8"; 143 _encoding = "UTF-8";
144 } 144 }
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 String _encoding; 313 String _encoding;
314 _Decoder _decoder; 314 _Decoder _decoder;
315 String _buffer; // String can be buffered here if readLine is used. 315 String _buffer; // String can be buffered here if readLine is used.
316 int _bufferLineStart; // Current offset into _buffer if any. 316 int _bufferLineStart; // Current offset into _buffer if any.
317 bool _inputClosed = false; // Is the underlying input stream closed? 317 bool _inputClosed = false; // Is the underlying input stream closed?
318 bool _closed = false; // Is this stream closed. 318 bool _closed = false; // Is this stream closed.
319 bool _eof = false; // Has all data been read from the decoder? 319 bool _eof = false; // Has all data been read from the decoder?
320 var _clientDataHandler; 320 var _clientDataHandler;
321 var _clientCloseHandler; 321 var _clientCloseHandler;
322 } 322 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698