By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,161 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,161 IT Pros & Developers. It's quick & easy.

Any C# equivalent of Java's Inflator class?

P: n/a
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!
Apr 5 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"MrNobody" <Mr******@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
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 :-)
Apr 5 '06 #2

P: n/a

"MrNobody" <Mr******@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
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.
Apr 5 '06 #3

P: n/a
MrNobody <Mr******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 5 '06 #4

P: n/a
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.In flaterInputBuffer.Fill()
at
ICSharpCode.SharpZipLib.Zip.Compression.Streams.In flaterInputStream.Fill()
at
ICSharpCode.SharpZipLib.Zip.Compression.Streams.In flaterInputStream.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...
Apr 5 '06 #5

P: n/a
Mike Schilling <ap@newsgroup.nospam> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 5 '06 #6

P: n/a
MrNobody <Mr******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 5 '06 #7

P: n/a
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 !
Apr 5 '06 #8

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Mike Schilling <ap@newsgroup.nospam> wrote:
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.


We have an important customer that disapproves of open source, and their
money outargues your good sense.
Apr 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.