Código Delphi
[-]
function LeerCom(Stream: TStream): string;
var
Buffer: array[0..$FFFF] of Char;
Header: array[1..4] of byte;
Size: Integer;
begin
Stream.Seek(0,soFromBeginning);
Stream.ReadBuffer(Header,2);
if (Header[1] <> $FF) or (Header[2] <> $D8) then
raise Exception.Create('Identificador incorrecto');
repeat
Stream.ReadBuffer(Header,4);
Size:= ((Header[3] shl 8) + Header[4]) - 2;
case Header[2] of
$FE: begin
FillChar(Buffer,Sizeof(Buffer),0);
Stream.ReadBuffer(Buffer,Size);
Result:= String(PChar(@Buffer));
end;
$01,$D0..$D9: Stream.Seek(-2,soFromCurrent) ;
else Stream.Seek(Size,soFromCurrent);
end;
until (Result <> '') or (Header[2] = $DA);
end;
Ejemplo de uso (Se utiliza un TOpenDialog):
Código Delphi
[-]
var
Stream: TFileStream;
begin
if OpenDialog1.Execute then
begin
Stream:= TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
try
ShowMessage(LeerCom(Stream));
finally
Stream.Free;
end;
end;
end;