ReaderFactory { public ISimpleChemObjectReader createReader(InputStream input) throws IOException { IChemFormat format = null; ISimpleChemObjectReader reader = null; if (input instanceof GZIPInputStream) { format = formatFactory.guessFormat(input); reader = createReader(format); if (reader != null) { try { reader.setReader(input); } catch (CDKException e1) { IOException wrapper = new IOException("Exception while setting the InputStream: " + e1.getMessage()); wrapper.initCause(e1); throw wrapper; } } } else { BufferedInputStream bistream = new BufferedInputStream(input, headerLength); InputStream istreamToRead = bistream; bistream.mark(5); int countRead = 0; byte[] abMagic = new byte[4]; countRead = bistream.read(abMagic, 0, 4); bistream.reset(); if (countRead == 4) { if (abMagic[0] == (byte) 0x1F && abMagic[1] == (byte) 0x8B) { istreamToRead = new BufferedInputStream(new GZIPInputStream(bistream)); } } format = formatFactory.guessFormat(istreamToRead); reader = createReader(format); if (reader != null) { try { reader.setReader(istreamToRead); } catch (CDKException e1) { IOException wrapper = new IOException("Exception while setting the InputStream: " + e1.getMessage()); wrapper.initCause(e1); throw wrapper; } } } return reader; } ReaderFactory();  ReaderFactory(int headerLength); void registerFormat(IChemFormatMatcher format); List<IChemFormatMatcher> getFormats(); ISimpleChemObjectReader createReader(InputStream input); ISimpleChemObjectReader createReader(IChemFormat format); ISimpleChemObjectReader createReader(Reader input);  }
@Test public void testCreateReader_IChemFormat() { IChemFormat format = (IChemFormat) XYZFormat.getInstance(); ISimpleChemObjectReader reader = factory.createReader(format); Assert.assertNotNull(reader); Assert.assertEquals(format.getFormatName(), reader.getFormat().getFormatName()); }
@Test public void testSmiles() throws Exception { InputStream is = this.getClass().getClassLoader().getResourceAsStream("data/smiles/drugs.smi"); Object reader = factory.createReader(is); Assert.assertNull(reader); }
@Test public void testReadGz() throws Exception { String filename = "data/xyz/bf3.xyz.gz"; InputStream input = new BufferedInputStream(new GZIPInputStream(this.getClass().getClassLoader() .getResourceAsStream(filename))); ISimpleChemObjectReader reader = factory.createReader(input); Assert.assertNotNull(reader); Assert.assertEquals(((IChemFormat) XYZFormat.getInstance()).getReaderClassName(), reader.getClass().getName()); IChemFile chemFile = (IChemFile) reader.read(new ChemFile()); IAtomContainer molecule = new AtomContainer(); for (IAtomContainer container : ChemFileManipulator.getAllAtomContainers(chemFile)) { molecule.add(container); } Assert.assertNotNull(molecule); Assert.assertEquals(4, molecule.getAtomCount()); }
@Test public void testReadGzWithGzipDetection() throws Exception { String filename = "data/xyz/bf3.xyz.gz"; InputStream input = this.getClass().getClassLoader().getResourceAsStream(filename); ISimpleChemObjectReader reader = factory.createReader(input); Assert.assertNotNull(reader); Assert.assertEquals(((IChemFormat) XYZFormat.getInstance()).getReaderClassName(), reader.getClass().getName()); IChemFile chemFile = (IChemFile) reader.read(new ChemFile()); IAtomContainer molecule = new AtomContainer(); for (IAtomContainer container : ChemFileManipulator.getAllAtomContainers(chemFile)) { molecule.add(container); } Assert.assertNotNull(molecule); Assert.assertEquals(4, molecule.getAtomCount()); }