mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2025-12-08 07:11:32 +01:00
Use try-with-resources
This commit is contained in:
@@ -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);
|
||||
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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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,51 +151,50 @@ 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];
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node n = nodes.get(i);
|
||||
int cm = n.move == null ? 0 : n.move.getCompressedMove();
|
||||
buf[0] = (byte)(cm >> 8); // Move, high byte
|
||||
buf[1] = (byte)(cm & 255); // Move, low byte
|
||||
buf[2] = (byte)(n.ecoIdx >> 8); // Index, high byte
|
||||
buf[3] = (byte)(n.ecoIdx & 255); // Index, low byte
|
||||
buf[4] = (byte)(n.opnIdx >> 8); // Index, high byte
|
||||
buf[5] = (byte)(n.opnIdx & 255); // Index, low byte
|
||||
buf[6] = (byte)(n.varIdx >> 8); // Index, high byte
|
||||
buf[7] = (byte)(n.varIdx & 255); // Index, low byte
|
||||
int firstChild = -1;
|
||||
if (n.children.size() > 0)
|
||||
firstChild = n.children.get(0).index;
|
||||
buf[8] = (byte)(firstChild >> 8);
|
||||
buf[9] = (byte)(firstChild & 255);
|
||||
int nextSibling = -1;
|
||||
if (n.parent != null) {
|
||||
ArrayList<Node> siblings = n.parent.children;
|
||||
for (int j = 0; j < siblings.size()-1; j++) {
|
||||
if (siblings.get(j).move.equals(n.move)) {
|
||||
nextSibling = siblings.get(j+1).index;
|
||||
break;
|
||||
// Write nodes
|
||||
byte[] buf = new byte[12];
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node n = nodes.get(i);
|
||||
int cm = n.move == null ? 0 : n.move.getCompressedMove();
|
||||
buf[0] = (byte)(cm >> 8); // Move, high byte
|
||||
buf[1] = (byte)(cm & 255); // Move, low byte
|
||||
buf[2] = (byte)(n.ecoIdx >> 8); // Index, high byte
|
||||
buf[3] = (byte)(n.ecoIdx & 255); // Index, low byte
|
||||
buf[4] = (byte)(n.opnIdx >> 8); // Index, high byte
|
||||
buf[5] = (byte)(n.opnIdx & 255); // Index, low byte
|
||||
buf[6] = (byte)(n.varIdx >> 8); // Index, high byte
|
||||
buf[7] = (byte)(n.varIdx & 255); // Index, low byte
|
||||
int firstChild = -1;
|
||||
if (n.children.size() > 0)
|
||||
firstChild = n.children.get(0).index;
|
||||
buf[8] = (byte)(firstChild >> 8);
|
||||
buf[9] = (byte)(firstChild & 255);
|
||||
int nextSibling = -1;
|
||||
if (n.parent != null) {
|
||||
ArrayList<Node> siblings = n.parent.children;
|
||||
for (int j = 0; j < siblings.size()-1; j++) {
|
||||
if (siblings.get(j).move.equals(n.move)) {
|
||||
nextSibling = siblings.get(j+1).index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
buf[10] = (byte)(nextSibling >> 8);
|
||||
buf[11] = (byte)(nextSibling & 255);
|
||||
out.write(buf);
|
||||
}
|
||||
buf[10] = (byte)(nextSibling >> 8);
|
||||
buf[11] = (byte)(nextSibling & 255);
|
||||
for (int i = 0; i < buf.length; i++)
|
||||
buf[i] = -1;
|
||||
out.write(buf);
|
||||
}
|
||||
for (int i = 0; i < buf.length; i++)
|
||||
buf[i] = -1;
|
||||
out.write(buf);
|
||||
|
||||
// Write strings
|
||||
buf = new byte[]{0};
|
||||
for (String name : strs) {
|
||||
out.write(name.getBytes("UTF-8"));
|
||||
out.write(buf);
|
||||
// Write strings
|
||||
buf = new byte[]{0};
|
||||
for (String name : strs) {
|
||||
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);
|
||||
InputStreamReader inFile = new InputStreamReader(inStream, "UTF-8");
|
||||
BufferedReader inBuf = new BufferedReader(inFile);
|
||||
String line;
|
||||
while ((line = inBuf.readLine()) != null)
|
||||
ret.add(line);
|
||||
inBuf.close();
|
||||
try (InputStream inStream = new FileInputStream(filename);
|
||||
InputStreamReader inFile = new InputStreamReader(inStream, "UTF-8");
|
||||
BufferedReader inBuf = new BufferedReader(inFile)) {
|
||||
String line;
|
||||
while ((line = inBuf.readLine()) != null)
|
||||
ret.add(line);
|
||||
}
|
||||
return ret.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user