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

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

Issue 18115002: Make writes consistent across socket and file synchronous/asynchronus writes in terms of truncation… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 5 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
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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 // Constants used when working with native ports. 7 // Constants used when working with native ports.
8 // These must match the constants in runtime/bin/dartutils.h class CObject. 8 // These must match the constants in runtime/bin/dartutils.h class CObject.
9 const int _SUCCESS_RESPONSE = 0; 9 const int _SUCCESS_RESPONSE = 0;
10 const int _ILLEGAL_ARGUMENT_RESPONSE = 1; 10 const int _ILLEGAL_ARGUMENT_RESPONSE = 1;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 90
91 91
92 // Object for holding a buffer and an offset. 92 // Object for holding a buffer and an offset.
93 class _BufferAndStart { 93 class _BufferAndStart {
94 _BufferAndStart(List this.buffer, int this.start); 94 _BufferAndStart(List this.buffer, int this.start);
95 List buffer; 95 List buffer;
96 int start; 96 int start;
97 } 97 }
98 98
99 // Ensure that the input List can be serialized through a native port. 99 // Ensure that the input List can be serialized through a native port.
100 // Only builtin Lists can be serialized through. If user-defined Lists 100 // Only Int8List and Uint8List Lists are serialized directly.
101 // get here, the contents is copied to a Uint8List. This has the added 101 // All other lists are first copied into a Uint8List. This has the added
102 // benefit that it is faster to access from the C code as well. 102 // benefit that it is faster to access from the C code as well.
103 _BufferAndStart _ensureFastAndSerializableData( 103 _BufferAndStart _ensureFastAndSerializableByteData(
104 List buffer, int start, int end) { 104 List buffer, int start, int end) {
105 if (buffer is Uint8List || 105 if (buffer is Uint8List || buffer is Int8List) {
106 buffer is Int8List ||
107 buffer is Uint16List ||
108 buffer is Int16List ||
109 buffer is Uint32List ||
110 buffer is Int32List ||
111 buffer is Uint64List ||
112 buffer is Int64List ||
113 buffer is ByteData ||
114 buffer is Float32List ||
115 buffer is Float64List ||
116 _BufferUtils._isBuiltinList(buffer)) {
117 return new _BufferAndStart(buffer, start); 106 return new _BufferAndStart(buffer, start);
118 } 107 }
119 int length = end - start; 108 int length = end - start;
120 var newBuffer = new Uint8List(length);
121 int j = start;
122 for (int i = 0; i < length; i++) {
123 int value = buffer[j];
124 if (value is! int) {
125 throw new ArgumentError("List element is not an integer at index $j");
126 }
127 newBuffer[i] = value;
128 j++;
129 }
130 return new _BufferAndStart(newBuffer, 0);
131 }
132
133
134 _BufferAndStart _ensureFastAndSerializableByteData(
135 List buffer, int start, int end) {
136 if (buffer is Uint8List) {
137 return new _BufferAndStart(buffer, start);
138 }
139 int length = end - start;
140 var newBuffer = new Uint8List(length); 109 var newBuffer = new Uint8List(length);
141 int j = start; 110 int j = start;
142 for (int i = 0; i < length; i++) { 111 for (int i = 0; i < length; i++) {
143 int value = buffer[j]; 112 int value = buffer[j];
144 if (value is! int || 113 if (value is! int ||
145 value < 0 || 255 < value) { 114 value < 0 || 255 < value) {
Ivan Posva 2013/06/28 23:10:13 I was under the impression that we agree on this w
siva 2013/06/29 18:19:01 I have changed this to truncate instead of throwin
146 throw new ArgumentError( 115 throw new ArgumentError(
147 "List element is not a byte value (value $value at index $j)"); 116 "List element is not a byte value (value $value at index $j)");
148 } 117 }
149 newBuffer[i] = value; 118 newBuffer[i] = value;
150 j++; 119 j++;
151 } 120 }
152 return new _BufferAndStart(newBuffer, 0); 121 return new _BufferAndStart(newBuffer, 0);
153 } 122 }
154 123
155 124
156 // TODO(ager): The only reason for the class here is that
157 // we cannot patch a top-level function.
158 class _BufferUtils {
159 // Check if a List is a builtin VM List type. Returns true
160 // if the List is a builtin VM List type and false if it is
161 // a user defined List type.
162 external static bool _isBuiltinList(List buffer);
163 }
164
165 class _IOCrypto { 125 class _IOCrypto {
166 external static Uint8List getRandomBytes(int count); 126 external static Uint8List getRandomBytes(int count);
167 } 127 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698