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.bticinosmarther.internal.util;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.io.Reader;
19 import java.io.StringWriter;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
25 * The {@code StringUtil} class defines common string utility functions used across the whole binding.
27 * @author Fabio Possieri - Initial contribution
30 public final class StringUtil {
32 private static final int EOF = -1;
33 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
36 * Checks if a string is whitespace, empty ("") or {@code null}.
39 * the string to check, may be {@code null}
41 * @return {@code true} if the string is {@code null}, empty or whitespace
43 public static boolean isBlank(@Nullable String str) {
44 return (str == null || str.trim().isEmpty());
48 * Returns either the passed in string or, if the string is {@code null}, an empty string ("").
51 * the string to check, may be {@code null}
53 * @return the passed in string, or the empty string if it was {@code null}
56 public static String defaultString(@Nullable String str) {
57 return (str == null) ? "" : str;
61 * Returns either the passed in string or, if the string is whitespace, empty ("") or {@code null}, a default value.
64 * the string to check, may be {@code null}
66 * the default string to return
68 * @return the passed in string, or the default one
70 public static String defaultIfBlank(String str, String defaultStr) {
71 return StringUtil.isBlank(str) ? defaultStr : str;
75 * Strips whitespace from the start and end of a string returning {@code null} if the string is empty ("") after the
79 * the string to be stripped, may be {@code null}
81 * @return the stripped string, {@code null} if whitespace, empty or {@code null} input string
83 public static @Nullable String stripToNull(@Nullable String str) {
87 String s = str.trim();
88 return (s.isEmpty()) ? null : s;
92 * Get the contents of an {@link InputStream} stream as a string using the default character encoding of the
93 * platform. This method buffers the input internally, so there is no need to use a {@code BufferedInputStream}.
96 * the {@code InputStream} to read from
98 * @return the string read from stream
100 * @throws IOException if an I/O error occurs
102 public static String streamToString(InputStream input) throws IOException {
103 InputStreamReader reader = new InputStreamReader(input);
105 final StringWriter writer = new StringWriter();
106 char[] buffer = new char[DEFAULT_BUFFER_SIZE];
109 while ((n = reader.read(buffer)) != EOF) {
110 writer.write(buffer, 0, n);
113 return writer.toString();
117 * Get the contents of a {@link Reader} stream as a string using the default character encoding of the platform.
118 * This method doesn't buffer the input internally, so eventually {@code BufferedReder} needs to be used externally.
121 * the {@code Reader} to read from
123 * @return the string read from stream
125 * @throws IOException if an I/O error occurs
127 public static String readerToString(Reader reader) throws IOException {
128 final StringWriter writer = new StringWriter();
131 while ((c = reader.read()) != EOF) {
135 return writer.toString();