2014-06-01 13:32:21 +00:00
|
|
|
unit X2Log.Util.Stream;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
System.Classes,
|
|
|
|
System.SysUtils;
|
|
|
|
|
|
|
|
type
|
|
|
|
TStreamUtil = class(TObject)
|
|
|
|
protected
|
|
|
|
class function GetEncoding(AEncoding: TEncoding): TEncoding;
|
|
|
|
public
|
2016-10-13 20:58:56 +00:00
|
|
|
class function ReadByte(AStream: TStream): Byte;
|
|
|
|
class procedure WriteByte(AStream: TStream; AValue: Byte);
|
|
|
|
|
2014-06-01 13:32:21 +00:00
|
|
|
class function ReadCardinal(AStream: TStream): Cardinal;
|
|
|
|
class procedure WriteCardinal(AStream: TStream; AValue: Cardinal);
|
|
|
|
|
2016-10-14 14:43:53 +00:00
|
|
|
class function ReadString(AStream: TStream; AEncoding: TEncoding = nil; AReadSize: Boolean = True; ASize: Cardinal = 0): string;
|
|
|
|
class procedure WriteString(AStream: TStream; const AValue: string; AEncoding: TEncoding = nil; AWriteSize: Boolean = True);
|
2014-06-01 13:32:21 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
|
|
{ TStreamUtil }
|
|
|
|
class function TStreamUtil.GetEncoding(AEncoding: TEncoding): TEncoding;
|
|
|
|
begin
|
|
|
|
if Assigned(AEncoding) then
|
|
|
|
Result := AEncoding
|
|
|
|
else
|
|
|
|
Result := TEncoding.UTF8;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-10-13 20:58:56 +00:00
|
|
|
class function TStreamUtil.ReadByte(AStream: TStream): Byte;
|
|
|
|
begin
|
|
|
|
AStream.ReadBuffer(Result, SizeOf(Byte));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
class procedure TStreamUtil.WriteByte(AStream: TStream; AValue: Byte);
|
|
|
|
begin
|
|
|
|
AStream.WriteBuffer(AValue, SizeOf(Byte));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-06-01 13:32:21 +00:00
|
|
|
class function TStreamUtil.ReadCardinal(AStream: TStream): Cardinal;
|
|
|
|
begin
|
|
|
|
AStream.ReadBuffer(Result, SizeOf(Cardinal));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
class procedure TStreamUtil.WriteCardinal(AStream: TStream; AValue: Cardinal);
|
|
|
|
begin
|
|
|
|
AStream.WriteBuffer(AValue, SizeOf(Cardinal));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-10-14 14:43:53 +00:00
|
|
|
class function TStreamUtil.ReadString(AStream: TStream; AEncoding: TEncoding; AReadSize: Boolean; ASize: Cardinal): string;
|
2014-06-01 13:32:21 +00:00
|
|
|
var
|
|
|
|
bytes: TBytes;
|
|
|
|
bytesLength: Cardinal;
|
|
|
|
|
|
|
|
begin
|
2016-10-14 14:43:53 +00:00
|
|
|
if AReadSize then
|
|
|
|
bytesLength := ReadCardinal(AStream)
|
|
|
|
else
|
|
|
|
bytesLength := ASize;
|
|
|
|
|
2014-06-01 13:32:21 +00:00
|
|
|
if bytesLength > 0 then
|
|
|
|
begin
|
|
|
|
SetLength(bytes, bytesLength);
|
|
|
|
AStream.ReadBuffer(bytes[0], bytesLength);
|
|
|
|
|
|
|
|
Result := GetEncoding(AEncoding).GetString(bytes);
|
|
|
|
end else
|
|
|
|
Result := '';
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-10-14 14:43:53 +00:00
|
|
|
class procedure TStreamUtil.WriteString(AStream: TStream; const AValue: string; AEncoding: TEncoding; AWriteSize: Boolean);
|
2014-06-01 13:32:21 +00:00
|
|
|
var
|
|
|
|
bytes: TBytes;
|
|
|
|
bytesLength: Cardinal;
|
|
|
|
|
|
|
|
begin
|
|
|
|
bytes := GetEncoding(AEncoding).GetBytes(AValue);
|
|
|
|
bytesLength := Length(bytes);
|
|
|
|
|
2016-10-14 14:43:53 +00:00
|
|
|
if AWriteSize then
|
|
|
|
WriteCardinal(AStream, bytesLength);
|
|
|
|
|
2014-06-01 13:32:21 +00:00
|
|
|
if bytesLength > 0 then
|
|
|
|
AStream.WriteBuffer(bytes[0], bytesLength);
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|