Hi

Hi,
We need to implement a functionality in our VCL application where the user selects a PDF-file, and that will turn it to a Base64 string that we can store in a text field.
Any who can help us?

Comments

  1. Uwe Raabe In my XE7 I see this:

    function TBase64Encoding.DoDecode(const Input, Output: TStream): Integer;
    var
    InBuf: array[0..1023] of Byte;
    OutBuf: array[0..767] of Byte;
    BytesRead, BytesWrite: Integer;
    State: TDecodeState;
    begin
    InitDecodeState(State);
    Result := 0;
    repeat
    BytesRead := Input.Read(InBuf[0], Length(InBuf));
    BytesWrite := DecodeBytes(@InBuf[0], @OutBuf[0], BytesRead, 1, State);
    Output.Write(Outbuf, BytesWrite);
    Result := Result + BytesWrite;
    until BytesRead = 0;
    end;

    function TBase64Encoding.DoEncode(const Input, Output: TStream): Integer;
    var
    InBuf: array[0..767] of Byte;
    OutBuf: array[0..1023] of Byte;
    BytesRead, BytesWrite: Integer;
    State: TEncodeState;
    LineSeparator: TBytes;
    begin
    LineSeparator := TEncoding.UTF8.GetBytes(FLineSeparator);
    InitEncodeState(State);
    Result := 0;
    repeat
    BytesRead := Input.Read(InBuf[0], Length(InBuf));
    BytesWrite := EncodeBytes(@InBuf[0], @OutBuf[0], BytesRead, 1, LineSeparator, State);
    Output.Write(Outbuf, BytesWrite);
    Result := Result + BytesWrite;
    until BytesRead = 0;
    BytesWrite := EncodeBytesEnd(@OutBuf[0], 1, State);
    Result := Result + BytesWrite;
    Output.Write(Outbuf, BytesWrite);
    end;

    So I think that for base64 it's all nicely buffer, and buffered on the stack too.

    It's true that TNetEncoding.DoDecode and TNetEncoding.DoEncode read the entire stream into a byte array. But they are only used for the HTML and URL encodings, as I read the source code.

    ReplyDelete
  2. David Heffernan You are right. I missed the different implementation of TBase64Encoding.

    ReplyDelete

Post a Comment