Move progress handling details out of PGNFile.getGameInfo()

This commit is contained in:
Peter Osterlund 2019-09-23 20:17:56 +02:00
parent 061eaf1869
commit 152f9600b5
3 changed files with 54 additions and 44 deletions

View File

@ -138,7 +138,7 @@ public class PGNFileTest extends TestCase {
assertEquals(137, gi.get(1).endPos); assertEquals(137, gi.get(1).endPos);
assertEquals("2. w - b 1-0", gi.get(1).info); assertEquals("2. w - b 1-0", gi.get(1).info);
res = pgnFile.getGameInfo(null, null, 1); res = pgnFile.getGameInfo(1);
assertEquals(GameInfoResult.OK, res.first); assertEquals(GameInfoResult.OK, res.first);
assertEquals(1, res.second.size()); assertEquals(1, res.second.size());
} }

View File

@ -798,7 +798,7 @@ public class DroidFish extends Activity
} }
PGNFile pgnFile = new PGNFile(fn); PGNFile pgnFile = new PGNFile(fn);
long fileLen = FileUtil.getFileLength(fn); long fileLen = FileUtil.getFileLength(fn);
Pair<GameInfoResult,ArrayList<GameInfo>> gi = pgnFile.getGameInfo(this, null, 2); Pair<GameInfoResult,ArrayList<GameInfo>> gi = pgnFile.getGameInfo(2);
if ((fileLen > 1024 * 1024) || (gi.first == GameInfoResult.OK && gi.second.size() > 1)) { if ((fileLen > 1024 * 1024) || (gi.first == GameInfoResult.OK && gi.second.size() > 1)) {
filename = fn; filename = fn;
} else { } else {

View File

@ -42,7 +42,7 @@ public class PGNFile {
this.fileName = new File(fileName); this.fileName = new File(fileName);
} }
public final String getName() { public String getName() {
return fileName.getAbsolutePath(); return fileName.getAbsolutePath();
} }
@ -51,14 +51,14 @@ public class PGNFile {
public long startPos; public long startPos;
public long endPos; public long endPos;
final GameInfo setNull(long currPos) { GameInfo setNull(long currPos) {
info = null; info = null;
startPos = currPos; startPos = currPos;
endPos = currPos; endPos = currPos;
return this; return this;
} }
final boolean isNull() { return info == null; } boolean isNull() { return info == null; }
public String toString() { public String toString() {
if (info == null) if (info == null)
@ -160,29 +160,49 @@ public class PGNFile {
} }
} }
/** Return info about all PGN games in a file. */ private static class ProgressHandler {
public final Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(Activity activity, final ProgressDialog progress;
final ProgressDialog progress) { final Activity activity;
return getGameInfo(activity, progress, -1); int percent = -1;
long fileLen = -1;
ProgressHandler(File file, Activity activity, ProgressDialog progress) {
this.activity = activity;
this.progress = progress;
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
fileLen = raf.length();
} catch (IOException ignore) {
}
}
void reportProgress(long nRead) {
int newPercent = fileLen > 0 ? (int)(nRead * 100 / fileLen) : 0;
if (newPercent > percent) {
percent = newPercent;
activity.runOnUiThread(() -> progress.setProgress(newPercent));
}
}
} }
/** Return info about all PGN games in a file. */ /** Return info about all PGN games in a file. */
public final Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(Activity activity, public Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(Activity activity,
final ProgressDialog progress, ProgressDialog progress) {
if (activity == null || progress == null)
return getGameInfo(null, -1);
ProgressHandler handler = new ProgressHandler(fileName, activity, progress);
return getGameInfo(handler, -1);
}
/** Return info about up to "maxGames" PGN games in a file. */
public Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(int maxGames) {
return getGameInfo(null, maxGames);
}
/** Return info about PGN games in a file. */
private Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(ProgressHandler progress,
int maxGames) { int maxGames) {
ArrayList<GameInfo> gamesInFile = new ArrayList<>(); ArrayList<GameInfo> gamesInFile = new ArrayList<>();
gamesInFile.clear(); try (BufferedInput f = new BufferedInput(new FileInputStream(fileName))) {
long fileLen = 0;
BufferedInput f = null;
try {
int percent = -1;
{
RandomAccessFile raf = new RandomAccessFile(fileName, "r");
fileLen = raf.length();
raf.close();
}
f = new BufferedInput(new FileInputStream(fileName));
GameInfo gi = null; GameInfo gi = null;
HeaderInfo hi = null; HeaderInfo hi = null;
boolean inHeader = false; boolean inHeader = false;
@ -339,13 +359,8 @@ public class PGNFile {
gi = null; gi = null;
break; break;
} }
final int newPercent = fileLen == 0 ? 0 : (int)(filePos * 100 / fileLen); if (progress != null)
if (newPercent > percent) { progress.reportProgress(filePos);
percent = newPercent;
if (progress != null) {
activity.runOnUiThread(() -> progress.setProgress(newPercent));
}
}
if (Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return new Pair<>(GameInfoResult.CANCEL, null); return new Pair<>(GameInfoResult.CANCEL, null);
} }
@ -363,14 +378,9 @@ public class PGNFile {
} }
} catch (IOException ignore) { } catch (IOException ignore) {
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
gamesInFile.clear();
gamesInFile = null;
return new Pair<>(GameInfoResult.OUT_OF_MEMORY, null); return new Pair<>(GameInfoResult.OUT_OF_MEMORY, null);
} finally {
if (f != null)
f.close();
} }
if ((gamesInFile.size() == 0) && (fileLen > 0)) if (gamesInFile.isEmpty())
return new Pair<>(GameInfoResult.NOT_PGN, null); return new Pair<>(GameInfoResult.NOT_PGN, null);
return new Pair<>(GameInfoResult.OK, gamesInFile); return new Pair<>(GameInfoResult.OK, gamesInFile);
@ -382,7 +392,7 @@ public class PGNFile {
} }
/** Read one game defined by gi. Return null on failure. */ /** Read one game defined by gi. Return null on failure. */
final String readOneGame(GameInfo gi) { String readOneGame(GameInfo gi) {
try (RandomAccessFile f = new RandomAccessFile(fileName, "r")) { try (RandomAccessFile f = new RandomAccessFile(fileName, "r")) {
byte[] pgnData = new byte[(int) (gi.endPos - gi.startPos)]; byte[] pgnData = new byte[(int) (gi.endPos - gi.startPos)];
f.seek(gi.startPos); f.seek(gi.startPos);
@ -394,7 +404,7 @@ public class PGNFile {
} }
/** Append PGN to the end of this PGN file. */ /** Append PGN to the end of this PGN file. */
public final void appendPGN(String pgn) { public void appendPGN(String pgn) {
mkDirs(); mkDirs();
try (FileWriter fw = new FileWriter(fileName, true)) { try (FileWriter fw = new FileWriter(fileName, true)) {
fw.write(pgn); fw.write(pgn);
@ -404,7 +414,7 @@ public class PGNFile {
} }
} }
final boolean deleteGame(GameInfo gi, ArrayList<GameInfo> gamesInFile) { boolean deleteGame(GameInfo gi, ArrayList<GameInfo> gamesInFile) {
try { try {
File tmpFile = new File(fileName + ".tmp_delete"); File tmpFile = new File(fileName + ".tmp_delete");
try (RandomAccessFile fileReader = new RandomAccessFile(fileName, "r"); try (RandomAccessFile fileReader = new RandomAccessFile(fileName, "r");
@ -419,8 +429,8 @@ public class PGNFile {
// Update gamesInFile // Update gamesInFile
if (gamesInFile != null) { if (gamesInFile != null) {
gamesInFile.remove(gi); gamesInFile.remove(gi);
final int nGames = gamesInFile.size(); int nGames = gamesInFile.size();
final long delta = gi.endPos - gi.startPos; long delta = gi.endPos - gi.startPos;
for (int i = 0; i < nGames; i++) { for (int i = 0; i < nGames; i++) {
GameInfo tmpGi = gamesInFile.get(i); GameInfo tmpGi = gamesInFile.get(i);
if (tmpGi.startPos > gi.startPos) { if (tmpGi.startPos > gi.startPos) {
@ -436,7 +446,7 @@ public class PGNFile {
return false; return false;
} }
final void replacePGN(String pgnToSave, GameInfo gi) { void replacePGN(String pgnToSave, GameInfo gi) {
try { try {
File tmpFile = new File(fileName + ".tmp_delete"); File tmpFile = new File(fileName + ".tmp_delete");
try (RandomAccessFile fileReader = new RandomAccessFile(fileName, "r"); try (RandomAccessFile fileReader = new RandomAccessFile(fileName, "r");
@ -467,7 +477,7 @@ public class PGNFile {
} }
} }
final boolean delete() { boolean delete() {
return fileName.delete(); return fileName.delete();
} }
} }