Stream to Sbyte[]

This is a discussion on Stream to Sbyte[] within the C# Programming forums, part of the General Programming Boards category; Hi, I'm trying a stream to sbyte conversion as follows: Code: private SByte[] streamToSbyte(Stream stream) { sbyte[] sbuffer = new ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    Stream to Sbyte[]

    Hi,

    I'm trying a stream to sbyte conversion as follows:

    Code:
     private SByte[] streamToSbyte(Stream stream)
            {
                sbyte[] sbuffer = new sbyte[stream.Length];
                byte[] buffer = new byte[stream.Length];
                int read=0;
                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        while (read <= 0)
                        {
                            read = stream.Read(buffer, 0, buffer.Length);
                            ms.Write(buffer, 0, read);
                        }
                    }
                    for (int index = 0; index < buffer.Length; index++)
                    {
                        sbuffer[index] = (sbyte)buffer[index];
                    }
                    return sbuffer;
                }
    
                catch (Exception e)
                {
                    Console.WriteLine("Exception caught here" + e.ToString());
                    return sbuffer;
                }
    
            }
    But I get Invalid operation exception at the while!
    Could anyone help please?

    AK

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I'm not exactly sure what you're doing there. It's not that complicated of an operation.
    Code:
    static private sbyte[] streamToSbyte(Stream stream)
    {
    	List<sbyte> list = new List<sbyte>();
    	{
    		int ch;
    		while ((ch = stream.ReadByte()) != -1)
    			list.Add((sbyte)ch);
    	}
    	return list.ToArray();
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    You rock piano
    Actually what I want to do is write a stream-decompress fn in C#. I've written it as:
    Code:
     public MemoryStream decompressStream(Stream sourceStream, String path)
            {
                String fileName;
                MemoryStream ms;
                sbyte[] inputBuff;
                java.io.ByteArrayInputStream bais;
                try
                {
                    fileName = extractFileName(path);
                    inputBuff = streamToSbyte(sourceStream);
                    bais = new java.io.ByteArrayInputStream(inputBuff);
    
                    java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(bais);
                    java.util.zip.ZipEntry ze;
                    java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
    
                    sbyte[] buf = new sbyte[1024];
                    int len;
                    while ((ze = zis.getNextEntry()) != null)
                    {
                        fileName = ze.getName();
                        while ((len = zis.read(buf)) >= 0)
                        {
                            baos.write(buf, 0, len);
                        }
                        baos.close();
                    }
                    zis.close();
                    bais.close();
                    Console.WriteLine("Decompression ok!");
                    ms = baosToStream(baos);
                    return ms;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    ms = new MemoryStream();
                    return ms;
                }
    
            }

    But I again get an error in the while ((len = zis.read(buf)) >= 0) line!
    An EOFException...

    Any help is appreciated...

    AK

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    The code is running for 3Kb zip file but the EOF error shows up when I try to run a 37Kb zip file

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    java.util.zip.ZipInputStream
    I've never used that class, so you might take a look at its documentation.

    If you just want compression/decompression, you could use either the GZipStream or the DeflateStream classes in the System.IO.Compression namespace.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    I've tried that but the main problem with it is that I cannot name the zipped file using that class.What I mean is say I want to zip abc.txt , this deflatestream class will create one abc.zip but when I extract it I dont get a file named abc.txt within it ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 02:30 AM
  2. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 05:15 PM
  3. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 09:40 AM
  4. Buffering??
    By Brain Cell in forum C Programming
    Replies: 15
    Last Post: 09-21-2004, 07:57 AM
  5. Replies: 9
    Last Post: 07-01-2002, 08:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21