From 7e0deee877b713c82189e65c84a6c624157984fa Mon Sep 17 00:00:00 2001 From: Christophe Dame Date: Thu, 23 Jun 2022 10:17:02 +0200 Subject: [PATCH] add a util class for commodities we lost from java 11 --- .../camunda/operate/CamundaOperateClient.java | 5 +- .../operate/CamundaOperateConstants.java | 14 +---- .../io/camunda/operate/util/Java8Utils.java | 51 +++++++++++++++++++ 3 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 src/main/java/io/camunda/operate/util/Java8Utils.java diff --git a/src/main/java/io/camunda/operate/CamundaOperateClient.java b/src/main/java/io/camunda/operate/CamundaOperateClient.java index 5c01cc8..12597b5 100644 --- a/src/main/java/io/camunda/operate/CamundaOperateClient.java +++ b/src/main/java/io/camunda/operate/CamundaOperateClient.java @@ -30,6 +30,7 @@ import io.camunda.operate.search.ProcessInstanceFilter; import io.camunda.operate.search.SearchQuery; import io.camunda.operate.search.VariableFilter; +import io.camunda.operate.util.Java8Utils; import io.camunda.operate.util.JsonUtils; import io.camunda.zeebe.model.bpmn.Bpmn; import io.camunda.zeebe.model.bpmn.BpmnModelInstance; @@ -75,7 +76,7 @@ public BpmnModelInstance getProcessDefinitionModel(Long key) throws OperateExcep try (CloseableHttpClient httpClient = HttpClients.createDefault()) { try (CloseableHttpResponse response = httpClient.execute(httpGet)) { InputStream processInputStream = new ByteArrayInputStream( - response.getEntity().getContent().readAllBytes()); + Java8Utils.readAllBytes(response.getEntity().getContent())); return Bpmn.readModelFromStream(processInputStream); } } catch (IOException e) { @@ -162,7 +163,7 @@ private T get(Long key, Class resultType) throws OperateException { protected String executeQuery(ClassicHttpRequest httpRequest) throws OperateException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { try (CloseableHttpResponse response = httpClient.execute(httpRequest)) { - return new String(response.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8); + return new String(Java8Utils.readAllBytes(response.getEntity().getContent()), StandardCharsets.UTF_8); } } catch (IOException e) { throw new OperateException(e); diff --git a/src/main/java/io/camunda/operate/CamundaOperateConstants.java b/src/main/java/io/camunda/operate/CamundaOperateConstants.java index 1553d96..6323b89 100644 --- a/src/main/java/io/camunda/operate/CamundaOperateConstants.java +++ b/src/main/java/io/camunda/operate/CamundaOperateConstants.java @@ -1,30 +1,20 @@ package io.camunda.operate; -import java.util.AbstractMap; import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.Stream; import io.camunda.operate.dto.FlownodeInstance; import io.camunda.operate.dto.Incident; import io.camunda.operate.dto.ProcessDefinition; import io.camunda.operate.dto.ProcessInstance; import io.camunda.operate.dto.Variable; +import io.camunda.operate.util.Java8Utils; public class CamundaOperateConstants { - public static Map, String> OBJECT_APIS = toMap(ProcessDefinition.class, "/v1/process-definitions", + public static Map, String> OBJECT_APIS = Java8Utils.toMap(ProcessDefinition.class, "/v1/process-definitions", ProcessInstance.class, "/v1/process-instances", FlownodeInstance.class, "/v1/flownode-instances", Incident.class, "/v1/incidents", Variable.class, "/v1/variables"); - private static Map, String> toMap(Object... array) { - AbstractMap.SimpleEntry, String>[] entryArray = new AbstractMap.SimpleEntry[array.length/2]; - for(int i=0;i, String>((Class)array[i*2], (String) array[i*2+1]); - } - return Stream.of(entryArray) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - } } diff --git a/src/main/java/io/camunda/operate/util/Java8Utils.java b/src/main/java/io/camunda/operate/util/Java8Utils.java new file mode 100644 index 0000000..af34838 --- /dev/null +++ b/src/main/java/io/camunda/operate/util/Java8Utils.java @@ -0,0 +1,51 @@ +package io.camunda.operate.util; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.AbstractMap; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Java8Utils { + + private Java8Utils() { + + } + + public static byte[] readAllBytes(InputStream inputStream) throws IOException { + final int bufLen = 4 * 0x400; // 4KB + byte[] buf = new byte[bufLen]; + int readLen; + IOException exception = null; + + try { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + while ((readLen = inputStream.read(buf, 0, bufLen)) != -1) + outputStream.write(buf, 0, readLen); + + return outputStream.toByteArray(); + } + } catch (IOException e) { + exception = e; + throw e; + } finally { + if (exception == null) inputStream.close(); + else try { + inputStream.close(); + } catch (IOException e) { + exception.addSuppressed(e); + } + } + } + + public static Map, String> toMap(Object... array) { + AbstractMap.SimpleEntry, String>[] entryArray = new AbstractMap.SimpleEntry[array.length/2]; + for(int i=0;i, String>((Class)array[i*2], (String) array[i*2+1]); + } + return Stream.of(entryArray) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } +}