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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/file_impl.dart
diff --git a/sdk/lib/io/file_impl.dart b/sdk/lib/io/file_impl.dart
index 05c33f605b68581652762c514dd82c05b6dd9c90..a262c21ca2a50afe9f519febe2c089e5c7bf1b80 100644
--- a/sdk/lib/io/file_impl.dart
+++ b/sdk/lib/io/file_impl.dart
@@ -461,21 +461,24 @@ class _File extends FileSystemEntity implements File {
List<int> readAsBytesSync() {
var opened = openSync();
- var data;
- var length = opened.lengthSync();
- if (length == 0) {
- // May be character device, try to read it in chunks.
- var builder = new BytesBuilder(copy: false);
- do {
- data = opened.readSync(_BLOCK_SIZE);
- if (data.length > 0) builder.add(data);
- } while (data.length == _BLOCK_SIZE);
- data = builder.takeBytes();
- } else {
- data = opened.readSync(length);
+ try {
+ var data;
+ var length = opened.lengthSync();
+ if (length == 0) {
+ // May be character device, try to read it in chunks.
+ var builder = new BytesBuilder(copy: false);
+ do {
+ data = opened.readSync(_BLOCK_SIZE);
+ if (data.length > 0) builder.add(data);
+ } while (data.length == _BLOCK_SIZE);
+ data = builder.takeBytes();
+ } else {
+ data = opened.readSync(length);
+ }
+ return data;
+ } finally {
+ opened.closeSync();
}
- opened.closeSync();
- return data;
}
String _tryDecode(List<int> bytes, Encoding encoding) {
@@ -539,9 +542,12 @@ class _File extends FileSystemEntity implements File {
{FileMode mode: FileMode.WRITE,
bool flush: false}) {
RandomAccessFile opened = openSync(mode: mode);
- opened.writeFromSync(bytes, 0, bytes.length);
- if (flush) opened.flushSync();
- opened.closeSync();
+ try {
+ opened.writeFromSync(bytes, 0, bytes.length);
+ if (flush) opened.flushSync();
+ } finally {
+ opened.closeSync();
+ }
}
Future<File> writeAsString(String contents,
« 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