Compression MQTT数据压缩
目前,我正在尝试找出MQTT是否支持数据的自动压缩。例如,如果我将MQTT与TLS结合起来,我可以使用TLS的压缩选项。MQTT有类似的功能吗?或者发布应用程序必须压缩其数据本身,然后才能使用MQTT发送压缩数据 到目前为止,我已经四处阅读,找不到关于MQTT是否支持数据压缩的任何具体信息Compression MQTT数据压缩,compression,mqtt,Compression,Mqtt,目前,我正在尝试找出MQTT是否支持数据的自动压缩。例如,如果我将MQTT与TLS结合起来,我可以使用TLS的压缩选项。MQTT有类似的功能吗?或者发布应用程序必须压缩其数据本身,然后才能使用MQTT发送压缩数据 到目前为止,我已经四处阅读,找不到关于MQTT是否支持数据压缩的任何具体信息 感谢您的帮助:)MQTT纯粹是一种传输,它将承载您作为有效负载提供的任何字节(高达256MB),并且不会以任何方式更改它们 如果您想先压缩有效负载,那完全取决于您。我为我的UFM(通用文件移动器)开源Java
感谢您的帮助:)MQTT纯粹是一种传输,它将承载您作为有效负载提供的任何字节(高达256MB),并且不会以任何方式更改它们
如果您想先压缩有效负载,那完全取决于您。我为我的UFM(通用文件移动器)开源Java项目创建了一个名为JZIP的包装类。您可以从下载源代码 下面是JZip类的示例。它有两种方法:压缩数据和解压缩数据。两者都期望一个字节[]作为输入,并给出一个字节[]作为输出。它使用起来非常简单
package com.capitalware.ufm.handlers;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import com.capitalware.ufm.UFM;
/**
* This class will handle all of UFM's compression and decompression work.
* @author Roger Lacroix
* @version 1.0
* @license Apache 2 License
*/
public class JZip
{
/**
* The constructor - make sure nothing instantiates this class.
* It must be accessed in a static way.
*/
private JZip()
{}
/**
* This method will compress a byte array of data.
*
* @param inputData an array of bytes
* @return byte array of the contents of the file.
*/
public static byte[] compressData(byte[] inputData)
{
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(inputData);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
// You cannot use an array that's the same size as the orginal because
// there is no guarantee that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(inputData.length);
// Compress the data
byte[] buf = new byte[65536];
while (!compressor.finished())
{
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try
{
bos.close();
}
catch (IOException e)
{
UFM.logger.fatal("IOException: " + e.getLocalizedMessage());
}
// return compressed data
return bos.toByteArray();
}
/**
* This method will decompress a byte array of compressed data.
*
* @param compressedData an array of bytes
* @return byte array of uncompressed data
*/
public static byte[] decompressData(byte[] compressedData)
{
// Create the decompressor and give it the data to compress
Inflater decompressor = new Inflater();
decompressor.setInput(compressedData);
// Create an expandable byte array to hold the decompressed data
ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
// Decompress the data
byte[] buf = new byte[65536];
while (!decompressor.finished())
{
try
{
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
catch (DataFormatException e)
{
UFM.logger.fatal("DataFormatException: " + e.getLocalizedMessage());
}
}
try
{
bos.close();
}
catch (IOException e)
{
UFM.logger.fatal("IOException: " + e.getLocalizedMessage());
}
// return uncompressed data
return bos.toByteArray();
}
}
这并没有回答被问到的问题,即MQTT是否支持压缩,而不是我如何用Java压缩数据……您给了此人正确的答案,我正在根据您的答案提供解决方案。