Chromium Code Reviews| Index: mojo/bindings/java/src/org/chromium/mojo/bindings/MessageWithHeader.java |
| diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/MessageWithHeader.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/MessageWithHeader.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f15f4675216323a8a707326a2f846ace60e9c72f |
| --- /dev/null |
| +++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/MessageWithHeader.java |
| @@ -0,0 +1,67 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.mojo.bindings; |
| + |
| +import java.nio.ByteBuffer; |
| +import java.nio.ByteOrder; |
| + |
| +/** |
| + * A wrapper around {@link Message} that handles parsing the message header. |
| + */ |
| +public class MessageWithHeader { |
| + |
| + private final Message mBaseMessage; |
| + private MessageHeader mHeader; |
| + private Message mPayload; |
| + |
| + /** |
| + * Reinterpret the given message as a message with header. |
| + */ |
| + public MessageWithHeader(Message baseMessage) { |
| + this.mBaseMessage = baseMessage; |
|
rmcilroy
2014/07/14 14:44:57
Can you validate that the message has a header her
qsr
2014/07/15 12:03:16
I expect not to need the header each time a method
rmcilroy
2014/07/15 18:11:57
How about a second constructor which takes a Messa
qsr
2014/07/16 08:53:31
Done.
|
| + this.mHeader = null; |
| + this.mPayload = null; |
| + } |
| + |
| + /** |
| + * Returns the header of the given message. This will throw a {@link DeserializationException} |
| + * if the start of the message is not a valid header. |
| + */ |
| + public MessageHeader getHeader() { |
| + if (mHeader == null) { |
| + mHeader = new org.chromium.mojo.bindings.MessageHeader(mBaseMessage); |
| + } |
| + return mHeader; |
| + } |
| + |
| + /** |
| + * Returns the payload of the message. |
| + */ |
| + public Message getPayload() { |
| + if (mPayload == null) { |
| + ByteBuffer truncatedBuffer = ((ByteBuffer) mBaseMessage.buffer.position( |
| + getHeader().getSize())).slice(); |
| + truncatedBuffer.order(ByteOrder.nativeOrder()); |
| + mPayload = new Message(truncatedBuffer, mBaseMessage.handles); |
| + } |
| + return mPayload; |
| + } |
| + |
| + /** |
| + * Returns the raw message. |
| + */ |
| + public Message getMessage() { |
| + return mBaseMessage; |
| + } |
| + |
| + /** |
| + * Set the request identifier on the message. |
| + */ |
| + void setRequestId(long requestId) { |
| + MessageHeader.setRequestId(mBaseMessage.buffer, requestId); |
| + mHeader = null; |
| + } |
| + |
| +} |