mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2025-12-08 15:12:40 +01:00
Use try-with-resources
This commit is contained in:
@@ -54,8 +54,7 @@ public class Book {
|
|||||||
rndGen = new SecureRandom();
|
rndGen = new SecureRandom();
|
||||||
rndGen.setSeed(System.currentTimeMillis());
|
rndGen.setSeed(System.currentTimeMillis());
|
||||||
numBookMoves = 0;
|
numBookMoves = 0;
|
||||||
try {
|
try (InputStream inStream = getClass().getResourceAsStream("/book.bin")) {
|
||||||
InputStream inStream = getClass().getResourceAsStream("/book.bin");
|
|
||||||
List<Byte> buf = new ArrayList<>(8192);
|
List<Byte> buf = new ArrayList<>(8192);
|
||||||
byte[] tmpBuf = new byte[1024];
|
byte[] tmpBuf = new byte[1024];
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -64,7 +63,6 @@ public class Book {
|
|||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
buf.add(tmpBuf[i]);
|
buf.add(tmpBuf[i]);
|
||||||
}
|
}
|
||||||
inStream.close();
|
|
||||||
Position startPos = TextIO.readFEN(TextIO.startPosFEN);
|
Position startPos = TextIO.readFEN(TextIO.startPosFEN);
|
||||||
Position pos = new Position(startPos);
|
Position pos = new Position(startPos);
|
||||||
UndoInfo ui = new UndoInfo();
|
UndoInfo ui = new UndoInfo();
|
||||||
|
|||||||
@@ -236,8 +236,7 @@ public class Evaluate {
|
|||||||
|
|
||||||
private byte[] readTable(String resource, int length) {
|
private byte[] readTable(String resource, int length) {
|
||||||
byte[] table = new byte[2*32*64*48/8];
|
byte[] table = new byte[2*32*64*48/8];
|
||||||
InputStream inStream = getClass().getResourceAsStream(resource);
|
try (InputStream inStream = getClass().getResourceAsStream(resource)) {
|
||||||
try {
|
|
||||||
int off = 0;
|
int off = 0;
|
||||||
while (off < table.length) {
|
while (off < table.length) {
|
||||||
int len = inStream.read(table, off, table.length - off);
|
int len = inStream.read(table, off, table.length - off);
|
||||||
@@ -245,10 +244,9 @@ public class Evaluate {
|
|||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
off += len;
|
off += len;
|
||||||
}
|
}
|
||||||
inStream.close();
|
|
||||||
return table;
|
return table;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -790,14 +790,11 @@ public class DroidFish extends Activity
|
|||||||
if ((filename == null) &&
|
if ((filename == null) &&
|
||||||
("content".equals(scheme) || "file".equals(scheme))) {
|
("content".equals(scheme) || "file".equals(scheme))) {
|
||||||
ContentResolver resolver = getContentResolver();
|
ContentResolver resolver = getContentResolver();
|
||||||
InputStream in = resolver.openInputStream(data);
|
|
||||||
String sep = File.separator;
|
String sep = File.separator;
|
||||||
String fn = Environment.getExternalStorageDirectory() + sep +
|
String fn = Environment.getExternalStorageDirectory() + sep +
|
||||||
pgnDir + sep + ".sharedfile.pgn";
|
pgnDir + sep + ".sharedfile.pgn";
|
||||||
try {
|
try (InputStream in = resolver.openInputStream(data)) {
|
||||||
FileUtil.writeFile(in, fn);
|
FileUtil.writeFile(in, fn);
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
PGNFile pgnFile = new PGNFile(fn);
|
PGNFile pgnFile = new PGNFile(fn);
|
||||||
long fileLen = FileUtil.getFileLength(fn);
|
long fileLen = FileUtil.getFileLength(fn);
|
||||||
@@ -805,11 +802,8 @@ public class DroidFish extends Activity
|
|||||||
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 {
|
||||||
in = new FileInputStream(fn);
|
try (FileInputStream in = new FileInputStream(fn)) {
|
||||||
try {
|
|
||||||
pgnOrFen = FileUtil.readFromStream(in);
|
pgnOrFen = FileUtil.readFromStream(in);
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2394,14 +2388,9 @@ public class DroidFish extends Activity
|
|||||||
File dir = new File(getFilesDir(), "shared");
|
File dir = new File(getFilesDir(), "shared");
|
||||||
dir.mkdirs();
|
dir.mkdirs();
|
||||||
File file = new File(dir, game ? "game.pgn" : "game.txt");
|
File file = new File(dir, game ? "game.pgn" : "game.txt");
|
||||||
try {
|
try (FileOutputStream fos = new FileOutputStream(file);
|
||||||
FileOutputStream fos = new FileOutputStream(file);
|
OutputStreamWriter ow = new OutputStreamWriter(fos, "UTF-8")) {
|
||||||
OutputStreamWriter ow = new OutputStreamWriter(fos, "UTF-8");
|
ow.write(pgn);
|
||||||
try {
|
|
||||||
ow.write(pgn);
|
|
||||||
} finally {
|
|
||||||
ow.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
|
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
|
||||||
return;
|
return;
|
||||||
@@ -2427,13 +2416,8 @@ public class DroidFish extends Activity
|
|||||||
imgDir.mkdirs();
|
imgDir.mkdirs();
|
||||||
File file = new File(imgDir, "screenshot.png");
|
File file = new File(imgDir, "screenshot.png");
|
||||||
try {
|
try {
|
||||||
OutputStream os = null;
|
try (OutputStream os = new FileOutputStream(file)) {
|
||||||
try {
|
|
||||||
os = new FileOutputStream(file);
|
|
||||||
b.compress(Bitmap.CompressFormat.PNG, 100, os);
|
b.compress(Bitmap.CompressFormat.PNG, 100, os);
|
||||||
} finally {
|
|
||||||
if (os != null)
|
|
||||||
os.close();
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
|
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
|
||||||
@@ -2521,15 +2505,16 @@ public class DroidFish extends Activity
|
|||||||
|
|
||||||
private Dialog aboutDialog() {
|
private Dialog aboutDialog() {
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||||
String title = getString(R.string.app_name);
|
|
||||||
WebView wv = new WebView(this);
|
WebView wv = new WebView(this);
|
||||||
builder.setView(wv);
|
builder.setView(wv);
|
||||||
InputStream is = getResources().openRawResource(R.raw.about);
|
try (InputStream is = getResources().openRawResource(R.raw.about)) {
|
||||||
String data = FileUtil.readFromStream(is);
|
String data = FileUtil.readFromStream(is);
|
||||||
if (data == null)
|
if (data == null)
|
||||||
data = "";
|
data = "";
|
||||||
try { is.close(); } catch (IOException ignore) {}
|
wv.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
|
||||||
wv.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
|
} catch (IOException ignore) {
|
||||||
|
}
|
||||||
|
String title = getString(R.string.app_name);
|
||||||
try {
|
try {
|
||||||
PackageInfo pi = getPackageManager().getPackageInfo("org.petero.droidfish", 0);
|
PackageInfo pi = getPackageManager().getPackageInfo("org.petero.droidfish", 0);
|
||||||
title += " " + pi.versionName;
|
title += " " + pi.versionName;
|
||||||
@@ -2577,7 +2562,7 @@ public class DroidFish extends Activity
|
|||||||
else if (item == numFiles + 2)
|
else if (item == numFiles + 2)
|
||||||
bookFile = "nobook:";
|
bookFile = "nobook:";
|
||||||
else
|
else
|
||||||
bookFile = items[item].toString();
|
bookFile = items[item];
|
||||||
editor.putString("bookFile", bookFile);
|
editor.putString("bookFile", bookFile);
|
||||||
editor.apply();
|
editor.apply();
|
||||||
bookOptions.filename = bookFile;
|
bookOptions.filename = bookFile;
|
||||||
@@ -2664,12 +2649,12 @@ public class DroidFish extends Activity
|
|||||||
|
|
||||||
private Dialog selectPgnFileDialog() {
|
private Dialog selectPgnFileDialog() {
|
||||||
return selectFileDialog(pgnDir, R.string.select_pgn_file, R.string.no_pgn_files,
|
return selectFileDialog(pgnDir, R.string.select_pgn_file, R.string.no_pgn_files,
|
||||||
"currentPGNFile", pathName -> loadPGNFromFile(pathName));
|
"currentPGNFile", this::loadPGNFromFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dialog selectFenFileDialog() {
|
private Dialog selectFenFileDialog() {
|
||||||
return selectFileDialog(fenDir, R.string.select_fen_file, R.string.no_fen_files,
|
return selectFileDialog(fenDir, R.string.select_fen_file, R.string.no_fen_files,
|
||||||
"currentFENFile", pathName -> loadFENFromFile(pathName));
|
"currentFENFile", this::loadFENFromFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dialog selectFileDialog(final String defaultDir, int selectFileMsg, int noFilesMsg,
|
private Dialog selectFileDialog(final String defaultDir, int selectFileMsg, int noFilesMsg,
|
||||||
@@ -2696,7 +2681,7 @@ public class DroidFish extends Activity
|
|||||||
builder.setSingleChoiceItems(fileNames, defaultItem, (dialog, item) -> {
|
builder.setSingleChoiceItems(fileNames, defaultItem, (dialog, item) -> {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
String sep = File.separator;
|
String sep = File.separator;
|
||||||
String fn = fileNames[item].toString();
|
String fn = fileNames[item];
|
||||||
String pathName = Environment.getExternalStorageDirectory() + sep + defaultDir + sep + fn;
|
String pathName = Environment.getExternalStorageDirectory() + sep + defaultDir + sep + fn;
|
||||||
loader.load(pathName);
|
loader.load(pathName);
|
||||||
});
|
});
|
||||||
@@ -3429,12 +3414,10 @@ public class DroidFish extends Activity
|
|||||||
final Runnable writeConfig = () -> {
|
final Runnable writeConfig = () -> {
|
||||||
String hostName1 = hostNameView.getText().toString();
|
String hostName1 = hostNameView.getText().toString();
|
||||||
String port1 = portView.getText().toString();
|
String port1 = portView.getText().toString();
|
||||||
try {
|
try (FileWriter fw = new FileWriter(new File(networkEngineToConfig), false)) {
|
||||||
FileWriter fw = new FileWriter(new File(networkEngineToConfig), false);
|
|
||||||
fw.write("NETE\n");
|
fw.write("NETE\n");
|
||||||
fw.write(hostName1); fw.write("\n");
|
fw.write(hostName1); fw.write("\n");
|
||||||
fw.write(port1); fw.write("\n");
|
fw.write(port1); fw.write("\n");
|
||||||
fw.close();
|
|
||||||
setEngineOptions(true);
|
setEngineOptions(true);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
|
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
|
||||||
|
|||||||
@@ -33,29 +33,26 @@ public class FileUtil {
|
|||||||
/** Read a text file. Return string array with one string per line. */
|
/** Read a text file. Return string array with one string per line. */
|
||||||
public static String[] readFile(String filename) throws IOException {
|
public static String[] readFile(String filename) throws IOException {
|
||||||
ArrayList<String> ret = new ArrayList<>();
|
ArrayList<String> ret = new ArrayList<>();
|
||||||
InputStream inStream = new FileInputStream(filename);
|
try (InputStream inStream = new FileInputStream(filename);
|
||||||
InputStreamReader inFile = new InputStreamReader(inStream, "UTF-8");
|
InputStreamReader inFile = new InputStreamReader(inStream, "UTF-8");
|
||||||
BufferedReader inBuf = new BufferedReader(inFile);
|
BufferedReader inBuf = new BufferedReader(inFile)) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = inBuf.readLine()) != null)
|
while ((line = inBuf.readLine()) != null)
|
||||||
ret.add(line);
|
ret.add(line);
|
||||||
inBuf.close();
|
return ret.toArray(new String[0]);
|
||||||
return ret.toArray(new String[0]);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read all data from an input stream. Return null if IO error. */
|
/** Read all data from an input stream. Return null if IO error. */
|
||||||
public static String readFromStream(InputStream is) {
|
public static String readFromStream(InputStream is) {
|
||||||
InputStreamReader isr;
|
try (InputStreamReader isr = new InputStreamReader(is, "UTF-8");
|
||||||
try {
|
BufferedReader br = new BufferedReader(isr)) {
|
||||||
isr = new InputStreamReader(is, "UTF-8");
|
|
||||||
BufferedReader br = new BufferedReader(isr);
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
String line;
|
String line;
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
sb.append(line);
|
sb.append(line);
|
||||||
sb.append('\n');
|
sb.append('\n');
|
||||||
}
|
}
|
||||||
br.close();
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
return null;
|
return null;
|
||||||
@@ -66,8 +63,7 @@ public class FileUtil {
|
|||||||
|
|
||||||
/** Read data from input stream and write to file. */
|
/** Read data from input stream and write to file. */
|
||||||
public static void writeFile(InputStream is, String outFile) throws IOException {
|
public static void writeFile(InputStream is, String outFile) throws IOException {
|
||||||
OutputStream os = new FileOutputStream(outFile);
|
try (OutputStream os = new FileOutputStream(outFile)) {
|
||||||
try {
|
|
||||||
byte[] buffer = new byte[16384];
|
byte[] buffer = new byte[16384];
|
||||||
while (true) {
|
while (true) {
|
||||||
int len = is.read(buffer);
|
int len = is.read(buffer);
|
||||||
@@ -75,20 +71,13 @@ public class FileUtil {
|
|||||||
break;
|
break;
|
||||||
os.write(buffer, 0, len);
|
os.write(buffer, 0, len);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
os.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return the length of a file, or -1 if length can not be determined. */
|
/** Return the length of a file, or -1 if length can not be determined. */
|
||||||
public static long getFileLength(String filename) {
|
public static long getFileLength(String filename) {
|
||||||
try {
|
try (RandomAccessFile raf = new RandomAccessFile(filename, "r")) {
|
||||||
RandomAccessFile raf = new RandomAccessFile(filename, "r");
|
return raf.length();
|
||||||
try {
|
|
||||||
return raf.length();
|
|
||||||
} finally {
|
|
||||||
raf.close();
|
|
||||||
}
|
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,12 +130,9 @@ public class ObjectCache {
|
|||||||
token++;
|
token++;
|
||||||
File f = new File(dir, String.valueOf(token));
|
File f = new File(dir, String.valueOf(token));
|
||||||
if (f.createNewFile()) {
|
if (f.createNewFile()) {
|
||||||
FileOutputStream fos = new FileOutputStream(f);
|
try (FileOutputStream fos = new FileOutputStream(f)) {
|
||||||
try {
|
|
||||||
fos.write(b);
|
fos.write(b);
|
||||||
return token;
|
return token;
|
||||||
} finally {
|
|
||||||
fos.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -152,8 +149,7 @@ public class ObjectCache {
|
|||||||
if (dir.exists()) {
|
if (dir.exists()) {
|
||||||
File f = new File(dir, String.valueOf(token));
|
File f = new File(dir, String.valueOf(token));
|
||||||
try {
|
try {
|
||||||
RandomAccessFile raf = new RandomAccessFile(f, "r");
|
try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
|
||||||
try {
|
|
||||||
int len = (int)raf.length();
|
int len = (int)raf.length();
|
||||||
byte[] buf = new byte[len];
|
byte[] buf = new byte[len];
|
||||||
int offs = 0;
|
int offs = 0;
|
||||||
@@ -164,8 +160,6 @@ public class ObjectCache {
|
|||||||
offs += l;
|
offs += l;
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
} finally {
|
|
||||||
raf.close();
|
|
||||||
}
|
}
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,8 +106,7 @@ public class PieceSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void parseSvgData() {
|
private void parseSvgData() {
|
||||||
try {
|
try (ZipInputStream zis = getZipStream()) {
|
||||||
ZipInputStream zis = getZipStream();
|
|
||||||
ZipEntry entry;
|
ZipEntry entry;
|
||||||
while ((entry = zis.getNextEntry()) != null) {
|
while ((entry = zis.getNextEntry()) != null) {
|
||||||
if (!entry.isDirectory()) {
|
if (!entry.isDirectory()) {
|
||||||
@@ -129,7 +128,6 @@ public class PieceSet {
|
|||||||
}
|
}
|
||||||
zis.closeEntry();
|
zis.closeEntry();
|
||||||
}
|
}
|
||||||
zis.close();
|
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new RuntimeException("Cannot read chess pieces data", ex);
|
throw new RuntimeException("Cannot read chess pieces data", ex);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,11 +17,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.petero.droidfish.activities;
|
package org.petero.droidfish.activities;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
|
|
||||||
final class BufferedRandomAccessFileReader {
|
final class BufferedRandomAccessFileReader implements Closeable {
|
||||||
private RandomAccessFile f;
|
private RandomAccessFile f;
|
||||||
private byte[] buffer = new byte[8192];
|
private byte[] buffer = new byte[8192];
|
||||||
private long bufStartFilePos = 0;
|
private long bufStartFilePos = 0;
|
||||||
@@ -37,7 +38,8 @@ final class BufferedRandomAccessFileReader {
|
|||||||
final long getFilePointer() {
|
final long getFilePointer() {
|
||||||
return bufStartFilePos + bufPos;
|
return bufStartFilePos + bufPos;
|
||||||
}
|
}
|
||||||
final void close() throws IOException {
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
f.close();
|
f.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,10 +66,9 @@ public class FENFile {
|
|||||||
public final Pair<FenInfoResult,ArrayList<FenInfo>> getFenInfo(Activity activity,
|
public final Pair<FenInfoResult,ArrayList<FenInfo>> getFenInfo(Activity activity,
|
||||||
final ProgressDialog progress) {
|
final ProgressDialog progress) {
|
||||||
ArrayList<FenInfo> fensInFile = new ArrayList<>();
|
ArrayList<FenInfo> fensInFile = new ArrayList<>();
|
||||||
try {
|
try (BufferedRandomAccessFileReader f =
|
||||||
|
new BufferedRandomAccessFileReader(fileName.getAbsolutePath())) {
|
||||||
int percent = -1;
|
int percent = -1;
|
||||||
fensInFile.clear();
|
|
||||||
BufferedRandomAccessFileReader f = new BufferedRandomAccessFileReader(fileName.getAbsolutePath());
|
|
||||||
long fileLen = f.length();
|
long fileLen = f.length();
|
||||||
long filePos = 0;
|
long filePos = 0;
|
||||||
int fenNo = 1;
|
int fenNo = 1;
|
||||||
@@ -92,7 +91,6 @@ public class FENFile {
|
|||||||
if (Thread.currentThread().isInterrupted())
|
if (Thread.currentThread().isInterrupted())
|
||||||
return new Pair<>(FenInfoResult.CANCEL, null);
|
return new Pair<>(FenInfoResult.CANCEL, null);
|
||||||
}
|
}
|
||||||
f.close();
|
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
} catch (OutOfMemoryError e) {
|
} catch (OutOfMemoryError e) {
|
||||||
fensInFile.clear();
|
fensInFile.clear();
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
package org.petero.droidfish.activities;
|
package org.petero.droidfish.activities;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
@@ -133,7 +134,7 @@ public class PGNFile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class BufferedInput {
|
private static class BufferedInput implements Closeable {
|
||||||
private byte buf[] = new byte[8192];
|
private byte buf[] = new byte[8192];
|
||||||
private int bufLen = 0;
|
private int bufLen = 0;
|
||||||
private int pos = 0;
|
private int pos = 0;
|
||||||
@@ -151,6 +152,7 @@ public class PGNFile {
|
|||||||
}
|
}
|
||||||
return buf[pos++] & 0xff;
|
return buf[pos++] & 0xff;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
try {
|
try {
|
||||||
is.close();
|
is.close();
|
||||||
@@ -382,12 +384,10 @@ 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) {
|
final String readOneGame(GameInfo gi) {
|
||||||
try {
|
try (RandomAccessFile f = new RandomAccessFile(fileName, "r")) {
|
||||||
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);
|
||||||
f.readFully(pgnData);
|
f.readFully(pgnData);
|
||||||
f.close();
|
|
||||||
return new String(pgnData);
|
return new String(pgnData);
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
}
|
}
|
||||||
@@ -396,11 +396,9 @@ 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 final void appendPGN(String pgn) {
|
||||||
try {
|
mkDirs();
|
||||||
mkDirs();
|
try (FileWriter fw = new FileWriter(fileName, true)) {
|
||||||
FileWriter fw = new FileWriter(fileName, true);
|
|
||||||
fw.write(pgn);
|
fw.write(pgn);
|
||||||
fw.close();
|
|
||||||
DroidFishApp.toast(R.string.game_saved, Toast.LENGTH_SHORT);
|
DroidFishApp.toast(R.string.game_saved, Toast.LENGTH_SHORT);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
DroidFishApp.toast(R.string.failed_to_save_game, Toast.LENGTH_SHORT);
|
DroidFishApp.toast(R.string.failed_to_save_game, Toast.LENGTH_SHORT);
|
||||||
@@ -410,13 +408,12 @@ public class PGNFile {
|
|||||||
final boolean deleteGame(GameInfo gi, ArrayList<GameInfo> gamesInFile) {
|
final boolean deleteGame(GameInfo gi, ArrayList<GameInfo> gamesInFile) {
|
||||||
try {
|
try {
|
||||||
File tmpFile = new File(fileName + ".tmp_delete");
|
File tmpFile = new File(fileName + ".tmp_delete");
|
||||||
RandomAccessFile fileReader = new RandomAccessFile(fileName, "r");
|
try (RandomAccessFile fileReader = new RandomAccessFile(fileName, "r");
|
||||||
RandomAccessFile fileWriter = new RandomAccessFile(tmpFile, "rw");
|
RandomAccessFile fileWriter = new RandomAccessFile(tmpFile, "rw")) {
|
||||||
copyData(fileReader, fileWriter, gi.startPos);
|
copyData(fileReader, fileWriter, gi.startPos);
|
||||||
fileReader.seek(gi.endPos);
|
fileReader.seek(gi.endPos);
|
||||||
copyData(fileReader, fileWriter, fileReader.length() - gi.endPos);
|
copyData(fileReader, fileWriter, fileReader.length() - gi.endPos);
|
||||||
fileReader.close();
|
}
|
||||||
fileWriter.close();
|
|
||||||
if (!tmpFile.renameTo(fileName))
|
if (!tmpFile.renameTo(fileName))
|
||||||
throw new IOException();
|
throw new IOException();
|
||||||
|
|
||||||
@@ -443,14 +440,13 @@ public class PGNFile {
|
|||||||
final void replacePGN(String pgnToSave, GameInfo gi) {
|
final void replacePGN(String pgnToSave, GameInfo gi) {
|
||||||
try {
|
try {
|
||||||
File tmpFile = new File(fileName + ".tmp_delete");
|
File tmpFile = new File(fileName + ".tmp_delete");
|
||||||
RandomAccessFile fileReader = new RandomAccessFile(fileName, "r");
|
try (RandomAccessFile fileReader = new RandomAccessFile(fileName, "r");
|
||||||
RandomAccessFile fileWriter = new RandomAccessFile(tmpFile, "rw");
|
RandomAccessFile fileWriter = new RandomAccessFile(tmpFile, "rw")) {
|
||||||
copyData(fileReader, fileWriter, gi.startPos);
|
copyData(fileReader, fileWriter, gi.startPos);
|
||||||
fileWriter.write(pgnToSave.getBytes());
|
fileWriter.write(pgnToSave.getBytes());
|
||||||
fileReader.seek(gi.endPos);
|
fileReader.seek(gi.endPos);
|
||||||
copyData(fileReader, fileWriter, fileReader.length() - gi.endPos);
|
copyData(fileReader, fileWriter, fileReader.length() - gi.endPos);
|
||||||
fileReader.close();
|
}
|
||||||
fileWriter.close();
|
|
||||||
if (!tmpFile.renameTo(fileName))
|
if (!tmpFile.renameTo(fileName))
|
||||||
throw new IOException();
|
throw new IOException();
|
||||||
DroidFishApp.toast(R.string.game_saved, Toast.LENGTH_SHORT);
|
DroidFishApp.toast(R.string.game_saved, Toast.LENGTH_SHORT);
|
||||||
|
|||||||
@@ -63,13 +63,9 @@ class CtgBook implements IOpeningBook {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<BookEntry> getBookEntries(Position pos) {
|
public ArrayList<BookEntry> getBookEntries(Position pos) {
|
||||||
RandomAccessFile ctgF = null;
|
try (RandomAccessFile ctgF = new RandomAccessFile(ctgFile, "r");
|
||||||
RandomAccessFile ctbF = null;
|
RandomAccessFile ctbF = new RandomAccessFile(ctbFile, "r");
|
||||||
RandomAccessFile ctoF = null;
|
RandomAccessFile ctoF = new RandomAccessFile(ctoFile, "r")) {
|
||||||
try {
|
|
||||||
ctgF = new RandomAccessFile(ctgFile, "r");
|
|
||||||
ctbF = new RandomAccessFile(ctbFile, "r");
|
|
||||||
ctoF = new RandomAccessFile(ctoFile, "r");
|
|
||||||
|
|
||||||
CtbFile ctb = new CtbFile(ctbF);
|
CtbFile ctb = new CtbFile(ctbF);
|
||||||
CtoFile cto = new CtoFile(ctoF);
|
CtoFile cto = new CtoFile(ctoF);
|
||||||
@@ -119,10 +115,6 @@ class CtgBook implements IOpeningBook {
|
|||||||
return ret;
|
return ret;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
|
||||||
if (ctgF != null) try { ctgF.close(); } catch (IOException ignore) { }
|
|
||||||
if (ctbF != null) try { ctbF.close(); } catch (IOException ignore) { }
|
|
||||||
if (ctoF != null) try { ctoF.close(); } catch (IOException ignore) { }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -252,16 +252,14 @@ public class EcoDb {
|
|||||||
posHashToNodeIdx = new HashMap<>();
|
posHashToNodeIdx = new HashMap<>();
|
||||||
posHashToNodeIdx2 = new HashMap<>();
|
posHashToNodeIdx2 = new HashMap<>();
|
||||||
gtNodeToIdx = new WeakLRUCache<>(50);
|
gtNodeToIdx = new WeakLRUCache<>(50);
|
||||||
try {
|
try (ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
|
||||||
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
|
InputStream inStream = DroidFishApp.getContext().getAssets().open("eco.dat")) {
|
||||||
InputStream inStream = DroidFishApp.getContext().getAssets().open("eco.dat");
|
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
while (true) {
|
while (true) {
|
||||||
int len = inStream.read(buf);
|
int len = inStream.read(buf);
|
||||||
if (len <= 0) break;
|
if (len <= 0) break;
|
||||||
bufStream.write(buf, 0, len);
|
bufStream.write(buf, 0, len);
|
||||||
}
|
}
|
||||||
inStream.close();
|
|
||||||
bufStream.flush();
|
bufStream.flush();
|
||||||
buf = bufStream.toByteArray();
|
buf = bufStream.toByteArray();
|
||||||
int nNodes = 0;
|
int nNodes = 0;
|
||||||
|
|||||||
@@ -372,8 +372,7 @@ class PolyglotBook implements IOpeningBook {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final ArrayList<BookEntry> getBookEntries(Position pos) {
|
public final ArrayList<BookEntry> getBookEntries(Position pos) {
|
||||||
try {
|
try (RandomAccessFile f = new RandomAccessFile(bookFile, "r")) {
|
||||||
RandomAccessFile f = new RandomAccessFile(bookFile, "r");
|
|
||||||
long numEntries = f.length() / 16;
|
long numEntries = f.length() / 16;
|
||||||
long key = getHashKey(pos);
|
long key = getHashKey(pos);
|
||||||
PGBookEntry ent = new PGBookEntry();
|
PGBookEntry ent = new PGBookEntry();
|
||||||
@@ -406,10 +405,7 @@ class PolyglotBook implements IOpeningBook {
|
|||||||
ret.add(be);
|
ret.add(be);
|
||||||
entNo++;
|
entNo++;
|
||||||
}
|
}
|
||||||
f.close();
|
|
||||||
return ret;
|
return ret;
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
return null;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,13 +47,11 @@ public class EngineUtil {
|
|||||||
/** Return true if file "engine" is a network engine. */
|
/** Return true if file "engine" is a network engine. */
|
||||||
public static boolean isNetEngine(String engine) {
|
public static boolean isNetEngine(String engine) {
|
||||||
boolean netEngine = false;
|
boolean netEngine = false;
|
||||||
try {
|
try (InputStream inStream = new FileInputStream(engine);
|
||||||
InputStream inStream = new FileInputStream(engine);
|
InputStreamReader inFile = new InputStreamReader(inStream)) {
|
||||||
InputStreamReader inFile = new InputStreamReader(inStream);
|
|
||||||
char[] buf = new char[4];
|
char[] buf = new char[4];
|
||||||
if ((inFile.read(buf) == 4) && "NETE".equals(new String(buf)))
|
if ((inFile.read(buf) == 4) && "NETE".equals(new String(buf)))
|
||||||
netEngine = true;
|
netEngine = true;
|
||||||
inFile.close();
|
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
}
|
}
|
||||||
return netEngine;
|
return netEngine;
|
||||||
|
|||||||
@@ -302,19 +302,14 @@ public class ExternalEngine extends UCIEngineBase {
|
|||||||
if (to.exists())
|
if (to.exists())
|
||||||
to.delete();
|
to.delete();
|
||||||
to.createNewFile();
|
to.createNewFile();
|
||||||
FileInputStream fis = null;
|
try (FileInputStream fis = new FileInputStream(from);
|
||||||
FileOutputStream fos = null;
|
FileChannel inFC = fis.getChannel();
|
||||||
try {
|
FileOutputStream fos = new FileOutputStream(to);
|
||||||
fis = new FileInputStream(from);
|
FileChannel outFC = fos.getChannel()) {
|
||||||
FileChannel inFC = fis.getChannel();
|
|
||||||
fos = new FileOutputStream(to);
|
|
||||||
FileChannel outFC = fos.getChannel();
|
|
||||||
long cnt = outFC.transferFrom(inFC, 0, inFC.size());
|
long cnt = outFC.transferFrom(inFC, 0, inFC.size());
|
||||||
if (cnt < inFC.size())
|
if (cnt < inFC.size())
|
||||||
throw new IOException("File copy failed");
|
throw new IOException("File copy failed");
|
||||||
} finally {
|
} finally {
|
||||||
if (fis != null) { try { fis.close(); } catch (IOException ignore) {} }
|
|
||||||
if (fos != null) { try { fos.close(); } catch (IOException ignore) {} }
|
|
||||||
to.setLastModified(from.lastModified());
|
to.setLastModified(from.lastModified());
|
||||||
}
|
}
|
||||||
return to.getAbsolutePath();
|
return to.getAbsolutePath();
|
||||||
|
|||||||
@@ -63,34 +63,25 @@ public class InternalStockFish extends ExternalEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private long readCheckSum(File f) {
|
private long readCheckSum(File f) {
|
||||||
InputStream is = null;
|
try (InputStream is = new FileInputStream(f);
|
||||||
try {
|
DataInputStream dis = new DataInputStream(is)) {
|
||||||
is = new FileInputStream(f);
|
|
||||||
DataInputStream dis = new DataInputStream(is);
|
|
||||||
return dis.readLong();
|
return dis.readLong();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return 0;
|
return 0;
|
||||||
} finally {
|
|
||||||
if (is != null) try { is.close(); } catch (IOException ignore) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeCheckSum(File f, long checkSum) {
|
private void writeCheckSum(File f, long checkSum) {
|
||||||
DataOutputStream dos = null;
|
try (OutputStream os = new FileOutputStream(f);
|
||||||
try {
|
DataOutputStream dos = new DataOutputStream(os)) {
|
||||||
OutputStream os = new FileOutputStream(f);
|
|
||||||
dos = new DataOutputStream(os);
|
|
||||||
dos.writeLong(checkSum);
|
dos.writeLong(checkSum);
|
||||||
} catch (IOException e) {
|
} catch (IOException ignore) {
|
||||||
} finally {
|
|
||||||
if (dos != null) try { dos.close(); } catch (IOException ignore) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private long computeAssetsCheckSum(String sfExe) {
|
private long computeAssetsCheckSum(String sfExe) {
|
||||||
InputStream is = null;
|
|
||||||
try {
|
try (InputStream is = context.getAssets().open(sfExe)) {
|
||||||
is = context.getAssets().open(sfExe);
|
|
||||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||||
byte[] buf = new byte[8192];
|
byte[] buf = new byte[8192];
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -109,8 +100,6 @@ public class InternalStockFish extends ExternalEngine {
|
|||||||
return -1;
|
return -1;
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
return -1;
|
return -1;
|
||||||
} finally {
|
|
||||||
if (is != null) try { is.close(); } catch (IOException ignore) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,10 +119,8 @@ public class InternalStockFish extends ExternalEngine {
|
|||||||
to.delete();
|
to.delete();
|
||||||
to.createNewFile();
|
to.createNewFile();
|
||||||
|
|
||||||
InputStream is = context.getAssets().open(sfExe);
|
try (InputStream is = context.getAssets().open(sfExe);
|
||||||
OutputStream os = new FileOutputStream(to);
|
OutputStream os = new FileOutputStream(to)) {
|
||||||
|
|
||||||
try {
|
|
||||||
byte[] buf = new byte[8192];
|
byte[] buf = new byte[8192];
|
||||||
while (true) {
|
while (true) {
|
||||||
int len = is.read(buf);
|
int len = is.read(buf);
|
||||||
@@ -141,9 +128,6 @@ public class InternalStockFish extends ExternalEngine {
|
|||||||
break;
|
break;
|
||||||
os.write(buf, 0, len);
|
os.write(buf, 0, len);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
if (is != null) try { is.close(); } catch (IOException ignore) {}
|
|
||||||
if (os != null) try { os.close(); } catch (IOException ignore) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
writeCheckSum(new File(internalSFPath()), newCSum);
|
writeCheckSum(new File(internalSFPath()), newCSum);
|
||||||
|
|||||||
@@ -76,14 +76,9 @@ public abstract class UCIEngineBase implements UCIEngine {
|
|||||||
public final void applyIniFile() {
|
public final void applyIniFile() {
|
||||||
File optionsFile = getOptionsFile();
|
File optionsFile = getOptionsFile();
|
||||||
Properties iniOptions = new Properties();
|
Properties iniOptions = new Properties();
|
||||||
FileInputStream is = null;
|
try (FileInputStream is = new FileInputStream(optionsFile)) {
|
||||||
try {
|
|
||||||
is = new FileInputStream(optionsFile);
|
|
||||||
iniOptions.load(is);
|
iniOptions.load(is);
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
} finally {
|
|
||||||
if (is != null)
|
|
||||||
try { is.close(); } catch (IOException ignore) {}
|
|
||||||
}
|
}
|
||||||
for (Map.Entry<Object,Object> ent : iniOptions.entrySet()) {
|
for (Map.Entry<Object,Object> ent : iniOptions.entrySet()) {
|
||||||
if (ent.getKey() instanceof String && ent.getValue() instanceof String) {
|
if (ent.getKey() instanceof String && ent.getValue() instanceof String) {
|
||||||
@@ -116,14 +111,9 @@ public abstract class UCIEngineBase implements UCIEngine {
|
|||||||
iniOptions.put(o.name, o.getStringValue());
|
iniOptions.put(o.name, o.getStringValue());
|
||||||
}
|
}
|
||||||
File optionsFile = getOptionsFile();
|
File optionsFile = getOptionsFile();
|
||||||
FileOutputStream os = null;
|
try (FileOutputStream os = new FileOutputStream(optionsFile)) {
|
||||||
try {
|
|
||||||
os = new FileOutputStream(optionsFile);
|
|
||||||
iniOptions.store(os, null);
|
iniOptions.store(os, null);
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
} finally {
|
|
||||||
if (os != null)
|
|
||||||
try { os.close(); } catch (IOException ignore) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -211,40 +211,24 @@ public class DroidChessController {
|
|||||||
|
|
||||||
/** De-serialize from byte array. */
|
/** De-serialize from byte array. */
|
||||||
public final synchronized void fromByteArray(byte[] data, int version) {
|
public final synchronized void fromByteArray(byte[] data, int version) {
|
||||||
ByteArrayInputStream bais = null;
|
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||||
DataInputStream dis = null;
|
DataInputStream dis = new DataInputStream(bais)) {
|
||||||
try {
|
|
||||||
bais = new ByteArrayInputStream(data);
|
|
||||||
dis = new DataInputStream(bais);
|
|
||||||
game.readFromStream(dis, version);
|
game.readFromStream(dis, version);
|
||||||
game.tree.translateMoves();
|
game.tree.translateMoves();
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
} catch (ChessParseError ignore) {
|
} catch (ChessParseError ignore) {
|
||||||
} finally {
|
|
||||||
if (dis != null)
|
|
||||||
try { dis.close(); } catch (IOException ignore) {}
|
|
||||||
if (bais != null)
|
|
||||||
try { bais.close(); } catch (IOException ignore) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Serialize to byte array. */
|
/** Serialize to byte array. */
|
||||||
public final synchronized byte[] toByteArray() {
|
public final synchronized byte[] toByteArray() {
|
||||||
ByteArrayOutputStream baos = null;
|
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32768);
|
||||||
DataOutputStream dos = null;
|
DataOutputStream dos = new DataOutputStream(baos)) {
|
||||||
try {
|
|
||||||
baos = new ByteArrayOutputStream(32768);
|
|
||||||
dos = new DataOutputStream(baos);
|
|
||||||
game.writeToStream(dos);
|
game.writeToStream(dos);
|
||||||
dos.flush();
|
dos.flush();
|
||||||
return baos.toByteArray();
|
return baos.toByteArray();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
|
||||||
if (dos != null)
|
|
||||||
try { dos.close(); } catch (IOException ignore) {}
|
|
||||||
if (baos != null)
|
|
||||||
try { baos.close(); } catch (IOException ignore) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Controls behavior when a new move is added to the game.*/
|
/** Controls behavior when a new move is added to the game.*/
|
||||||
public static enum AddMoveBehavior {
|
public enum AddMoveBehavior {
|
||||||
/** Add the new move first in the list of variations. */
|
/** Add the new move first in the list of variations. */
|
||||||
ADD_FIRST,
|
ADD_FIRST,
|
||||||
/** Add the new move last in the list of variations. */
|
/** Add the new move last in the list of variations. */
|
||||||
|
|||||||
@@ -59,8 +59,7 @@ public class Book {
|
|||||||
rndGen = new SecureRandom();
|
rndGen = new SecureRandom();
|
||||||
rndGen.setSeed(System.currentTimeMillis());
|
rndGen.setSeed(System.currentTimeMillis());
|
||||||
numBookMoves = 0;
|
numBookMoves = 0;
|
||||||
try {
|
try (InputStream inStream = getClass().getResourceAsStream("/book.bin")) {
|
||||||
InputStream inStream = getClass().getResourceAsStream("/book.bin");
|
|
||||||
List<Byte> buf = new ArrayList<>(8192);
|
List<Byte> buf = new ArrayList<>(8192);
|
||||||
byte[] tmpBuf = new byte[1024];
|
byte[] tmpBuf = new byte[1024];
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -69,7 +68,6 @@ public class Book {
|
|||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
buf.add(tmpBuf[i]);
|
buf.add(tmpBuf[i]);
|
||||||
}
|
}
|
||||||
inStream.close();
|
|
||||||
Position startPos = TextIO.readFEN(TextIO.startPosFEN);
|
Position startPos = TextIO.readFEN(TextIO.startPosFEN);
|
||||||
Position pos = new Position(startPos);
|
Position pos = new Position(startPos);
|
||||||
UndoInfo ui = new UndoInfo();
|
UndoInfo ui = new UndoInfo();
|
||||||
@@ -191,22 +189,21 @@ public class Book {
|
|||||||
}
|
}
|
||||||
public static void main2(String inFile, String outFile) throws IOException {
|
public static void main2(String inFile, String outFile) throws IOException {
|
||||||
List<Byte> binBook = createBinBook(inFile);
|
List<Byte> binBook = createBinBook(inFile);
|
||||||
FileOutputStream out = new FileOutputStream(outFile);
|
try (FileOutputStream out = new FileOutputStream(outFile)) {
|
||||||
int bookLen = binBook.size();
|
int bookLen = binBook.size();
|
||||||
byte[] binBookA = new byte[bookLen];
|
byte[] binBookA = new byte[bookLen];
|
||||||
for (int i = 0; i < bookLen; i++)
|
for (int i = 0; i < bookLen; i++)
|
||||||
binBookA[i] = binBook.get(i);
|
binBookA[i] = binBook.get(i);
|
||||||
out.write(binBookA);
|
out.write(binBookA);
|
||||||
out.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Byte> createBinBook(String inFileName) {
|
public static List<Byte> createBinBook(String inFileName) {
|
||||||
List<Byte> binBook = new ArrayList<>(0);
|
List<Byte> binBook = new ArrayList<>(0);
|
||||||
try {
|
try (InputStream inStream = new FileInputStream(inFileName);
|
||||||
InputStream inStream = new FileInputStream(inFileName);
|
|
||||||
InputStreamReader inFile = new InputStreamReader(inStream);
|
InputStreamReader inFile = new InputStreamReader(inStream);
|
||||||
BufferedReader inBuf = new BufferedReader(inFile);
|
BufferedReader inBuf = new BufferedReader(inFile);
|
||||||
LineNumberReader lnr = new LineNumberReader(inBuf);
|
LineNumberReader lnr = new LineNumberReader(inBuf)) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = lnr.readLine()) != null) {
|
while ((line = lnr.readLine()) != null) {
|
||||||
if (line.startsWith("#") || (line.length() == 0)) {
|
if (line.startsWith("#") || (line.length() == 0)) {
|
||||||
@@ -218,7 +215,6 @@ public class Book {
|
|||||||
}
|
}
|
||||||
// System.out.printf("no:%d line:%s%n", lnr.getLineNumber(), line);
|
// System.out.printf("no:%d line:%s%n", lnr.getLineNumber(), line);
|
||||||
}
|
}
|
||||||
lnr.close();
|
|
||||||
} catch (ChessParseError ex) {
|
} catch (ChessParseError ex) {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
|
|||||||
@@ -151,51 +151,50 @@ public class EcoBuilder {
|
|||||||
|
|
||||||
/** Write the binary ECO code data file. */
|
/** Write the binary ECO code data file. */
|
||||||
private void writeDataFile(String ecoDatFile) throws Throwable {
|
private void writeDataFile(String ecoDatFile) throws Throwable {
|
||||||
FileOutputStream out = new FileOutputStream(ecoDatFile);
|
try (FileOutputStream out = new FileOutputStream(ecoDatFile)) {
|
||||||
|
|
||||||
// Write nodes
|
// Write nodes
|
||||||
byte[] buf = new byte[12];
|
byte[] buf = new byte[12];
|
||||||
for (int i = 0; i < nodes.size(); i++) {
|
for (int i = 0; i < nodes.size(); i++) {
|
||||||
Node n = nodes.get(i);
|
Node n = nodes.get(i);
|
||||||
int cm = n.move == null ? 0 : n.move.getCompressedMove();
|
int cm = n.move == null ? 0 : n.move.getCompressedMove();
|
||||||
buf[0] = (byte)(cm >> 8); // Move, high byte
|
buf[0] = (byte)(cm >> 8); // Move, high byte
|
||||||
buf[1] = (byte)(cm & 255); // Move, low byte
|
buf[1] = (byte)(cm & 255); // Move, low byte
|
||||||
buf[2] = (byte)(n.ecoIdx >> 8); // Index, high byte
|
buf[2] = (byte)(n.ecoIdx >> 8); // Index, high byte
|
||||||
buf[3] = (byte)(n.ecoIdx & 255); // Index, low byte
|
buf[3] = (byte)(n.ecoIdx & 255); // Index, low byte
|
||||||
buf[4] = (byte)(n.opnIdx >> 8); // Index, high byte
|
buf[4] = (byte)(n.opnIdx >> 8); // Index, high byte
|
||||||
buf[5] = (byte)(n.opnIdx & 255); // Index, low byte
|
buf[5] = (byte)(n.opnIdx & 255); // Index, low byte
|
||||||
buf[6] = (byte)(n.varIdx >> 8); // Index, high byte
|
buf[6] = (byte)(n.varIdx >> 8); // Index, high byte
|
||||||
buf[7] = (byte)(n.varIdx & 255); // Index, low byte
|
buf[7] = (byte)(n.varIdx & 255); // Index, low byte
|
||||||
int firstChild = -1;
|
int firstChild = -1;
|
||||||
if (n.children.size() > 0)
|
if (n.children.size() > 0)
|
||||||
firstChild = n.children.get(0).index;
|
firstChild = n.children.get(0).index;
|
||||||
buf[8] = (byte)(firstChild >> 8);
|
buf[8] = (byte)(firstChild >> 8);
|
||||||
buf[9] = (byte)(firstChild & 255);
|
buf[9] = (byte)(firstChild & 255);
|
||||||
int nextSibling = -1;
|
int nextSibling = -1;
|
||||||
if (n.parent != null) {
|
if (n.parent != null) {
|
||||||
ArrayList<Node> siblings = n.parent.children;
|
ArrayList<Node> siblings = n.parent.children;
|
||||||
for (int j = 0; j < siblings.size()-1; j++) {
|
for (int j = 0; j < siblings.size()-1; j++) {
|
||||||
if (siblings.get(j).move.equals(n.move)) {
|
if (siblings.get(j).move.equals(n.move)) {
|
||||||
nextSibling = siblings.get(j+1).index;
|
nextSibling = siblings.get(j+1).index;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
buf[10] = (byte)(nextSibling >> 8);
|
||||||
|
buf[11] = (byte)(nextSibling & 255);
|
||||||
|
out.write(buf);
|
||||||
}
|
}
|
||||||
buf[10] = (byte)(nextSibling >> 8);
|
for (int i = 0; i < buf.length; i++)
|
||||||
buf[11] = (byte)(nextSibling & 255);
|
buf[i] = -1;
|
||||||
out.write(buf);
|
out.write(buf);
|
||||||
}
|
|
||||||
for (int i = 0; i < buf.length; i++)
|
|
||||||
buf[i] = -1;
|
|
||||||
out.write(buf);
|
|
||||||
|
|
||||||
// Write strings
|
// Write strings
|
||||||
buf = new byte[]{0};
|
buf = new byte[]{0};
|
||||||
for (String name : strs) {
|
for (String name : strs) {
|
||||||
out.write(name.getBytes("UTF-8"));
|
out.write(name.getBytes("UTF-8"));
|
||||||
out.write(buf);
|
out.write(buf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,13 +33,13 @@ public class FileUtil {
|
|||||||
/** Read a text file. Return string array with one string per line. */
|
/** Read a text file. Return string array with one string per line. */
|
||||||
public static String[] readFile(String filename) throws IOException {
|
public static String[] readFile(String filename) throws IOException {
|
||||||
ArrayList<String> ret = new ArrayList<>();
|
ArrayList<String> ret = new ArrayList<>();
|
||||||
InputStream inStream = new FileInputStream(filename);
|
try (InputStream inStream = new FileInputStream(filename);
|
||||||
InputStreamReader inFile = new InputStreamReader(inStream, "UTF-8");
|
InputStreamReader inFile = new InputStreamReader(inStream, "UTF-8");
|
||||||
BufferedReader inBuf = new BufferedReader(inFile);
|
BufferedReader inBuf = new BufferedReader(inFile)) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = inBuf.readLine()) != null)
|
while ((line = inBuf.readLine()) != null)
|
||||||
ret.add(line);
|
ret.add(line);
|
||||||
inBuf.close();
|
}
|
||||||
return ret.toArray(new String[0]);
|
return ret.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user