Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-04

Can't copy TMemoryStream to TProcess.Input

13 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

Hi, it must be that I've been working for more than 8 hrs but I can't find where the problem is...

I need to pass a file via stdIn to TProcess, then capture the StdOut. This program should do what I expect, but, instead it hangs after writing aprox 293000 bytes to Proc.Input stream.

Can you take a look at it?.

program test;
{$mode objfpc}{$H+}

uses
SysUtils, Classes,
process,

  math;
function CompressLZMA(AStream: TMemoryStream): TMemoryStream;
var
  Proc: TProcess;
  lBuffer: array [0..2048] of byte;
  ReadCount: integer;
  lPos: LongInt;
begin
  Result := TMemoryStream.Create;
  AStream.Position:= 0;
  writeln('-1-');
  Proc := TProcess.Create(nil); //Create a new process
  try
    Proc.Executable := '/usr/bin/lzma'; //Run lzma
    Proc.Parameters.Add('--compress');
    Proc.Options := [poUsePipes, poStderrToOutPut];
    Proc.Execute;
    writeln(format('Writing %d bytes...', [AStream.Size]));
    lPos := 0;
    while(lPos < AStream.Size) do
    begin
      writeln(format('Pos: %d - Size: %d', [lPos, AStream.Size]));
      ReadCount := AStream.Read(lBuffer, SizeOf(lBuffer));
      Proc.Input.Write(lBuffer, ReadCount);
      lPos := lPos + ReadCount;
    end;
  writeln('-2-');
    Proc.CloseInput;
  writeln('-3-');
    while Proc.Running or (Proc.Output.NumBytesAvailable > 0) do
    begin
  writeln('-4-');
      while Proc.Output.NumBytesAvailable > 0 do
      begin
        ReadCount := Min(512, Proc.Output.NumBytesAvailable);
        Proc.Output.Read(lBuffer, ReadCount);
        Result.Write(lBuffer, ReadCount);
        writeln(format('%d', [ReadCount]));
      end;
    end;
    Result.Position:= 0;
  finally
    Proc.Free;
  end;
end;
var
  lStr: TMemoryStream;
begin
  lStr := TMemoryStream.Create;
  try
    lStr.LoadFromFile('binary.file');
    CompressLZMA(lStr).SaveToFile('output.lzma');
  finally
    lStr.Free;
  end;
end.

Reply by email

Mattias Gaertner
to Leonardo M. Ramé
Show quoted text 44 lines
Hi, it must be that I've been working for more than 8 hrs but I can't find where the problem is... I need to pass a file via stdIn to TProcess, then capture the StdOut. This program should do what I expect, but, instead it hangs after writing aprox 293000 bytes to Proc.Input stream. Can you take a look at it?. program test; {$mode objfpc}{$H+} uses SysUtils, Classes, process, math; function CompressLZMA(AStream: TMemoryStream): TMemoryStream; var Proc: TProcess; lBuffer: array [0..2048] of byte; ReadCount: integer; lPos: LongInt; begin Result := TMemoryStream.Create; AStream.Position:= 0; writeln('-1-'); Proc := TProcess.Create(nil); //Create a new process try Proc.Executable := '/usr/bin/lzma'; //Run lzma Proc.Parameters.Add('--compress'); Proc.Options := [poUsePipes, poStderrToOutPut]; Proc.Execute; writeln(format('Writing %d bytes...', [AStream.Size])); lPos := 0; while(lPos < AStream.Size) do begin writeln(format('Pos: %d - Size: %d', [lPos, AStream.Size])); ReadCount := AStream.Read(lBuffer, SizeOf(lBuffer)); Proc.Input.Write(lBuffer, ReadCount); lPos := lPos + ReadCount;

After every write to Input, read all available from Output and StdErr.

Mattias

Reply by email

