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

Side by Side Diff: mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java

Issue 522353003: mojo: Run validation tests on java (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move backward compatibility test o its own package. Created 6 years, 3 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.mojo.bindings; 5 package org.chromium.mojo.bindings;
6 6
7 import org.chromium.mojo.bindings.Interface.Proxy; 7 import org.chromium.mojo.bindings.Interface.Proxy;
8 import org.chromium.mojo.bindings.Struct.DataHeader; 8 import org.chromium.mojo.bindings.Struct.DataHeader;
9 import org.chromium.mojo.system.DataPipe; 9 import org.chromium.mojo.system.DataPipe;
10 import org.chromium.mojo.system.Handle; 10 import org.chromium.mojo.system.Handle;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } 114 }
115 115
116 /** 116 /**
117 * Deserializes a {@link DataHeader} at the given offset. 117 * Deserializes a {@link DataHeader} at the given offset.
118 */ 118 */
119 public DataHeader readDataHeader() { 119 public DataHeader readDataHeader() {
120 // Claim the memory for the header. 120 // Claim the memory for the header.
121 mValidator.claimMemory(mBaseOffset, mBaseOffset + DataHeader.HEADER_SIZE ); 121 mValidator.claimMemory(mBaseOffset, mBaseOffset + DataHeader.HEADER_SIZE );
122 int size = readInt(DataHeader.SIZE_OFFSET); 122 int size = readInt(DataHeader.SIZE_OFFSET);
123 int numFields = readInt(DataHeader.NUM_FIELDS_OFFSET); 123 int numFields = readInt(DataHeader.NUM_FIELDS_OFFSET);
124 if (size < 0) {
125 throw new DeserializationException(
126 "Negative size. Unsigned integers are not valid for java.");
127 }
128 if (numFields < 0) {
129 throw new DeserializationException(
130 "Negative number of fields. Unsigned integers are not valid for java.");
131 }
132
124 // Claim the remaining memory. 133 // Claim the remaining memory.
125 mValidator.claimMemory(mBaseOffset + DataHeader.HEADER_SIZE, mBaseOffset + size); 134 mValidator.claimMemory(mBaseOffset + DataHeader.HEADER_SIZE, mBaseOffset + size);
126 DataHeader res = new DataHeader(size, numFields); 135 DataHeader res = new DataHeader(size, numFields);
127 return res; 136 return res;
128 } 137 }
129 138
130 /** 139 /**
131 * Deserializes a {@link DataHeader} of an array at the given offset. 140 * Deserializes a {@link DataHeader} at the given offset and checks if it is correct for an
132 * 141 * array where element have the given size.
133 * @param expectedLength the expected length of the array.
134 */ 142 */
135 public DataHeader readArrayDataHeader(int expectedLength) { 143 public DataHeader readDataHeaderForPointerArray(int expectedLength) {
136 DataHeader dataHeader = readDataHeader(); 144 return readDataHeaderForArray(8, expectedLength);
137 if (expectedLength != BindingsHelper.UNSPECIFIED_ARRAY_LENGTH
138 && dataHeader.numFields != expectedLength) {
139 throw new DeserializationException("Incorrect array length. Expected : " +
140 expectedLength + ", but got: " + dataHeader.numFields + ".") ;
141 }
142 return dataHeader;
143 } 145 }
144 146
145 /** 147 /**
146 * Deserializes a byte at the given offset. 148 * Deserializes a byte at the given offset.
147 */ 149 */
148 public byte readByte(int offset) { 150 public byte readByte(int offset) {
151 validateBufferSize(offset, 1);
149 return mMessage.getData().get(mBaseOffset + offset); 152 return mMessage.getData().get(mBaseOffset + offset);
150 } 153 }
151 154
152 /** 155 /**
153 * Deserializes a boolean at the given offset, re-using any partially read b yte. 156 * Deserializes a boolean at the given offset, re-using any partially read b yte.
154 */ 157 */
155 public boolean readBoolean(int offset, int bit) { 158 public boolean readBoolean(int offset, int bit) {
159 validateBufferSize(offset, 1);
156 return (readByte(offset) & (1 << bit)) != 0; 160 return (readByte(offset) & (1 << bit)) != 0;
157 } 161 }
158 162
159 /** 163 /**
160 * Deserializes a short at the given offset. 164 * Deserializes a short at the given offset.
161 */ 165 */
162 public short readShort(int offset) { 166 public short readShort(int offset) {
167 validateBufferSize(offset, 2);
163 return mMessage.getData().getShort(mBaseOffset + offset); 168 return mMessage.getData().getShort(mBaseOffset + offset);
164 } 169 }
165 170
166 /** 171 /**
167 * Deserializes an int at the given offset. 172 * Deserializes an int at the given offset.
168 */ 173 */
169 public int readInt(int offset) { 174 public int readInt(int offset) {
175 validateBufferSize(offset, 4);
170 return mMessage.getData().getInt(mBaseOffset + offset); 176 return mMessage.getData().getInt(mBaseOffset + offset);
171 } 177 }
172 178
173 /** 179 /**
174 * Deserializes a float at the given offset. 180 * Deserializes a float at the given offset.
175 */ 181 */
176 public float readFloat(int offset) { 182 public float readFloat(int offset) {
183 validateBufferSize(offset, 4);
177 return mMessage.getData().getFloat(mBaseOffset + offset); 184 return mMessage.getData().getFloat(mBaseOffset + offset);
178 } 185 }
179 186
180 /** 187 /**
181 * Deserializes a long at the given offset. 188 * Deserializes a long at the given offset.
182 */ 189 */
183 public long readLong(int offset) { 190 public long readLong(int offset) {
191 validateBufferSize(offset, 8);
184 return mMessage.getData().getLong(mBaseOffset + offset); 192 return mMessage.getData().getLong(mBaseOffset + offset);
185 } 193 }
186 194
187 /** 195 /**
188 * Deserializes a double at the given offset. 196 * Deserializes a double at the given offset.
189 */ 197 */
190 public double readDouble(int offset) { 198 public double readDouble(int offset) {
199 validateBufferSize(offset, 8);
191 return mMessage.getData().getDouble(mBaseOffset + offset); 200 return mMessage.getData().getDouble(mBaseOffset + offset);
192 } 201 }
193 202
194 /** 203 /**
195 * Deserializes a pointer at the given offset. Returns a Decoder suitable to decode the content 204 * Deserializes a pointer at the given offset. Returns a Decoder suitable to decode the content
196 * of the pointer. 205 * of the pointer.
197 */ 206 */
198 public Decoder readPointer(int offset, boolean nullable) { 207 public Decoder readPointer(int offset, boolean nullable) {
199 int basePosition = mBaseOffset + offset; 208 int basePosition = mBaseOffset + offset;
200 long pointerOffset = readLong(offset); 209 long pointerOffset = readLong(offset);
(...skipping 11 matching lines...) Expand all
212 } 221 }
213 222
214 /** 223 /**
215 * Deserializes an array of boolean at the given offset. 224 * Deserializes an array of boolean at the given offset.
216 */ 225 */
217 public boolean[] readBooleans(int offset, int arrayNullability, int expected Length) { 226 public boolean[] readBooleans(int offset, int arrayNullability, int expected Length) {
218 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 227 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
219 if (d == null) { 228 if (d == null) {
220 return null; 229 return null;
221 } 230 }
222 DataHeader si = d.readArrayDataHeader(expectedLength); 231 DataHeader si = d.readDataHeaderForBooleanArray(expectedLength);
223 byte[] bytes = new byte[si.numFields + 7 / BindingsHelper.ALIGNMENT]; 232 byte[] bytes = new byte[(si.numFields + 7) / BindingsHelper.ALIGNMENT];
224 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE); 233 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE);
225 d.mMessage.getData().get(bytes); 234 d.mMessage.getData().get(bytes);
226 boolean[] result = new boolean[si.numFields]; 235 boolean[] result = new boolean[si.numFields];
227 for (int i = 0; i < bytes.length; ++i) { 236 for (int i = 0; i < bytes.length; ++i) {
228 for (int j = 0; j < BindingsHelper.ALIGNMENT; ++j) { 237 for (int j = 0; j < BindingsHelper.ALIGNMENT; ++j) {
229 int booleanIndex = i * BindingsHelper.ALIGNMENT + j; 238 int booleanIndex = i * BindingsHelper.ALIGNMENT + j;
230 if (booleanIndex < result.length) { 239 if (booleanIndex < result.length) {
231 result[booleanIndex] = (bytes[i] & (1 << j)) != 0; 240 result[booleanIndex] = (bytes[i] & (1 << j)) != 0;
232 } 241 }
233 } 242 }
234 } 243 }
235 return result; 244 return result;
236 } 245 }
237 246
238 /** 247 /**
239 * Deserializes an array of bytes at the given offset. 248 * Deserializes an array of bytes at the given offset.
240 */ 249 */
241 public byte[] readBytes(int offset, int arrayNullability, int expectedLength ) { 250 public byte[] readBytes(int offset, int arrayNullability, int expectedLength ) {
242 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 251 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
243 if (d == null) { 252 if (d == null) {
244 return null; 253 return null;
245 } 254 }
246 DataHeader si = d.readArrayDataHeader(expectedLength); 255 DataHeader si = d.readDataHeaderForArray(1, expectedLength);
247 byte[] result = new byte[si.numFields]; 256 byte[] result = new byte[si.numFields];
248 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE); 257 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE);
249 d.mMessage.getData().get(result); 258 d.mMessage.getData().get(result);
250 return result; 259 return result;
251 } 260 }
252 261
253 /** 262 /**
254 * Deserializes an array of shorts at the given offset. 263 * Deserializes an array of shorts at the given offset.
255 */ 264 */
256 public short[] readShorts(int offset, int arrayNullability, int expectedLeng th) { 265 public short[] readShorts(int offset, int arrayNullability, int expectedLeng th) {
257 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 266 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
258 if (d == null) { 267 if (d == null) {
259 return null; 268 return null;
260 } 269 }
261 DataHeader si = d.readArrayDataHeader(expectedLength); 270 DataHeader si = d.readDataHeaderForArray(2, expectedLength);
262 short[] result = new short[si.numFields]; 271 short[] result = new short[si.numFields];
263 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE); 272 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE);
264 d.mMessage.getData().asShortBuffer().get(result); 273 d.mMessage.getData().asShortBuffer().get(result);
265 return result; 274 return result;
266 } 275 }
267 276
268 /** 277 /**
269 * Deserializes an array of ints at the given offset. 278 * Deserializes an array of ints at the given offset.
270 */ 279 */
271 public int[] readInts(int offset, int arrayNullability, int expectedLength) { 280 public int[] readInts(int offset, int arrayNullability, int expectedLength) {
272 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 281 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
273 if (d == null) { 282 if (d == null) {
274 return null; 283 return null;
275 } 284 }
276 DataHeader si = d.readArrayDataHeader(expectedLength); 285 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
277 int[] result = new int[si.numFields]; 286 int[] result = new int[si.numFields];
278 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE); 287 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE);
279 d.mMessage.getData().asIntBuffer().get(result); 288 d.mMessage.getData().asIntBuffer().get(result);
280 return result; 289 return result;
281 } 290 }
282 291
283 /** 292 /**
284 * Deserializes an array of floats at the given offset. 293 * Deserializes an array of floats at the given offset.
285 */ 294 */
286 public float[] readFloats(int offset, int arrayNullability, int expectedLeng th) { 295 public float[] readFloats(int offset, int arrayNullability, int expectedLeng th) {
287 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 296 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
288 if (d == null) { 297 if (d == null) {
289 return null; 298 return null;
290 } 299 }
291 DataHeader si = d.readArrayDataHeader(expectedLength); 300 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
292 float[] result = new float[si.numFields]; 301 float[] result = new float[si.numFields];
293 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE); 302 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE);
294 d.mMessage.getData().asFloatBuffer().get(result); 303 d.mMessage.getData().asFloatBuffer().get(result);
295 return result; 304 return result;
296 } 305 }
297 306
298 /** 307 /**
299 * Deserializes an array of longs at the given offset. 308 * Deserializes an array of longs at the given offset.
300 */ 309 */
301 public long[] readLongs(int offset, int arrayNullability, int expectedLength ) { 310 public long[] readLongs(int offset, int arrayNullability, int expectedLength ) {
302 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 311 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
303 if (d == null) { 312 if (d == null) {
304 return null; 313 return null;
305 } 314 }
306 DataHeader si = d.readArrayDataHeader(expectedLength); 315 DataHeader si = d.readDataHeaderForArray(8, expectedLength);
307 long[] result = new long[si.numFields]; 316 long[] result = new long[si.numFields];
308 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE); 317 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE);
309 d.mMessage.getData().asLongBuffer().get(result); 318 d.mMessage.getData().asLongBuffer().get(result);
310 return result; 319 return result;
311 } 320 }
312 321
313 /** 322 /**
314 * Deserializes an array of doubles at the given offset. 323 * Deserializes an array of doubles at the given offset.
315 */ 324 */
316 public double[] readDoubles(int offset, int arrayNullability, int expectedLe ngth) { 325 public double[] readDoubles(int offset, int arrayNullability, int expectedLe ngth) {
317 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 326 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
318 if (d == null) { 327 if (d == null) {
319 return null; 328 return null;
320 } 329 }
321 DataHeader si = d.readArrayDataHeader(expectedLength); 330 DataHeader si = d.readDataHeaderForArray(8, expectedLength);
322 double[] result = new double[si.numFields]; 331 double[] result = new double[si.numFields];
323 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE); 332 d.mMessage.getData().position(d.mBaseOffset + DataHeader.HEADER_SIZE);
324 d.mMessage.getData().asDoubleBuffer().get(result); 333 d.mMessage.getData().asDoubleBuffer().get(result);
325 return result; 334 return result;
326 } 335 }
327 336
328 /** 337 /**
329 * Deserializes an |Handle| at the given offset. 338 * Deserializes an |Handle| at the given offset.
330 */ 339 */
331 public Handle readHandle(int offset, boolean nullable) { 340 public Handle readHandle(int offset, boolean nullable) {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 } 424 }
416 425
417 /** 426 /**
418 * Deserializes an array of |Handle| at the given offset. 427 * Deserializes an array of |Handle| at the given offset.
419 */ 428 */
420 public Handle[] readHandles(int offset, int arrayNullability, int expectedLe ngth) { 429 public Handle[] readHandles(int offset, int arrayNullability, int expectedLe ngth) {
421 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 430 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
422 if (d == null) { 431 if (d == null) {
423 return null; 432 return null;
424 } 433 }
425 DataHeader si = d.readArrayDataHeader(expectedLength); 434 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
426 Handle[] result = new Handle[si.numFields]; 435 Handle[] result = new Handle[si.numFields];
427 for (int i = 0; i < result.length; ++i) { 436 for (int i = 0; i < result.length; ++i) {
428 result[i] = d.readHandle( 437 result[i] = d.readHandle(
429 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i, 438 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i,
430 BindingsHelper.isElementNullable(arrayNullability)); 439 BindingsHelper.isElementNullable(arrayNullability));
431 } 440 }
432 return result; 441 return result;
433 } 442 }
434 443
435 /** 444 /**
436 * Deserializes an array of |UntypedHandle| at the given offset. 445 * Deserializes an array of |UntypedHandle| at the given offset.
437 */ 446 */
438 public UntypedHandle[] readUntypedHandles( 447 public UntypedHandle[] readUntypedHandles(
439 int offset, int arrayNullability, int expectedLength) { 448 int offset, int arrayNullability, int expectedLength) {
440 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 449 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
441 if (d == null) { 450 if (d == null) {
442 return null; 451 return null;
443 } 452 }
444 DataHeader si = d.readArrayDataHeader(expectedLength); 453 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
445 UntypedHandle[] result = new UntypedHandle[si.numFields]; 454 UntypedHandle[] result = new UntypedHandle[si.numFields];
446 for (int i = 0; i < result.length; ++i) { 455 for (int i = 0; i < result.length; ++i) {
447 result[i] = d.readUntypedHandle( 456 result[i] = d.readUntypedHandle(
448 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i, 457 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i,
449 BindingsHelper.isElementNullable(arrayNullability)); 458 BindingsHelper.isElementNullable(arrayNullability));
450 } 459 }
451 return result; 460 return result;
452 } 461 }
453 462
454 /** 463 /**
455 * Deserializes an array of |ConsumerHandle| at the given offset. 464 * Deserializes an array of |ConsumerHandle| at the given offset.
456 */ 465 */
457 public DataPipe.ConsumerHandle[] readConsumerHandles( 466 public DataPipe.ConsumerHandle[] readConsumerHandles(
458 int offset, int arrayNullability, int expectedLength) { 467 int offset, int arrayNullability, int expectedLength) {
459 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 468 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
460 if (d == null) { 469 if (d == null) {
461 return null; 470 return null;
462 } 471 }
463 DataHeader si = d.readArrayDataHeader(expectedLength); 472 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
464 DataPipe.ConsumerHandle[] result = new DataPipe.ConsumerHandle[si.numFie lds]; 473 DataPipe.ConsumerHandle[] result = new DataPipe.ConsumerHandle[si.numFie lds];
465 for (int i = 0; i < result.length; ++i) { 474 for (int i = 0; i < result.length; ++i) {
466 result[i] = d.readConsumerHandle( 475 result[i] = d.readConsumerHandle(
467 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i, 476 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i,
468 BindingsHelper.isElementNullable(arrayNullability)); 477 BindingsHelper.isElementNullable(arrayNullability));
469 } 478 }
470 return result; 479 return result;
471 } 480 }
472 481
473 /** 482 /**
474 * Deserializes an array of |ProducerHandle| at the given offset. 483 * Deserializes an array of |ProducerHandle| at the given offset.
475 */ 484 */
476 public DataPipe.ProducerHandle[] readProducerHandles( 485 public DataPipe.ProducerHandle[] readProducerHandles(
477 int offset, int arrayNullability, int expectedLength) { 486 int offset, int arrayNullability, int expectedLength) {
478 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 487 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
479 if (d == null) { 488 if (d == null) {
480 return null; 489 return null;
481 } 490 }
482 DataHeader si = d.readArrayDataHeader(expectedLength); 491 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
483 DataPipe.ProducerHandle[] result = new DataPipe.ProducerHandle[si.numFie lds]; 492 DataPipe.ProducerHandle[] result = new DataPipe.ProducerHandle[si.numFie lds];
484 for (int i = 0; i < result.length; ++i) { 493 for (int i = 0; i < result.length; ++i) {
485 result[i] = d.readProducerHandle( 494 result[i] = d.readProducerHandle(
486 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i, 495 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i,
487 BindingsHelper.isElementNullable(arrayNullability)); 496 BindingsHelper.isElementNullable(arrayNullability));
488 } 497 }
489 return result; 498 return result;
490 499
491 } 500 }
492 501
493 /** 502 /**
494 * Deserializes an array of |MessagePipeHandle| at the given offset. 503 * Deserializes an array of |MessagePipeHandle| at the given offset.
495 */ 504 */
496 public MessagePipeHandle[] readMessagePipeHandles( 505 public MessagePipeHandle[] readMessagePipeHandles(
497 int offset, int arrayNullability, int expectedLength) { 506 int offset, int arrayNullability, int expectedLength) {
498 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 507 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
499 if (d == null) { 508 if (d == null) {
500 return null; 509 return null;
501 } 510 }
502 DataHeader si = d.readArrayDataHeader(expectedLength); 511 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
503 MessagePipeHandle[] result = new MessagePipeHandle[si.numFields]; 512 MessagePipeHandle[] result = new MessagePipeHandle[si.numFields];
504 for (int i = 0; i < result.length; ++i) { 513 for (int i = 0; i < result.length; ++i) {
505 result[i] = d.readMessagePipeHandle( 514 result[i] = d.readMessagePipeHandle(
506 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i, 515 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i,
507 BindingsHelper.isElementNullable(arrayNullability)); 516 BindingsHelper.isElementNullable(arrayNullability));
508 } 517 }
509 return result; 518 return result;
510 519
511 } 520 }
512 521
513 /** 522 /**
514 * Deserializes an array of |SharedBufferHandle| at the given offset. 523 * Deserializes an array of |SharedBufferHandle| at the given offset.
515 */ 524 */
516 public SharedBufferHandle[] readSharedBufferHandles( 525 public SharedBufferHandle[] readSharedBufferHandles(
517 int offset, int arrayNullability, int expectedLength) { 526 int offset, int arrayNullability, int expectedLength) {
518 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 527 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
519 if (d == null) { 528 if (d == null) {
520 return null; 529 return null;
521 } 530 }
522 DataHeader si = d.readArrayDataHeader(expectedLength); 531 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
523 SharedBufferHandle[] result = new SharedBufferHandle[si.numFields]; 532 SharedBufferHandle[] result = new SharedBufferHandle[si.numFields];
524 for (int i = 0; i < result.length; ++i) { 533 for (int i = 0; i < result.length; ++i) {
525 result[i] = d.readSharedBufferHandle( 534 result[i] = d.readSharedBufferHandle(
526 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i, 535 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i,
527 BindingsHelper.isElementNullable(arrayNullability)); 536 BindingsHelper.isElementNullable(arrayNullability));
528 } 537 }
529 return result; 538 return result;
530 539
531 } 540 }
532 541
533 /** 542 /**
534 * Deserializes an array of |ServiceHandle| at the given offset. 543 * Deserializes an array of |ServiceHandle| at the given offset.
535 */ 544 */
536 public <S extends Interface, P extends Proxy> S[] readServiceInterfaces( 545 public <S extends Interface, P extends Proxy> S[] readServiceInterfaces(
537 int offset, int arrayNullability, int expectedLength, Interface.Mana ger<S, P> manager) { 546 int offset, int arrayNullability, int expectedLength, Interface.Mana ger<S, P> manager) {
538 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 547 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
539 if (d == null) { 548 if (d == null) {
540 return null; 549 return null;
541 } 550 }
542 DataHeader si = d.readArrayDataHeader(expectedLength); 551 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
543 S[] result = manager.buildArray(si.numFields); 552 S[] result = manager.buildArray(si.numFields);
544 for (int i = 0; i < result.length; ++i) { 553 for (int i = 0; i < result.length; ++i) {
545 // This cast is necessary because java 6 doesn't handle wildcard cor rectly when using 554 // This cast is necessary because java 6 doesn't handle wildcard cor rectly when using
546 // Manager<S, ? extends S> 555 // Manager<S, ? extends S>
547 @SuppressWarnings("unchecked") 556 @SuppressWarnings("unchecked")
548 S value = (S) d.readServiceInterface( 557 S value = (S) d.readServiceInterface(
549 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i, 558 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i,
550 BindingsHelper.isElementNullable(arrayNullability), manager) ; 559 BindingsHelper.isElementNullable(arrayNullability), manager) ;
551 result[i] = value; 560 result[i] = value;
552 } 561 }
553 return result; 562 return result;
554 } 563 }
555 564
556 /** 565 /**
557 * Deserializes an array of |InterfaceRequest| at the given offset. 566 * Deserializes an array of |InterfaceRequest| at the given offset.
558 */ 567 */
559 public <I extends Interface> InterfaceRequest<I>[] readInterfaceRequests( 568 public <I extends Interface> InterfaceRequest<I>[] readInterfaceRequests(
560 int offset, int arrayNullability, int expectedLength) { 569 int offset, int arrayNullability, int expectedLength) {
561 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability)); 570 Decoder d = readPointer(offset, BindingsHelper.isArrayNullable(arrayNull ability));
562 if (d == null) { 571 if (d == null) {
563 return null; 572 return null;
564 } 573 }
565 DataHeader si = d.readArrayDataHeader(expectedLength); 574 DataHeader si = d.readDataHeaderForArray(4, expectedLength);
566 @SuppressWarnings("unchecked") 575 @SuppressWarnings("unchecked")
567 InterfaceRequest<I>[] result = new InterfaceRequest[si.numFields]; 576 InterfaceRequest<I>[] result = new InterfaceRequest[si.numFields];
568 for (int i = 0; i < result.length; ++i) { 577 for (int i = 0; i < result.length; ++i) {
569 result[i] = d.readInterfaceRequest( 578 result[i] = d.readInterfaceRequest(
570 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i, 579 DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SI ZE * i,
571 BindingsHelper.isElementNullable(arrayNullability)); 580 BindingsHelper.isElementNullable(arrayNullability));
572 } 581 }
573 return result; 582 return result;
574 } 583 }
575 584
576 /** 585 /**
577 * Returns a view of this decoder at the offset |offset|. 586 * Returns a view of this decoder at the offset |offset|.
578 */ 587 */
579 private Decoder getDecoderAtPosition(int offset) { 588 private Decoder getDecoderAtPosition(int offset) {
580 return new Decoder(mMessage, mValidator, offset); 589 return new Decoder(mMessage, mValidator, offset);
581 } 590 }
591
592 /**
593 * Deserializes a {@link DataHeader} at the given offset and checks if it is correct for an
594 * array of booleans.
595 */
596 private DataHeader readDataHeaderForBooleanArray(int expectedLength) {
597 DataHeader dataHeader = readDataHeader();
598 if (dataHeader.size < DataHeader.HEADER_SIZE + (dataHeader.numFields + 7 ) / 8) {
599 throw new DeserializationException("Array header is incorrect.");
600 }
601 if (expectedLength != BindingsHelper.UNSPECIFIED_ARRAY_LENGTH
602 && dataHeader.numFields != expectedLength) {
603 throw new DeserializationException("Incorrect array length. Expected : " +
604 expectedLength + ", but got: " + dataHeader.numFields + ".") ;
605 }
606 return dataHeader;
607 }
608
609 /**
610 * Deserializes a {@link DataHeader} of an array at the given offset.
611 */
612 private DataHeader readDataHeaderForArray(long elementSize, int expectedLeng th) {
613 DataHeader dataHeader = readDataHeader();
614 if (dataHeader.size < (DataHeader.HEADER_SIZE + elementSize * dataHeader .numFields)) {
615 throw new DeserializationException("Array header is incorrect.");
616 }
617 if (expectedLength != BindingsHelper.UNSPECIFIED_ARRAY_LENGTH
618 && dataHeader.numFields != expectedLength) {
619 throw new DeserializationException("Incorrect array length. Expected : " +
620 expectedLength + ", but got: " + dataHeader.numFields + ".") ;
621 }
622 return dataHeader;
623 }
624
625 private void validateBufferSize(int offset, int size) {
626 if (mMessage.getData().limit() < offset + size) {
627 throw new DeserializationException("Buffer is smaller than expected. ");
628 }
629 }
582 } 630 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698