Contains helper classes that I find useful every now and then.
Used to compress image files to JPEG format using the desired quality for the compressed image.
// Compression quality (value >= 0 and <= 1)
final float quality = 0.35f;
// Compressing to bytes from source image bytes
byte[] compressed = ImageCompressionUtils.compressImage(sourceBytes, quality);A Thread-Safe Object-Oriented wrapper for the ImageCompressionUtils utility class with Base64 and method chaining support.
// Creating a new instance and setting the compressed image quality
ImageCompressor mImageCompressor = new ImageCompressor().setCompressedImageQuality(0.75f);
// Compressing an image to byte array
byte[] compressed = mImageCompressor.setSourceImage(sourceBytes).compressImageToByteArray();
// Modifying the compressed image quality, and compressing a new image to Base64
String compressedBase64 = mImageCompressor.setCompressedImageQuality(0.35f)
.setSourceImage(sourceBytesTwo)
.compressImageToBase64();Cache implementation with a periodic memory clean up process for objects that haven't been accessed for a specified period of time. Implements AutoCloseable to shut down the cleanup executor.
long cleanUpInterval = 100; // milliseconds
long objectTTL = 2000; // milliseconds
int cacheSize = 25;
try(ConcurrentCache<Integer, String> mCache = new ConcurrentCache<>(objectTTL, cleanUpInterval, cacheSize)){
mCache.put(1, "value");
String value = mCache.get(1);
}Wrapper class to help manipulate PBKDF2 parameters and generate keys.
// Using the builder
PBKDF2Helper mHelper = new PBKDF2Helper.Builder(KeySize.KEY_256)
.iterations(Iterations.MEDIUM)
.saltSize(SaltSize.SALT_128)
.build();
// Generating a key (password as char[] for secure memory handling)
char[] password = "myPassword".toCharArray();
byte[] key = mHelper.createKeyFromPassword(password);
// Getting configurations for storage
String config = mHelper.getPbkdf2Configurations();
// Creation using the configurations string
PBKDF2Helper mHelper = new PBKDF2Helper.Builder(config).build();Used to encrypt files using AES-GCM with PBKDF2. Instances are single-use.
// Using factory method for creation (password as char[])
char[] password = "myPassword".toCharArray();
FileEncryptorAES mEncryptor = FileEncryptorAES.createEncryptorWithHighSecurityParams(password);
// Setting the progress monitor
mEncryptor.setProgressMonitor((int progress) -> System.out.println(progress));
// Encryption
BufferedInputStream mInputStream = new BufferedInputStream(new FileInputStream(mSourceFile));
BufferedOutputStream mOutputStream = new BufferedOutputStream(new FileOutputStream(mTargetFile));
mEncryptor.encrypt(mInputStream, mOutputStream);Used to decrypt files encrypted with FileEncryptorAES. Implements AutoCloseable to zero the internal password copy.
// Creation (password as char[])
char[] password = "myPassword".toCharArray();
try(FileDecryptorAES mDecryptor = new FileDecryptorAES(password)){
// Setting the progress monitor
mDecryptor.setProgressMonitor((int progress) -> System.out.println(progress));
// Decryption
BufferedInputStream mInputStream = new BufferedInputStream(new FileInputStream(mSourceFile));
BufferedOutputStream mOutputStream = new BufferedOutputStream(new FileOutputStream(mTargetFile));
mDecryptor.decrypt(mInputStream, mOutputStream);
}Provides an AES encryption layer over the java.util.prefs.Preferences class. Implements AutoCloseable to zero the internal password copy.
// Setting up the prefs (password as char[])
try(SecurePreferences mPreferences = new SecurePreferencesImpl("test-node", "pa$$word".toCharArray())){
// Storing a string value
String str = "test-data";
mPreferences.putString("string", str);
// Getting the value
Optional<String> mOptional = mPreferences.getString("string");
if(mOptional.isPresent()){
String loaded = mOptional.get();
}
}Used to deep clone objects that implements the Serializable interface. (Any objects used by the class must also implement Serializable).
SomeObject clone = CloneUtils.deepClone(origin, SomeObject.class);Used to check card numbers using the Luhn formula.
boolean isValid = LuhnChecker.checkNumberValidity("4532136548631895");-
Hussain Al-Derry
Email - hussain.derry@gmail.com
Copyright 2017 Hussain Al-Derry
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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.