-
-
Save naik899/1b3f11779ebaf82a0d93887a961bf16f to your computer and use it in GitHub Desktop.
WhiteSpaceFilter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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