| 128 | |
| 129 | |
| 130 | static byte[] generateMask() { |
| 131 | // SecureRandom is not thread-safe so need to make sure only one thread |
| 132 | // uses it at a time. In theory, the pool could grow to the same size |
| 133 | // as the number of request processing threads. In reality, it will be |
| 134 | // a lot smaller. |
| 135 | |
| 136 | // Get a SecureRandom from the pool |
| 137 | SecureRandom sr = randoms.poll(); |
| 138 | |
| 139 | // If one isn't available, generate a new one |
| 140 | if (sr == null) { |
| 141 | try { |
| 142 | sr = SecureRandom.getInstance("SHA1PRNG"); |
| 143 | } catch (NoSuchAlgorithmException e) { |
| 144 | // Fall back to platform default |
| 145 | sr = new SecureRandom(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Generate the mask |
| 150 | byte[] result = new byte[4]; |
| 151 | sr.nextBytes(result); |
| 152 | |
| 153 | // Put the SecureRandom back in the poll |
| 154 | randoms.add(sr); |
| 155 | |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | static Class<?> getMessageType(MessageHandler listener) { |