OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 part of dart.io; |
| 6 |
| 7 // Constants used when working with native ports. |
| 8 // These must match the constants in runtime/bin/dartutils.h class CObject. |
| 9 const int _SUCCESS_RESPONSE = 0; |
| 10 const int _ILLEGAL_ARGUMENT_RESPONSE = 1; |
| 11 const int _OSERROR_RESPONSE = 2; |
| 12 const int _FILE_CLOSED_RESPONSE = 3; |
| 13 |
| 14 const int _ERROR_RESPONSE_ERROR_TYPE = 0; |
| 15 const int _OSERROR_RESPONSE_ERROR_CODE = 1; |
| 16 const int _OSERROR_RESPONSE_MESSAGE = 2; |
| 17 |
| 18 // Functions used to receive exceptions from native ports. |
| 19 bool _isErrorResponse(response) => |
| 20 response is List && response[0] != _SUCCESS_RESPONSE; |
| 21 |
| 22 /** |
| 23 * Returns an Exception or an Error |
| 24 */ |
| 25 _exceptionFromResponse(response, String message, String path) { |
| 26 assert(_isErrorResponse(response)); |
| 27 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
| 28 case _ILLEGAL_ARGUMENT_RESPONSE: |
| 29 return new ArgumentError(); |
| 30 case _OSERROR_RESPONSE: |
| 31 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
| 32 response[_OSERROR_RESPONSE_ERROR_CODE]); |
| 33 return new FileSystemException(message, path, err); |
| 34 case _FILE_CLOSED_RESPONSE: |
| 35 return new FileSystemException("File closed", path); |
| 36 default: |
| 37 return new Exception("Unknown error"); |
| 38 } |
| 39 } |
| 40 |
| 41 /** |
| 42 * Base class for all IO related exceptions. |
| 43 */ |
| 44 abstract class IOException implements Exception { |
| 45 String toString() => "IOException"; |
| 46 } |
| 47 |
| 48 /** |
| 49 * An [OSError] object holds information about an error from the |
| 50 * operating system. |
| 51 */ |
| 52 class OSError { |
| 53 /** Constant used to indicate that no OS error code is available. */ |
| 54 static const int noErrorCode = -1; |
| 55 |
| 56 /** |
| 57 * Error message supplied by the operating system. null if no message is |
| 58 * associated with the error. |
| 59 */ |
| 60 final String message; |
| 61 |
| 62 /** |
| 63 * Error code supplied by the operating system. Will have the value |
| 64 * [noErrorCode] if there is no error code associated with the error. |
| 65 */ |
| 66 final int errorCode; |
| 67 |
| 68 /** Creates an OSError object from a message and an errorCode. */ |
| 69 const OSError([this.message = "", this.errorCode = noErrorCode]); |
| 70 |
| 71 /** Converts an OSError object to a string representation. */ |
| 72 String toString() { |
| 73 StringBuffer sb = new StringBuffer(); |
| 74 sb.write("OS Error"); |
| 75 if (!message.isEmpty) { |
| 76 sb..write(": ") |
| 77 ..write(message); |
| 78 if (errorCode != noErrorCode) { |
| 79 sb..write(", errno = ") |
| 80 ..write(errorCode.toString()); |
| 81 } |
| 82 } else if (errorCode != noErrorCode) { |
| 83 sb..write(": errno = ") |
| 84 ..write(errorCode.toString()); |
| 85 } |
| 86 return sb.toString(); |
| 87 } |
| 88 } |
| 89 |
| 90 |
| 91 // Object for holding a buffer and an offset. |
| 92 class _BufferAndStart { |
| 93 List buffer; |
| 94 int start; |
| 95 _BufferAndStart(this.buffer, this.start); |
| 96 } |
| 97 |
| 98 // Ensure that the input List can be serialized through a native port. |
| 99 // Only Int8List and Uint8List Lists are serialized directly. |
| 100 // All other lists are first copied into a Uint8List. This has the added |
| 101 // benefit that it is faster to access from the C code as well. |
| 102 _BufferAndStart _ensureFastAndSerializableByteData( |
| 103 List buffer, int start, int end) { |
| 104 if (buffer is Uint8List || buffer is Int8List) { |
| 105 return new _BufferAndStart(buffer, start); |
| 106 } |
| 107 int length = end - start; |
| 108 var newBuffer = new Uint8List(length); |
| 109 int j = start; |
| 110 for (int i = 0; i < length; i++) { |
| 111 int value = buffer[j]; |
| 112 if (value is! int) { |
| 113 throw new ArgumentError("List element is not an integer at index $j"); |
| 114 } |
| 115 newBuffer[i] = value; |
| 116 j++; |
| 117 } |
| 118 return new _BufferAndStart(newBuffer, 0); |
| 119 } |
| 120 |
| 121 |
| 122 class _IOCrypto { |
| 123 external static Uint8List getRandomBytes(int count); |
| 124 } |
OLD | NEW |