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