目前,我正在尝试找出MQTT是否支持数据的自动压缩。例如,如果我将MQTT与TLS结合使用,则可以使用TLS的压缩选项。MQTT有没有类似的东西?或者发布应用程序必须自己压缩数据,然后才能使用MQTT发送压缩数据?
到目前为止,我已经到处阅读,没有找到任何关于MQTT是否支持数据压缩的具体信息。
感谢您的帮助:)
发布于 2020-04-16 18:58:28
MQTT纯粹是一种传输,它将携带您给它的任何字节作为有效负载(最大256MB),并且不会以任何方式更改它们。
如果你想先压缩有效载荷,那完全取决于你。
发布于 2020-04-17 05:15:05
我为我的UFM (通用文件移动器)开源Java项目创建了一个名为JZIP的包装类。您可以从here下载源代码。
下面就是JZip类。它有两种方法: compressData和decompressData。两者都希望输入为byte[],并提供byte[]作为输出。它使用起来非常简单。
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();
}
}https://stackoverflow.com/questions/61247631
复制相似问题