[TestPrep7] Encapsulate JsonWriter creation

Change-Id: I4e97fa28dd0bb1101edc0db03f024432fee6f6fe
main
Kurt Partridge 2013-02-15 12:43:51 -08:00
parent 07050549b1
commit 78c1ad32e6
1 changed files with 19 additions and 8 deletions

View File

@ -209,26 +209,37 @@ public class ResearchLog {
public JsonWriter getInitializedJsonWriterLocked() { public JsonWriter getInitializedJsonWriterLocked() {
try { try {
if (mJsonWriter == NULL_JSON_WRITER && mFile != null) { if (mJsonWriter == NULL_JSON_WRITER && mFile != null) {
final FileOutputStream fos = final JsonWriter jsonWriter = createJsonWriter(mContext, mFile);
mContext.openFileOutput(mFile.getName(), Context.MODE_PRIVATE); if (jsonWriter != null) {
mJsonWriter = new JsonWriter(new BufferedWriter(new OutputStreamWriter(fos))); jsonWriter.beginArray();
mJsonWriter.beginArray(); mJsonWriter = jsonWriter;
mHasWrittenData = true; mHasWrittenData = true;
} }
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(TAG, "Error in JsonWriter; disabling logging", e);
Log.w(TAG, "Error in JsonWriter; disabling logging");
try { try {
mJsonWriter.close(); mJsonWriter.close();
} catch (IllegalStateException e1) { } catch (IllegalStateException e1) {
// Assume that this is just the json not being terminated properly. // Assume that this is just the json not being terminated properly.
// Ignore // Ignore
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); Log.w(TAG, "Error in closing JsonWriter; disabling logging", e1);
} finally { } finally {
mJsonWriter = NULL_JSON_WRITER; mJsonWriter = NULL_JSON_WRITER;
} }
} }
return mJsonWriter; return mJsonWriter;
} }
/**
* Create the JsonWriter to write the ResearchLog to.
*
* This method may be overriden in testing to redirect the output.
*/
/* package for test */ JsonWriter createJsonWriter(final Context context, final File file)
throws IOException {
return new JsonWriter(new BufferedWriter(new OutputStreamWriter(
context.openFileOutput(file.getName(), Context.MODE_PRIVATE))));
}
} }