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