delphi – 如何吃流出流的字节?

我正在修复一个ZIP库类.在内部,几乎所有ZIP实现都使用 DEFLATE compression (RFC1951).

问题是,在Delphi中,我无法访问任何DEFLATE压缩库.但是我们做的一件事就是ZLIB compression code (RFC1950).它甚至还附带Delphi,还有其他六种实现方式.

在内部,ZLIB也使用DEFLATE进行压缩.所以我想做每个人都做过的事情 – 使用Delphi zlib库来实现其DEFLATE压缩功能.

问题是ZLIB在DEFLATED数据中添加了一个2字节的前缀和4字节的尾部:

[CMF]                1 byte
[FLG]                1 byte
[...deflate compressed data...]
[Adler-32 checksum]  4 bytes

所以我需要的是一种使用标准TCompressionStream(或TZCompressionStream,或TZCompressionStreamEx,取决于您正在使用的源代码)流来压缩数据的方法:

procedure CompressDataToTargetStream(sourceStream: TStream; targetStream: TStream);
var
   compressor: TCompressionStream;
begin
   compressor := TCompressionStream.Create(clDefault,targetStream); //clDefault = CompressionLevel
   try
      compressor.CopyFrom(sourceStream,sourceStream.Length)
   finally
      compressor.Free; 
   end;
end;

这是有效的,除了它写出前导2字节和尾随4字节;我需要去除那些.

所以我写了一个TByteEaterStream:

TByteEaterStream = class(TStream)
public
   constructor Create(TargetStream: TStream; 
         LeadingBytesToEat,TrailingBytesToEat: Integer);
end;

例如

procedure CompressDataToTargetStream(sourceStream: TStream; targetStream: TStream);
var
   byteEaterStream: TByteEaterStream;
   compressor: TCompressionStream;
begin
   byteEaterStream := TByteEaterStream.Create(targetStream,2,4); //2 leading bytes,4 trailing bytes
   try
      compressor := TCompressionStream.Create(clDefault,byteEaterStream); //clDefault = CompressionLevel
      try
         compressor.CopyFrom(sourceStream,sourceStream.Length)
      finally
         compressor.Free; 
      end;
   finally
      byteEaterStream.Free;
   end;
end;

此流将覆盖write方法.吃前2个字节是微不足道的.诀窍是吃掉4个字节.

食者流有一个4字节的数组,我总是保持缓冲区中每次写入的最后四个字节.当EaterStream被销毁时,尾随的四个字节随之而来.

问题是通过这个缓冲区洗几百万次写入会破坏性能.上游的典型用途是:

for each of a million data rows
    stream.Write(s,Length(s)); //30-90 character string

我绝对不希望上游用户必须表明“结束就在附近”.我只是希望它更快.

问题

观察流过的字节流,保留最后四个字节的最佳方法是什么;鉴于你不知道什么时候写作将是最后一次.

我正在修复的代码将整个压缩版本写入TStringStream,然后只抓取900MB – 6个字节来获取内部DEFLATE数据:

cs := TStringStream.Create('');
....write compressed data to cs
S := Copy(CS.DataString,3,Length(CS.DataString) - 6);

除了运行用户内存不足.最初我改变它以写入TFileStream,然后我可以执行相同的技巧.

但我想要更好的解决方案;流解决方案.我希望数据进入压缩的最终流,没有任何中间存储.

我的实施

并不是说它有所帮助;因为我不是要求系统甚至使用适应流来进行修剪

TByteEaterStream = class(TStream)
private
    FTargetStream: TStream;
    FTargetStreamOwnership: TStreamOwnership;
    FLeadingBytesToEat: Integer;
    FTrailingBytesToEat: Integer;
    FLeadingBytesRemaining: Integer;

    FBuffer: array of Byte;
    FValidBufferLength: Integer;
    function GetBufferValidLength: Integer;
public
    constructor Create(TargetStream: TStream; LeadingBytesToEat,TrailingBytesToEat: Integer; StreamOwnership: TStreamOwnership=soReference);
    destructor Destroy; override;

    class procedure SelfTest;

    procedure Flush;

    function Read(var Buffer; Count: Longint): Longint; override;
    function Write(const Buffer; Count: Longint): Longint; override;
    function Seek(Offset: Longint; Origin: Word): Longint; override;
