From 7d72ca0b20334aba077e3a01d7b12f6f34618076 Mon Sep 17 00:00:00 2001 From: Kurt Partridge Date: Thu, 9 May 2013 14:25:28 -0700 Subject: Avoid JsonWriter multi-write error JsonWriter requires that its clients pass it only a single top-level object. The existing implementation tries to make code cleaner by having mJsonWriter never be null, and instead use a global static "NULL_JSON_WRITER" that just discards data. But because JsonWriter complains if more than one top-level object is passed, making this a global object does not work. This change instead copes with mJsonWriter being null. Change-Id: Ia37ccfc8646e91f11a64713dd92d2846eb86ac54 --- .../com/android/inputmethod/research/MainLogBuffer.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'java/src/com/android/inputmethod/research/MainLogBuffer.java') diff --git a/java/src/com/android/inputmethod/research/MainLogBuffer.java b/java/src/com/android/inputmethod/research/MainLogBuffer.java index 9bdedbf6d..9aa349906 100644 --- a/java/src/com/android/inputmethod/research/MainLogBuffer.java +++ b/java/src/com/android/inputmethod/research/MainLogBuffer.java @@ -23,6 +23,7 @@ import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.Suggest; import com.android.inputmethod.latin.define.ProductionFlag; +import java.io.IOException; import java.util.ArrayList; import java.util.LinkedList; @@ -177,7 +178,7 @@ public abstract class MainLogBuffer extends FixedLogBuffer { return numWordsInLogUnitList == minNGramSize; } - public void shiftAndPublishAll() { + public void shiftAndPublishAll() throws IOException { final LinkedList logUnits = getLogUnits(); while (!logUnits.isEmpty()) { publishLogUnitsAtFrontOfBuffer(); @@ -186,10 +187,16 @@ public abstract class MainLogBuffer extends FixedLogBuffer { @Override protected final void onBufferFull() { - publishLogUnitsAtFrontOfBuffer(); + try { + publishLogUnitsAtFrontOfBuffer(); + } catch (final IOException e) { + if (DEBUG) { + Log.w(TAG, "IOException when publishing front of LogBuffer", e); + } + } } - protected final void publishLogUnitsAtFrontOfBuffer() { + protected final void publishLogUnitsAtFrontOfBuffer() throws IOException { // TODO: Refactor this method to require fewer passes through the LogUnits. Should really // require only one pass. ArrayList logUnits = peekAtFirstNWords(N_GRAM_SIZE); @@ -224,9 +231,11 @@ public abstract class MainLogBuffer extends FixedLogBuffer { * @param logUnits The list of logUnits to be published. * @param canIncludePrivateData Whether the private data in the logUnits can be included in * publication. + * + * @throws IOException if publication to the log file is not possible */ protected abstract void publish(final ArrayList logUnits, - final boolean canIncludePrivateData); + final boolean canIncludePrivateData) throws IOException; @Override protected int shiftOutWords(final int numWords) { -- cgit v1.2.3-83-g751a