OLD | NEW |
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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * String encodings. | 8 * String encodings. |
9 */ | 9 */ |
10 class Encoding { | 10 class Encoding { |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 } | 160 } |
161 | 161 |
162 | 162 |
163 class LineTransformer extends StreamEventTransformer<String, String> { | 163 class LineTransformer extends StreamEventTransformer<String, String> { |
164 const int _LF = 10; | 164 const int _LF = 10; |
165 const int _CR = 13; | 165 const int _CR = 13; |
166 | 166 |
167 StringBuffer _buffer = new StringBuffer(); | 167 StringBuffer _buffer = new StringBuffer(); |
168 String _carry; | 168 String _carry; |
169 | 169 |
170 void _handle(String data, StreamSink<String> sink, bool isClosing) { | 170 void _handle(String data, EventSink<String> sink, bool isClosing) { |
171 if (_carry != null) { | 171 if (_carry != null) { |
172 data = _carry.concat(data); | 172 data = _carry.concat(data); |
173 _carry = null; | 173 _carry = null; |
174 } | 174 } |
175 int startPos = 0; | 175 int startPos = 0; |
176 int pos = 0; | 176 int pos = 0; |
177 while (pos < data.length) { | 177 while (pos < data.length) { |
178 int skip = 0; | 178 int skip = 0; |
179 int char = data.codeUnitAt(pos); | 179 int char = data.codeUnitAt(pos); |
180 if (char == _LF) { | 180 if (char == _LF) { |
(...skipping 21 matching lines...) Expand all Loading... |
202 if (pos != startPos) { | 202 if (pos != startPos) { |
203 // Add remaining | 203 // Add remaining |
204 _buffer.write(data.substring(startPos, pos)); | 204 _buffer.write(data.substring(startPos, pos)); |
205 } | 205 } |
206 if (isClosing && !_buffer.isEmpty) { | 206 if (isClosing && !_buffer.isEmpty) { |
207 sink.add(_buffer.toString()); | 207 sink.add(_buffer.toString()); |
208 _buffer = new StringBuffer(); | 208 _buffer = new StringBuffer(); |
209 } | 209 } |
210 } | 210 } |
211 | 211 |
212 void handleData(String data, StreamSink<String> sink) { | 212 void handleData(String data, EventSink<String> sink) { |
213 _handle(data, sink, false); | 213 _handle(data, sink, false); |
214 } | 214 } |
215 | 215 |
216 void handleDone(StreamSink<String> sink) { | 216 void handleDone(EventSink<String> sink) { |
217 _handle("", sink, true); | 217 _handle("", sink, true); |
218 sink.close(); | 218 sink.close(); |
219 } | 219 } |
220 } | 220 } |
221 | 221 |
222 | 222 |
223 abstract class _SingleByteDecoder | 223 abstract class _SingleByteDecoder |
224 extends StreamEventTransformer<List<int>, String> { | 224 extends StreamEventTransformer<List<int>, String> { |
225 final int _replacementChar; | 225 final int _replacementChar; |
226 | 226 |
227 _SingleByteDecoder(this._replacementChar); | 227 _SingleByteDecoder(this._replacementChar); |
228 | 228 |
229 void handleData(List<int> data, StreamSink<String> sink) { | 229 void handleData(List<int> data, EventSink<String> sink) { |
230 var buffer = new List<int>(data.length); | 230 var buffer = new List<int>(data.length); |
231 for (int i = 0; i < data.length; i++) { | 231 for (int i = 0; i < data.length; i++) { |
232 int char = _decodeByte(data[i]); | 232 int char = _decodeByte(data[i]); |
233 if (char < 0) char = _replacementChar; | 233 if (char < 0) char = _replacementChar; |
234 buffer[i] = char; | 234 buffer[i] = char; |
235 } | 235 } |
236 sink.add(new String.fromCharCodes(buffer)); | 236 sink.add(new String.fromCharCodes(buffer)); |
237 } | 237 } |
238 | 238 |
239 int _decodeByte(int byte); | 239 int _decodeByte(int byte); |
(...skipping 13 matching lines...) Expand all Loading... |
253 // bytes. | 253 // bytes. |
254 class _Latin1Decoder extends _SingleByteDecoder { | 254 class _Latin1Decoder extends _SingleByteDecoder { |
255 _Latin1Decoder(int replacementChar) : super(replacementChar); | 255 _Latin1Decoder(int replacementChar) : super(replacementChar); |
256 | 256 |
257 int _decodeByte(int byte) => ((byte & 0xFF) == byte) ? byte : -1; | 257 int _decodeByte(int byte) => ((byte & 0xFF) == byte) ? byte : -1; |
258 } | 258 } |
259 | 259 |
260 | 260 |
261 abstract class _SingleByteEncoder | 261 abstract class _SingleByteEncoder |
262 extends StreamEventTransformer<String, List<int>> { | 262 extends StreamEventTransformer<String, List<int>> { |
263 void handleData(String data, StreamSink<List<int>> sink) { | 263 void handleData(String data, EventSink<List<int>> sink) { |
264 var bytes = _encode(data); | 264 var bytes = _encode(data); |
265 if (bytes == null) { | 265 if (bytes == null) { |
266 sink.signalError( | 266 sink.addError( |
267 new AsyncError( | 267 new AsyncError( |
268 new FormatException("Invalid character for encoding"))); | 268 new FormatException("Invalid character for encoding"))); |
269 sink.close(); | 269 sink.close(); |
270 } else { | 270 } else { |
271 sink.add(bytes); | 271 sink.add(bytes); |
272 } | 272 } |
273 } | 273 } |
274 | 274 |
275 List<int> _encode(String string); | 275 List<int> _encode(String string); |
276 } | 276 } |
(...skipping 30 matching lines...) Expand all Loading... |
307 class _WindowsCodePageEncoder extends _SingleByteEncoder { | 307 class _WindowsCodePageEncoder extends _SingleByteEncoder { |
308 List<int> _encode(String string) => _encodeString(string); | 308 List<int> _encode(String string) => _encodeString(string); |
309 | 309 |
310 external static List<int> _encodeString(String string); | 310 external static List<int> _encodeString(String string); |
311 } | 311 } |
312 | 312 |
313 | 313 |
314 // Utility class for decoding Windows current code page data delivered | 314 // Utility class for decoding Windows current code page data delivered |
315 // as a stream of bytes. | 315 // as a stream of bytes. |
316 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String>
{ | 316 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String>
{ |
317 void handleData(List<int> data, StreamSink<String> sink) { | 317 void handleData(List<int> data, EventSink<String> sink) { |
318 sink.add(_decodeBytes(data)); | 318 sink.add(_decodeBytes(data)); |
319 } | 319 } |
320 | 320 |
321 external static String _decodeBytes(List<int> bytes); | 321 external static String _decodeBytes(List<int> bytes); |
322 } | 322 } |
OLD | NEW |