end;

{ TByteEaterStream }

constructor TByteEaterStream.Create(TargetStream: TStream; LeadingBytesToEat,TrailingBytesToEat: Integer; StreamOwnership: TStreamOwnership=soReference);
begin
    inherited Create;

    //User requested state
    FTargetStream := TargetStream;
    FTargetStreamOwnership := StreamOwnership;
    FLeadingBytesToEat := LeadingBytesToEat;
    FTrailingBytesToEat := TrailingBytesToEat;

    //internal housekeeping
    FLeadingBytesRemaining := FLeadingBytesToEat;

    SetLength(FBuffer,FTrailingBytesToEat);
    FValidBufferLength := 0;
end;

destructor TByteEaterStream.Destroy;
begin
    if FTargetStreamOwnership = soOwned then
        FTargetStream.Free;
    FTargetStream := nil;

    inherited;
end;

procedure TByteEaterStream.Flush;
begin
    if FValidBufferLength > 0 then
    begin
        FTargetStream.Write(FBuffer[0],FValidBufferLength);
        FValidBufferLength  := 0;
    end;
end;

function TByteEaterStream.Write(const Buffer; Count: Integer): Longint;
var
    newStart: Pointer;
    totalCount: Integer;
    addIndex: Integer;
    bufferValidLength: Integer;
    bytesToWrite: Integer;
begin
    Result := Count;

    if Count = 0 then
        Exit;

    if FLeadingBytesRemaining > 0 then
    begin
        newStart := Addr(Buffer);
        Inc(Cardinal(newStart));
        Dec(Count);
        Dec(FLeadingBytesRemaining);
        Result := Self.Write(newStart^,Count)+1; //tell the upstream guy that we wrote it

        Exit;
    end;

    if FTrailingBytesToEat > 0 then
    begin
        if (Count < FTrailingBytesToEat) then
        begin
            //There's less bytes incoming than an entire buffer
            //But the buffer might overfloweth
            totalCount := FValidBufferLength+Count;

            //If it could all fit in the buffer,then let it
            if (totalCount <= FTrailingBytesToEat) then
            begin
                Move(Buffer,FBuffer[FValidBufferLength],Count);
                FValidBufferLength := totalCount;
            end
            else
            begin
                //We're going to overflow the buffer.

                //Purge from the buffer the amount that would get pushed
                FTargetStream.Write(FBuffer[0],totalCount-FTrailingBytesToEat);

                //Shuffle the buffer down (overlapped move)
                bufferValidLength := bufferValidLength - (totalCount-FTrailingBytesToEat);
                Move(FBuffer[totalCount-FTrailingBytesToEat],FBuffer[0],bufferValidLength);

                addIndex := bufferValidLength ; //where we will add the data to
                Move(Buffer,FBuffer[addIndex],Count);
            end;
        end
        else if (Count = FTrailingBytesToEat) then
        begin
            //The incoming bytes exactly fill the buffer. Flush what we have and eat the incoming amounts
            Flush;
            Move(Buffer,FTrailingBytesToEat);
            FValidBufferLength := FTrailingBytesToEat;
            Result := FTrailingBytesToEat; //we "wrote" n bytes
        end
        else
        begin
            //Count is greater than trailing buffer eat size
            Flush;

            //Write the data that definitely not to be eaten
            bytesToWrite := Count-FTrailingBytesToEat;
            FTargetStream.Write(Buffer,bytesToWrite);

            //Buffer the remainder
            newStart := Addr(Buffer);
            Inc(Cardinal(newStart),bytesToWrite);

            Move(newStart^,FTrailingBytesToEat);
            FValidBufferLength := 4;
        end;
    end;
end;

function TByteEaterStream.Seek(Offset: Integer; Origin: Word): Longint;
begin
    //what does it mean if they want to seek around when i'm supposed to be eating data?
    //i don't know; so results are,by definition,undefined. Don't use at your own risk
    Result := FTargetStream.Seek(Offset,Origin);
end;

