1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wifi.util; 18 19 import java.util.Calendar; 20 import java.util.Random; 21 import java.util.stream.Collectors; 22 23 /** Basic string utilities */ 24 public class StringUtil { 25 static final byte ASCII_PRINTABLE_MIN = ' '; 26 static final byte ASCII_PRINTABLE_MAX = '~'; 27 28 /** Returns true if-and-only-if |byteArray| can be safely printed as ASCII. */ isAsciiPrintable(byte[] byteArray)29 public static boolean isAsciiPrintable(byte[] byteArray) { 30 if (byteArray == null) { 31 return true; 32 } 33 34 for (byte b : byteArray) { 35 switch (b) { 36 // Control characters which actually are printable. Fall-throughs are deliberate. 37 case 0x07: // bell ('\a' not recognized in Java) 38 case '\f': // form feed 39 case '\n': // new line 40 case '\t': // horizontal tab 41 case 0x0b: // vertical tab ('\v' not recognized in Java) 42 continue; 43 } 44 45 if (b < ASCII_PRINTABLE_MIN || b > ASCII_PRINTABLE_MAX) { 46 return false; 47 } 48 } 49 50 return true; 51 } 52 53 /** Returns a random number string. */ generateRandomNumberString(int length)54 public static String generateRandomNumberString(int length) { 55 final String pool = "0123456789"; 56 return new Random(System.currentTimeMillis()) 57 .ints(length, 0, pool.length()) 58 .mapToObj(i -> Character.toString(pool.charAt(i))) 59 .collect(Collectors.joining()); 60 } 61 62 /** Returns a random string which consists of alphabets and numbers. */ generateRandomString(int length)63 public static String generateRandomString(int length) { 64 final String pool = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 65 return new Random(System.currentTimeMillis()) 66 .ints(length, 0, pool.length()) 67 .mapToObj(i -> Character.toString(pool.charAt(i))) 68 .collect(Collectors.joining()); 69 } 70 71 /** Returns the date and time of the calendar as a formatted string. */ calendarToString(Calendar c)72 public static String calendarToString(Calendar c) { 73 // Date format: "%tm-%td %tH:%tM:%tS.%tL" 74 return new StringBuilder().append(c.get(Calendar.MONTH) + 1 - Calendar.JANUARY).append("-") 75 .append(c.get(Calendar.DAY_OF_MONTH)).append(" ") 76 .append(c.get(Calendar.HOUR_OF_DAY)).append(":") 77 .append(c.get(Calendar.MINUTE)).append(":") 78 .append(c.get(Calendar.SECOND)).append(".") 79 .append(c.get(Calendar.MILLISECOND)).toString(); 80 } 81 82 /** Returns the string representation of the float number, with specified digits (up to 4) 83 * after the decimal point. 84 * @param num the float number 85 * @param digits the number of digits after the decimal point 86 * @return the string representation 87 */ doubleToString(double num, int digits)88 public static String doubleToString(double num, int digits) { 89 if (digits < 0 || digits > 4) return "Err"; 90 91 final boolean isNegative = num < 0; 92 num = isNegative ? -num : num; 93 long mask = 1; 94 for (int i = 0; i < digits; i++) { 95 mask *= 10; 96 } 97 long integral = (long) num; 98 long fraction = (long) (num * mask) - integral * mask; 99 100 StringBuilder sb = new StringBuilder(); 101 if (isNegative) sb.append("-"); 102 sb.append(integral); 103 if (digits == 0) return sb.toString(); 104 sb.append("."); 105 for (long f1 = fraction; f1 > 0; f1 /= 10) { 106 digits--; 107 } 108 for (int i = 0; i < digits; i++) { 109 sb.append(0); 110 } 111 return fraction == 0 ? sb.toString() : sb.append(fraction).toString(); 112 } 113 } 114