[MAGNOLIA-4897] Improve memory optimalization while activation Created: 12/Mar/13  Updated: 25/Apr/13  Resolved: 25/Apr/13

Status: Closed
Project: Magnolia
Component/s: None
Affects Version/s: 4.5.7
Fix Version/s: 4.5.9

Type: Improvement Priority: Neutral
Reporter: Zdenek Skodik Assignee: Jaroslav Simak
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Cloners
is cloned by MGNLACTIVATION-23 CLONE - Improve memory optimalization... Closed
relation
is related to MAGNOLIA-4985 Provide method to get digest from str... Closed
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Date of First Response:

 Description   

The process of creating a temp xml file to be streamed to response requires at least four times more free memory than what the resource size is. This is very expensive especially for blobs. There should be a room for optimization.



 Comments   
Comment by Jan Haderka [ 23/Apr/13 ]

-        ByteArrayOutputStream md5 = new ByteArrayOutputStream();
-        InputStream documentInputStream = new TeeInputStream(resourceDocument.getStream(), md5);
+
+        MessageDigest md = null;
+        try {
+            md = MessageDigest.getInstance("md5");
+        } catch (NoSuchAlgorithmException e) {
+            log.error(e.getMessage());
+            throw new RuntimeException(e);
+        }
+        InputStream documentInputStream = new DigestInputStream(resourceDocument.getStream(), md);
         org.jdom.Document jdomDocument = builder.build(documentInputStream);
         IOUtils.closeQuietly(documentInputStream);
 
-        String sign = SecurityUtil.getMD5Hex(md5.toByteArray());
+        final String sign = SecurityUtil.byteArrayToHex(md.digest());

Since we already have methods for getting md5hex from various input (string, byte array) wouldn't it make more sense to have such code in there rather then having everyone who needs to calculate it on a stream to do the same again and again? ... Even in your code you have to do it 2 times more below the code I've pasted above.

Generated at Mon Feb 12 04:00:04 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.