1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.hc.core5.util; 29 30 import java.util.Locale; 31 32 import org.apache.hc.core5.annotation.Internal; 33 34 /** 35 * @since 4.3 36 */ 37 public final class TextUtils { 38 39 private TextUtils() { 40 // Do not allow utility class to be instantiated. 41 } 42 43 /** 44 * Returns true if the parameter is null or of zero length 45 */ 46 public static boolean isEmpty(final CharSequence s) { 47 return length(s) == 0; 48 } 49 50 /** 51 * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p> 52 * 53 * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p> 54 * 55 * <pre> 56 * TextUtils.isBlank(null) = true 57 * TextUtils.isBlank("") = true 58 * TextUtils.isBlank(" ") = true 59 * TextUtils.isBlank("abg") = false 60 * TextUtils.isBlank(" abg ") = false 61 * </pre> 62 * 63 * @param s the CharSequence to check, may be null 64 * @return {@code true} if the CharSequence is null, empty or whitespace only 65 */ 66 public static boolean isBlank(final CharSequence s) { 67 final int strLen = length(s); 68 if (strLen == 0) { 69 return true; 70 } 71 for (int i = 0; i < strLen; i++) { 72 if (!Character.isWhitespace(s.charAt(i))) { 73 return false; 74 } 75 } 76 return true; 77 } 78 79 /** 80 * Gets a CharSequence length or {@code 0} if the CharSequence is 81 * {@code null}. 82 * 83 * @param cs 84 * a CharSequence or {@code null} 85 * @return CharSequence length or {@code 0} if the CharSequence is 86 * {@code null}. 87 * @since 5.1 88 */ 89 public static int length(final CharSequence cs) { 90 return cs == null ? 0 : cs.length(); 91 } 92 93 /** 94 * @since 4.4 95 */ 96 public static boolean containsBlanks(final CharSequence s) { 97 final int strLen = length(s); 98 if (strLen == 0) { 99 return false; 100 } 101 for (int i = 0; i < s.length(); i++) { 102 if (Character.isWhitespace(s.charAt(i))) { 103 return true; 104 } 105 } 106 return false; 107 } 108 109 /** 110 * Returns a hexadecimal string with lowercase letters, representing the 111 * values of the {@code bytes}. 112 * 113 * @param bytes whose hex string should be created 114 * @return hex string for the bytes 115 * 116 * @since 5.0 117 */ 118 public static String toHexString(final byte[] bytes) { 119 if (bytes == null) { 120 return null; 121 } 122 final StringBuilder buffer = new StringBuilder(); 123 for (int i = 0; i < bytes.length; i++) { 124 final int unsignedB = bytes[i] & 0xff; 125 if (unsignedB < 16) { 126 buffer.append('0'); 127 } 128 buffer.append(Integer.toHexString(unsignedB)); 129 } 130 return buffer.toString(); 131 } 132 133 /** 134 * Returns lower case representation of the given string 135 * using {@link Locale#ROOT}. 136 * 137 * @since 5.2 138 */ 139 public static String toLowerCase(final String s) { 140 if (s == null) { 141 return null; 142 } 143 return s.toLowerCase(Locale.ROOT); 144 } 145 146 /** 147 * Casts character to byte filtering non-visible and non-ASCII characters 148 * before conversion 149 * 150 * @since 5.2 151 */ 152 @Internal 153 public static byte castAsByte(final int c) { 154 if ((c >= 0x20 && c <= 0x7E) || // Visible ASCII 155 (c >= 0xA0 && c <= 0xFF) || // Visible ISO-8859-1 156 c == 0x09) { // TAB 157 return (byte) c; 158 } else { 159 return '?'; 160 } 161 } 162 163 }