/** * * @param payload * @return * @throws IOException */ public static SparkplugBPayload compress(SparkplugBPayload payload) throws IOException { logger.trace("Compressing payload"); SparkplugBPayloadEncoder encoder = new SparkplugBPayloadEncoder(); // Encode bytes byte[] encoded = encoder.getBytes(payload); // Default to DEFLATE byte[] compressedBytes = deflateBytes(encoded); // Create new payload, add the bytes as the body, and return. return new SparkplugBPayloadBuilder(payload.getSeq()).setBody(compressedBytes).setUuid(UUID_COMPRESSED) .createPayload(); }
private static void divideAndAddMessages(Collection<Message> messages, Message message, int maxBytes) throws SparkplugException, JsonProcessingException { Topic topic = message.getTopic(); SparkplugBPayload payload = message.getPayload(); List<Metric> metrics = payload.getMetrics(); final int metricCount = message.getPayload().getMetricCount(); final int size = toJsonString(message).getBytes().length; // Check if the message can be divided if (metricCount <= 1) { throw new SparkplugException("Cannot divide SparkplugBPayload with only " + metricCount + " metric(s)"); } int newMessageCount = size / maxBytes + ((size % maxBytes > 0) ? 1 : 0); int metricsPerMessageCount = metricCount / newMessageCount + ((metricCount % newMessageCount > 0) ? 1 : 0); int index = 0; while (index < metricCount) { int toIndex = metricCount < (index + metricsPerMessageCount) ? metricCount : index + metricsPerMessageCount; // build a new Message with the payload containing the next subset (count) of metrics Message newMessage = new MessageBuilder(topic, new SparkplugBPayloadBuilder().setTimestamp(payload.getTimestamp()).setUuid(payload.getUuid()) .setSeq(payload.getSeq()).addMetrics(new ArrayList<Metric>(metrics.subList(index, toIndex))) .createPayload()).build(); String jsonMessage = toJsonString(newMessage); if (jsonMessage.getBytes().length < maxBytes) { messages.add(newMessage); } else { divideAndAddMessages(messages, newMessage, maxBytes); } index += metricsPerMessageCount; } }
protoMsg.setSeq(payload.getSeq());
/** * * @param payload * @return * @throws IOException */ public static SparkplugBPayload compress(SparkplugBPayload payload, CompressionAlgorithm algorithm) throws IOException, SparkplugException { logger.trace("Compressing payload"); SparkplugBPayloadEncoder encoder = new SparkplugBPayloadEncoder(); // Encode bytes byte[] encoded = encoder.getBytes(payload); byte[] compressed = null; Metric algorithmMetric = new MetricBuilder(METRIC_ALGORITHM, MetricDataType.String, algorithm.toString()) .createMetric(); // Switch over compression algorithm switch (algorithm) { case GZIP: compressed = GZipUtil.compress(encoded); break; case DEFLATE: compressed = deflateBytes(encoded); break; default: throw new SparkplugException("Unknown or unsupported algorithm " + algorithm); } // Wrap and return the payload return new SparkplugBPayloadBuilder(payload.getSeq()).setBody(compressed).setUuid(UUID_COMPRESSED) .addMetric(algorithmMetric).createPayload(); }