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