/** * Reads the header information from the passed in stream and returns it. It also returns * a BinaryReader that is endian-appropriate for the data stream. * * @param ParserStream source stream of data to read from * @param BinaryStream [out] binary reader used for reading from stream * @return serialized header */ public static StreamHeader ReadHeader(Stream ParserStream, out BinaryReader BinaryStream) { // Create a binary stream for data, we might toss this later for are an endian swapping one. BinaryStream = new BinaryReader(ParserStream, System.Text.Encoding.ASCII); // Serialize header. StreamHeader Header = new StreamHeader(BinaryStream); // Determine whether read file has magic header. If no, try again byteswapped. if (Header.Magic != StreamHeader.ExpectedMagic) { // Seek back to beginning of stream before we retry. ParserStream.Seek(0, SeekOrigin.Begin); // Use big endian reader. It transparently endian swaps data on read. BinaryStream = new BinaryReaderBigEndian(ParserStream); // Serialize header a second time. Header = new StreamHeader(BinaryStream); } // At this point we should have a valid header. If no, throw an exception. if (Header.Magic != StreamHeader.ExpectedMagic) { ShowDialog("Error", "Unrecognized file"); throw new InvalidDataException(); } if (Header.Version != StreamHeader.ExpectedVersion) { ShowDialog("Error", "Invalid version"); throw new InvalidDataException(); } return(Header); }
/** * Reads the header information from the passed in stream and returns it. It also returns * a BinaryReader that is endian-appropriate for the data stream. * * @param ParserStream source stream of data to read from * @param BinaryStream [out] binary reader used for reading from stream * @return serialized header */ public static StreamHeader ReadHeader( Stream ParserStream, out BinaryReader BinaryStream ) { // Create a binary stream for data, we might toss this later for are an endian swapping one. BinaryStream = new BinaryReader(ParserStream,System.Text.Encoding.ASCII); // Serialize header. StreamHeader Header = new StreamHeader(BinaryStream); // Determine whether read file has magic header. If no, try again byteswapped. if(Header.Magic != StreamHeader.ExpectedMagic) { // Seek back to beginning of stream before we retry. ParserStream.Seek(0,SeekOrigin.Begin); // Use big endian reader. It transparently endian swaps data on read. BinaryStream = new BinaryReaderBigEndian(ParserStream); // Serialize header a second time. Header = new StreamHeader(BinaryStream); } // At this point we should have a valid header. If no, throw an exception. if( Header.Magic != StreamHeader.ExpectedMagic ) { ShowDialog( "Error", "Unrecognized file" ); throw new InvalidDataException(); } if ( Header.Version != StreamHeader.ExpectedVersion ) { ShowDialog( "Error", "Invalid version" ); throw new InvalidDataException(); } return Header; }