Thread overview
std.stream.MemoryStream.writeBlock bug.
Dec 12, 2004
Hiroshi Sakurai
Dec 13, 2004
Ben Hinkle
Dec 13, 2004
Hiroshi Sakurai
December 12, 2004
std.stream.MemoryStream.writeBlock bug.

The size beyond the length of array is specified.
Even the data of domains other than array will write out.

source code

import std.stream;

void main() {
MemoryStream ms = new MemoryStream();
byte[] b;
b.length = 10;
for (int i = 0; i < b.length; i++) {
b[i] = i;
}
ms.writeBlock(b, 11);
printf("length = %d\n", ms.data.length);
}

output

11


December 13, 2004
Hiroshi Sakurai wrote:

> std.stream.MemoryStream.writeBlock bug.
> 
> The size beyond the length of array is specified.
> Even the data of domains other than array will write out.
> 
> source code
> 
> import std.stream;
> 
> void main() {
> MemoryStream ms = new MemoryStream();
> byte[] b;
> b.length = 10;
> for (int i = 0; i < b.length; i++) {
> b[i] = i;
> }
> ms.writeBlock(b, 11);
> printf("length = %d\n", ms.data.length);
> }
> 
> output
> 
> 11


I don't think that is a bug. The "writeBlock" function takes a void* buffer
pointer and a length, so the fact that the buffer originally came from an
array is forgotten inside writeBlock. To write out exactly a buffer try
"write(ubyte[] buf)". I suppose you'll have to cast the buffer since byte[]
isn't implicitly convertable to ubyte[]. In other words instead of
 ms.writeBlock(b,11)
try
 ms.write(b);

-Ben
December 13, 2004
In article <cpipj0$6jv$1@digitaldaemon.com>, Ben Hinkle says...
>
>Hiroshi Sakurai wrote:
>
>> std.stream.MemoryStream.writeBlock bug.
>> 
>> The size beyond the length of array is specified.
>> Even the data of domains other than array will write out.
>> 
>> source code
>> 
>> import std.stream;
>> 
>> void main() {
>> MemoryStream ms = new MemoryStream();
>> byte[] b;
>> b.length = 10;
>> for (int i = 0; i < b.length; i++) {
>> b[i] = i;
>> }
>> ms.writeBlock(b, 11);
>> printf("length = %d\n", ms.data.length);
>> }
>> 
>> output
>> 
>> 11
>
>
>I don't think that is a bug. The "writeBlock" function takes a void* buffer pointer and a length, so the fact that the buffer originally came from an array is forgotten inside writeBlock. To write out exactly a buffer try "write(ubyte[] buf)". I suppose you'll have to cast the buffer since byte[] isn't implicitly convertable to ubyte[]. In other words instead of
> ms.writeBlock(b,11)
>try
> ms.write(b);
>
>-Ben

Thank you for a reply. Ben.
Well.
I mistook about Phobos spec.

-Hiroshi Sakurai