-
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
-
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();
}
-
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
-
The code is running for 3Kb zip file but the EOF error shows up when I try to run a 37Kb zip file :(
-
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.
-
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 ...