Skip to content

Instantly share code, notes, and snippets.

@naik899
Created March 8, 2019 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naik899/1b3f11779ebaf82a0d93887a961bf16f to your computer and use it in GitHub Desktop.
Save naik899/1b3f11779ebaf82a0d93887a961bf16f to your computer and use it in GitHub Desktop.
WhiteSpaceFilter
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace HtmlMinification
{
internal class WhitespaceFilter : Stream
{
private static readonly Regex Pattern = new Regex(@"^\s+", RegexOptions.Multiline | RegexOptions.Compiled);
private readonly Stream _stream;
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get { return 0; }
}
public override long Position { get; set; }
public override void Flush()
{
_stream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return _stream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
_stream.SetLength(value);
}
public override void Close()
{
_stream.Close();
}
public override void Write(byte[] buffer, int offset, int count)
{
byte[] data = new byte[count];
Buffer.BlockCopy(buffer, offset, data, 0, count);
string content = Encoding.Default.GetString(buffer);
content = Pattern.Replace(content, string.Empty);
byte[] output = Encoding.Default.GetBytes(content);
_stream.Write(output, 0, output.GetLength(0));
}
public WhitespaceFilter(Stream stream)
{
_stream = stream;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment