Serializes the given {@link ControllerEvent} to a {@link ByteBuffer}.

@param value The {@link ControllerEvent} to serialize.
@return A new {@link ByteBuffer} wrapping an array that contains the serialization.
769283
@SneakyThrows(IOException.class)
    public ByteBuffer toByteBuffer(ControllerEvent value) {
        ByteArraySegment s = serialize(value);
        return ByteBuffer.wrap(s.array(), s.arrayOffset(), s.getLength());
    }
1274216
public static <T extends Appendable> T byteToHexString(T buf, int value) {
        try {
            buf.append(byteToHexString(value));
        } catch (IOException e) {
            PlatformDependent.throwException(e);
        }
        return buf;
    }
799942
public JBBPOut Byte(final int... value) throws IOException {
    assertNotEnded();
    assertArrayNotNull(value);
    if (this.processCommands) {
      for (final int v : value) {
        _writeByte(v);
      }
    }
    return this;
  }
1018658
public JBBPOut Byte(final byte[] value) throws IOException {
    assertNotEnded();
    assertArrayNotNull(value);
    if (this.processCommands) {
      this.outStream.write(value);
    }
    return this;
  }
1171711
@Override
    public void write(int value) throws IOException {
        validate();
        byte[] buf = new byte[1];
        buf[0] = (byte) value;
        writeToBuffers(buf, 0, 1);

    }
465181
public static byte[] intToBytes(int value) {
        byte[] result = new byte[4];
        result[0] = (byte)(value >> 24 & 0xff);
        result[1] = (byte)(value >> 16 & 0xff);
        result[2] = (byte)(value >> 8 & 0xff);
        result[3] = (byte)(value & 0xff);
        return result;
    }
992037
public static ByteBuffer cloneByteBuffer(ByteBuffer buf) {
    ByteBuffer ret = ByteBuffer.allocate(buf.limit() - buf.position());
    if (buf.hasArray()) {
      ret.put(buf.array(), buf.position(), buf.limit() - buf.position());
    } else {
      // direct buffer
      ret.put(buf);
    }
    ret.flip();
    return ret;
  }
652726
public DynamicByteBuffer add( byte value ) {

        if ( 1 + length < capacity ) {
            DynamicByteBufferHelper.idx( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer );
            capacity = buffer.length;

            DynamicByteBufferHelper.idx( buffer, length, value );
        }

        length += 1;

        return this;

    }
1013389
public ISynchronizationPoint<? extends Exception> serializeValue(
		SerializationContext context, Object value, TypeDefinition typeDef, String path, List<SerializationRule> rules
	) {
		if (value == null)
			return serializeNullValue();
		
		for (SerializationRule rule : rules)
			value = rule.convertSerializationValue(value, typeDef, context);

		Class<?> type = value.getClass();
		
		if (type.isArray()) {
			if (value instanceof byte[])
				return serializeByteArrayValue(context, (byte[])value, path, rules);
			Class<?> elementType = type.getComponentType();
			CollectionContext ctx = new CollectionContext(context, value, typeDef, new TypeDefinition(elementType));
			return serializeCollectionValue(ctx, path, rules);
		}
				
		if (boolean.class.equals(type) || Boolean.class.equals(type))
			return serializeBooleanValue(((Boolean)value).booleanValue());
		
		if (byte.class.equals(type) ||
			short.class.equals(type) ||
			int.class.equals(type) ||
			long.class.equals(type) ||
			float.class.equals(type) ||
			double.class.equals(type) ||
			Number.class.isAssignableFrom(type))
			return serializeNumericValue((Number)value);
		
		if (char.class.equals(type) || Character.class.equals(type))
			return serializeCharacterValue(((Character)value).charValue());
		
		if (CharSequence.class.isAssignableFrom(type))
			return serializeStringValue((CharSequence)value);

		if (type.isEnum())
			return serializeStringValue(((Enum<?>)value).name());

		if (Collection.class.isAssignableFrom(type)) {
			TypeDefinition elementType;
			if (typeDef.getParameters().isEmpty())
				elementType = null;
			else
				elementType = typeDef.getParameters().get(0);
			CollectionContext ctx = new CollectionContext(context, value, typeDef, elementType);
			return serializeCollectionValue(ctx, path, rules);
		}

		if (Map.class.isAssignableFrom(type))
			return serializeMapValue(context, (Map<?,?>)value, typeDef, path, rules);
		
		if (InputStream.class.isAssignableFrom(type))
			return serializeInputStreamValue(context, (InputStream)value, path, rules);
		
		if (IO.Readable.class.isAssignableFrom(type))
			return serializeIOReadableValue(context, (IO.Readable)value, path, rules);

		return serializeObjectValue(context, value, typeDef, path, rules);
	}
1350167
public static byte[] asArray(ByteBuffer byteBuffer) {
        ByteBuffer bb = ByteBufferUtil.clone(byteBuffer);
        byte[] bytes = new byte[bb.remaining()];
        bb.get(bytes);
        return bytes;
    }
