在Java中压缩客户机/服务器传输的字符串 [英] Compressing strings for client/server transport in Java
问题描述
我使用适当的客户端/服务器消息格式,限制我可以通过电线发送。我不能发送序列化对象,我必须将数据作为字符串存储在消息中。我发送的数据是大的逗号分隔的值,我想压缩数据,然后将其作为字符串包装到消息中。
I work with a propriety client/server message format that restricts what I can send over the wire. I can't send a serialized object, I have to store the data in the message as a String. The data I am sending are large comma-separated values, and I want to compress the data before I pack it into the message as a String.
我试图使用Deflater /充气器来实现这一点,但沿线的某处,我遇到了困难。
I attempted to use Deflater/Inflater to achieve this, but somewhere along the line I am getting stuck.
我使用下面的两个方法deflate / inflate。但是,将compressString()方法的结果传递给decompressStringMethod()会返回一个空结果。
I am using the two methods below to deflate/inflate. However, passing the result of the compressString() method to decompressStringMethod() returns a null result.
public String compressString(String data) {
Deflater deflater = new Deflater();
byte[] target = new byte[100];
try {
deflater.setInput(data.getBytes(UTF8_CHARSET));
deflater.finish();
int deflateLength = deflater.deflate(target);
return new String(target);
} catch (UnsupportedEncodingException e) {
//TODO
}
return data;
}
public String decompressString(String data) {
String result = null;
try {
byte[] input = data.getBytes();
Inflater inflater = new Inflater();
int inputLength = input.length;
inflater.setInput(input, 0, inputLength);
byte[] output = new byte[100];
int resultLength = inflater.inflate(output);
inflater.end();
result = new String(output, 0, resultLength, UTF8_CHARSET);
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
推荐答案
您当前的方法是:
- 使用
getBytes(UTF-8)将字符串转换为字节数组
。 - 压缩字节数组
- 使用
将新的字符串
传送压缩字串 - 接收压缩字串
- 使用
getBytes(UTF-8)
将压缩字符串转换为字节数组。 - li>
- 使用
new String(bytes,...,UTF-8)
将解压缩的字节数组转换为String。
- Convert String to byte array using
getBytes("UTF-8")
. - Compress byte array
- Convert compressed byte array to String using
new String(bytes, ..., "UTF-8")
. - Transmit compressed string
- Receive compressed string
- Convert compressed string to byte array using
getBytes("UTF-8")
. - Decompress byte array
- Convert decompressed byte array to String using
new String(bytes, ..., "UTF-8")
.
这种方法的问题在于步骤3.当压缩字节数组时,将创建一个字节序列,它可能不再是有效的UTF-8 。结果将是第3步中的异常。
The problem with this approach is in step 3. When you compress the byte array, you create a sequence of bytes which may no longer be valid UTF-8. The result will be an exception in step 3.
解决方案是使用字节到字符编码方案(如Base64)将压缩字节转换为可传输的字符串。换句话说,将第3步替换为对Base64编码函数的调用,第6步使用对Base64解码函数的调用。
The solution is to use a "bytes to characters" encoding scheme like Base64 to turn the compressed bytes into a transmissible string. In other words, replace step 3 with a call to a Base64 encode function, and step 6 with a call to a Base64 decode function.
注意:
- 对于小字符串,压缩和
编码实际上可能会增加传输字符串的大小。 - 如果要将压缩的字符串合并到网址中,您可能需要为Base64选择不同的编码,以避免需要进行网址转义的字符。
- 对您正在传输的数据的性质,您可能会发现域特定压缩比一个通用的压缩更好。考虑在创建逗号分隔的字符串之前压缩数据。考虑使用逗号分隔字符串的替代方法。
这篇关于在Java中压缩客户机/服务器传输的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!