mytonytiger
asked on
Unexpected end of ZLIB input stream
I'm pulling data from a SQL database. The data is compressed.
I'm using the following code to read the data:
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ------
if (rs.next())
{
InputStreamReader in = new InputStreamReader(
new InflaterInputStream(
new ByteArrayInputStream((byte [])rs.getObject("Compresse d_Data")), new Inflater(true)),"UTF-16LE" );
char[] cbuf = new char[16000];
sb = new StringBuffer("");
int i = 0;
while (i > -1) {
i = in.read(cbuf);
if (i > -1){
sb.append(new String(cbuf).substring(0,i ));
// System.out.println(new String(cbuf).substring(0,i ));
}
}
System.out.println( sb.toString());
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ------
When the commented println statement is uncommented, you can see each piece of data that is decompressed. The data is XML. When it gets to the last read, I get an exception. Also, it's interesting to note that if I change "cbuf" to char[1], and I print out each read as it's being appended, I can verify that the final tag of the xml is in fact being decompressed and appended to "sb". However, java continues to try and read past the xml and is throwing the following exception:
java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInpu tStream.fi ll(Inflate rInputStre am.java:21 5)
at java.util.zip.InflaterInpu tStream.re ad(Inflate rInputStre am.java:13 4)
at sun.nio.cs.StreamDecoder$C harsetSD.r eadBytes(S treamDecod er.java:40 8)
at sun.nio.cs.StreamDecoder$C harsetSD.i mplRead(St reamDecode r.java:450 )
at sun.nio.cs.StreamDecoder.r ead(Stream Decoder.ja va:182)
at java.io.InputStreamReader. read(Input StreamRead er.java:16 7)
at java.io.Reader.read(Reader .java:100)
at clsMain.GetStatement(clsMa in.java:58 )
at clsMain.main(clsMain.java: 28)
I need to know what I can do to correct this. Another application that was written in VB.NET can succesfully read the entire compressed XML without any problems. So, I'm sure it's not a corrupt compression. Java can read the entire compressed xml, but at the end of the stream throws the exception. Is there a bug in my code? or a bug in java? or am I missing something that would correct this?
If you need more information, please let me know and I'll see what I can provide.
Thanks,
Tony
I'm using the following code to read the data:
//------------------------
if (rs.next())
{
InputStreamReader in = new InputStreamReader(
new InflaterInputStream(
new ByteArrayInputStream((byte
char[] cbuf = new char[16000];
sb = new StringBuffer("");
int i = 0;
while (i > -1) {
i = in.read(cbuf);
if (i > -1){
sb.append(new String(cbuf).substring(0,i
// System.out.println(new String(cbuf).substring(0,i
}
}
System.out.println( sb.toString());
}
//------------------------
When the commented println statement is uncommented, you can see each piece of data that is decompressed. The data is XML. When it gets to the last read, I get an exception. Also, it's interesting to note that if I change "cbuf" to char[1], and I print out each read as it's being appended, I can verify that the final tag of the xml is in fact being decompressed and appended to "sb". However, java continues to try and read past the xml and is throwing the following exception:
java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInpu
at java.util.zip.InflaterInpu
at sun.nio.cs.StreamDecoder$C
at sun.nio.cs.StreamDecoder$C
at sun.nio.cs.StreamDecoder.r
at java.io.InputStreamReader.
at java.io.Reader.read(Reader
at clsMain.GetStatement(clsMa
at clsMain.main(clsMain.java:
I need to know what I can do to correct this. Another application that was written in VB.NET can succesfully read the entire compressed XML without any problems. So, I'm sure it's not a corrupt compression. Java can read the entire compressed xml, but at the end of the stream throws the exception. Is there a bug in my code? or a bug in java? or am I missing something that would correct this?
If you need more information, please let me know and I'll see what I can provide.
Thanks,
Tony
Read this: http://forum.java.sun.com/thread.jsp?thread=190326&forum=31&message=640415
I hope you don't use JDK 1.1.x because
There is a bug in JDK 1.1.x which causes the ZipFile class to read beyond the end-of-file.
JavaSoft recommends the following workaround: Do not attempt to read more bytes than the entry contains.
Call ZipEntry.getSize() to get the actual size of the entry and use that value to keep track of the remaining number of bytes while reading the entry.
There is a bug in JDK 1.1.x which causes the ZipFile class to read beyond the end-of-file.
JavaSoft recommends the following workaround: Do not attempt to read more bytes than the entry contains.
Call ZipEntry.getSize() to get the actual size of the entry and use that value to keep track of the remaining number of bytes while reading the entry.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm using 1.4.2_04
It seems to be a java bug.
What fun!
It seems to be a java bug.
What fun!
Thanks for accepting
>> What fun!
;)
>> What fun!
;)