Any C# equivalent of Java's Inflator class?

G

Guest

basically, I need to port this part of an app into C#:

byte[] data // some compressed data separated from a Stream
Inflater inf = new Inflater();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
inf.setInput(data, 0, data.length);
byte[] result = new byte[1024];

int resLen;
while ((resLen = inf.inflate(result)) > 0) {
buffer.write(result, 0, resLen);
}
inf.end();

I tried using SharpZipLib but they don't see to have a way to simplay call
inflate on a specific byte array, they have a filter Stream so it makes it
ore compicated to do what I wanted. And when I tried it, I got a "EOF not
found" tpye of error... I just used MemoryStream instead of the byte array as
the input, and wrote the data from my byte array into the MemoryStream.

any suggestions? thanks!
 
M

Mike Schilling

MrNobody said:
basically, I need to port this part of an app into C#:

byte[] data // some compressed data separated from a Stream
Inflater inf = new Inflater();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
inf.setInput(data, 0, data.length);
byte[] result = new byte[1024];

int resLen;
while ((resLen = inf.inflate(result)) > 0) {
buffer.write(result, 0, resLen);
}
inf.end();

I tried using SharpZipLib but they don't see to have a way to simplay call
inflate on a specific byte array, they have a filter Stream so it makes it
ore compicated to do what I wanted. And when I tried it, I got a "EOF not
found" tpye of error... I just used MemoryStream instead of the byte array
as
the input, and wrote the data from my byte array into the MemoryStream.

any suggestions? thanks!

Do the J# libraries have what you need? You should be able to call them
from C#.

And if that doesn't work, please let me know. I have a background task to
replace our use of SharpZipLib with J#'s zip classes, and if it won't work,
tell me now :)
 
D

Daniel O'Connell [C# MVP]

MrNobody said:
basically, I need to port this part of an app into C#:

byte[] data // some compressed data separated from a Stream
Inflater inf = new Inflater();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
inf.setInput(data, 0, data.length);
byte[] result = new byte[1024];

int resLen;
while ((resLen = inf.inflate(result)) > 0) {
buffer.write(result, 0, resLen);
}
inf.end();

I tried using SharpZipLib but they don't see to have a way to simplay call
inflate on a specific byte array, they have a filter Stream so it makes it
ore compicated to do what I wanted. And when I tried it, I got a "EOF not
found" tpye of error... I just used MemoryStream instead of the byte array
as
the input, and wrote the data from my byte array into the MemoryStream.

any suggestions? thanks!

I think that you are mostly stuck with stream based deflaters. The .NET 2.0
support is also based on streams.
 
J

Jon Skeet [C# MVP]

MrNobody said:
basically, I need to port this part of an app into C#:

byte[] data // some compressed data separated from a Stream
Inflater inf = new Inflater();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
inf.setInput(data, 0, data.length);
byte[] result = new byte[1024];

int resLen;
while ((resLen = inf.inflate(result)) > 0) {
buffer.write(result, 0, resLen);
}
inf.end();

I tried using SharpZipLib but they don't see to have a way to simplay call
inflate on a specific byte array, they have a filter Stream so it makes it
ore compicated to do what I wanted. And when I tried it, I got a "EOF not
found" tpye of error... I just used MemoryStream instead of the byte array as
the input, and wrote the data from my byte array into the MemoryStream.

any suggestions? thanks!

Using a MemoryStream will let you port the code above directly to C# -
that's the only bit which is different, really.

I'd personally use an InflaterInputStream instead, but to make it a
straight port, just use a MemoryStream.
 
G

Guest

Thanks to everyone for your replies

This is what I did when I ported to C#:

byte[] data // some compressed data separated from a Stream

// since we cant call inflate on a byte[] array we must use a MemoryStream...
MemoryStream memTemp = new MemoryStream();
memTemp.Write(data, 0, data.Length);

InflaterInputStream inf = new InflaterInputStream(memTemp);
MemoryStream buffer = new MemoryStream();
byte[] result = new byte[1024];

int resLen;
while ((resLen = inf.Read(result, 0, result.Length)) > 0) {
buffer.Write(result, 0, resLen);
}
inf.Close();

When I ran this though I get: "Unexpected EOF"
at
ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputBuffer.Fill()
at
ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream.Fill()
at
ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream.Read(Byte[] b, Int32 off, Int32 len)

So I stepped through the code both in Java and C# to see if I can find any
differences. The input data (in memTemp or the data byte array) is the right
length but when I looked inside I noticed in Java all the bytes were signed,
there were negative numbers, but while in C# there were no signed numbers.

How could I get around this? MemoryStream does not accept sbyte arrays...
 
J

Jon Skeet [C# MVP]

Mike Schilling said:
And if that doesn't work, please let me know. I have a background task to
replace our use of SharpZipLib with J#'s zip classes, and if it won't work,
tell me now :)

Out of interest, why? I know I'd rather have one library which ports a
single part of the Java library than be stuck with a dependency like
J#. Also, being open source, if you find a bug in it, you can fix it.

I'm not saying it's the wrong thing to do - I'm just interested in the
reasons.
 
J

Jon Skeet [C# MVP]

MrNobody said:
This is what I did when I ported to C#:

byte[] data // some compressed data separated from a Stream

// since we cant call inflate on a byte[] array we must use a MemoryStream...
MemoryStream memTemp = new MemoryStream();
memTemp.Write(data, 0, data.Length);

You haven't reset the position within memTemp, so there's nothing to
read.

An easier way of doing things is:

MemoryStream memTemp = new MemoryStream(data);
So I stepped through the code both in Java and C# to see if I can find any
differences. The input data (in memTemp or the data byte array) is the right
length but when I looked inside I noticed in Java all the bytes were signed,
there were negative numbers, but while in C# there were no signed numbers.

How could I get around this? MemoryStream does not accept sbyte arrays...

The difference between signed and unsigned bytes isn't an issue in this
case.
 
G

Guest

Ah, I see! Makes perfect sense now. I was writing to this MemoryStream and
the position was always at the end of the data I was writing, so when I
passed it to the Inflater it found no data!

Great, I passed the data in to the constructor this time instead and it
works like a charm now.

Thanks !
 
M

Mike Schilling

Jon Skeet said:
Out of interest, why? I know I'd rather have one library which ports a
single part of the Java library than be stuck with a dependency like
J#. Also, being open source, if you find a bug in it, you can fix it.

I'm not saying it's the wrong thing to do - I'm just interested in the
reasons.

We have an important customer that disapproves of open source, and their
money outargues your good sense.
 

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

Top