]> git.basschouten.com Git - openhab-addons.git/blob
3a48a1dd8acdeb72488b04651a8f35184b05cd01
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.bticinosmarther.internal.util;
14
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;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 /**
25  * The {@code StringUtil} class defines common string utility functions used across the whole binding.
26  *
27  * @author Fabio Possieri - Initial contribution
28  */
29 @NonNullByDefault
30 public final class StringUtil {
31
32     private static final int EOF = -1;
33     private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
34
35     /**
36      * Checks if a string is whitespace, empty ("") or {@code null}.
37      *
38      * @param str
39      *            the string to check, may be {@code null}
40      *
41      * @return {@code true} if the string is {@code null}, empty or whitespace
42      */
43     public static boolean isBlank(@Nullable String str) {
44         return (str == null || str.trim().isEmpty());
45     }
46
47     /**
48      * Returns either the passed in string or, if the string is {@code null}, an empty string ("").
49      *
50      * @param str
51      *            the string to check, may be {@code null}
52      *
53      * @return the passed in string, or the empty string if it was {@code null}
54      *
55      */
56     public static String defaultString(@Nullable String str) {
57         return (str == null) ? "" : str;
58     }
59
60     /**
61      * Returns either the passed in string or, if the string is whitespace, empty ("") or {@code null}, a default value.
62      *
63      * @param str
64      *            the string to check, may be {@code null}
65      * @param defaultStr
66      *            the default string to return
67      *
68      * @return the passed in string, or the default one
69      */
70     public static String defaultIfBlank(String str, String defaultStr) {
71         return StringUtil.isBlank(str) ? defaultStr : str;
72     }
73
74     /**
75      * Strips whitespace from the start and end of a string returning {@code null} if the string is empty ("") after the
76      * strip.
77      *
78      * @param str
79      *            the string to be stripped, may be {@code null}
80      *
81      * @return the stripped string, {@code null} if whitespace, empty or {@code null} input string
82      */
83     public static @Nullable String stripToNull(@Nullable String str) {
84         if (str == null) {
85             return null;
86         }
87         String s = str.trim();
88         return (s.isEmpty()) ? null : s;
89     }
90
91     /**
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}.
94      *
95      * @param input
96      *            the {@code InputStream} to read from
97      *
98      * @return the string read from stream
99      *
100      * @throws IOException if an I/O error occurs
101      */
102     public static String streamToString(InputStream input) throws IOException {
103         InputStreamReader reader = new InputStreamReader(input);
104
105         final StringWriter writer = new StringWriter();
106         char[] buffer = new char[DEFAULT_BUFFER_SIZE];
107
108         int n = 0;
109         while ((n = reader.read(buffer)) != EOF) {
110             writer.write(buffer, 0, n);
111         }
112
113         return writer.toString();
114     }
115
116     /**
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.
119      *
120      * @param reader
121      *            the {@code Reader} to read from
122      *
123      * @return the string read from stream
124      *
125      * @throws IOException if an I/O error occurs
126      */
127     public static String readerToString(Reader reader) throws IOException {
128         final StringWriter writer = new StringWriter();
129
130         int c;
131         while ((c = reader.read()) != EOF) {
132             writer.write(c);
133         }
134
135         return writer.toString();
136     }
137 }