[jetrix-cvs] SF.net SVN: jetrix:[870] jetrix/trunk/src/java/net/jetrix/Field.java
Brought to you by:
smanux
From: <sm...@us...> - 2010-09-16 20:50:39
|
Revision: 870 http://jetrix.svn.sourceforge.net/jetrix/?rev=870&view=rev Author: smanux Date: 2010-09-16 20:50:33 +0000 (Thu, 16 Sep 2010) Log Message: ----------- Field loading from a stream Modified Paths: -------------- jetrix/trunk/src/java/net/jetrix/Field.java Modified: jetrix/trunk/src/java/net/jetrix/Field.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/Field.java 2010-08-28 22:22:39 UTC (rev 869) +++ jetrix/trunk/src/java/net/jetrix/Field.java 2010-09-16 20:50:33 UTC (rev 870) @@ -266,44 +266,54 @@ } } - /** - * Load the field from the specified file + * Load the field from the specified stream * * @since 0.3 */ - public void load(String file) throws IOException + public void load(InputStream in) throws IOException { - BufferedReader in = null; - - try + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + + for (int i = 0; i < HEIGHT; i++) { - in = new BufferedReader(new FileReader(file)); + String line = reader.readLine(); - for (int i = 0; i < HEIGHT; i++) + if (line == null || line.length() != WIDTH) { - String line = in.readLine(); + throw new IOException("Field format error at line " + i); + } + + for (int j = 0; j < WIDTH; j++) + { + byte block = (byte) line.charAt(j); - if (line == null || line.length() != WIDTH) + if (isValidBlock(block)) { - throw new IOException("Field format error at line " + i); + field[j][HEIGHT - i - 1] = block; } - - for (int j = 0; j < WIDTH; j++) + else { - byte block = (byte) line.charAt(j); - - if (isValidBlock(block)) - { - field[j][HEIGHT - i - 1] = block; - } - else - { - throw new IOException("Illegal block '" + block + "' at line " + i + " , column " + j); - } + throw new IOException("Illegal block '" + block + "' at line " + i + " , column " + j); } } } + } + + /** + * Load the field from the specified file + * + * @since 0.3 + */ + public void load(String file) throws IOException + { + InputStream in = null; + + try + { + in = new FileInputStream(file); + load(in); + } finally { if (in != null) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |