]> git.basschouten.com Git - openhab-addons.git/blob
0c1a78802970bda54eccea118c00a779bbad5d94
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lifx.internal.fields;
14
15 import java.nio.ByteBuffer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * @author Tim Buckley - Initial contribution
21  * @author Karel Goderis - Initial contribution
22  */
23 @NonNullByDefault
24 public class MACAddressField extends Field<MACAddress> {
25
26     public MACAddressField() {
27         super(8);
28     }
29
30     @Override
31     public int defaultLength() {
32         return 8;
33     }
34
35     @Override
36     public MACAddress value(ByteBuffer bytes) {
37         byte[] data = new byte[length];
38         bytes.get(data);
39
40         ByteBuffer buffer = ByteBuffer.wrap(data);
41         buffer.limit(length - 2);
42         return new MACAddress(buffer);
43     }
44
45     @Override
46     protected ByteBuffer bytesInternal(MACAddress value) {
47         return value.getBytes().duplicate();
48     }
49
50     @Override
51     public ByteBuffer bytes(MACAddress value) {
52         byte[] data = new byte[length];
53         ByteBuffer bytes = bytesInternal(value);
54         bytes.rewind();
55         bytes.get(data, 0, bytes.limit());
56         ByteBuffer buf = ByteBuffer.wrap(data);
57         buf.rewind();
58         return buf;
59     }
60 }