Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
* in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.amazonaws.encryptionsdk.internal;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Comparator;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;
/**
* Internal utility methods.
*/
public final class Utils {
// SecureRandom objects can both be expensive to initialize and incur synchronization costs.
// This allows us to minimize both initializations and keep SecureRandom usage thread local
// to avoid lock contention.
private static final ThreadLocal LOCAL_RANDOM = new ThreadLocal() {
@Override
protected SecureRandom initialValue() {
final SecureRandom rnd = new SecureRandom();
rnd.nextBoolean(); // Force seeding
return rnd;
}
};
private Utils() {
// Prevent instantiation
}
/*
* In some areas we need to be able to assign a total order over Java objects - generally with some primary sort,
* but we need a fallback sort that always works in order to ensure that we don't falsely claim objects A and B
* are equal just because the primary sort declares them to have equal rank.
*
* To do this, we'll define a fallback sort that assigns an arbitrary order to all objects. This order is
* implemented by first comparing hashcode, and in the rare case where we are asked to compare two objects with
* equal hashcode, we explicitly assign an index to them - using a WeakHashMap to track this index - and sort
* based on this index.
*/
private static AtomicLong FALLBACK_COUNTER = new AtomicLong(0);
private static WeakHashMap