Leonardo M. Ramé
to Mattias Gaertner
Mattias Gaertner wrote 52 lines
On Fri, 5 Apr 2013 17:33:22 -0300 Leonardo M. Ram? wrote: > Hi, it must be that I've been working for more than 8 hrs but I can't find where the problem is... > > I need to pass a file via stdIn to TProcess, then capture the StdOut. This > program should do what I expect, but, instead it hangs after writing aprox > 293000 bytes to Proc.Input stream. > > Can you take a look at it?. > > program test; > > {$mode objfpc}{$H+} > > uses > SysUtils, Classes, > process, > math; > > > function CompressLZMA(AStream: TMemoryStream): TMemoryStream; > var > Proc: TProcess; > lBuffer: array [0..2048] of byte; > ReadCount: integer; > lPos: LongInt; > begin > Result := TMemoryStream.Create; > AStream.Position:= 0; > writeln('-1-'); > Proc := TProcess.Create(nil); //Create a new process > try > Proc.Executable := '/usr/bin/lzma'; //Run lzma > Proc.Parameters.Add('--compress'); > Proc.Options := [poUsePipes, poStderrToOutPut]; > Proc.Execute; > > writeln(format('Writing %d bytes...', [AStream.Size])); > > lPos := 0; > while(lPos < AStream.Size) do > begin > writeln(format('Pos: %d - Size: %d', [lPos, AStream.Size])); > ReadCount := AStream.Read(lBuffer, SizeOf(lBuffer)); > Proc.Input.Write(lBuffer, ReadCount); > lPos := lPos + ReadCount; After every write to Input, read all available from Output and StdErr. Mattias

Thanks Mattias, I went a step further.

Sadly now I'm getting an "unexpected end of input" error when I try to decompress the generated file using "lzma -d -c output.lzma > output.dcm".

Attached is the modified program.

Regards,

Leonardo M. Ram? http://leonardorame.blogspot.com -------------- next part -------------- program test; {$mode objfpc}{$H+} uses SysUtils, Classes, process, math; function CompressLZMA(AStream: TMemoryStream): TMemoryStream; var Proc: TProcess; lBuffer: array [0..2047] of byte; ReadCount: longint; WriteCount: longint; begin Result := TMemoryStream.Create; AStream.Position:= 0; Proc := TProcess.Create(nil); try Proc.Executable := '/usr/bin/lzma'; Proc.Parameters.Add('-z'); Proc.Parameters.Add('-c'); Proc.Options := [poUsePipes, poStderrToOutPut]; Proc.Execute; while(AStream.Position < AStream.Size) do begin ReadCount := AStream.Read(lBuffer, SizeOf(lBuffer)); Proc.Input.Write(lBuffer, ReadCount); while (Proc.Output.NumBytesAvailable > 0) do begin WriteCount := Min(SizeOf(lBuffer), Proc.Output.NumBytesAvailable); Proc.Output.Read(lBuffer, WriteCount); Result.Write(lBuffer, WriteCount); end; end; Result.Position:= 0; finally Proc.Free; end; end; var lStr: TMemoryStream; begin lStr := TMemoryStream.Create; try lStr.LoadFromFile('mg.dcm'); CompressLZMA(lStr).SaveToFile('output.lzma'); finally lStr.Free; end; end.

Reply by email

Mattias Gaertner
to Leonardo M. Ramé
Show quoted text 59 lines
On 2013-04-06 00:04:07 +0200, Mattias Gaertner wrote: > On Fri, 5 Apr 2013 17:33:22 -0300 > Leonardo M. Ram? wrote: > > > Hi, it must be that I've been working for more than 8 hrs but I can't find where the problem is... > > > > I need to pass a file via stdIn to TProcess, then capture the StdOut. This > > program should do what I expect, but, instead it hangs after writing aprox > > 293000 bytes to Proc.Input stream. > > > > Can you take a look at it?. > > > > program test; > > > > {$mode objfpc}{$H+} > > > > uses > > SysUtils, Classes, > > process, > > math; > > > > > > function CompressLZMA(AStream: TMemoryStream): TMemoryStream; > > var > > Proc: TProcess; > > lBuffer: array [0..2048] of byte; > > ReadCount: integer; > > lPos: LongInt; > > begin > > Result := TMemoryStream.Create; > > AStream.Position:= 0; > > writeln('-1-'); > > Proc := TProcess.Create(nil); //Create a new process > > try > > Proc.Executable := '/usr/bin/lzma'; //Run lzma > > Proc.Parameters.Add('--compress'); > > Proc.Options := [poUsePipes, poStderrToOutPut]; > > Proc.Execute; > > > > writeln(format('Writing %d bytes...', [AStream.Size])); > > > > lPos := 0; > > while(lPos < AStream.Size) do > > begin > > writeln(format('Pos: %d - Size: %d', [lPos, AStream.Size])); > > ReadCount := AStream.Read(lBuffer, SizeOf(lBuffer)); > > Proc.Input.Write(lBuffer, ReadCount); > > lPos := lPos + ReadCount; > > After every write to Input, read all available from Output and StdErr. > > Mattias > Thanks Mattias, I went a step further. Sadly now I'm getting an "unexpected end of input" error when I try to decompress the generated file using "lzma -d -c output.lzma > output.dcm".

