Chromium Code Reviews| Index: mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java |
| diff --git a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java |
| index c820ac5284583ae93f103bc981d22cfac3dacbbc..432610285555c7a9561c4a75b91b1b64fa861e0f 100644 |
| --- a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java |
| +++ b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java |
| @@ -5,7 +5,6 @@ |
| package org.chromium.mojo.bindings; |
| import org.chromium.mojo.bindings.Interface.AbstractProxy.HandlerImpl; |
| -import org.chromium.mojo.bindings.Struct.DataHeader; |
| import org.chromium.mojo.system.Core; |
| import org.chromium.mojo.system.Handle; |
| import org.chromium.mojo.system.MessagePipeHandle; |
| @@ -50,8 +49,8 @@ public class Encoder { |
| public int dataEnd; |
| /** |
| - * @param core the |Core| implementation used to generate handles. Only used if the |Struct| |
| - * being encoded contains interfaces, can be |null| otherwise. |
| + * @param core the |Core| implementation used to generate handles. Only used if the data |
| + * structure being encoded contains interfaces, can be |null| otherwise. |
| * @param bufferSize A hint on the size of the message. Used to build the initial byte |
| * buffer. |
| */ |
| @@ -119,8 +118,8 @@ public class Encoder { |
| /** |
| * Constructor. |
| * |
| - * @param core the |Core| implementation used to generate handles. Only used if the |Struct| |
| - * being encoded contains interfaces, can be |null| otherwise. |
| + * @param core the |Core| implementation used to generate handles. Only used if the data |
| + * structure being encoded contains interfaces, can be |null| otherwise. |
| * @param sizeHint A hint on the size of the message. Used to build the initial byte buffer. |
| */ |
| public Encoder(Core core, int sizeHint) { |
| @@ -220,6 +219,22 @@ public class Encoder { |
| } |
| /** |
| + * Encode a {@link Union} at the given offset. |
| + */ |
| + public void encode(Union v, int offset, boolean nullable) { |
| + if (v == null && !nullable) { |
| + throw new SerializationException( |
| + "Trying to encode a null pointer for a non-nullable type."); |
| + } |
| + if (v == null) { |
| + encode(0L, offset); |
| + encode(0L, offset + DataHeader.HEADER_SIZE); |
| + return; |
| + } |
| + v.encode(this, offset); |
| + } |
| + |
| + /** |
| * Encodes a String. |
| */ |
| public void encode(String v, int offset, boolean nullable) { |
| @@ -300,6 +315,13 @@ public class Encoder { |
| } |
| /** |
| + * Returns an {@link Encoder} suitable for encoding an array of union of the given length. |
| + */ |
| + public Encoder encodeUnionArray(int length, int offset, int expectedLength) { |
| + return encoderForArray(BindingsHelper.UNION_SIZE, length, offset, expectedLength); |
| + } |
| + |
| + /** |
| * Encodes an array of booleans. |
| */ |
| public void encode(boolean[] v, int offset, int arrayNullability, int expectedLength) { |
| @@ -431,6 +453,13 @@ public class Encoder { |
| return getEncoderAtDataOffset(BindingsHelper.MAP_STRUCT_HEADER); |
| } |
| + public Encoder encoderForUnionPointer(int offset) { |
|
azani
2015/05/19 21:13:23
Maybe document whether this encodes a pointer to a
qsr
2015/05/20 09:22:37
Done.
|
| + encodePointerToNextUnclaimedData(offset); |
| + Encoder result = new Encoder(mEncoderState); |
| + result.mEncoderState.claimMemory(16); |
| + return result; |
| + } |
| + |
| /** |
| * Encodes an array of {@link InterfaceRequest}. |
| */ |
| @@ -471,6 +500,13 @@ public class Encoder { |
| mEncoderState.byteBuffer.putInt(mBaseOffset + offset, -1); |
| } |
| + /** |
| + * Claim the given amount of memory at the end of the buffer, resizing it if needed. |
| + */ |
| + void claimMemory(int size) { |
| + mEncoderState.claimMemory(BindingsHelper.align(size)); |
| + } |
| + |
| private void encodePointerToNextUnclaimedData(int offset) { |
| encode((long) mEncoderState.dataEnd - (mBaseOffset + offset), offset); |
| } |