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;
16 import java.nio.charset.Charset;
17 import java.nio.charset.StandardCharsets;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
22 * @author Tim Buckley - Initial contribution
25 public class StringField extends Field<String> {
27 public static final Charset CHARSET = StandardCharsets.US_ASCII;
29 private Charset charset;
31 public StringField() {
32 charset = StandardCharsets.US_ASCII;
35 public StringField(int length) {
38 charset = StandardCharsets.US_ASCII;
41 public StringField(int length, Charset charset) {
44 this.charset = charset;
48 public int defaultLength() {
53 public String value(ByteBuffer bytes) {
54 byte[] buf = new byte[length];
57 ByteBuffer field = ByteBuffer.wrap(buf);
59 String ret = charset.decode(field).toString();
60 ret = ret.replace("\0", "");
66 public ByteBuffer bytesInternal(String value) {
67 return CHARSET.encode(value);
70 public StringField ascii() {
71 charset = StandardCharsets.US_ASCII;
75 public StringField utf8() {
76 charset = StandardCharsets.UTF_8;