/// <summary> /// Detects the format of the Reader input, and if known, it will return /// a CDK Reader to read the format, or null when the reader is not /// implemented. /// </summary> /// <param name="input"></param> /// <returns><see langword="null"/> if CDK does not contain a reader for the detected format.</returns> /// <seealso cref="CreateReader(TextReader)"/> public ISimpleChemObjectReader CreateReader(Stream input) { IChemFormat format = null; ISimpleChemObjectReader reader = null; if (input is GZipStream) { var istreamToRead = new ReadSeekableStream(input, 65536); format = formatFactory.GuessFormat(istreamToRead); var type = GetReaderType(format); if (type != null) { try { reader = (ISimpleChemObjectReader)type.GetConstructor(new Type[] { typeof(Stream) }).Invoke(new object[] { istreamToRead }); } catch (CDKException e1) { var wrapper = new IOException("Exception while setting the Stream: " + e1.Message, e1); throw wrapper; } } } else { var bistream = input; var istreamToRead = bistream; // if gzip test fails, then take default // bistream.Mark(5); int countRead = 0; var abMagic = new byte[4]; countRead = bistream.Read(abMagic, 0, 4); bistream.Seek(0, SeekOrigin.Begin); if (countRead == 4) { if (abMagic[0] == (byte)0x1F && abMagic[1] == (byte)0x8B) { istreamToRead = new GZipStream(bistream, CompressionMode.Decompress); return(CreateReader(istreamToRead)); } } format = formatFactory.GuessFormat(istreamToRead); var type = GetReaderType(format); if (type != null) { try { reader = (ISimpleChemObjectReader)type.GetConstructor(new Type[] { typeof(Stream) }).Invoke(new object[] { istreamToRead }); } catch (CDKException e1) { var wrapper = new IOException("Exception while setting the Stream: " + e1.Message, e1); throw wrapper; } } } return(reader); }
private static void ExpectFormat(string filename, IResourceFormat expectedFormat) { var ins = ResourceLoader.GetAsStream(filename); Assert.IsNotNull(ins, $"Cannot find file: {filename}"); if (expectedFormat is IChemFormatMatcher) { factory.RegisterFormat((IChemFormatMatcher)expectedFormat); } ins = new BufferedStream(ins); IChemFormat format = factory.GuessFormat(ins); Assert.IsNotNull(format); Assert.AreEqual(expectedFormat.FormatName, format.FormatName); }