0
votes

Hi I am working on Adobe Experience manager(AEM) CQ5. Now my task is I have to count the size unused dam images from repository. While iterating it I am getting the byte size each file but I am not able to count the bytes of all images at a time in a loop. Here is the code sample

while (iterator.hasNext()) {
    Node node = session.getNode(iterator.nextNode().getPaath());
    Resource res = resourceResolver = resourceResolver.getResource(node.getPath());
    byte[] result = null;
    try {
        Node ntFileNode = getSession().getNode("Path");
        Node ntResourceNode = ntFileNode.getNode("jcr:content");
        InputStream is = ntResourceNode.getProperty("jcr:data").getBinary().getStream();
        BufferedInputStream bin = new BufferedInputStream(is);
        result = IOUtils.toByteArray(bin);
        bin.close();
        is.close();
        System.out.print("Bytes of Images " + result); // here I am getting bytes of images but in individual. I want to get the total count of bytes of all images.
    }  // while loop ends

Can anyone suggest me where I have to make changes in the above code?

1

1 Answers

1
votes

Try the below.. You can refactor the code below.. but i'm just focussing on the Total Bytes:

int totalBytes = 0;
while(iterator.hasNext()){
   Node node = session.getNode(iterator.nextNode().getPaath());
   Resource res = resourceResolver = resourceResolver.getResource(node.getPath());
   byte[] result = null;
   try { 
      Node ntFileNode = getSession().getNode("Path"); 
      Node ntResourceNode = ntFileNode.getNode("jcr:content");
      InputStream is = ntResourceNode.getProperty("jcr:data").getBinary().getStream();
      BufferedInputStream bin = new BufferedInputStream(is);
      result = IOUtils.toByteArray(bin);

      totalBytes = totalBytes + result;

      bin.close();
      is.close(); 
      System.out.print("Bytes of Image "+ result ); // here I am getting bytes of images but in individual. I want to get the totoal count of bytes of all images.

      result=null; //optional - just to be sure.
  }  // while loop ends

  System.out.print("Total bytes of all images= "+ totalBytes);