CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: reading bytes

  1. #1
    Join Date
    Jan 2005
    Posts
    17

    reading bytes

    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.

  2. #2
    Join Date
    Jul 1999
    Location
    Malmö, Sweden
    Posts
    126

    Re: reading bytes

    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()?

  3. #3
    Join Date
    Jan 2005
    Posts
    17

    Re: reading bytes

    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.
    Last edited by request; February 14th, 2005 at 12:01 AM.

  4. #4
    Join Date
    Jul 1999
    Location
    Malmö, Sweden
    Posts
    126

    Re: reading bytes

    Please use CODE tag when posting code. Makes it easier to read . Like this.

    Code:
    //
    // 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:
    //
    // 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured