U
User3DX
Guest
Hello,
As title suggests, I want to insert wrapper into System.IO as a subset with following example;
Example
public class EnhancedBinaryReader
{
private BinaryReader BR;
public string FPOS
{
get
{
if ( this.BR is BinaryReader )
{
return String.Format( "{0:f2}", ( ( float ) this.BR.BaseStream.Position / ( float ) this.BR.BaseStream.Length ) * 100 );
}
return null;
}
}
public bool? EOF
{
get
{
if ( this.BR is BinaryReader )
{
return ( ( this.BR.BaseStream.Position < this.BR.BaseStream.Length ) ? false : true );
}
return null;
}
}
private int ReadLoop;
private int ReadLast;
public int BufferSize
{
get;
private set;
}
public byte[] Buffer
{
get;
private set;
}
public EnhancedBinaryReader( string filepath )
{
if ( filepath == null )
{
throw new ArgumentNullException();
}
if ( File.Exists( filepath ) )
{
this.BufferSize = 1024;
this.Buffer = new byte[ this.BufferSize ];
this.BufferSize = this.Buffer.Length;
this.BR = new BinaryReader( File.OpenRead( filepath ) );
this.ReadLoop = ( int ) ( this.BR.BaseStream.Length / this.BufferSize );
this.ReadLast = ( int ) ( this.BR.BaseStream.Length % this.BufferSize );
return;
}
throw new IOException();
}
public int Read()
{
if ( this.BR is BinaryReader )
{
if ( this.ReadLoop > 0 )
{
this.Buffer = this.BR.ReadBytes( this.BufferSize );
this.ReadLoop--;
return this.BufferSize;
}
if ( this.ReadLast > 0 )
{
this.Buffer = this.BR.ReadBytes( this.ReadLast );
return this.ReadLast;
}
return 0;
}
return -1;
}
public void Close()
{
if ( this.BR is BinaryReader )
{
this.BR.Close();
this.BR.Dispose();
}
}
}
Normally, we have "using System.IO;" declared and create an instance of "BinaryReader" object. So, does the
above example code offer benefits and how to implement into System.IO as Wrapper for the following changes;
Currently
using System.IO;
BinaryReader obj = new BinaryReader( File.OpenRead( string filepath ) );
while( obj.BaseStream.Position < obj.BaseStream.Length)
{
// doing something
}
obj.Close();
obj.Dispose();
Versus Wrapper
using System.IO;
EnhancedBinaryReader obj = new EnhancedBinaryReader( string filepath );
while( obj.EOF == false )
{
// doing something
}
obj.Close();
For some, you may think, why do this at all. I can only say for myself, its a learning experience. The Wrapper
offers an internal buffer, end of file/stream check, file position percentage, IO error check, block mode
reading all in a subset of System.IO.
Any helpful suggestions, comments, etc are always welcome.
Thanks
Continue reading...
As title suggests, I want to insert wrapper into System.IO as a subset with following example;
Example
public class EnhancedBinaryReader
{
private BinaryReader BR;
public string FPOS
{
get
{
if ( this.BR is BinaryReader )
{
return String.Format( "{0:f2}", ( ( float ) this.BR.BaseStream.Position / ( float ) this.BR.BaseStream.Length ) * 100 );
}
return null;
}
}
public bool? EOF
{
get
{
if ( this.BR is BinaryReader )
{
return ( ( this.BR.BaseStream.Position < this.BR.BaseStream.Length ) ? false : true );
}
return null;
}
}
private int ReadLoop;
private int ReadLast;
public int BufferSize
{
get;
private set;
}
public byte[] Buffer
{
get;
private set;
}
public EnhancedBinaryReader( string filepath )
{
if ( filepath == null )
{
throw new ArgumentNullException();
}
if ( File.Exists( filepath ) )
{
this.BufferSize = 1024;
this.Buffer = new byte[ this.BufferSize ];
this.BufferSize = this.Buffer.Length;
this.BR = new BinaryReader( File.OpenRead( filepath ) );
this.ReadLoop = ( int ) ( this.BR.BaseStream.Length / this.BufferSize );
this.ReadLast = ( int ) ( this.BR.BaseStream.Length % this.BufferSize );
return;
}
throw new IOException();
}
public int Read()
{
if ( this.BR is BinaryReader )
{
if ( this.ReadLoop > 0 )
{
this.Buffer = this.BR.ReadBytes( this.BufferSize );
this.ReadLoop--;
return this.BufferSize;
}
if ( this.ReadLast > 0 )
{
this.Buffer = this.BR.ReadBytes( this.ReadLast );
return this.ReadLast;
}
return 0;
}
return -1;
}
public void Close()
{
if ( this.BR is BinaryReader )
{
this.BR.Close();
this.BR.Dispose();
}
}
}
Normally, we have "using System.IO;" declared and create an instance of "BinaryReader" object. So, does the
above example code offer benefits and how to implement into System.IO as Wrapper for the following changes;
Currently
using System.IO;
BinaryReader obj = new BinaryReader( File.OpenRead( string filepath ) );
while( obj.BaseStream.Position < obj.BaseStream.Length)
{
// doing something
}
obj.Close();
obj.Dispose();
Versus Wrapper
using System.IO;
EnhancedBinaryReader obj = new EnhancedBinaryReader( string filepath );
while( obj.EOF == false )
{
// doing something
}
obj.Close();
For some, you may think, why do this at all. I can only say for myself, its a learning experience. The Wrapper
offers an internal buffer, end of file/stream check, file position percentage, IO error check, block mode
reading all in a subset of System.IO.
Any helpful suggestions, comments, etc are always welcome.
Thanks
Continue reading...