98 lines
3.3 KiB
C#
98 lines
3.3 KiB
C#
/*
|
|
* File.Writer.CharacterCountingStreamWriter.cs
|
|
*
|
|
* Copyright © 2009
|
|
* Diversified Technical Systems, Inc.
|
|
* All Rights Reserved
|
|
*/
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace DTS.Serialization
|
|
{
|
|
public abstract partial class File
|
|
{ // *** see File.cs ***
|
|
public abstract partial class Writer<T>
|
|
{ // *** see File.Writer.cs ***
|
|
|
|
/// <summary>
|
|
/// A StreamWriter-style class for counting characters written to its stream.
|
|
/// </summary>
|
|
protected class CharacterCountingStreamWriter : StreamWriter
|
|
{
|
|
public CharacterCountingStreamWriter(Stream stream)
|
|
: base(stream)
|
|
{
|
|
CharactersCounted = 0;
|
|
}
|
|
|
|
public CharacterCountingStreamWriter(string path)
|
|
: base(path)
|
|
{
|
|
CharactersCounted = 0;
|
|
}
|
|
|
|
public CharacterCountingStreamWriter(Stream stream, Encoding encoding)
|
|
: base(stream, encoding)
|
|
{
|
|
CharactersCounted = 0;
|
|
}
|
|
|
|
public CharacterCountingStreamWriter(String path, bool append)
|
|
: base(path, append)
|
|
{
|
|
CharactersCounted = 0;
|
|
}
|
|
|
|
public CharacterCountingStreamWriter(Stream stream, Encoding encoding, int bufferSize)
|
|
: base(stream, encoding, bufferSize)
|
|
{
|
|
CharactersCounted = 0;
|
|
}
|
|
|
|
public CharacterCountingStreamWriter(String path, bool append, Encoding encoding)
|
|
: base(path, append, encoding)
|
|
{
|
|
CharactersCounted = 0;
|
|
}
|
|
|
|
public CharacterCountingStreamWriter(String path, bool append, Encoding encoding, int bufferSize)
|
|
: base(path, append, encoding, bufferSize)
|
|
{
|
|
CharactersCounted = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the <see cref="uint"/> number of characters that have been counted by this
|
|
/// object since creation/reset.
|
|
/// </summary>
|
|
public uint CharactersCounted
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tally the characters contained in the specified string.
|
|
/// </summary>
|
|
///
|
|
/// <param name="value">
|
|
/// The <see cref="string"/> to be character-counted.
|
|
/// </param>
|
|
///
|
|
public override void WriteLine(string value)
|
|
{
|
|
try { CharactersCounted += (uint)value.Length; }
|
|
catch (System.Exception ex)
|
|
{
|
|
throw new ApplicationException("encountered problem counting written character string " + (null != value ? "\"" + value + "\"" : "<NULL>"), ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // *** end File.Writer ***
|
|
} // *** end File ***
|
|
}
|