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 package com.android.compatibility.common.util; 17 18 import com.android.json.stream.JsonWriter; 19 20 import java.io.File; 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 import java.io.OutputStreamWriter; 24 import java.nio.charset.StandardCharsets; 25 import java.util.Base64; 26 import java.util.List; 27 28 public class HostInfoStore extends InfoStore { 29 30 protected File mJsonFile; 31 protected JsonWriter mJsonWriter = null; 32 HostInfoStore()33 public HostInfoStore() { 34 mJsonFile = null; 35 } 36 HostInfoStore(File file)37 public HostInfoStore(File file) throws Exception { 38 mJsonFile = file; 39 } 40 41 /** 42 * Opens the file for storage and creates the writer. 43 */ 44 @Override open()45 public void open() throws IOException { 46 FileOutputStream out = new FileOutputStream(mJsonFile); 47 mJsonWriter = new JsonWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)); 48 // TODO(agathaman): remove to make json output less pretty 49 mJsonWriter.setIndent(" "); 50 mJsonWriter.beginObject(); 51 } 52 53 /** 54 * Closes the writer. 55 */ 56 @Override close()57 public void close() throws Exception { 58 mJsonWriter.endObject(); 59 mJsonWriter.flush(); 60 mJsonWriter.close(); 61 } 62 63 /** 64 * Start a new group of result. 65 */ 66 @Override startGroup()67 public void startGroup() throws IOException { 68 mJsonWriter.beginObject(); 69 } 70 71 /** 72 * Start a new group of result with specified name. 73 */ 74 @Override startGroup(String name)75 public void startGroup(String name) throws IOException { 76 mJsonWriter.name(name); 77 mJsonWriter.beginObject(); 78 } 79 80 /** 81 * Complete adding result to the last started group. 82 */ 83 @Override endGroup()84 public void endGroup() throws IOException { 85 mJsonWriter.endObject(); 86 } 87 88 /** 89 * Start a new array of result. 90 */ 91 @Override startArray()92 public void startArray() throws IOException { 93 mJsonWriter.beginArray(); 94 } 95 96 /** 97 * Start a new array of result with specified name. 98 */ 99 @Override startArray(String name)100 public void startArray(String name) throws IOException { 101 checkName(name); 102 mJsonWriter.name(name); 103 mJsonWriter.beginArray(); 104 } 105 106 /** 107 * Complete adding result to the last started array. 108 */ 109 @Override endArray()110 public void endArray() throws IOException { 111 mJsonWriter.endArray(); 112 } 113 114 /** 115 * Adds a int value to the InfoStore 116 */ 117 @Override addResult(String name, int value)118 public void addResult(String name, int value) throws IOException { 119 checkName(name); 120 mJsonWriter.name(name); 121 mJsonWriter.value(value); 122 } 123 124 /** 125 * Adds a long value to the InfoStore 126 */ 127 @Override addResult(String name, long value)128 public void addResult(String name, long value) throws IOException { 129 checkName(name); 130 mJsonWriter.name(name); 131 mJsonWriter.value(value); 132 } 133 134 /** 135 * Adds a float value to the InfoStore 136 */ 137 @Override addResult(String name, float value)138 public void addResult(String name, float value) throws IOException { 139 checkName(name); 140 mJsonWriter.name(name); 141 mJsonWriter.value(value); 142 } 143 144 /** 145 * Adds a double value to the InfoStore 146 */ 147 @Override addResult(String name, double value)148 public void addResult(String name, double value) throws IOException { 149 checkName(name); 150 mJsonWriter.name(name); 151 mJsonWriter.value(value); 152 } 153 154 /** 155 * Adds a boolean value to the InfoStore 156 */ 157 @Override addResult(String name, boolean value)158 public void addResult(String name, boolean value) throws IOException { 159 checkName(name); 160 mJsonWriter.name(name); 161 mJsonWriter.value(value); 162 } 163 164 /** 165 * Adds a String value to the InfoStore 166 */ 167 @Override addResult(String name, String value)168 public void addResult(String name, String value) throws IOException { 169 checkName(name); 170 mJsonWriter.name(name); 171 mJsonWriter.value(checkString(value)); 172 } 173 174 /** 175 * Adds a int array to the InfoStore 176 */ 177 @Override addArrayResult(String name, int[] array)178 public void addArrayResult(String name, int[] array) throws IOException { 179 checkName(name); 180 mJsonWriter.name(name); 181 mJsonWriter.beginArray(); 182 for (int value : checkArray(array)) { 183 mJsonWriter.value(value); 184 } 185 mJsonWriter.endArray(); 186 } 187 188 /** 189 * Adds a long array to the InfoStore 190 */ 191 @Override addArrayResult(String name, long[] array)192 public void addArrayResult(String name, long[] array) throws IOException { 193 checkName(name); 194 mJsonWriter.name(name); 195 mJsonWriter.beginArray(); 196 for (long value : checkArray(array)) { 197 mJsonWriter.value(value); 198 } 199 mJsonWriter.endArray(); 200 } 201 202 /** 203 * Adds a float array to the InfoStore 204 */ 205 @Override addArrayResult(String name, float[] array)206 public void addArrayResult(String name, float[] array) throws IOException { 207 checkName(name); 208 mJsonWriter.name(name); 209 mJsonWriter.beginArray(); 210 for (float value : checkArray(array)) { 211 mJsonWriter.value(value); 212 } 213 mJsonWriter.endArray(); 214 } 215 216 /** 217 * Adds a double array to the InfoStore 218 */ 219 @Override addArrayResult(String name, double[] array)220 public void addArrayResult(String name, double[] array) throws IOException { 221 checkName(name); 222 mJsonWriter.name(name); 223 mJsonWriter.beginArray(); 224 for (double value : checkArray(array)) { 225 mJsonWriter.value(value); 226 } 227 mJsonWriter.endArray(); 228 } 229 230 /** 231 * Adds a boolean array to the InfoStore 232 */ 233 @Override addArrayResult(String name, boolean[] array)234 public void addArrayResult(String name, boolean[] array) throws IOException { 235 checkName(name); 236 mJsonWriter.name(name); 237 mJsonWriter.beginArray(); 238 for (boolean value : checkArray(array)) { 239 mJsonWriter.value(value); 240 } 241 mJsonWriter.endArray(); 242 } 243 244 /** 245 * Adds a List of String to the InfoStore 246 */ 247 @Override addListResult(String name, List<String> list)248 public void addListResult(String name, List<String> list) throws IOException { 249 checkName(name); 250 mJsonWriter.name(name); 251 mJsonWriter.beginArray(); 252 for (String value : checkStringList(list)) { 253 mJsonWriter.value(checkString(value)); 254 } 255 mJsonWriter.endArray(); 256 } 257 258 /** 259 * Adds some bytes to the InfoStore 260 */ 261 @Override addBytesResult(String name, byte[] bytes)262 public void addBytesResult(String name, byte[] bytes) throws IOException { 263 checkName(name); 264 mJsonWriter.name(name); 265 mJsonWriter.value(Base64.getEncoder().encodeToString(bytes)); 266 } 267 } 268