binarywriter and buffer size?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Check the following code

///Buffer to store the data
byte[] data = new byte[DEFAULT_BUFF_SIZE];

///Create a memory stream from the buffer
MemoryStream memStream = new MemoryStream(data);

///Binary writer
BinaryWriter newData = new BinaryWriter(memStream);

///Write the data
newData.Write(Convert.ToUInt32(nResourceID));

///need to identify available free size in data
///newData.Write(Convert.ToUInt32(nResLength));

/// write(byte[], index, count);
newData.Write(
buffer having data more than DEFAULT_BUFF_SIZE,
anywhere in the memory buff,
should fill up to DEFAULT_BUFF_SIZE);


Now the problem is what is the best possible solution to know how much data
is written in the buffer i.e. "data" variable?

I need to stick to the DEFAULT_BUFF_SIZE limit while writing the data

BinaryWriter gives me more flexibility to write any kind of data?
MemoryStream only accepts the byte kind of data? which is not readily
available.
 
fundoo_kr said:
Check the following code

///Buffer to store the data
byte[] data = new byte[DEFAULT_BUFF_SIZE];

///Create a memory stream from the buffer
MemoryStream memStream = new MemoryStream(data);

///Binary writer
BinaryWriter newData = new BinaryWriter(memStream);

///Write the data
newData.Write(Convert.ToUInt32(nResourceID));

///need to identify available free size in data
///newData.Write(Convert.ToUInt32(nResLength));

/// write(byte[], index, count);
newData.Write(
buffer having data more than DEFAULT_BUFF_SIZE,
anywhere in the memory buff,
should fill up to DEFAULT_BUFF_SIZE);

Now the problem is what is the best possible solution to know how much data
is written in the buffer i.e. "data" variable?

MemoryStream has 3 properties:
Capacity
Length
Position

Do they give you what you want ?

Arne
 
Arne Vajhøj said:
MemoryStream has 3 properties:
Capacity
Length
Position

Do they give you what you want ?

Arne

Thanks for your reply but they don't seem to work, since i have assigned the
buffer it is giving me the same amount of value in this case it is 1000.

Moreover i have tried even after flushing the binarystream.
Still i don't find any way to identify the amount of bytes used up.
In C since every write used to return the number of bytes written, something
similar i am looking atleast?
 
fundoo_kr said:
Thanks for your reply but they don't seem to work, since i have assigned the
buffer it is giving me the same amount of value in this case it is 1000.

Weird.

using System;
using System.IO;

namespace E
{
public class MainClass
{
public static void Main(string[] args)
{
byte[] data = new byte[1000];
MemoryStream memStream = new MemoryStream(data);
BinaryWriter newData = new BinaryWriter(memStream);
newData.Write(123);
Console.WriteLine(memStream.Position);
}
}
}

writes 4 at PC !

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top