#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using System.IO;
using log4net.Core;
namespace log4net.Util
{
///
/// Subclass of that maintains a count of
/// the number of bytes written.
///
///
///
/// This writer counts the number of bytes written.
///
///
/// Nicko Cadell
/// Gert Driesen
public class CountingQuietTextWriter : QuietTextWriter
{
#region Public Instance Constructors
///
/// Constructor
///
/// The to actually write to.
/// The to report errors to.
///
///
/// Creates a new instance of the class
/// with the specified and .
///
///
public CountingQuietTextWriter(TextWriter writer, IErrorHandler errorHandler) : base(writer, errorHandler)
{
m_countBytes = 0;
}
#endregion Public Instance Constructors
#region Override implementation of QuietTextWriter
///
/// Writes a character to the underlying writer and counts the number of bytes written.
///
/// the char to write
///
///
/// Overrides implementation of . Counts
/// the number of bytes written.
///
///
public override void Write(char value)
{
try
{
base.Write(value);
// get the number of bytes needed to represent the
// char using the supplied encoding.
m_countBytes += this.Encoding.GetByteCount(new char[] { value });
}
catch(Exception e)
{
this.ErrorHandler.Error("Failed to write [" + value + "].", e, ErrorCode.WriteFailure);
}
}
///
/// Writes a buffer to the underlying writer and counts the number of bytes written.
///
/// the buffer to write
/// the start index to write from
/// the number of characters to write
///
///
/// Overrides implementation of . Counts
/// the number of bytes written.
///
///
public override void Write(char[] buffer, int index, int count)
{
if (count > 0)
{
try
{
base.Write(buffer, index, count);
// get the number of bytes needed to represent the
// char array using the supplied encoding.
m_countBytes += this.Encoding.GetByteCount(buffer, index, count);
}
catch(Exception e)
{
this.ErrorHandler.Error("Failed to write buffer.", e, ErrorCode.WriteFailure);
}
}
}
///
/// Writes a string to the output and counts the number of bytes written.
///
/// The string data to write to the output.
///
///
/// Overrides implementation of . Counts
/// the number of bytes written.
///
///
public override void Write(string str)
{
if (str != null && str.Length > 0)
{
try
{
base.Write(str);
// get the number of bytes needed to represent the
// string using the supplied encoding.
m_countBytes += this.Encoding.GetByteCount(str);
}
catch(Exception e)
{
this.ErrorHandler.Error("Failed to write [" + str + "].", e, ErrorCode.WriteFailure);
}
}
}
#endregion Override implementation of QuietTextWriter
#region Public Instance Properties
///
/// Gets or sets the total number of bytes written.
///
///
/// The total number of bytes written.
///
///
///
/// Gets or sets the total number of bytes written.
///
///
public long Count
{
get { return m_countBytes; }
set { m_countBytes = value; }
}
#endregion Public Instance Properties
#region Private Instance Fields
///
/// Total number of bytes written.
///
private long m_countBytes;
#endregion Private Instance Fields
}
}