U
User3DX
Guest
Thanks to Alberto, my project and knowledge are improved. Original post Link
UPDATED CODE
namespace System.IO
{
public class EnhanceBinaryReader : BinaryReader
{
public byte[] IOBuffer
{
get;
private set;
}
public int BufferSize
{
get;
private set;
}
private int _decimalPlaces;
public int DecimalPlaces
{
get
{
return this._decimalPlaces;
}
set
{
this._decimalPlaces = ( value < 0 ? 0 : ( value > 5 ? 5 : value ) );
}
}
private FPOut _fpOut;
public static enum FPOut
{
STR = 0, DEC, ABS
}
public dynamic FPOS
{
get
{
dynamic rValue = null;
float f = ( ( float ) this.BaseStream.Position ) / ( ( float ) this.BaseStream.Length );
switch ( this._fpOut )
{
case FPOut.STR:
string floatFormat = "{0:f" + this._decimalPlaces.ToString() + "}";
rValue = String.Format( floatFormat, f * 100 );
break;
case FPOut.DEC:
rValue = f;
break;
default:
rValue = this.BaseStream.Position;
break;
}
return rValue;
}
}
private int ReadLoop;
private int ReadLast;
public bool? EOF
{
get
{
if ( this is BinaryReader )
{
return ( this.BaseStream.Position < this.BaseStream.Length ? false : true );
}
return null;
}
}
public EnhanceBinaryReader( string filepath )
: base( new FileStream( filepath, FileMode.Open ) )
{
this._fpOut = FPOut.ABS;
this._decimalPlaces = 0;
this.IOBuffer = new byte[ 1024 ];
this.BufferSize = this.IOBuffer.Length;
if ( filepath == null )
{
throw new ArgumentNullException();
}
if ( filepath == String.Empty )
{
throw new Exception();
}
if ( File.Exists( filepath ) )
{
if ( this is BinaryReader )
{
this.ReadLoop = ( int ) ( this.BaseStream.Length / this.BufferSize );
this.ReadLast = ( int ) ( this.BaseStream.Length % this.BufferSize );
}
}
}
public new int Read()
{
if ( this is BinaryReader )
{
if ( this.ReadLoop > 0 )
{
this.IOBuffer = this.ReadBytes( this.BufferSize );
this.ReadLoop--;
return this.BufferSize;
}
if ( this.ReadLast > 0 )
{
this.IOBuffer = this.ReadBytes( this.ReadLast );
return this.ReadLast;
}
return 0;
}
return -1;
}
}
}
The project test results was great. Performed as hoped. However, made some changes and left some code as is;
1. FPOS return is dynamic using FPOut set to String, Decimal, or Absolute. Precision 0 to 5 via DecimalPlaces
2. READ mechanics no change, returns bytes read, 0 end of stream, -1 no object
3. EOF no change, return 0 not at stream end, 1 end of stream, null no object
I am sure the code can use tweaking here and there. Any are welcome and thanks!
Continue reading...
UPDATED CODE
namespace System.IO
{
public class EnhanceBinaryReader : BinaryReader
{
public byte[] IOBuffer
{
get;
private set;
}
public int BufferSize
{
get;
private set;
}
private int _decimalPlaces;
public int DecimalPlaces
{
get
{
return this._decimalPlaces;
}
set
{
this._decimalPlaces = ( value < 0 ? 0 : ( value > 5 ? 5 : value ) );
}
}
private FPOut _fpOut;
public static enum FPOut
{
STR = 0, DEC, ABS
}
public dynamic FPOS
{
get
{
dynamic rValue = null;
float f = ( ( float ) this.BaseStream.Position ) / ( ( float ) this.BaseStream.Length );
switch ( this._fpOut )
{
case FPOut.STR:
string floatFormat = "{0:f" + this._decimalPlaces.ToString() + "}";
rValue = String.Format( floatFormat, f * 100 );
break;
case FPOut.DEC:
rValue = f;
break;
default:
rValue = this.BaseStream.Position;
break;
}
return rValue;
}
}
private int ReadLoop;
private int ReadLast;
public bool? EOF
{
get
{
if ( this is BinaryReader )
{
return ( this.BaseStream.Position < this.BaseStream.Length ? false : true );
}
return null;
}
}
public EnhanceBinaryReader( string filepath )
: base( new FileStream( filepath, FileMode.Open ) )
{
this._fpOut = FPOut.ABS;
this._decimalPlaces = 0;
this.IOBuffer = new byte[ 1024 ];
this.BufferSize = this.IOBuffer.Length;
if ( filepath == null )
{
throw new ArgumentNullException();
}
if ( filepath == String.Empty )
{
throw new Exception();
}
if ( File.Exists( filepath ) )
{
if ( this is BinaryReader )
{
this.ReadLoop = ( int ) ( this.BaseStream.Length / this.BufferSize );
this.ReadLast = ( int ) ( this.BaseStream.Length % this.BufferSize );
}
}
}
public new int Read()
{
if ( this is BinaryReader )
{
if ( this.ReadLoop > 0 )
{
this.IOBuffer = this.ReadBytes( this.BufferSize );
this.ReadLoop--;
return this.BufferSize;
}
if ( this.ReadLast > 0 )
{
this.IOBuffer = this.ReadBytes( this.ReadLast );
return this.ReadLast;
}
return 0;
}
return -1;
}
}
}
The project test results was great. Performed as hoped. However, made some changes and left some code as is;
1. FPOS return is dynamic using FPOut set to String, Decimal, or Absolute. Precision 0 to 5 via DecimalPlaces
2. READ mechanics no change, returns bytes read, 0 end of stream, -1 no object
3. EOF no change, return 0 not at stream end, 1 end of stream, null no object
I am sure the code can use tweaking here and there. Any are welcome and thanks!
Continue reading...