View Javadoc
1   /*
2    * Copyright (C) 2019 uwe
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.sw4j.sample.memory.heap;
18  
19  import java.security.MessageDigest;
20  import java.security.NoSuchAlgorithmException;
21  import java.util.Random;
22  import java.util.logging.Logger;
23  
24  /**
25   *
26   * @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
27   */
28  public class Allocator {
29          //implements Runnable {
30  
31      private static final int kiByte = 1024;
32  
33      private static final Logger logger = Logger.getLogger(Allocator.class.getName());
34  
35      private byte[] data;
36  
37      private byte[] digest;
38  
39      private final Random rand = new Random();
40  
41      /**
42       * Creates a new allocator and reserves {@code blocks} 1 KiB blocks.
43       *
44       * @param blocks the number of blocks to reserve.
45       */
46      public Allocator(int blocks) {
47          data = new byte[blocks * kiByte];
48      }
49  
50      public void calculateHash() throws NoSuchAlgorithmException {
51          byte[] filler = new byte[kiByte];
52          for (int i = 0; i < data.length / kiByte; i++) {
53              rand.nextBytes(filler);
54              System.arraycopy(filler, 0, data, kiByte * i, kiByte);
55          }
56          String digestAlgorithm = "SHA3-512";
57          String javaVersion = System.getProperty("java.specification.version");
58          try {
59              double version = Double.parseDouble(javaVersion);
60              if (version < 9.0d) {
61                  logger.config("Pre Java 9 VM detected.");
62                  digestAlgorithm = "SHA-512";
63              } else {
64                  logger.config("Post Java 8 VM detected.");
65              }
66          } catch (NumberFormatException nfex) {
67              logger.config(String.format("Unknown Java version: %s.", javaVersion));
68              digestAlgorithm = "SHA-512";
69          }
70          logger.config(String.format("Using hash algorithm %s", digestAlgorithm));
71          MessageDigest digester = MessageDigest.getInstance(digestAlgorithm);
72          digest = digester.digest(data);
73      }
74  
75      public String getHash() {
76          StringBuilder sb = new StringBuilder();
77          for (int i = 0; i < digest.length; i++) {
78              sb.append(String.format("%02x", digest[i]));
79          }
80          return sb.toString();
81      }
82  
83  //    @Override
84  //    public void run() {
85  //        data = null;
86  //    }
87  
88  }