aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/research/MainLogBuffer.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/research/MainLogBuffer.java')
-rw-r--r--java/src/com/android/inputmethod/research/MainLogBuffer.java48
1 files changed, 32 insertions, 16 deletions
diff --git a/java/src/com/android/inputmethod/research/MainLogBuffer.java b/java/src/com/android/inputmethod/research/MainLogBuffer.java
index 42ef5d3b6..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<LogUnit> logUnits = getLogUnits();
while (!logUnits.isEmpty()) {
publishLogUnitsAtFrontOfBuffer();
@@ -186,26 +187,40 @@ 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<LogUnit> logUnits = peekAtFirstNWords(N_GRAM_SIZE);
if (isSafeNGram(logUnits, N_GRAM_SIZE)) {
// Good n-gram at the front of the buffer. Publish it, disclosing details.
publish(logUnits, true /* canIncludePrivateData */);
shiftOutWords(N_GRAM_SIZE);
mNumWordsUntilSafeToSample = mNumWordsBetweenNGrams;
- } else {
- // No good n-gram at front, and buffer is full. Shift out up through the first logUnit
- // with associated words (or if there is none, all the existing logUnits).
- logUnits.clear();
- for (LogUnit logUnit = shiftOut(); logUnit != null && !logUnit.hasOneOrMoreWords();
- logUnit = shiftOut()) {
- logUnits.add(logUnit);
+ return;
+ }
+ // No good n-gram at front, and buffer is full. Shift out up through the first logUnit
+ // with associated words (or if there is none, all the existing logUnits).
+ logUnits.clear();
+ LogUnit logUnit = shiftOut();
+ while (logUnit != null) {
+ logUnits.add(logUnit);
+ final int numWords = logUnit.getNumWords();
+ if (numWords > 0) {
+ mNumWordsUntilSafeToSample = Math.max(0, mNumWordsUntilSafeToSample - numWords);
+ break;
}
- publish(logUnits, false /* canIncludePrivateData */);
+ logUnit = shiftOut();
}
+ publish(logUnits, false /* canIncludePrivateData */);
}
/**
@@ -216,18 +231,19 @@ 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<LogUnit> logUnits,
- final boolean canIncludePrivateData);
+ final boolean canIncludePrivateData) throws IOException;
@Override
protected int shiftOutWords(final int numWords) {
- final int numWordContainingLogUnitsShiftedOut = super.shiftOutWords(numWords);
- mNumWordsUntilSafeToSample = Math.max(0, mNumWordsUntilSafeToSample
- - numWordContainingLogUnitsShiftedOut);
+ final int numWordsShiftedOut = super.shiftOutWords(numWords);
+ mNumWordsUntilSafeToSample = Math.max(0, mNumWordsUntilSafeToSample - numWordsShiftedOut);
if (DEBUG) {
Log.d(TAG, "wordsUntilSafeToSample now at " + mNumWordsUntilSafeToSample);
}
- return numWordContainingLogUnitsShiftedOut;
+ return numWordsShiftedOut;
}
}