//******************************************************************************************************
// RiffHeaderChunk.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 07/29/2009 - J. Ritchie Carroll
// Generated original version of source code.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
#region [ Contributor License Agreements ]
/**************************************************************************\
Copyright © 2009 - J. Ritchie Carroll
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/
#endregion
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using GSF.Parsing;
namespace GSF.Media
{
///
/// Represents the header chunk in a RIFF media format file.
///
public class RiffHeaderChunk : RiffChunk, ISupportBinaryImage
{
#region [ Members ]
// Constants
///
/// Type ID of a RIFF header chunk.
///
public const string RiffTypeID = "RIFF";
///
/// The fixed byte length of a instance.
///
public new const int FixedLength = RiffChunk.FixedLength + 4;
// Fields
private string m_format;
#endregion
#region [ Constructors ]
///
/// Constructs a new RIFF header chunk for the specified format.
///
/// RIFF header chunk header format.
public RiffHeaderChunk(string format)
: base(RiffTypeID)
{
if (string.IsNullOrWhiteSpace(format))
throw new ArgumentNullException("value");
if (format.Length != 4)
throw new ArgumentOutOfRangeException("value", "Format must be exactly 4 characters in length");
m_format = format;
}
/// Reads a new RIFF header from the specified stream.
/// Pre-parsed header.
/// Source stream to read data from.
/// Expected RIFF media format (e.g., "WAVE").
/// cannot be null.
/// must be extactly 4 characters in length.
public RiffHeaderChunk(RiffChunk preRead, Stream source, string format)
: base(preRead, RiffTypeID)
{
if (string.IsNullOrWhiteSpace(format))
throw new ArgumentNullException("value");
if (format.Length != 4)
throw new ArgumentOutOfRangeException("value", "Format must be exactly 4 characters in length");
byte[] buffer = new byte[4];
int bytesRead = source.Read(buffer, 0, 4);
if (bytesRead < 4)
throw new InvalidOperationException("RIFF format section too small, media file corrupted");
// Read format stored in RIFF section
m_format = Encoding.ASCII.GetString(buffer, 0, 4);
if (m_format != format)
throw new InvalidDataException(string.Format("{0} format expected but got {1}, this does not appear to be a valid {0} file", format, m_format));
}
#endregion
#region [ Properties ]
///
/// Gets or sets format for RIFF header chunk.
///
public string Format
{
get
{
return m_format;
}
set
{
if ((object)value == null)
throw new ArgumentNullException(nameof(value));
if (value.Length != 4)
throw new ArgumentOutOfRangeException(nameof(value), "Format must be exactly 4 characters in length");
m_format = value;
}
}
///
/// Gets length of binary representation of RIFF header chunk.
///
public new int BinaryLength
{
get
{
return FixedLength;
}
}
#endregion
#region [ Methods ]
///
/// Generates binary image of the object and copies it into the given buffer.
///
/// Buffer used to hold generated binary image of the source object.
/// 0-based starting index in the to start writing.
/// The number of bytes written to the .
/// is null.
///
/// or is less than 0 -or-
/// and will exceed length.
///
public override int GenerateBinaryImage(byte[] buffer, int startIndex)
{
buffer.ValidateParameters(startIndex, FixedLength);
startIndex += base.GenerateBinaryImage(buffer, startIndex);
Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_format), 0, buffer, startIndex, 4);
return FixedLength;
}
// This is not currently needed since the RIFF header chunk is initialized during construction
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
int ISupportBinaryImage.ParseBinaryImage(byte[] buffer, int startIndex, int length)
{
throw new NotImplementedException();
}
///
/// Returns a cloned instance of this RIFF header chunk.
///
/// A cloned instance of this RIFF header chunk.
public new RiffHeaderChunk Clone()
{
return new RiffHeaderChunk(m_format);
}
#endregion
}
}