Check return value of Proc.Input.Write.
And call Proc.CloseInput when your input was written completely.

Mattias

Reply by email

Leonardo M. Ramé
to Mattias Gaertner
Mattias Gaertner wrote 67 lines
On Sat, 6 Apr 2013 11:55:41 -0300 Leonardo M. Ram? wrote: > On 2013-04-06 00:04:07 +0200, Mattias Gaertner wrote: > > On Fri, 5 Apr 2013 17:33:22 -0300 > > Leonardo M. Ram? wrote: > > > > > Hi, it must be that I've been working for more than 8 hrs but I can't find where the problem is... > > > > > > I need to pass a file via stdIn to TProcess, then capture the StdOut. This > > > program should do what I expect, but, instead it hangs after writing aprox > > > 293000 bytes to Proc.Input stream. > > > > > > Can you take a look at it?. > > > > > > program test; > > > > > > {$mode objfpc}{$H+} > > > > > > uses > > > SysUtils, Classes, > > > process, > > > math; > > > > > > > > > function CompressLZMA(AStream: TMemoryStream): TMemoryStream; > > > var > > > Proc: TProcess; > > > lBuffer: array [0..2048] of byte; > > > ReadCount: integer; > > > lPos: LongInt; > > > begin > > > Result := TMemoryStream.Create; > > > AStream.Position:= 0; > > > writeln('-1-'); > > > Proc := TProcess.Create(nil); //Create a new process > > > try > > > Proc.Executable := '/usr/bin/lzma'; //Run lzma > > > Proc.Parameters.Add('--compress'); > > > Proc.Options := [poUsePipes, poStderrToOutPut]; > > > Proc.Execute; > > > > > > writeln(format('Writing %d bytes...', [AStream.Size])); > > > > > > lPos := 0; > > > while(lPos < AStream.Size) do > > > begin > > > writeln(format('Pos: %d - Size: %d', [lPos, AStream.Size])); > > > ReadCount := AStream.Read(lBuffer, SizeOf(lBuffer)); > > > Proc.Input.Write(lBuffer, ReadCount); > > > lPos := lPos + ReadCount; > > > > After every write to Input, read all available from Output and StdErr. > > > > Mattias > > > > Thanks Mattias, I went a step further. > > Sadly now I'm getting an "unexpected end of input" error when I try to > decompress the generated file using "lzma -d -c output.lzma > > output.dcm". Check return value of Proc.Input.Write. And call Proc.CloseInput when your input was written completely.

Mmm, I'm getting the same result.

I attached the new code.

