2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lifx.internal.fields;
15 import java.nio.ByteBuffer;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
20 * Reads a wrapped field in reversed byte order.
22 * @author Tim Buckley - Initial contribution
25 public class LittleField<T> extends Field<T> {
27 private final Field<T> wrapped;
29 public LittleField(Field<T> wrapped) {
30 super(wrapped.length);
32 this.wrapped = wrapped;
36 public int defaultLength() {
37 return wrapped.defaultLength();
41 public T value(ByteBuffer bytes) {
42 byte[] field = new byte[wrapped.length];
45 ByteBuffer flipped = flip(ByteBuffer.wrap(field));
47 T value = wrapped.value(flipped);
53 public ByteBuffer bytesInternal(T value) {
54 return flip(wrapped.bytes(value));
57 public static ByteBuffer flip(ByteBuffer buf) {
60 ByteBuffer ret = ByteBuffer.allocate(buf.limit());
62 for (int i = buf.limit() - 1; i >= 0; i--) {