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 /** Basic string utilities */
20 public class StringUtil {
21     static final byte ASCII_PRINTABLE_MIN = ' ';
22     static final byte ASCII_PRINTABLE_MAX = '~';
23 
24     /** Returns true if-and-only-if |byteArray| can be safely printed as ASCII. */
isAsciiPrintable(byte[] byteArray)25     public static boolean isAsciiPrintable(byte[] byteArray) {
26         if (byteArray == null) {
27             return true;
28         }
29 
30         for (byte b : byteArray) {
31             switch (b) {
32                 // Control characters which actually are printable. Fall-throughs are deliberate.
33                 case 0x07:      // bell ('\a' not recognized in Java)
34                 case '\f':      // form feed
35                 case '\n':      // new line
36                 case '\t':      // horizontal tab
37                 case 0x0b:      // vertical tab ('\v' not recognized in Java)
38                     continue;
39             }
40 
41             if (b < ASCII_PRINTABLE_MIN || b > ASCII_PRINTABLE_MAX) {
42                 return false;
43             }
44         }
45 
46         return true;
47     }
48 }
49