Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / sysprops.groovy
Created May 20, 2013 19:27
Groovy way of accessing System properties.
System.properties['javax.net.ssl.trustStore'] = '/some/trust.store'
System.properties['javax.net.ssl.trustStorePassword'] = 'pass'
System.properties.each { k, v ->
println "$k = $v"
}
@bdkosher
bdkosher / groovyinlinecut.bat
Created May 20, 2013 19:29
Using a Groovy inline script as a poor man's `cut` command
groovy -e "new File('data.in').eachLine { println it[0..12] }" > data.out
@bdkosher
bdkosher / charsets.groovy
Created May 20, 2013 19:33
Lists all of the available character sets using Groovy
java.nio.charset.Charset.availableCharsets().each { name, charset -> println name }
@bdkosher
bdkosher / launch_batchfile_no_window.js
Created May 20, 2013 19:35
A JScript snippet for launching a Windows batch file (replace {BATCHFILENAME} with the name of your batch file, which should be on %PATH%). Create shortcuts with the JScript file as the target; this allows you to launch the batch files sans command prompt window using the shortcut.
var WindowStyle_Hidden = 0
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c {BATCHFILENAME}.bat", WindowStyle_Hidden)
@bdkosher
bdkosher / Unzipper.groovy
Created May 20, 2013 20:38
Groovy class for examining/extracting zip files.
import java.util.zip.*
/**
* Makes it easier to unzip files by adding extraction methods to the entries themselves and providing Closure-based methods for finding specific entries.
*
* For example, to extract all XML files to a certain directory:
* <code>
* new Unzipper(zip).findAllEntries { it.name.endsWith(".xml") }.each { it.extractTo(someDir) }
* </code>
*/
@bdkosher
bdkosher / regex.groovy
Last active December 17, 2015 16:29
Regular expression literals are just String literals
// a regex literal is just a String literal with '/' deliminters and backslash-friendliness
def rx1 = /^foo$/
assert rx1.class == String.class
assert 'foo' ==~ rx1
assert 'FOO' !=~ rx1
assert 'Foo' !=~ rx1
// so you can use String methods and operators on them
def rx2 = /(?i)/ + rx1
assert 'foo' ==~ rx2
@bdkosher
bdkosher / SimultaneousFileAccessTest.java
Last active December 18, 2015 16:59
JUnit test to demonstrate the necessity of synchronizing both reads and writes to a shared resource (in this case, a java.io.File).
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
@bdkosher
bdkosher / calcBase64.groovy
Created June 20, 2013 21:10
Groovy script for calculating how much space a file will consume when Base64 encoded.
/*
* Script for calculating how much space a file will consume when Base64 encoded.
*/
def showUsage = { ->
println '''
Usages: calcBase64.groovy [options] file
calcBase64.groovy [options] number
Options: provide as single string, e.g. 'gp' or 'pq' (with optional prefix '-')
@bdkosher
bdkosher / urlCleaning.groovy
Created August 15, 2013 14:25
Quick and dirty methods for cleaning URLs represented as Strings using String methods.
// Clean up double slashes
// uses negative lookbehind pattern (?<!text)
def input = 'http://host//path/with//some/segments//double//slashed'
assert input.replaceAll("(?<!\\:)//", "/") == 'http://host/path/with/some/segments/double/slashed'
@bdkosher
bdkosher / DoubleCheckedLocking.groovy
Created August 21, 2013 13:02
tests double-checked locking approach
import java.util.concurrent.*
class Factory {
def rand = new Random()
def instance
def getInstance() {
if (instance == null) {
synchronized(this) {
if (instance == null) {
instance = createInstance()