456878
public boolean add(Byte value) {
		if (value == null) {
			stateList.add(Type.NULL);
		} else {
			stateList.add(Type.LONG);
		}

		return super.add(value);
	}
1167315
public static Buffer wrap(final int value) {
        final int size = value < 0 ? stringSize(-value) + 1 : stringSize(value);
        final byte[] bytes = new byte[size];
        getBytes(value, size, bytes);
        return new ByteBuffer(bytes);
    }
1232169
public static byte[] toBytes(int value) {
		final byte[] result = new byte[4];

		result[0] = (byte) (value >> 24);
		result[1] = (byte) (value >> 16);
		result[2] = (byte) (value >> 8);
		result[3] = (byte) (value /* >> 0 */);

		return result;
	}
888067
public void sendSerialMessage(byte[] value) {
        Buffer buffer = new Buffer();
        buffer.write(value);
        sendMessage(BeanMessageID.SERIAL_DATA, buffer);
    }
1193112
public static function serializeType($type, $value, $byteBuffer)
    {
        if ($type === self::TYPE_STRING) {
            //Writes a UTF8 encoded string prefixed 32bit base 128 variable-length integer.
            $strLength = strlen($value);

            if ($strLength <= 128) {
                $byteBuffer->writeInt8($strLength);
            } elseif ($strLength <= 16511) {
                $strLength = ceil($strLength / 128) * 256
                    + ($strLength - ceil($strLength / 128) * 128);
                $byteBuffer->writeInt16LE($strLength);
            } else {
                $n3 = ceil($strLength / (128 * 128));
                $n2 = ceil(($strLength - $n3 * 128 * 128) / 128);
                $strLength = $n3 * 256 * 256
                    + $n2 * 256
                    + ($strLength - $n3 * 128 * 128 - $n2 * 128);
                $byteBuffer->writeInt32LE($strLength);
                if ($strLength <= 16777215) {
                    $byteBuffer->setCurrentOffset($byteBuffer->getCurrentOffset() - 1);
                }
            }
            $byteBuffer->writeVStringLE($value);
        } elseif ($type === self::TYPE_SET_STRING) {
            $byteBuffer->writeInt8(count($value));
            foreach ($value as $string) {
                self::serializeType(self::TYPE_STRING, $string, $byteBuffer);
            }
        } elseif ($type === self::TYPE_INT16) {
            $byteBuffer->writeInt16LE($value);
        } elseif ($type === self::TYPE_ASSET) {
            list($amount, $symbol) = explode(' ', $value);

            //TODO FIXME have to be writeInt64
            $byteBuffer->writeInt32LE(str_replace('.', '', $amount));
            $byteBuffer->writeInt32LE(0);

            $dot = strpos($amount, '.');
            $precision = $dot === false ? 0 : strlen($amount) - $dot - 1;
            $byteBuffer->writeInt8($precision);

            $byteBuffer->writeVStringLE(strtoupper($symbol));
            for ($i = 0; $i < 7 - strlen($symbol); $i++) {
                $byteBuffer->writeInt8(0);
            }
        } elseif ($type === self::TYPE_SET_EXTENSIONS) {
            $byteBuffer->writeInt8(count($value));
            foreach ($value as $extension) {
                $byteBuffer->writeInt8($extension[0]);
                if ($extension[0] === 0) {
                    self::serializeType(self::TYPE_SET_BENEFICIARIES, $extension[1], $byteBuffer);
                } else {
                    throw new \Exception("There is no serializer logic for '{$extension[0]}' extension");
                }
            }
        } elseif ($type === self::TYPE_SET_BENEFICIARIES) {
            $byteBuffer->writeInt8(count($value['beneficiaries']));
            foreach ($value['beneficiaries'] as $beneficiary) {
                self::serializeType(self::TYPE_BENEFICIARY, $beneficiary, $byteBuffer);
            }
        } elseif ($type === self::TYPE_BENEFICIARY) {
            self::serializeType(self::TYPE_STRING, $value['account'], $byteBuffer);
            self::serializeType(self::TYPE_INT16, $value['weight'], $byteBuffer);
        } elseif ($type === self::TYPE_INT8) {
            $byteBuffer->writeInt8($value);
        } elseif ($type === self::TYPE_BOOL) {
            self::serializeType(self::TYPE_INT8, $value ? 1 : 0, $byteBuffer);
        }

        return $byteBuffer;
    }
1244938
@Override
	public RequestBody convert(T value) throws IOException {
		Buffer buffer = new Buffer();

		try {
			binderContext.serialize(value, buffer.outputStream());
			return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			buffer.close();
		}

	}
1293262
private Class<?> serializerToClass(String value) {
    try {
      return Thread.currentThread().getContextClassLoader().loadClass(value);
    } catch (ClassNotFoundException e) {
      throw new ConfigurationException("unknown serializable type: " + value);
    }
  }