• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.connectivity.ipmemorystore;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.ipmemorystore.Blob;
22 
23 /** {@hide} */
24 public class Utils {
25     /** Pretty print */
blobToString(@ullable final Blob blob)26     public static String blobToString(@Nullable final Blob blob) {
27         return "Blob : " + byteArrayToString(null == blob ? null : blob.data);
28     }
29 
30     /** Pretty print */
byteArrayToString(@ullable final byte[] data)31     public static String byteArrayToString(@Nullable final byte[] data) {
32         if (null == data) return "null";
33         final StringBuilder sb = new StringBuilder("[");
34         if (data.length <= 24) {
35             appendByteArray(sb, data, 0, data.length);
36         } else {
37             appendByteArray(sb, data, 0, 16);
38             sb.append("...");
39             appendByteArray(sb, data, data.length - 8, data.length);
40         }
41         sb.append("]");
42         return sb.toString();
43     }
44 
45     // Adds the hex representation of the array between the specified indices (inclusive, exclusive)
appendByteArray(@onNull final StringBuilder sb, @NonNull final byte[] ar, final int from, final int to)46     private static void appendByteArray(@NonNull final StringBuilder sb, @NonNull final byte[] ar,
47             final int from, final int to) {
48         for (int i = from; i < to; ++i) {
49             sb.append(String.format("%02X", ar[i]));
50         }
51     }
52 }
53