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 android.util.JsonWriter;
19 
20 import java.io.BufferedReader;
21 import java.io.BufferedWriter;
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 
27 public class ReportLogDeviceInfoStore extends DeviceInfoStore {
28 
29     private final String mStreamName;
30     private File tempJsonFile;
31 
ReportLogDeviceInfoStore(File jsonFile, String streamName)32     public ReportLogDeviceInfoStore(File jsonFile, String streamName) throws Exception {
33         mJsonFile = jsonFile;
34         mStreamName = streamName;
35     }
36 
37     /**
38      * Creates the writer and starts the JSON Object for the metric stream.
39      */
40     @Override
open()41     public void open() throws IOException {
42         // Write new metrics to a temp file to avoid invalid JSON files due to failed tests.
43         BufferedWriter formatWriter;
44         tempJsonFile = File.createTempFile(mStreamName, "-temp-report-log");
45         formatWriter = new BufferedWriter(new FileWriter(tempJsonFile));
46         if (mJsonFile.exists()) {
47             BufferedReader jsonReader = new BufferedReader(new FileReader(mJsonFile));
48             String currentLine;
49             String nextLine = jsonReader.readLine();
50             while ((currentLine = nextLine) != null) {
51                 nextLine = jsonReader.readLine();
52                 if (nextLine == null && currentLine.charAt(currentLine.length() - 1) == '}') {
53                     // Reopen overall JSON object to write new metrics.
54                     currentLine = currentLine.substring(0, currentLine.length() - 1) + ",";
55                 }
56                 // Copy to temp file directly to avoid large metrics string in memory.
57                 formatWriter.write(currentLine, 0, currentLine.length());
58             }
59             jsonReader.close();
60         } else {
61             formatWriter.write("{", 0 , 1);
62         }
63         // Start new JSON object for new metrics.
64         formatWriter.write("\"" + mStreamName + "\":", 0, mStreamName.length() + 3);
65         formatWriter.flush();
66         formatWriter.close();
67         mJsonWriter = new JsonWriter(new FileWriter(tempJsonFile, true));
68         mJsonWriter.beginObject();
69     }
70 
71     /**
72      * Closes the writer.
73      */
74     @Override
close()75     public void close() throws IOException {
76         // Close JSON Writer.
77         mJsonWriter.endObject();
78         mJsonWriter.close();
79         // Close overall JSON Object.
80         try (BufferedWriter formatWriter = new BufferedWriter(new FileWriter(tempJsonFile, true))) {
81             formatWriter.write("}", 0, 1);
82         }
83         // Copy metrics from temp file and delete temp file.
84         mJsonFile.createNewFile();
85         try (
86                 BufferedReader jsonReader = new BufferedReader(new FileReader(tempJsonFile));
87                 BufferedWriter metricsWriter = new BufferedWriter(new FileWriter(mJsonFile))
88         ) {
89             String line;
90             while ((line = jsonReader.readLine()) != null) {
91                 // Copy from temp file directly to avoid large metrics string in memory.
92                 metricsWriter.write(line, 0, line.length());
93             }
94         }
95     }
96 }
97