//******************************************************************************************************
// BinaryImageBase.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:
// ----------------------------------------------------------------------------------------------------
// 01/06/2009 - J. Ritchie Carroll
// Generated original version of source code.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 11/23/2011 - J. Ritchie Carroll
// Modified to support buffer optimized ISupportBinaryImage.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
namespace GSF.Parsing
{
///
/// Defines a base class that represents binary images for parsing or generation in terms of a header, body and footer.
///
[Serializable]
public abstract class BinaryImageBase : ISupportBinaryImage
{
#region [ Properties ]
///
/// Gets the length of the object.
///
///
/// This property is not typically overridden since it is the sum of the header, body and footer lengths.
///
public virtual int BinaryLength
{
get
{
return HeaderLength + BodyLength + FooterLength;
}
}
///
/// Gets the length of the header portion of the object.
///
///
/// This property is typically overridden by a specific protocol implementation.
///
protected virtual int HeaderLength
{
get
{
return 0;
}
}
///
/// Gets the length of the body portion of the object.
///
///
/// This property is typically overridden by a specific protocol implementation.
///
protected virtual int BodyLength
{
get
{
return 0;
}
}
///
/// Gets the length of the footer portion of the object.
///
///
/// This property is typically overridden by a specific protocol implementation.
///
protected virtual int FooterLength
{
get
{
return 0;
}
}
#endregion
#region [ Methods ]
///
/// Initializes object by parsing the specified containing a binary image.
///
/// Buffer containing binary image to parse.
/// 0-based starting index in the to start parsing.
/// Valid number of bytes within from .
/// The number of bytes used for initialization in the (i.e., the number of bytes parsed).
/// is null.
///
/// or is less than 0 -or-
/// and will exceed length.
///
///
/// This method is not typically overridden since it is parses the header, body and footer images in sequence.
///
public virtual int ParseBinaryImage(byte[] buffer, int startIndex, int length)
{
buffer.ValidateParameters(startIndex, length);
int index = startIndex;
// Parse out header, body and footer images
index += ParseHeaderImage(buffer, index, length);
index += ParseBodyImage(buffer, index, length - (index - startIndex));
index += ParseFooterImage(buffer, index, length - (index - startIndex));
return (index - startIndex);
}
///
/// Parses the binary header image.
///
/// Buffer containing binary image to parse.
/// 0-based starting index in the to start parsing.
/// Valid number of bytes within from .
/// The number of bytes used for initialization in the (i.e., the number of bytes parsed).
///
/// This method is typically overridden by a specific protocol implementation.
///
protected virtual int ParseHeaderImage(byte[] buffer, int startIndex, int length)
{
return 0;
}
///
/// Parses the binary body image.
///
/// Buffer containing binary image to parse.
/// 0-based starting index in the to start parsing.
/// Valid number of bytes within from .
/// The number of bytes used for initialization in the (i.e., the number of bytes parsed).
///
/// This method is typically overridden by a specific protocol implementation.
///
protected virtual int ParseBodyImage(byte[] buffer, int startIndex, int length)
{
return 0;
}
///
/// Parses the binary footer image.
///
/// Buffer containing binary image to parse.
/// 0-based starting index in the to start parsing.
/// Valid number of bytes within from .
/// The number of bytes used for initialization in the (i.e., the number of bytes parsed).
///
/// This method is typically overridden by a specific protocol implementation.
///
protected virtual int ParseFooterImage(byte[] buffer, int startIndex, int length)
{
return 0;
}
///
/// Generates binary image of the object and copies it into the given buffer, for bytes.
///
/// 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.
///
///
/// This property is not typically overridden since it is the generates the combination of the header, body and footer images.
///
public virtual int GenerateBinaryImage(byte[] buffer, int startIndex)
{
buffer.ValidateParameters(startIndex, BinaryLength);
int index = startIndex;
// Generate header, body and footer images
index += GenerateHeaderImage(buffer, index);
index += GenerateBodyImage(buffer, index);
index += GenerateFooterImage(buffer, index);
return (index - startIndex);
}
///
/// Generates the binary header image and copies it into the given buffer, for bytes.
///
/// 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 .
///
/// This method is typically overridden by a specific protocol implementation.
///
protected virtual int GenerateHeaderImage(byte[] buffer, int startIndex)
{
return 0;
}
///
/// Generates the binary body image and copies it into the given buffer, for bytes.
///
/// 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 .
///
/// This method is typically overridden by a specific protocol implementation.
///
protected virtual int GenerateBodyImage(byte[] buffer, int startIndex)
{
return 0;
}
///
/// Generates the binary footer image and copies it into the given buffer, for bytes.
///
/// 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 .
///
/// This method is typically overridden by a specific protocol implementation.
///
protected virtual int GenerateFooterImage(byte[] buffer, int startIndex)
{
return 0;
}
#endregion
}
}