rendered paste bodypackage oving6;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.MalformedURLException;
public class ColonyIO {
public static Colony readColony(File file) throws IOException {
return ColonyIO.readColony(new BufferedReader(new FileReader(file)));
}
public static Colony readColony(URI url) throws IOException {
InputStream is = null;
BufferedReader br = null;
try {
is = url.toURL().openStream();
br = new BufferedReader(new InputStreamReader(is));
} catch (MalformedURLException murle) {
System.err.println(murle);
} catch (IOException ioe) {
System.err.println(ioe);
}
return ColonyIO.readColony(br);
}
public static Colony readColony(BufferedReader br) throws IOException {
if (!br.ready()) throw new IOException("Error: Buffer not ready.");
if (br == null) throw new IOException("Error: Buffer is empty.") ;
String currentLine = br.readLine();
String[] temp = currentLine.split("x");
int width = Integer.parseInt(temp[0]);
int height = Integer.parseInt(temp[1]);
Colony colony = new BooleanArrayColony(height, width);
int counter = 0;
try {
while (counter < colony.getHeight()) {
currentLine = br.readLine();
if (currentLine == null) {
throw new EOFException("BUHU!");
} else if (currentLine.startsWith("#")) {
continue;
}
for (int i = 0; i < width && i < currentLine.length(); i++) {
colony.setCell(counter, i,currentLine.charAt(i) == '*');
}
counter++;
}
return colony;
} finally {
if(br != null) br.close();
}
}
private static boolean isValid(String s) {
for (int i = 0;i < s.length();i++) {
char c = s.charAt(i);
if (Character.isWhitespace(c) || c == '.' || c == '*') {
continue;
} else {
return false;
}
}
return true;
}
}