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

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

Issue 1193653002: Add file modes for opening a file write only (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Added Mac, Android and WIndows implementations Created 5 years, 6 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
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 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 337
338 File copySync(String newPath) { 338 File copySync(String newPath) {
339 var result = _copy(path, newPath); 339 var result = _copy(path, newPath);
340 throwIfError(result, "Cannot copy file to '$newPath'", path); 340 throwIfError(result, "Cannot copy file to '$newPath'", path);
341 return new File(newPath); 341 return new File(newPath);
342 } 342 }
343 343
344 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) { 344 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) {
345 if (mode != FileMode.READ && 345 if (mode != FileMode.READ &&
346 mode != FileMode.WRITE && 346 mode != FileMode.WRITE &&
347 mode != FileMode.APPEND) { 347 mode != FileMode.APPEND &&
348 return new Future.error(new ArgumentError()); 348 mode != FileMode.WRITE_ONLY &&
349 mode != FileMode.WRITE_ONLY_APPEND) {
350 return new Future.error(
351 new ArgumentError('Invalid file mode for this operation'));
349 } 352 }
350 return _IOService._dispatch(_FILE_OPEN, [path, mode._mode]) 353 return _IOService._dispatch(_FILE_OPEN, [path, mode._mode])
351 .then((response) { 354 .then((response) {
352 if (_isErrorResponse(response)) { 355 if (_isErrorResponse(response)) {
353 throw _exceptionFromResponse(response, "Cannot open file", path); 356 throw _exceptionFromResponse(response, "Cannot open file", path);
354 } 357 }
355 return new _RandomAccessFile(response, path); 358 return new _RandomAccessFile(response, path);
356 }); 359 });
357 } 360 }
358 361
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 var ms = _lastModified(path); 397 var ms = _lastModified(path);
395 throwIfError(ms, "Cannot retrieve modification time", path); 398 throwIfError(ms, "Cannot retrieve modification time", path);
396 return new DateTime.fromMillisecondsSinceEpoch(ms); 399 return new DateTime.fromMillisecondsSinceEpoch(ms);
397 } 400 }
398 401
399 external static _open(String path, int mode); 402 external static _open(String path, int mode);
400 403
401 RandomAccessFile openSync({FileMode mode: FileMode.READ}) { 404 RandomAccessFile openSync({FileMode mode: FileMode.READ}) {
402 if (mode != FileMode.READ && 405 if (mode != FileMode.READ &&
403 mode != FileMode.WRITE && 406 mode != FileMode.WRITE &&
404 mode != FileMode.APPEND) { 407 mode != FileMode.APPEND &&
405 throw new FileSystemException("Unknown file mode. Use FileMode.READ, " 408 mode != FileMode.WRITE_ONLY &&
406 "FileMode.WRITE or FileMode.APPEND.", 409 mode != FileMode.WRITE_ONLY_APPEND) {
407 path); 410 return new Future.error(
411 new ArgumentError('Invalid file mode for this operation'));
408 } 412 }
409 var id = _open(path, mode._mode); 413 var id = _open(path, mode._mode);
410 throwIfError(id, "Cannot open file", path); 414 throwIfError(id, "Cannot open file", path);
411 return new _RandomAccessFile(id, path); 415 return new _RandomAccessFile(id, path);
412 } 416 }
413 417
414 external static int _openStdio(int fd); 418 external static int _openStdio(int fd);
415 419
416 static RandomAccessFile _openStdioSync(int fd) { 420 static RandomAccessFile _openStdioSync(int fd) {
417 var id = _openStdio(fd); 421 var id = _openStdio(fd);
418 if (id == 0) { 422 if (id == 0) {
419 throw new FileSystemException("Cannot open stdio file for: $fd"); 423 throw new FileSystemException("Cannot open stdio file for: $fd");
420 } 424 }
421 return new _RandomAccessFile(id, ""); 425 return new _RandomAccessFile(id, "");
422 } 426 }
423 427
424 Stream<List<int>> openRead([int start, int end]) { 428 Stream<List<int>> openRead([int start, int end]) {
425 return new _FileStream(path, start, end); 429 return new _FileStream(path, start, end);
426 } 430 }
427 431
428 IOSink openWrite({FileMode mode: FileMode.WRITE, 432 IOSink openWrite({FileMode mode: FileMode.WRITE,
429 Encoding encoding: UTF8}) { 433 Encoding encoding: UTF8}) {
430 if (mode != FileMode.WRITE && 434 if (mode != FileMode.WRITE &&
431 mode != FileMode.APPEND) { 435 mode != FileMode.APPEND &&
432 throw new ArgumentError( 436 mode != FileMode.WRITE_ONLY &&
433 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); 437 mode != FileMode.WRITE_ONLY_APPEND) {
438 return new Future.error(
439 new ArgumentError('Invalid file mode for this operation'));
434 } 440 }
435 var consumer = new _FileStreamConsumer(this, mode); 441 var consumer = new _FileStreamConsumer(this, mode);
436 return new IOSink(consumer, encoding: encoding); 442 return new IOSink(consumer, encoding: encoding);
437 } 443 }
438 444
439 Future<List<int>> readAsBytes() { 445 Future<List<int>> readAsBytes() {
440 Future<List<int>> readDataChunked(file) { 446 Future<List<int>> readDataChunked(file) {
441 var builder = new BytesBuilder(copy: false); 447 var builder = new BytesBuilder(copy: false);
442 var completer = new Completer(); 448 var completer = new Completer();
443 void read() { 449 void read() {
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after
1038 void _checkAvailable() { 1044 void _checkAvailable() {
1039 if (_asyncDispatched) { 1045 if (_asyncDispatched) {
1040 throw new FileSystemException("An async operation is currently pending", 1046 throw new FileSystemException("An async operation is currently pending",
1041 path); 1047 path);
1042 } 1048 }
1043 if (closed) { 1049 if (closed) {
1044 throw new FileSystemException("File closed", path); 1050 throw new FileSystemException("File closed", path);
1045 } 1051 }
1046 } 1052 }
1047 } 1053 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698