Leonardo M. Ram? http://leonardorame.blogspot.com -------------- next part -------------- program test; {$mode objfpc}{$H+} uses SysUtils, Classes, process, math; function CompressLZMA(AStream: TMemoryStream): TMemoryStream; var Proc: TProcess; lInBuffer: array [0..2047] of byte; lOutBuffer: array [0..2047] of byte; ReadCount: longint; WriteCount: longint; InputWriteCount: longint; begin Result := TMemoryStream.Create; AStream.Position:= 0; Proc := TProcess.Create(nil); try Proc.Executable := '/usr/bin/lzma'; Proc.Parameters.Add('-z'); Proc.Parameters.Add('-c'); Proc.Options := [poUsePipes, poStderrToOutPut]; Proc.Execute; InputWriteCount := 0; while true do begin ReadCount := AStream.Read(lInBuffer, SizeOf(lInBuffer)); InputWriteCount := InputWriteCount + Proc.Input.Write(lInBuffer, ReadCount); writeln(Format('%d - %d', [InputWriteCount, AStream.Size])); if InputWriteCount = AStream.Size then Proc.CloseInput; while (Proc.Output.NumBytesAvailable > 0) do begin WriteCount := Min(SizeOf(lOutBuffer), Proc.Output.NumBytesAvailable); Proc.Output.Read(lOutBuffer, WriteCount); Result.Write(lOutBuffer, WriteCount); end; if InputWriteCount = AStream.Size then break; end; Result.Position:= 0; finally Proc.Free; end; end; var lStr: TMemoryStream; begin lStr := TMemoryStream.Create; try lStr.LoadFromFile('mg.dcm'); CompressLZMA(lStr).SaveToFile('output.lzma'); finally lStr.Free; end; end.

Reply by email

Mattias Gaertner
to Leonardo M. Ramé
Show quoted text 9 lines
[...] > Check return value of Proc.Input.Write. > And call Proc.CloseInput when your input was written completely. > > Mmm, I'm getting the same result. I attached the new code.

When you give Proc.Input.Write 200 bytes and it returns that the other process could only process 100 of them. What bytes do you have to write next?

Mattias

Reply by email

Leonardo M. Ramé
to Mattias Gaertner
Mattias Gaertner wrote 17 lines
On Sat, 6 Apr 2013 12:37:06 -0300 Leonardo M. Ram? wrote: >[...] > > Check return value of Proc.Input.Write. > > And call Proc.CloseInput when your input was written completely. > > > > > > Mmm, I'm getting the same result. > > I attached the new code. When you give Proc.Input.Write 200 bytes and it returns that the other process could only process 100 of them. What bytes do you have to write next?

Sorry, I'm not getting it. Which other process?, apart from that, Proc.Input.Write is always returning the sizeof(buffer) except when it reaches the end, where it can be less than it.

