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 // Constants used when working with native ports. | |
6 const int _SUCCESS_RESPONSE = 0; | |
7 const int _ILLEGAL_ARGUMENT_RESPONSE = 1; | |
8 const int _OSERROR_RESPONSE = 2; | |
9 const int _FILE_CLOSED_RESPONSE = 3; | |
10 | |
11 const int _ERROR_RESPONSE_ERROR_TYPE = 0; | |
12 const int _OSERROR_RESPONSE_ERROR_CODE = 1; | |
13 const int _OSERROR_RESPONSE_MESSAGE = 2; | |
14 | |
15 /** | |
16 * An [OSError] object holds information about an error from the | |
17 * operating system. | |
18 */ | |
19 class OSError { | |
20 /** Constant used to indicate that no OS error code is available. */ | |
21 static const int noErrorCode = -1; | |
22 | |
23 /** Creates an OSError object from a message and an errorCode. */ | |
24 const OSError([String this.message = "", int this.errorCode = noErrorCode]); | |
25 | |
26 /** Converts an OSError object to a string representation. */ | |
27 String toString() { | |
28 StringBuffer sb = new StringBuffer(); | |
29 sb.add("OS Error"); | |
30 if (!message.isEmpty) { | |
31 sb.add(": "); | |
32 sb.add(message); | |
33 if (errorCode != noErrorCode) { | |
34 sb.add(", errno = "); | |
35 sb.add(errorCode.toString()); | |
36 } | |
37 } else if (errorCode != noErrorCode) { | |
38 sb.add(": errno = "); | |
39 sb.add(errorCode.toString()); | |
40 } | |
41 return sb.toString(); | |
42 } | |
43 | |
44 /** | |
45 * Error message supplied by the operating system. null if no message is | |
46 * associated with the error. | |
47 */ | |
48 final String message; | |
49 | |
50 /** | |
51 * Error code supplied by the operating system. Will have the value | |
52 * [noErrorCode] if there is no error code associated with the error. | |
53 */ | |
54 final int errorCode; | |
55 } | |
56 | |
57 | |
58 // Check if a List is a builtin VM List type. Returns true | |
59 // if the List is a builtin VM List type and false if it is | |
60 // a user defined List type. | |
61 bool _isBuiltinList(List buffer) native "Common_IsBuiltinList"; | |
62 | |
63 | |
64 // Object for holding a buffer and an offset. | |
65 class _BufferAndOffset { | |
66 _BufferAndOffset(List this.buffer, int this.offset); | |
67 List buffer; | |
68 int offset; | |
69 } | |
70 | |
71 | |
72 // Ensure that the input List can be serialized through a native port. | |
73 // Only builtin Lists can be serialized through. If user-defined Lists | |
74 // get here, the contents is copied to a Uint8List. This has the added | |
75 // benefit that it is faster to access from the C code as well. | |
76 _BufferAndOffset _ensureFastAndSerializableBuffer( | |
77 List buffer, int offset, int bytes) { | |
78 if (buffer is Uint8List || _isBuiltinList(buffer)) { | |
79 return new _BufferAndOffset(buffer, offset); | |
80 } | |
81 var newBuffer = new Uint8List(bytes); | |
82 int j = offset; | |
83 for (int i = 0; i < bytes; i++) { | |
84 int value = buffer[j]; | |
85 if (value is! int) { | |
86 throw new FileIOException("List element is not an integer at index $j"); | |
87 } | |
88 newBuffer[i] = value; | |
89 j++; | |
90 } | |
91 return new _BufferAndOffset(newBuffer, 0); | |
92 } | |
OLD | NEW |