Use exception to notify caller when user the wrong type of ByteBuffer. Fixes https://github.com/domokit/mojo/issues/230 R=ppi@chromium.org Review URL: https://codereview.chromium.org/1173413002.
diff --git a/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java b/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java index 0cc2b8c..759b5ae 100644 --- a/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java +++ b/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java
@@ -274,6 +274,9 @@ */ void writeMessage(MessagePipeHandleImpl pipeHandle, ByteBuffer bytes, List<? extends Handle> handles, MessagePipeHandle.WriteFlags flags) { + if (bytes != null && !bytes.isDirect()) { + throw new IllegalArgumentException("ByteBuffer must be direct."); + } ByteBuffer handlesBuffer = null; if (handles != null && !handles.isEmpty()) { handlesBuffer = allocateDirectBuffer(handles.size() * HANDLE_SIZE); @@ -302,6 +305,9 @@ */ ResultAnd<MessagePipeHandle.ReadMessageResult> readMessage(MessagePipeHandleImpl handle, ByteBuffer bytes, int maxNumberOfHandles, MessagePipeHandle.ReadFlags flags) { + if (bytes != null && !bytes.isDirect()) { + throw new IllegalArgumentException("ByteBuffer must be direct."); + } ByteBuffer handlesBuffer = null; if (maxNumberOfHandles > 0) { handlesBuffer = allocateDirectBuffer(maxNumberOfHandles * HANDLE_SIZE); @@ -349,6 +355,9 @@ */ ResultAnd<Integer> readData( DataPipeConsumerHandleImpl handle, ByteBuffer elements, DataPipe.ReadFlags flags) { + if (elements != null && !elements.isDirect()) { + throw new IllegalArgumentException("ByteBuffer must be direct."); + } ResultAnd<Integer> result = nativeReadData(handle.getMojoHandle(), elements, elements == null ? 0 : elements.capacity(), flags.getFlags()); if (result.getMojoResult() != MojoResult.OK @@ -391,6 +400,9 @@ */ ResultAnd<Integer> writeData( DataPipeProducerHandleImpl handle, ByteBuffer elements, DataPipe.WriteFlags flags) { + if (!elements.isDirect()) { + throw new IllegalArgumentException("ByteBuffer must be direct."); + } return nativeWriteData( handle.getMojoHandle(), elements, elements.limit(), flags.getFlags()); }