using System;
using System.IO;
namespace ResponseFilters
{
public class UpperCaseFilter : Stream
// This filter changes all characters passed through it to uppercase.
{
private Stream _sink;
private long _position;
public UpperCaseFilter ( Stream sink )
{
_sink = sink;
}
// The following members of Stream must be overriden.
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 { return _position; }
set { _position = value; }
}
public override long Seek ( long offset, System.IO.SeekOrigin direction )
{
return _sink.Seek ( offset, direction );
}
public override void SetLength ( long length )
{
_sink.SetLength ( length );
}
public override void Close ( )
{
_sink.Close ( );
}
public override void Flush ( )
{
_sink.Flush ( );
}
public override int Read ( byte [ ] buffer, int offset, int count )
{
return _sink.Read ( buffer, offset, count );
}
// The Write method actually does the filtering.
public override void Write ( byte [ ] buffer, int offset, int count )
{
byte [ ] data = new byte [ count ];
Buffer.BlockCopy ( buffer, offset, data, 0, count );
for ( int i = 0; i < count; i++ )
// Change lowercase chars to uppercase.
{
If ( data [ i ] >= 'a' && data [ i ] <= 'z' )
data [ i ] -= ( 'a'-'A' );
}
_sink.Write ( data, 0, count );
}
// Main is included for code example testing.
// It is not required when compiling to a dll.
public static void Main ( )
{
}
}
}
/*
______________________________________________________________
The following .aspx page uses the above filter to modify HTTP output.
<%@ Page Language="C#" %>
<%@ assembly name = "ResponseFilters" %>
<script runat=server>
protected void Page_Load ( object src, EventArgs ev )
{
Response.Filter = new ResponseFilters.UpperCaseFilter ( Response.Filter );
}
</script>
<HTML>
<BODY>
<%
Response.Write ( "This text is changed to upper case by UpperCaseFilter.<br>" );
%>
</BODY>
</HTML>
*/
. . .
Imports System
Imports System.IO
Namespace ResponseFilters
Public Class UpperCaseFilter
Inherits Stream
' This filter changes all characters passed through it to uppercase.
Private _sink As Stream
Private _position As Long
Public Sub New ( sink As Stream )
_sink = sink
End Sub 'New
' The following members of Stream must be overriden.
Public Overrides ReadOnly Property CanRead ( ) As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property CanSeek ( ) As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property CanWrite ( ) As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property Length ( ) As Long
Get
Return 0
End Get
End Property
Public Overrides Property Position ( ) As Long
Get
Return _position
End Get
Set
_position = value
End Set
End Property
Public Overrides Function Seek ( offset As Long, direction As System.IO.SeekOrigin ) As Long
Return _sink.Seek ( offset, direction )
End Function 'Seek
Public Overrides Sub SetLength ( length As Long )
_sink.SetLength ( length )
End Sub 'SetLength
Public Overrides Sub Close ( )
_sink.Close ( )
End Sub 'Close
Public Overrides Sub Flush ( )
_sink.Flush ( )
End Sub 'Flush
Public Overrides Function Read ( MyBuffer ( ) As Byte, offset As Integer, count As Integer ) As Integer
_sink.Read ( MyBuffer, offset, count )
End Function
' Write is the method that actually does the filtering.
Public Overrides Sub Write ( MyBuffer ( ) As Byte, offset As Integer, count As Integer )
Dim data ( count ) As Byte
Buffer.BlockCopy ( MyBuffer, offset, data, 0, count )
Dim i As Integer
For i = 0 To count - 1
' Change lowercase chars to uppercase.
If data ( i ) >= Convert.ToByte ( "a"c ) And data ( i ) <= Convert.ToByte ( "z"c ) Then
data ( i ) -= Convert.ToByte ( "a"c ) - Convert.ToByte ( "A"c )
End If
Next i
_sink.Write ( data, 0, count )
End Sub 'Write
' Main is included for code example testing.
' It is not required when compiling to a dll.
Public Shared Sub Main ( )
End Sub
End Class 'UpperCaseFilter
End Namespace 'ResponseFilters
'______________________________________________________________
'The following .aspx page uses the above filter to modify the HTTP output.
'<%@ Page Language="VB" %>
'<%@ assembly name = "ResponseFilters" %>
'
'<script runat=server>
' Protected Sub Page_Load ( sender As Object, ev As EventArgs )
' Response.Filter = New ResponseFilters.UpperCaseFilter ( Response.Filter )
' End Sub 'Page_Load
'</script>
'
'<HTML>
' <BODY>
' <%
' Response.Write ( "This text is changed to upper case by UpperCaseFilter.<br>" )
' %>
' </BODY>
'</HTML>
. . . |