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