{ Use TStringStream as a buffer, you can use this unittoread/writebyte,word,orinteger values. }
unit BufferReader;
interface
uses Classes, Windows;
type TValueType =(vtInt8, vtInt16, vtInt32);
TBufferReader =class(TStringStream) private function RevertValue(Value:Integer):Integer; function GetValueTypeLen(ValueType: TValueType):Integer; public function ReadValue(ValueType: TValueType):Integer; procedure WriteValue(Value:Integer; ValueType: TValueType); end;
implementation
{ TBufferReader }
function TBufferReader.GetValueTypeLen(ValueType: TValueType):Integer; begin case ValueType of vtInt8: Result := 1; vtInt16: Result := 2; else Result := 4; end; end;
procedure TBufferReader.WriteValue(Value:Integer; ValueType: TValueType); begin Value := RevertValue(Value); Write(Value, GetValueTypeLen(ValueType)); end;
function TBufferReader.ReadValue(ValueType: TValueType):Integer; var Value:Integer; begin Read(Value, GetValueTypeLen(ValueType)); Result := RevertValue(Value); end;
function TBufferReader.RevertValue(Value:Integer):Integer; var HiWord, LoWord:Word; HiByte, LoByte:Byte; begin LoWord := Value and $FFFF; HiWord :=(Value and $FFFF0000)shr 16;
LoByte := LoWord and $FF; HiByte :=(LoWord and $FF00)shr 8; if DWORD(Value)<= $FF then LoWord := LoByte else LoWord :=(LoByte shl 8)or HiByte;
LoByte := HiWord and $FF; HiByte :=(HiWord and $FF00)shr 8; HiWord :=(LoByte shl 8)or HiByte;
if DWORD(Value)<= $FFFF then Result := LoWord else Result :=(LoWord shl 16)or HiWord; end;