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 // Read the file in blocks of size 64k. | 7 // Read the file in blocks of size 64k. |
8 const int _BLOCK_SIZE = 64 * 1024; | 8 const int _BLOCK_SIZE = 64 * 1024; |
9 | 9 |
10 | 10 |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 var result = _fullPath(path); | 459 var result = _fullPath(path); |
460 throwIfError(result, "Cannot retrieve full path", path); | 460 throwIfError(result, "Cannot retrieve full path", path); |
461 return result; | 461 return result; |
462 } | 462 } |
463 | 463 |
464 Stream<List<int>> openRead([int start, int end]) { | 464 Stream<List<int>> openRead([int start, int end]) { |
465 return new _FileStream(path, start, end); | 465 return new _FileStream(path, start, end); |
466 } | 466 } |
467 | 467 |
468 IOSink openWrite({FileMode mode: FileMode.WRITE, | 468 IOSink openWrite({FileMode mode: FileMode.WRITE, |
469 Encoding encoding: Encoding.UTF_8}) { | 469 Encoding encoding: UTF8}) { |
470 if (mode != FileMode.WRITE && | 470 if (mode != FileMode.WRITE && |
471 mode != FileMode.APPEND) { | 471 mode != FileMode.APPEND) { |
472 throw new ArgumentError( | 472 throw new ArgumentError( |
473 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); | 473 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); |
474 } | 474 } |
475 var consumer = new _FileStreamConsumer(this, mode); | 475 var consumer = new _FileStreamConsumer(this, mode); |
476 return new IOSink(consumer, encoding: encoding); | 476 return new IOSink(consumer, encoding: encoding); |
477 } | 477 } |
478 | 478 |
479 Future<List<int>> readAsBytes() { | 479 Future<List<int>> readAsBytes() { |
(...skipping 16 matching lines...) Expand all Loading... |
496 var opened = openSync(); | 496 var opened = openSync(); |
497 var builder = new BytesBuilder(); | 497 var builder = new BytesBuilder(); |
498 var data; | 498 var data; |
499 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { | 499 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { |
500 builder.add(data); | 500 builder.add(data); |
501 } | 501 } |
502 opened.closeSync(); | 502 opened.closeSync(); |
503 return builder.takeBytes(); | 503 return builder.takeBytes(); |
504 } | 504 } |
505 | 505 |
506 Future<String> readAsString({Encoding encoding: Encoding.UTF_8}) { | 506 Future<String> readAsString({Encoding encoding: UTF8}) { |
507 _ensureFileService(); | 507 _ensureFileService(); |
508 return readAsBytes().then((bytes) { | 508 return readAsBytes().then((bytes) { |
509 return _decodeString(bytes, encoding); | 509 return encoding.decode(bytes); |
510 }); | 510 }); |
511 } | 511 } |
512 | 512 |
513 String readAsStringSync({Encoding encoding: Encoding.UTF_8}) { | 513 String readAsStringSync({Encoding encoding: UTF8}) { |
514 List<int> bytes = readAsBytesSync(); | 514 List<int> bytes = readAsBytesSync(); |
515 return _decodeString(bytes, encoding); | 515 return encoding.decode(bytes); |
516 } | 516 } |
517 | 517 |
518 static List<String> _decodeLines(List<int> bytes, Encoding encoding) { | 518 static List<String> _decodeLines(List<int> bytes, Encoding encoding) { |
519 if (bytes.length == 0) return []; | 519 if (bytes.length == 0) return []; |
520 var list = []; | 520 var list = []; |
521 var controller = new StreamController(sync: true); | 521 var controller = new StreamController(sync: true); |
522 controller.stream | 522 controller.stream |
523 .transform(new StringDecoder(encoding)) | 523 .transform(encoding.decoder) |
524 .transform(new LineSplitter()) | 524 .transform(new LineSplitter()) |
525 .listen((line) => list.add(line)); | 525 .listen((line) => list.add(line)); |
526 controller.add(bytes); | 526 controller.add(bytes); |
527 controller.close(); | 527 controller.close(); |
528 return list; | 528 return list; |
529 } | 529 } |
530 | 530 |
531 Future<List<String>> readAsLines({Encoding encoding: Encoding.UTF_8}) { | 531 Future<List<String>> readAsLines({Encoding encoding: UTF8}) { |
532 _ensureFileService(); | 532 _ensureFileService(); |
533 return readAsBytes().then((bytes) { | 533 return readAsBytes().then((bytes) { |
534 return _decodeLines(bytes, encoding); | 534 return _decodeLines(bytes, encoding); |
535 }); | 535 }); |
536 } | 536 } |
537 | 537 |
538 List<String> readAsLinesSync({Encoding encoding: Encoding.UTF_8}) { | 538 List<String> readAsLinesSync({Encoding encoding: UTF8}) { |
539 return _decodeLines(readAsBytesSync(), encoding); | 539 return _decodeLines(readAsBytesSync(), encoding); |
540 } | 540 } |
541 | 541 |
542 Future<File> writeAsBytes(List<int> bytes, | 542 Future<File> writeAsBytes(List<int> bytes, |
543 {FileMode mode: FileMode.WRITE}) { | 543 {FileMode mode: FileMode.WRITE}) { |
544 try { | 544 try { |
545 IOSink sink = openWrite(mode: mode); | 545 IOSink sink = openWrite(mode: mode); |
546 sink.add(bytes); | 546 sink.add(bytes); |
547 sink.close(); | 547 sink.close(); |
548 return sink.done.then((_) => this); | 548 return sink.done.then((_) => this); |
549 } catch (e) { | 549 } catch (e) { |
550 return new Future.error(e); | 550 return new Future.error(e); |
551 } | 551 } |
552 } | 552 } |
553 | 553 |
554 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { | 554 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { |
555 RandomAccessFile opened = openSync(mode: mode); | 555 RandomAccessFile opened = openSync(mode: mode); |
556 opened.writeFromSync(bytes, 0, bytes.length); | 556 opened.writeFromSync(bytes, 0, bytes.length); |
557 opened.closeSync(); | 557 opened.closeSync(); |
558 } | 558 } |
559 | 559 |
560 Future<File> writeAsString(String contents, | 560 Future<File> writeAsString(String contents, |
561 {FileMode mode: FileMode.WRITE, | 561 {FileMode mode: FileMode.WRITE, |
562 Encoding encoding: Encoding.UTF_8}) { | 562 Encoding encoding: UTF8}) { |
563 try { | 563 try { |
564 return writeAsBytes(_encodeString(contents, encoding), mode: mode); | 564 return writeAsBytes(encoding.encode(contents), mode: mode); |
565 } catch (e) { | 565 } catch (e) { |
566 return new Future.error(e); | 566 return new Future.error(e); |
567 } | 567 } |
568 } | 568 } |
569 | 569 |
570 void writeAsStringSync(String contents, | 570 void writeAsStringSync(String contents, |
571 {FileMode mode: FileMode.WRITE, | 571 {FileMode mode: FileMode.WRITE, |
572 Encoding encoding: Encoding.UTF_8}) { | 572 Encoding encoding: UTF8}) { |
573 writeAsBytesSync(_encodeString(contents, encoding), mode: mode); | 573 writeAsBytesSync(encoding.encode(contents), mode: mode); |
574 } | 574 } |
575 | 575 |
576 String toString() => "File: '$path'"; | 576 String toString() => "File: '$path'"; |
577 | 577 |
578 void _ensureFileService() { | 578 void _ensureFileService() { |
579 if (_fileService == null) { | 579 if (_fileService == null) { |
580 _fileService = _FileUtils._newServicePort(); | 580 _fileService = _FileUtils._newServicePort(); |
581 } | 581 } |
582 } | 582 } |
583 | 583 |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
817 var result = _writeFrom(_id, | 817 var result = _writeFrom(_id, |
818 bufferAndStart.buffer, | 818 bufferAndStart.buffer, |
819 bufferAndStart.start, | 819 bufferAndStart.start, |
820 end - (start - bufferAndStart.start)); | 820 end - (start - bufferAndStart.start)); |
821 if (result is OSError) { | 821 if (result is OSError) { |
822 throw new FileException("writeFrom failed", path, result); | 822 throw new FileException("writeFrom failed", path, result); |
823 } | 823 } |
824 } | 824 } |
825 | 825 |
826 Future<RandomAccessFile> writeString(String string, | 826 Future<RandomAccessFile> writeString(String string, |
827 {Encoding encoding: Encoding.UTF_8}) { | 827 {Encoding encoding: UTF8}) { |
828 if (encoding is! Encoding) { | 828 if (encoding is! Encoding) { |
829 throw new ArgumentError(encoding); | 829 throw new ArgumentError(encoding); |
830 } | 830 } |
831 var data = _encodeString(string, encoding); | 831 var data = encoding.encode(string); |
832 return writeFrom(data, 0, data.length); | 832 return writeFrom(data, 0, data.length); |
833 } | 833 } |
834 | 834 |
835 void writeStringSync(String string, {Encoding encoding: Encoding.UTF_8}) { | 835 void writeStringSync(String string, {Encoding encoding: UTF8}) { |
836 if (encoding is! Encoding) { | 836 if (encoding is! Encoding) { |
837 throw new ArgumentError(encoding); | 837 throw new ArgumentError(encoding); |
838 } | 838 } |
839 var data = _encodeString(string, encoding); | 839 var data = encoding.encode(string); |
840 writeFromSync(data, 0, data.length); | 840 writeFromSync(data, 0, data.length); |
841 } | 841 } |
842 | 842 |
843 Future<int> position() { | 843 Future<int> position() { |
844 _ensureFileService(); | 844 _ensureFileService(); |
845 if (closed) return _closedException(); | 845 if (closed) return _closedException(); |
846 List request = new List(2); | 846 List request = new List(2); |
847 request[0] = _POSITION_REQUEST; | 847 request[0] = _POSITION_REQUEST; |
848 request[1] = _id; | 848 request[1] = _id; |
849 return _fileService.call(request).then((response) { | 849 return _fileService.call(request).then((response) { |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 void _checkNotClosed() { | 977 void _checkNotClosed() { |
978 if (closed) { | 978 if (closed) { |
979 throw new FileException("File closed", path); | 979 throw new FileException("File closed", path); |
980 } | 980 } |
981 } | 981 } |
982 | 982 |
983 Future _closedException() { | 983 Future _closedException() { |
984 return new Future.error(new FileException("File closed", path)); | 984 return new Future.error(new FileException("File closed", path)); |
985 } | 985 } |
986 } | 986 } |
OLD | NEW |