Click to See Complete Forum and Search --> : reading bytes


request
February 11th, 2005, 06:25 AM
Hi all
i have converted java file to c#. in java inputstream class read() method is there which reads byte by byte.
i have replaced it with readbyte() method. but i could n't get the result.

int i=memorystreamvar.readbyte()
giving -1.

but this in java is taking byte by byte.

one strange thing is i have data in the buffer..

could any one tell the solution for this problem.

Thanks
Request.

Anders
February 11th, 2005, 07:25 AM
Assuming memorystreamvar is an instance of class System.IO.MemoryStream, and that you have written to this stream previously, are you sure you have reset the position to beginning of the stream? Otherwise you are at the end of stream, and ReadByte() returns -1 (end of stream reached) correctly.

I think I don't understand your problem though. Are you trying to read a file or what? I need more info. What do you do before you call memorystreamvar.ReadByte()?

request
February 13th, 2005, 10:32 PM
Hi
Thanq for responding

my java code is


InputStream in_Renamed = new FileInputStream(filename);


Stream in_Renamed



srcPos = 0;
srcCount = 0;
String enc = _enc;

if (is == null)
throw new IllegalArgumentException();

try {

if (enc == null) {
// read four bytes

int chk = 0;

while (srcCount < 4) {
int i = is.read();
if (i == -1)
break;
chk = (chk << 8) | i;
srcBuf[srcCount++] = (char) i;
}



converted in to C#
as readbyte method is returning -1 gng to break;


MemoryStreamis_Renamed= new MemoryStream();
ASCIIEncoding ae = new ASCIIEncoding();
byte[] bArray = ae.GetBytes(filename);
is_Renamed.Write(bArray, 0 , filename.Length);

in fuction
Stream in_Renamed;

srcPos = 0;
srcCount = 0;
System.String enc = _enc;

if (is_Renamed== null)
throw new System.ArgumentException();

if (is_Renamed == null)
throw new System.ArgumentException();

try
{

if (enc == null)
{
// read four bytes

int chk = 0;

while (srcCount < 4)
{
int i = is_Renamed.ReadByte();
if (i == - 1)
break;
chk= (chk << 8) | i;
srcBuf[srcCount++] = (char) i;
}
}


in C# readbyte is returning -1




Regards
request.

Anders
February 14th, 2005, 06:25 AM
Please use CODE tag when posting code. Makes it easier to read :). Like this.


//
// Code block 1
//
MemoryStream is_Renamed= new MemoryStream();
ASCIIEncoding ae = new ASCIIEncoding();
byte[] bArray = ae.GetBytes(filename);
is_Renamed.Write(bArray, 0 , filename.Length);

So this piece creates a memorystream, then it writes the string filename as bytes to the memorystream. It looks ok to me, and if you inspect is_Renamed._buffer in the debugger I'm fairly sure you will see that filename is there as bytes, as well as that is_Renamed.Length is equal to filename.Length, and so is is_Renamed.Position, that is, you are at end of stream. Later down we have

//
// Code block 2
//
while (srcCount < 4)
{
int i = is_Renamed.ReadByte();
if (i == - 1)
break;
chk= (chk << 8) | i;
srcBuf[srcCount++] = (char) i;
}

Now, if the variable is_Renamed is the same in code block 1 as in code block 2, then you have to reset its position before you try reading from it. Otherwise you are already at end of stream since you just wrote filename to the stream in code block 1.