Reputation: 615
I have a big data in the page and need to post to the server.
so I use https://github.com/dankogai/js-deflate to deflate the big data.
It seems that working fine.
helloworld -> w4tIw43DicOJL8OPL8OKSQEA
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<h1>Demo</h1>
<p>$Id: demo.html,v 0.4 2013/04/09 14:25:38 dankogai Exp dankogai $</p>
<dl>
<dt>Inflated + Base64-Decoded (Original):</dt>
<dd><textarea id="inflated" cols="64" rows="16" onkeyup="(function(that, dst){
setTimeout(function(){
dst.value = Base64.toBase64(RawDeflate.deflate(Base64.utob(that.value)));
},0)
})(this,$('deflated'))"></textarea></dd>
<dt>Deflated + Base64-Encoded (Compressed):</dt>
<dd><textarea id="deflated" cols="64" rows="16" onkeyup="(function(that, dst){
setTimeout(function(){
dst.value = Base64.btou(RawDeflate.inflate(Base64.fromBase64(that.value)));
},0);
})(this, $('inflated'))"></textarea></dd>
</dl>
<script src="./base64.js"></script>
<script src="../rawinflate.js"></script>
<script src="../rawdeflate.js"></script>
<script>
$ = function(id){ return document.getElementById(id) };
</script>
</body>
</html>
so I want to get hellowold by putting the result of deflate into the java code I got the error:
java.util.zip.DataFormatException: incorrect header check
the following is my code:
public static void main(String[] args) throws IOException {
try {
// Encode a String into bytes
String output1 = "w4tIw43DicOJL8OPL8OKSQEA";
byte[] output2 = Base64Util.decode(output1);
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output2);
System.out.println("a:" + new String(output2));
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println("b:" + outputString);
} catch (java.io.UnsupportedEncodingException ex) {
ex.printStackTrace();
// handle
} catch (java.util.zip.DataFormatException ex) {
ex.printStackTrace();
// handle
}
}
so what's the problem?
And this is my Base64Util Class, I've tested. it's ok:
public class Base64Util {
private final static char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
private static int[] toInt = new int[128];
static {
for(int i=0; i< ALPHABET.length; i++){
toInt[ALPHABET[i]]= i;
}
}
/**
* Translates the specified byte array into Base64 string.
*
* @param buf the byte array (not null)
* @return the translated Base64 string (not null)
*/
public static String encode(byte[] buf){
int size = buf.length;
char[] ar = new char[((size + 2) / 3) * 4];
int a = 0;
int i=0;
while(i < size){
byte b0 = buf[i++];
byte b1 = (i < size) ? buf[i++] : 0;
byte b2 = (i < size) ? buf[i++] : 0;
int mask = 0x3F;
ar[a++] = ALPHABET[(b0 >> 2) & mask];
ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask];
ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask];
ar[a++] = ALPHABET[b2 & mask];
}
switch(size % 3){
case 1: ar[--a] = '=';
case 2: ar[--a] = '=';
}
return new String(ar);
}
/**
* Translates the specified Base64 string into a byte array.
*
* @param s the Base64 string (not null)
* @return the byte array (not null)
*/
public static byte[] decode(String s){
int delta = s.endsWith( "==" ) ? 2 : s.endsWith( "=" ) ? 1 : 0;
byte[] buffer = new byte[s.length()*3/4 - delta];
int mask = 0xFF;
int index = 0;
for(int i=0; i< s.length(); i+=4){
int c0 = toInt[s.charAt( i )];
int c1 = toInt[s.charAt( i + 1)];
buffer[index++]= (byte)(((c0 << 2) | (c1 >> 4)) & mask);
if(index >= buffer.length){
return buffer;
}
int c2 = toInt[s.charAt( i + 2)];
buffer[index++]= (byte)(((c1 << 4) | (c2 >> 2)) & mask);
if(index >= buffer.length){
return buffer;
}
int c3 = toInt[s.charAt( i + 3 )];
buffer[index++]= (byte)(((c2 << 6) | c3) & mask);
}
return buffer;
}
}
Upvotes: 2
Views: 12591
Reputation: 615
Finally, I found the solution. Maybe it is not the best solution, but solve my problem.
although with not good performance.
use the Javascript both client side and server side(Java js engine)..
So I can keep the result are same..
Upvotes: 0
Reputation: 112502
"w4tIw43DicOJL8OPL8OKSQEA" is not a Base-64 encoding of a valid raw deflate stream. So it does not seem to be working fine to me. I can't really tell what your Javascript code is doing. See my questions in the comments to your question.
Even if it were, your Java code is expecting a zlib wrapper on the deflate data, whereas the Javascript code is written to produce and consume raw deflate data without a wrapper. To get Java to inflate raw deflate data, you would need to:
Inflater decompresser = new Inflater(true);
to select the nowrap
option.
Upvotes: 7