Open zuozonglin opened 6 years ago
Snappy is a compression standard which does not depend on specific implementation. I think you can just use snappyjs for compression in node, and use Java's snappy implementation to uncompress the data on the Java server.
yeah, I tried it . but got the messy string. ---in node like this--- var snappyjs = require('snappyjs'); var fs = require('fs'); function stringToArrayBuffer(source) { var arrayBuffer = new ArrayBuffer(source.length * 2) var view = new Uint16Array(arrayBuffer) var i for (i = 0; i < source.length; i++) { view[i] = source.charCodeAt(i) } return arrayBuffer }
function arrayBufferToBuffer(arrayBuffer) { var view = new Uint8Array(arrayBuffer) var buffer = Buffer.alloc(view.length) var i for (i = 0; i < view.length; i++) { buffer[i] = view[i] } return buffer } var r = 'UTF-8 Eight-bit UCS Transformation Format.添加中文测试'; var data = snappyjs.compress(arrayBufferToBuffer(stringToArrayBuffer(r))); fs.writeFileSync('test-buffer', data);
---then java uncompress--- public static void main(String[] args) throws IOException { byte[] bs = readSnappy("test-buffer"); String s = null; byte[] res = Snappy.uncompress(bs);//org.xerial.snappy.Snappy s = new String(res,"UTF-8"); System.out.println(s); }
@SuppressWarnings("unused") public static byte[] readSnappy(String path) throws IOException { File file = new File(path); FileInputStream fis = new FileInputStream(file); byte[] rs = new byte[(int) file.length()]; int byteRead = 0; while ((byteRead = fis.read(rs)) != -1) { } fis.close(); return rs; }
---then got result is messy---
something wrong ? I really appreciate your help.
It's somehow an encoding-related issue. The function stringToArrayBuffer
does not encode the string in UTF8. I wrote this function because there is no library function in JavaScript to get the UTF8 stream of a string.
However, you are in Node.js environment. You can use Buffer
in Node.js which can give you UTF8 stream of a string. See https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding for a reference.
I need snappy compress in node ,and then uncompress it on the java server, how this can be realize