It seems that the Proc.Input part is ok, aparently the Proc.Output is wrong :(

I attached a slightly modified code.

Leonardo M. Ram? http://leonardorame.blogspot.com -------------- next part -------------- program test; {$mode objfpc}{$H+} uses SysUtils, Classes, process, math; function CompressLZMA(AStream: TMemoryStream): TMemoryStream; var Proc: TProcess; lInBuffer: array [0..2047] of byte; lOutBuffer: array [0..2047] of byte; ReadCount: longint; WriteCount: longint; InputWriteCount: longint; begin Result := TMemoryStream.Create; AStream.Position:= 0; Proc := TProcess.Create(nil); try Proc.Executable := '/usr/bin/lzma'; Proc.Parameters.Add('-z'); Proc.Parameters.Add('-c'); Proc.Options := [poUsePipes, poStderrToOutPut]; Proc.Execute; InputWriteCount := 0; repeat ReadCount := AStream.Read(lInBuffer, SizeOf(lInBuffer)); InputWriteCount := Proc.Input.Write(lInBuffer, ReadCount); writeln(Format('%d - %d', [InputWriteCount, AStream.Size])); while (Proc.Output.NumBytesAvailable > 0) do begin WriteCount := Min(SizeOf(lOutBuffer), Proc.Output.NumBytesAvailable); WriteCount := Proc.Output.Read(lOutBuffer, WriteCount); Result.Write(lOutBuffer, WriteCount); end; if AStream.Position = AStream.Size then Proc.CloseInput; until Proc.Input = nil; Result.Position:= 0; finally Proc.Free; end; end; var lStr: TMemoryStream; begin lStr := TMemoryStream.Create; try lStr.LoadFromFile('mg.dcm'); CompressLZMA(lStr).SaveToFile('output.lzma'); finally lStr.Free; end; end.

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé
Leonardo M. Ram? wrote 28 lines
On 2013-04-06 17:46:06 +0200, Mattias Gaertner wrote: > On Sat, 6 Apr 2013 12:37:06 -0300 > Leonardo M. Ram? wrote: > > >[...] > > > Check return value of Proc.Input.Write. > > > And call Proc.CloseInput when your input was written completely. > > > > > > > > > > Mmm, I'm getting the same result. > > > > I attached the new code. > > When you give Proc.Input.Write 200 bytes and it returns that the other > process could only process 100 of them. What bytes do you have to write > next? > Sorry, I'm not getting it. Which other process?, apart from that, Proc.Input.Write is always returning the sizeof(buffer) except when it reaches the end, where it can be less than it. It seems that the Proc.Input part is ok, aparently the Proc.Output is wrong :( I attached a slightly modified code.

Finally got it working, I was forgetting to read the last part of the Output stream. ...I still don't know why this last chunk of bytes doesn't show up in NumBytesAvailable.

I attached the code.

Regards,

Leonardo M. Ram? http://leonardorame.blogspot.com -------------- next part -------------- program test; {$mode objfpc}{$H+} uses SysUtils, Classes, process, math; const READ_BYTES = 2048; procedure CompressLZMA(InStream, OutStream: TMemoryStream); var Proc: TProcess; lInBuffer: array [0..2047] of byte; BytesRead: longint; NumBytes: longint; begin InStream.Position:= 0; Proc := TProcess.Create(nil); try Proc.Executable := '/usr/bin/lzma'; Proc.Parameters.Add('-z'); Proc.Parameters.Add('-c'); Proc.Options := [poUsePipes, poStderrToOutPut]; Proc.Execute; BytesRead := 0; repeat NumBytes := InStream.Read(lInBuffer, SizeOf(lInBuffer)); Proc.Input.Write(lInBuffer, NumBytes); if InStream.Position >= InStream.Size then Proc.CloseInput; while Proc.Output.NumBytesAvailable > 0 do begin OutStream.SetSize(BytesRead + READ_BYTES); NumBytes := Proc.Output.Read((OutStream.Memory + BytesRead)^, READ_BYTES); if NumBytes > 0 then begin Inc(BytesRead, NumBytes); end; end; until Proc.Input = nil; // read the last part repeat OutStream.SetSize(BytesRead + READ_BYTES); NumBytes := Proc.Output.Read((OutStream.Memory + BytesRead)^, READ_BYTES); if NumBytes > 0 then Inc(BytesRead, NumBytes); until NumBytes <= 0; OutStream.SetSize(BytesRead); OutStream.Position:= 0; finally Proc.Free; end; end; var lInputStream: TMemoryStream; lOutputStream: TMemoryStream; begin lInputStream := TMemoryStream.Create; lOutputStream := TMemoryStream.Create; try lInputStream.LoadFromFile('mg.dcm'); CompressLZMA(lInputStream, lOutputStream); lOutputStream.SaveToFile('output.lzma'); finally lInputStream.Free; lOutputStream.Free; end; end.

Reply by email

Michael Van Canneyt
to Leonardo M. Ramé
Leonardo M. Ram? wrote 33 lines
On 2013-04-06 13:22:39 -0300, Leonardo M. Ram? wrote: > On 2013-04-06 17:46:06 +0200, Mattias Gaertner wrote: >> On Sat, 6 Apr 2013 12:37:06 -0300 >> Leonardo M. Ram? wrote: >> >>> [...] >>>> Check return value of Proc.Input.Write. >>>> And call Proc.CloseInput when your input was written completely. >>>> >>>> >>> >>> Mmm, I'm getting the same result. >>> >>> I attached the new code. >> >> When you give Proc.Input.Write 200 bytes and it returns that the other >> process could only process 100 of them. What bytes do you have to write >> next? >> > > Sorry, I'm not getting it. Which other process?, apart from that, > Proc.Input.Write is always returning the sizeof(buffer) except when it > reaches the end, where it can be less than it. > > It seems that the Proc.Input part is ok, aparently the Proc.Output is > wrong :( > > I attached a slightly modified code. > Finally got it working, I was forgetting to read the last part of the Output stream. ...I still don't know why this last chunk of bytes doesn't show up in NumBytesAvailable.

NumBytesAvailable is inherently unreliable. I didn't like it's addition.

You should never use it in critical code.

Michael.

Reply by email

Leonardo M. Ramé
to Michael Van Canneyt
Michael Van Canneyt wrote 43 lines
On Sat, 6 Apr 2013, Leonardo M. Ram? wrote: >On 2013-04-06 13:22:39 -0300, Leonardo M. Ram? wrote: >>On 2013-04-06 17:46:06 +0200, Mattias Gaertner wrote: >>>On Sat, 6 Apr 2013 12:37:06 -0300 >>>Leonardo M. Ram? wrote: >>> >>>>[...] >>>>>Check return value of Proc.Input.Write. >>>>>And call Proc.CloseInput when your input was written completely. >>>>> >>>>> >>>> >>>>Mmm, I'm getting the same result. >>>> >>>>I attached the new code. >>> >>>When you give Proc.Input.Write 200 bytes and it returns that the other >>>process could only process 100 of them. What bytes do you have to write >>>next? >>> >> >>Sorry, I'm not getting it. Which other process?, apart from that, >>Proc.Input.Write is always returning the sizeof(buffer) except when it >>reaches the end, where it can be less than it. >> >>It seems that the Proc.Input part is ok, aparently the Proc.Output is >>wrong :( >> >>I attached a slightly modified code. >> > >Finally got it working, I was forgetting to read the last part of the >Output stream. ...I still don't know why this last chunk of bytes >doesn't show up in NumBytesAvailable. NumBytesAvailable is inherently unreliable. I didn't like it's addition. You should never use it in critical code. Michael.

Maybe that's why the same program doesn't work on FreeBSD?. What can I use instead of it?.

Reply by email

Michael Van Canneyt
to Leonardo M. Ramé
Leonardo M. Ram? wrote 47 lines
On 2013-04-07 00:04:45 +0200, Michael Van Canneyt wrote: > > > On Sat, 6 Apr 2013, Leonardo M. Ram? wrote: > >> On 2013-04-06 13:22:39 -0300, Leonardo M. Ram? wrote: >>> On 2013-04-06 17:46:06 +0200, Mattias Gaertner wrote: >>>> On Sat, 6 Apr 2013 12:37:06 -0300 >>>> Leonardo M. Ram? wrote: >>>> >>>>> [...] >>>>>> Check return value of Proc.Input.Write. >>>>>> And call Proc.CloseInput when your input was written completely. >>>>>> >>>>>> >>>>> >>>>> Mmm, I'm getting the same result. >>>>> >>>>> I attached the new code. >>>> >>>> When you give Proc.Input.Write 200 bytes and it returns that the other >>>> process could only process 100 of them. What bytes do you have to write >>>> next? >>>> >>> >>> Sorry, I'm not getting it. Which other process?, apart from that, >>> Proc.Input.Write is always returning the sizeof(buffer) except when it >>> reaches the end, where it can be less than it. >>> >>> It seems that the Proc.Input part is ok, aparently the Proc.Output is >>> wrong :( >>> >>> I attached a slightly modified code. >>> >> >> Finally got it working, I was forgetting to read the last part of the >> Output stream. ...I still don't know why this last chunk of bytes >> doesn't show up in NumBytesAvailable. > > NumBytesAvailable is inherently unreliable. I didn't like it's addition. > > You should never use it in critical code. > > Michael. Maybe that's why the same program doesn't work on FreeBSD?. What can I use instead of it?.

Read until the number of read bytes is zero.

Michael.

Reply by email

Leonardo M. Ramé
to Michael Van Canneyt
Michael Van Canneyt wrote 54 lines
On Sat, 6 Apr 2013, Leonardo M. Ram? wrote: >On 2013-04-07 00:04:45 +0200, Michael Van Canneyt wrote: >> >> >>On Sat, 6 Apr 2013, Leonardo M. Ram? wrote: >> >>>On 2013-04-06 13:22:39 -0300, Leonardo M. Ram? wrote: >>>>On 2013-04-06 17:46:06 +0200, Mattias Gaertner wrote: >>>>>On Sat, 6 Apr 2013 12:37:06 -0300 >>>>>Leonardo M. Ram? wrote: >>>>> >>>>>>[...] >>>>>>>Check return value of Proc.Input.Write. >>>>>>>And call Proc.CloseInput when your input was written completely. >>>>>>> >>>>>>> >>>>>> >>>>>>Mmm, I'm getting the same result. >>>>>> >>>>>>I attached the new code. >>>>> >>>>>When you give Proc.Input.Write 200 bytes and it returns that the other >>>>>process could only process 100 of them. What bytes do you have to write >>>>>next? >>>>> >>>> >>>>Sorry, I'm not getting it. Which other process?, apart from that, >>>>Proc.Input.Write is always returning the sizeof(buffer) except when it >>>>reaches the end, where it can be less than it. >>>> >>>>It seems that the Proc.Input part is ok, aparently the Proc.Output is >>>>wrong :( >>>> >>>>I attached a slightly modified code. >>>> >>> >>>Finally got it working, I was forgetting to read the last part of the >>>Output stream. ...I still don't know why this last chunk of bytes >>>doesn't show up in NumBytesAvailable. >> >>NumBytesAvailable is inherently unreliable. I didn't like it's addition. >> >>You should never use it in critical code. >> >>Michael. > >Maybe that's why the same program doesn't work on FreeBSD?. What can I >use instead of it?. Read until the number of read bytes is zero.

This cannot be done, because when you don't have bytes available, Proc.Output.Read hangs.

Reply by email

Michael Van Canneyt
to Leonardo M. Ramé
Leonardo M. Ram? wrote 58 lines
On 2013-04-07 10:54:12 +0200, Michael Van Canneyt wrote: > > > On Sat, 6 Apr 2013, Leonardo M. Ram? wrote: > >> On 2013-04-07 00:04:45 +0200, Michael Van Canneyt wrote: >>> >>> >>> On Sat, 6 Apr 2013, Leonardo M. Ram? wrote: >>> >>>> On 2013-04-06 13:22:39 -0300, Leonardo M. Ram? wrote: >>>>> On 2013-04-06 17:46:06 +0200, Mattias Gaertner wrote: >>>>>> On Sat, 6 Apr 2013 12:37:06 -0300 >>>>>> Leonardo M. Ram? wrote: >>>>>> >>>>>>> [...] >>>>>>>> Check return value of Proc.Input.Write. >>>>>>>> And call Proc.CloseInput when your input was written completely. >>>>>>>> >>>>>>>> >>>>>>> >>>>>>> Mmm, I'm getting the same result. >>>>>>> >>>>>>> I attached the new code. >>>>>> >>>>>> When you give Proc.Input.Write 200 bytes and it returns that the other >>>>>> process could only process 100 of them. What bytes do you have to write >>>>>> next? >>>>>> >>>>> >>>>> Sorry, I'm not getting it. Which other process?, apart from that, >>>>> Proc.Input.Write is always returning the sizeof(buffer) except when it >>>>> reaches the end, where it can be less than it. >>>>> >>>>> It seems that the Proc.Input part is ok, aparently the Proc.Output is >>>>> wrong :( >>>>> >>>>> I attached a slightly modified code. >>>>> >>>> >>>> Finally got it working, I was forgetting to read the last part of the >>>> Output stream. ...I still don't know why this last chunk of bytes >>>> doesn't show up in NumBytesAvailable. >>> >>> NumBytesAvailable is inherently unreliable. I didn't like it's addition. >>> >>> You should never use it in critical code. >>> >>> Michael. >> >> Maybe that's why the same program doesn't work on FreeBSD?. What can I >> use instead of it?. > > Read until the number of read bytes is zero. > This cannot be done, because when you don't have bytes available, Proc.Output.Read hangs.

Not if the process stops and closes it's output, then it should return with the last bytes in the buffer.

Michael.

Reply by email