FormatFactory { public IChemFormat guessFormat(Reader input) throws IOException { if (input == null) { throw new IllegalArgumentException("input cannot be null"); } char[] header = new char[this.headerLength]; if (!input.markSupported()) { throw new IllegalArgumentException("input must support mark"); } input.mark(this.headerLength); input.read(header, 0, this.headerLength); input.reset(); BufferedReader buffer = new BufferedReader(new CharArrayReader(header)); IChemFormat chemFormat = getMatchResult(buffer); if(chemFormat == null){ buffer = new BufferedReader(new CharArrayReader(header)); chemFormat = getXYZFormat(buffer); } return chemFormat; } FormatFactory();  FormatFactory(int headerLength); void registerFormat(IChemFormatMatcher format); List<IChemFormatMatcher> getFormats(); IChemFormat guessFormat(Reader input); IChemFormat guessFormat(InputStream input);  }
@Test public void testGuessFormat() throws Exception { String filename = "data/xyz/bf3.xyz"; InputStream input = this.getClass().getClassLoader().getResourceAsStream(filename); input = new BufferedInputStream(input); IChemFormat format = factory.guessFormat(input); Assert.assertNotNull(format); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertEquals("4", line); line = reader.readLine(); Assert.assertNotNull(line); Assert.assertEquals("Bortrifluorid", line); }
@Test public void testGuessFormat_Gz() throws Exception { String filename = "data/xyz/bf3.xyz.gz"; InputStream input = new BufferedInputStream(new GZIPInputStream(this.getClass().getClassLoader() .getResourceAsStream(filename))); IChemFormat format = factory.guessFormat(input); Assert.assertNotNull(format); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertEquals("4", line); line = reader.readLine(); Assert.assertNotNull(line); Assert.assertEquals("Bortrifluorid", line); }
@Test public void testGuessFormat_Reader() throws Exception { String filename = "data/xyz/bf3.xyz"; InputStream input = this.getClass().getClassLoader().getResourceAsStream(filename); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); IChemFormat format = factory.guessFormat(reader); Assert.assertNotNull(format); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertEquals("4", line); line = reader.readLine(); Assert.assertNotNull(line); Assert.assertEquals("Bortrifluorid", line); }