Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MemoryStream to ByteArray?

6,536 views
Skip to first unread message

Mark Wenzel

unread,
Feb 1, 2001, 12:39:02 PM2/1/01
to
Hi all..
I need to pass a blobfield to a JNI call to a java method (Don't ask why,
you will laugh). Have all of the JNI stuff worked out, but am having a
problem converting the stream to a ByteArray for the hand off. I have tried
the following

var
Stream: TMemoryStream;
HoldArray: array of Byte;
P: Pointer;

begin

Stream := TMemoryStream.Create;

//Reference to a blob field
Query1COMPANY_DATA.SaveToStream(Stream);

SetLength(HoldArray,Stream.size);
P := @HoldArray;

//If position not set to zero, no bytes read
Stream.Position := 0;
Stream.Read(TByteArray(P^),Length(TByteArray(P^)));

//Pass to JNI Wrapper, this part is fine
Company.setCompanyBlob(HoldArray);

end;

But of course this baggs badly, any hints as to why?
Pardon my coding style, I am a Java guy.
Thanks in advance
Mark.


whs

unread,
Feb 1, 2001, 12:54:55 PM2/1/01
to

"Mark Wenzel" <mwe...@holtalliances.com> wrote in message
news:95c72j$8g...@bornews.inprise.com...

> Hi all..
> I need to pass a blobfield to a JNI call to a java method (Don't ask why,
> you will laugh). Have all of the JNI stuff worked out, but am having a
> problem converting the stream to a ByteArray for the hand off. I have
tried
> the following

(snip)

Here's the correct way.


procedure X;

var
Stream: TStream;
HoldArray: array of Byte;

begin
Stream := TMemoryStream.Create;
try
Query1COMPANY_DATA.SaveToStream(Stream);
Stream.Position := 0;
SetLength(HoldArray, Stream.Size);
Stream.Read(HoldArray[0], Stream.Size);
finally
Stream.Free;
end;
end;

stone...@gmail.com

unread,
Dec 4, 2016, 7:19:47 PM12/4/16
to
This was posted a long time ago but saved me countless hours. Thank you!!
0 new messages