function TByteEaterStream.Read(var Buffer; Count: Integer): Longint;
begin
    //what does it mean if they want to read back bytes when i'm supposed to be eating data?
    //i don't know; so results are,undefined. Don't use at your own risk
    Result := FTargetStream.Read({var}Buffer,Count);
end;

class procedure TByteEaterStream.SelfTest;

    procedure CheckEquals(Expected,Actual: string; Message: string);
    begin
        if Actual <> Expected then
            raise Exception.CreateFmt('TByteEaterStream self-test failed. Expected "%s",but was "%s". Message: %s',[Expected,Actual,Message]);
    end;

    procedure Test(const InputString: string; ExpectedString: string);
    var
        s: TStringStream;
        eater: TByteEaterStream;
    begin
        s := TStringStream.Create('');
        try
            eater := TByteEaterStream.Create(s,4,soReference);
            try
                eater.Write(InputString[1],Length(InputString));
            finally
                eater.Free;
            end;
            CheckEquals(ExpectedString,s.DataString,InputString);
        finally
            s.Free;
        end;
    end;
begin
    Test('1','');
    Test('11','');
    Test('113','');
    Test('1133','');
    Test('11333','');
    Test('113333','');
    Test('11H3333','H');
    Test('11He3333','He');
    Test('11Hel3333','Hel');
    Test('11Hell3333','Hell');
    Test('11Hello3333','Hello');
    Test('11Hello,3333','Hello,');
    Test('11Hello,W3333',W');
    Test('11Hello,Wo3333',Wo');
    Test('11Hello,Wor3333',Wor');
    Test('11Hello,Worl3333',Worl');
    Test('11Hello,World3333',World');
    Test('11Hello,World!3333',World!');
end;

解决方法

您需要推迟写入,直到您确定要知道要写入的字节不是必须吃掉的尾随字节.这一观察结果使您认为缓冲将提供解决方案.

所以,我建议这样做:

>使用使用缓冲的流适配器.
>吃前导字节很容易.刚刚将前两个字节发送到遗忘状态.
>在缓冲区之后写入要写入的字节,当需要刷新时,刷新缓冲区中除最后四个字节外的所有字节.
>刷新时,将未刷新的四个字节复制到缓冲区的开头,这样就不会丢失它们.
>关闭流时,将其冲洗,就像对缓冲流一样.并使用与以前相同的刷新技术,以便保持最后的四个字节.此时您知道这些是流的最后四个字节.

上述方法要求的一个要求是缓冲区的大小必须大于要剥离的尾随字节数.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都是大家转来转去,原文出处我已经找不到了。这篇文章写的很不错,但最后部分“PermuteFunction 的终极版本”描述的不太清楚,完全按照该文章代码执行,是不行的。可能是作者故意这样做的?本文最后提供修正后的下载地址。原文如下:一、关于API Hook1.什么是API Hook不知道大家是否还记得,在DO
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都是大家转来转去,原文出处我已经找不到了。 这篇文章写的很不错,但最后部分“PermuteFunction 的终极版本”描述的不太清楚,完全按照该文章代码执行,是不行的。需要修改mess.pas中代码才行。其实文中提到的一个结构,代码中并没有使用typePIMAGE_IMPORT_DESCRIPTOR = ^IMA
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c++ 调用, 抽空研究了一下该库的最新版 ,把部分api 翻译成了dephi版的 记录一下 地址 ffmpegvcl.zip
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和EDX)2个变址和指针寄存器(ESI和EDI) 2个指针寄存器(ESP和EBP)6个段寄存器(ES、CS、SS、DS、FS和GS)
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的功能是:将源操作数送到目的操作数中,即:(dst) &lt;--(src) 1.dst和src类型必须匹配,即必须同为字节
有三个API函数可以运行可执行文件WinExec、ShellExecute和CreateProcess。 1.CreateProcess因为使用复杂,比较少用。 2.WinExec主要运行EXE文件。如:WinExec('Notepad.exe Readme.txt', SW_SHOW); 3.ShellExecute不仅可以运行EXE文件,也可以运行已经关联的文件。 首先必须引用shellapi
API原型: Declare Function MoveFileEx& Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) 参数 类型及说明 lpExistingFileName String,欲移
附带通用控件安装方法: ---------- 基本安装 1、对于单个控件,Componet-->install component..-->PAS或DCU文件-->install; 2、对于带*.dpk文件的控件包,File-->Open(下拉列表框中选*.dpk)-->install即可; 3、对于带*.bpl文件的控件包,Install Packages-->Add-->bpl文件名即可; 4
type   TRec=Record     msg:string;     pic:TMemoryStream; end; procedure TForm2.BitBtn1Click(Sender: TObject); var   ms:TMemoryStream;   Rec1,Rec2:TRec;   cc:tmemorystream;   jpg:TJPEGImage; begin   R
program Project1; { Types and Structures Definition } type   WNDCLASSEX = packed record     cbSize: LongWord;     style: LongWord;     lpfnWndProc: Pointer;     cbClsExtra: Integer;     cbWndExtra: In
   在Windows大行其道的今天,windows界面程序受到广大用户的欢迎。对这些程序的操作不外乎两种,键盘输入控制和鼠标输入控制。有时,对于繁杂 的,或重复性的操作,我们能否通过编制程序来代替手工输入,而用程序来模拟键盘及鼠标的输入呢?答案是肯定的。这主要是通过两个API函数来实现的。      下面以Delphi为例来介绍一下如何实现这两个功能。模拟键盘我们用Keybd_event这个ap
delphi中经常见到以下两种定义 Type TMouseProc = procedure (X,Y:integer); TMouseEvent = procedure (X,Y:integer) of Object; 两者样子差不多但实际意义却不一样, TMouseProc只是单一的函数指针类型; TMouseEvent是对象的函数指针,也就是对象/类的函数/方法 区
Windows 2000/XP和2003等支持一种叫做"服务程序"的东西.程序作为服务启动有以下几个好处:     (1)不用登陆进系统即可运行.     (2)具有SYSTEM特权.所以你在进程管理器里面是无法结束它的.     笔者在2003年为一公司开发机顶盒项目的时候,曾经写过课件上传和媒体服务,下面就介绍一下如何用Delphi7创建一个Service程序.     运行Delphi7,选
方法一: 1.调试delphi 写的服务程序,有这么一个办法。原来每次都是用attach to process方法,很麻烦。并且按照服务线程的执行线路,可能会停不到想要的断点。笨办法是,在procedure TsvcFrm.ServiceExecute(Sender: TService);中想要下断的语句前加个人定胜天的sleep(20000),但实际上这种办法是主观臆测的。可行,没问题。记得大学
Delphi For iOS开发指南(17):让应用程序禁止竖屏(也就是只显示横屏)     最近好多人问,怎么样让Delphi For iOS开发的应用程序禁止竖屏,也就是想让它一直横屏显示,横屏是好,一行可以放好几个控件,表格的话也可以多显示几列,看起来方便。 只要一句代码就可以让Delphi For iOS开发的应用程序禁止竖屏,如下: Application.FormFactor.Orie
一个比较完整的Inno Setup 安装脚本,增加了对ini文件设置的功能,一个安装包常用的功能都具备了。 [Setup] ; 注: AppId的值为单独标识该应用程序。 ; 不要为其他安装程序使用相同的AppId值。 ; (生成新的GUID,点击 工具|在IDE中生成GUID。) AppId={{A9861883-31C5-4324-BD9A-DC3271EEB675} ;程序名 AppName
在Delphi自带的Indy控件中其实是提供了MD2,MD4,MD5对象的,我们可以直接使用它们来完成MD5的签名算法。而不需要再去找其它的DLL或是Pas了。 在Uses单元中引用 IdHashMessageDigest,IdGlobal, IdHash 单元,再写如下代码即可以达到MD5的实现。 示例代码 procedure TForm1.Button1Click(Sender: TObjec
在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本。定义如下: 1 2 3 4 5 6 7 8 9 _NOTIFYICONDATAA = record    cbSize: DWORD;    Wnd: HWND;    uID: UINT;    uFlags: UINT;    uCallback
声明: 1. type Name = Existing type; 2. type Name = type Existing type; 3. type Name = (EnumValue1 [=value], EnumValue2 [=value] ...); 4. type Name = Expression1..Expression2; 5. type Name = ^Existing ty