RAD Studio VCL Reference
|
Reads bytes from the stream into Buffer.
procedure ReadBuffer(var Buffer; Count: Longint);
__fastcall ReadBuffer( Buffer, Longint Count);
Use ReadBuffer to read Count bytes from the stream into a buffer in cases where the number of bytes is known and fixed, for example when reading in structures. ReadBuffer is used internally for loading from a stream and copying from a stream.
ReadBuffer calls Read to do the actual reading. If Count bytes cannot be read from the stream, an EReadError exception is raised.
Delphi Examples:
{ This example determines the encoding of a source file and then writes the file in any encoding chosen. Use this project to create files of any encoding. This example requires two text edits and a combobox. } procedure TForm1.Button1Click(Sender: TObject); var LBuffer: TBytes; LByteOrderMark: TBytes; LOffset: Integer; LEncoding, DestEncoding: TEncoding; LFileStream: TFileStream; EncodingArray: array[0..5] of TEncoding; begin LEncoding:= nil; EncodingArray[0]:= TEncoding.UTF8; EncodingArray[1]:= TEncoding.UTF7; EncodingArray[2]:= TEncoding.Unicode; EncodingArray[3]:= TEncoding.Default; EncodingArray[4]:= TEncoding.BigEndianUnicode; EncodingArray[5]:= TEncoding.ASCII; DestEncoding := EncodingArray[ComboBox1.ItemIndex]; LFileStream := TFileStream.Create(Edit1.Text, fmOpenRead); try // Read file into buffer SetLength(LBuffer, LFileStream.Size); // LFileStream.Read(LBuffer[0], Length(LBuffer)); LFileStream.ReadBuffer(Pointer(LBuffer)^, Length(LBuffer)); // Identify encoding and convert buffer to UTF8 LOffset := TEncoding.GetBufferEncoding(LBuffer, LEncoding); // for I := 0 to Length(LBuffer) - 1 do // Memo1.Lines.Add((LBuffer[I])); LBuffer := LEncoding.Convert(LEncoding, DestEncoding, LBuffer, LOffset, Length(LBuffer) - LOffset); finally LFileStream.Free; end; LFileStream := TFileStream.Create(Edit2.Text, fmCreate); try // Write an encoding byte order mark and buffer to output file LByteOrderMark := DestEncoding.GetPreamble; LFileStream.Write(LByteOrderMark[0], Length(LByteOrderMark)); LFileStream.Write(LBuffer[0], Length(LBuffer)); finally LFileStream.Free; end; end; procedure TForm1.FormCreate(Sender: TObject); var StringList: TStrings; begin StringList := TStringList.Create; try with StringList do begin Add('TEncoding.UTF8'); Add('TEncoding.UTF7'); Add('TEncoding.Unicode'); Add('TEncoding.Default'); Add('TEncoding.BigEndianUnicode'); Add('TEncoding.ASCII'); end; with ComboBox1 do begin Items.Assign(StringList); ItemIndex := 0; end; finally StringList.free; end; end;
{ This example requires TListBox, TMemo, TRichEdit, and TButton controls placed on the form. The list box should contain one or more items. When the form becomes visible, click on the button and the contents of the list box will be transferred to a stream and then to the rich edit control. } procedure TForm1.Button1Click(Sender: TObject); var TempStream : TMemoryStream; begin TempStream := TMemoryStream.Create; ListBox1.Items.SaveToStream(TempStream); // write list box contents to the // stream TempStream.Position := 0; // reset to the beginning of the stream // RichEdit1.Lines.LoadFromStream( TempStream); // load stream contents into rich // edit control LoadFromStream(TempStream, RichEdit1.Lines, nil); TempStream.Free; end; procedure TForm1.LoadFromStream(Stream: TStream; myStrings: TStrings; Encoding: TEncoding); var Size: Integer; Buffer: TBytes; begin myStrings.BeginUpdate; try Size := Stream.Size - Stream.Position; SetLength(Buffer, Size); Stream.ReadBuffer(Pointer(Buffer)^, Size); Size := TEncoding.GetBufferEncoding(Buffer, Encoding); myStrings.Text := Encoding.GetString(Buffer, Size, Length(Buffer) - Size); finally myStrings.EndUpdate; end; end; procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.Items.Add('Apples'); ListBox1.Items.Add('Oranges'); ListBox1.Items.Add('Pears'); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|