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

Side by Side Diff: sdk/lib/io/file_impl.dart

Issue 230513002: Fix leaking FD from readAs*Sync and writeAs*Sync. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 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 | 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) 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 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 // May be character device, try to read it in chunks. 454 // May be character device, try to read it in chunks.
455 return readDataChunked(file); 455 return readDataChunked(file);
456 } 456 }
457 return file.read(length); 457 return file.read(length);
458 }).whenComplete(file.close); 458 }).whenComplete(file.close);
459 }); 459 });
460 } 460 }
461 461
462 List<int> readAsBytesSync() { 462 List<int> readAsBytesSync() {
463 var opened = openSync(); 463 var opened = openSync();
464 var data; 464 try {
465 var length = opened.lengthSync(); 465 var data;
466 if (length == 0) { 466 var length = opened.lengthSync();
467 // May be character device, try to read it in chunks. 467 if (length == 0) {
468 var builder = new BytesBuilder(copy: false); 468 // May be character device, try to read it in chunks.
469 do { 469 var builder = new BytesBuilder(copy: false);
470 data = opened.readSync(_BLOCK_SIZE); 470 do {
471 if (data.length > 0) builder.add(data); 471 data = opened.readSync(_BLOCK_SIZE);
472 } while (data.length == _BLOCK_SIZE); 472 if (data.length > 0) builder.add(data);
473 data = builder.takeBytes(); 473 } while (data.length == _BLOCK_SIZE);
474 } else { 474 data = builder.takeBytes();
475 data = opened.readSync(length); 475 } else {
476 data = opened.readSync(length);
477 }
478 return data;
479 } finally {
480 opened.closeSync();
476 } 481 }
477 opened.closeSync();
478 return data;
479 } 482 }
480 483
481 String _tryDecode(List<int> bytes, Encoding encoding) { 484 String _tryDecode(List<int> bytes, Encoding encoding) {
482 try { 485 try {
483 return encoding.decode(bytes); 486 return encoding.decode(bytes);
484 } catch (_) { 487 } catch (_) {
485 throw new FileSystemException( 488 throw new FileSystemException(
486 "Failed to decode data using encoding '${encoding.name}'", path); 489 "Failed to decode data using encoding '${encoding.name}'", path);
487 } 490 }
488 } 491 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 return this; 535 return this;
533 }) 536 })
534 .whenComplete(file.close); 537 .whenComplete(file.close);
535 }); 538 });
536 } 539 }
537 540
538 void writeAsBytesSync(List<int> bytes, 541 void writeAsBytesSync(List<int> bytes,
539 {FileMode mode: FileMode.WRITE, 542 {FileMode mode: FileMode.WRITE,
540 bool flush: false}) { 543 bool flush: false}) {
541 RandomAccessFile opened = openSync(mode: mode); 544 RandomAccessFile opened = openSync(mode: mode);
542 opened.writeFromSync(bytes, 0, bytes.length); 545 try {
543 if (flush) opened.flushSync(); 546 opened.writeFromSync(bytes, 0, bytes.length);
544 opened.closeSync(); 547 if (flush) opened.flushSync();
548 } finally {
549 opened.closeSync();
550 }
545 } 551 }
546 552
547 Future<File> writeAsString(String contents, 553 Future<File> writeAsString(String contents,
548 {FileMode mode: FileMode.WRITE, 554 {FileMode mode: FileMode.WRITE,
549 Encoding encoding: UTF8, 555 Encoding encoding: UTF8,
550 bool flush: false}) { 556 bool flush: false}) {
551 try { 557 try {
552 return writeAsBytes(encoding.encode(contents), mode: mode, flush: flush); 558 return writeAsBytes(encoding.encode(contents), mode: mode, flush: flush);
553 } catch (e) { 559 } catch (e) {
554 return new Future.error(e); 560 return new Future.error(e);
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
915 void _checkAvailable() { 921 void _checkAvailable() {
916 if (_asyncDispatched) { 922 if (_asyncDispatched) {
917 throw new FileSystemException("An async operation is currently pending", 923 throw new FileSystemException("An async operation is currently pending",
918 path); 924 path);
919 } 925 }
920 if (closed) { 926 if (closed) {
921 throw new FileSystemException("File closed", path); 927 throw new FileSystemException("File closed", path);
922 } 928 }
923 } 929 }
924 } 930 }
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