/// <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); }
public void TestGuessFormat_Gz() { var filename = "NCDK.Data.XYZ.bf3.xyz.gz"; Stream input = new ReadSeekableStream(new GZipStream(ResourceLoader.GetAsStream(filename), CompressionMode.Decompress), 60000); IChemFormat format = factory.GuessFormat(input); Assert.IsNotNull(format); // make sure the Stream is properly reset var reader = new StreamReader(input); string line = reader.ReadLine(); Assert.IsNotNull(line); Assert.AreEqual("4", line); line = reader.ReadLine(); Assert.IsNotNull(line); Assert.AreEqual("Bortrifluorid", line); }