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
3 // found in the LICENSE file.
5 package org.chromium.mojo.bindings;
7 import org.chromium.mojo.bindings.Struct.DataHeader;
8 import org.chromium.mojo.system.AsyncWaiter;
9 import org.chromium.mojo.system.Handle;
14 public class BindingsHelper {
16 * Alignment in bytes for mojo serialization.
18 public static final int ALIGNMENT = 8;
21 * The size, in bytes, of a serialized handle. A handle is serialized as an int representing the
22 * offset of the handle in the list of handles.
24 public static final int SERIALIZED_HANDLE_SIZE = 4;
27 * The size, in bytes, of a serialized pointer. A pointer is serializaed as an unsigned long
28 * representing the offset from its position to the pointed elemnt.
30 public static final int POINTER_SIZE = 8;
33 * The header for a serialized map element.
35 public static final DataHeader MAP_STRUCT_HEADER = new DataHeader(24, 2);
38 * The value used for the expected length of a non-fixed size array.
40 public static final int UNSPECIFIED_ARRAY_LENGTH = -1;
43 * Passed as |arrayNullability| when neither the array nor its elements are nullable.
45 public static final int NOTHING_NULLABLE = 0;
48 * "Array bit" of |arrayNullability| is set iff the array itself is nullable.
50 public static final int ARRAY_NULLABLE = (1 << 0);
53 * "Element bit" of |arrayNullability| is set iff the array elements are nullable.
55 public static final int ELEMENT_NULLABLE = (1 << 1);
57 public static boolean isArrayNullable(int arrayNullability) {
58 return (arrayNullability & ARRAY_NULLABLE) > 0;
61 public static boolean isElementNullable(int arrayNullability) {
62 return (arrayNullability & ELEMENT_NULLABLE) > 0;
66 * Align |size| on {@link BindingsHelper#ALIGNMENT}.
68 public static int align(int size) {
69 return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
73 * Align |size| on {@link BindingsHelper#ALIGNMENT}.
75 public static long align(long size) {
76 return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
80 * Compute the size in bytes of the given string encoded as utf8.
82 public static int utf8StringSizeInBytes(String s) {
84 for (int i = 0; i < s.length(); ++i) {
89 char c2 = s.charAt(i);
90 codepoint = Character.toCodePoint(c, c2);
93 if (codepoint > 0x7f) {
95 if (codepoint > 0x7ff) {
97 if (codepoint > 0xffff) {
99 if (codepoint > 0x1fffff) {
101 if (codepoint > 0x3ffffff) {
113 * Returns |true| if and only if the two objects are equals, handling |null|.
115 public static boolean equals(Object o1, Object o2) {
122 return o1.equals(o2);
126 * Returns the hash code of the object, handling |null|.
128 public static int hashCode(Object o) {
136 * Returns the hash code of the value.
138 public static int hashCode(boolean o) {
139 return o ? 1231 : 1237;
143 * Returns the hash code of the value.
145 public static int hashCode(long o) {
146 return (int) (o ^ (o >>> 32));
150 * Returns the hash code of the value.
152 public static int hashCode(float o) {
153 return Float.floatToIntBits(o);
157 * Returns the hash code of the value.
159 public static int hashCode(double o) {
160 return hashCode(Double.doubleToLongBits(o));
164 * Returns the hash code of the value.
166 public static int hashCode(int o) {
171 * Determines if the given {@code char} value is a Unicode <i>surrogate code unit</i>. See
172 * {@link Character#isSurrogate}. Extracting here because the method only exists at API level
175 private static boolean isSurrogate(char c) {
176 return c >= Character.MIN_SURROGATE && c < (Character.MAX_SURROGATE + 1);
180 * Returns an {@link AsyncWaiter} to use with the given handle, or |null| if none if available.
182 static AsyncWaiter getDefaultAsyncWaiterForHandle(Handle handle) {
183 if (handle.getCore() != null) {
184 return handle.getCore().getDefaultAsyncWaiter();