Skip to content

Instantly share code, notes, and snippets.

View Fullplate's full-sized avatar
🤓

Michael Harmer Fullplate

🤓
  • Sydney, Australia
View GitHub Profile
@Fullplate
Fullplate / batch.java
Created July 11, 2014 04:29
[Java] Split list into batches of max size n
private static <T> List<List<T>> batchList(List<T> inputList, final int maxSize) {
List<List<T>> sublists = new ArrayList<>();
final int size = inputList.size();
for (int i = 0; i < size; i += maxSize) {
// Math.min... size will be smaller than i + maxSize for the last batch (unless perfectly divisible),
// including the first batch if size is smaller than max size
sublists.add(new ArrayList<>(inputList.subList(i, Math.min(size, i + maxSize))));
}
@Fullplate
Fullplate / ConnectionChecker.java
Created July 15, 2014 00:07
Android Connection Checking
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
/**
* Checks for a network connection at intervals until connection is found, or timeout has been reached.
* Result is sent via a message containing {"connected":boolean} sent to passed-in Handler.
* Log output is sent via a message containing {"log":string} sent to passed-in Handler.
@Fullplate
Fullplate / ProgressBar.java
Created July 16, 2014 06:37
Android Progress Bar
import android.content.Context;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
/**
* A simple progress bar for Android.
*
* Usage:
* - initialize with a LinearLayout of suitable size, the number of progress events you expect, and the desired bar colour
* - call increase(x) to increase the progress bar by x progress events
@Fullplate
Fullplate / aes
Created August 1, 2014 05:42
Binary file encryption and decryption (AES) (Java)
public static void encryptFile(SecretKeySpec key, IvParameterSpec iv, String inputFilePath, String outputFilePath) {
try {
Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, key, iv);
File inFile = new File(inputFilePath);
File outFile = new File(outputFilePath);
Files.touch(outFile);
@Fullplate
Fullplate / imageViewScaling
Created August 13, 2014 07:20
Android - Programmatic ImageView scaling
/**
* Create an ImageView with a set height and a scalable width that is scaled based on
* a provided image.
* Returns a null ImageView if image was not found.
*/
private ImageView getScaledImageView(String imagePath) {
int desiredHeightPx = 75;
ImageView iv = null;
File imageFile = new File(imagePath);
max="$1"
date
echo "url: $2
rate: $max calls / second
additional header: $3"
START=$(date +%s);
get () {
curl -s -v --header "$2" "$1" 2>&1 | tr '\r\n' '\\n' | awk -v date="$(date +'%r')" '{print $0"\n-----", date}' >> /tmp/perf-test.log
}