Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Steven Devijver

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Format code with line numbers in Markdown notation

C:\>type Format.groovy | groovy Format
     1: def counter = 1
     2:
     3: System.in.eachLine {
     4:         line ->
     5:
     6:         def result = "    "
     7:         (2 - ("" + counter).length()).times { result += " " }
     8:         result += "${counter}: ${line}"
     9:         println result
    10:         counter++
    11: }

C:\>


def counter = 1

System.in.eachLine {
	line ->
	
	def result = "    "
	(2 - ("" + counter).length()).times { result += " " }
	result += "${counter}: ${line}"
	println result
	counter++
}

Send a bad request to a web service for testing garbage resilience

// description of your code here

def link = "http://localhost:8080/webservices/MyWebService"

def url = new URL(link)

def conn = url.openConnection()
conn.doOutput = true

conn.outputStream << "BROL"

conn.inputStream.eachLine { println it }

Custom base conversion (to and from)

Handy to store large numbers in a shorter notation. Doesn't work with negative numbers (for negative numbers you need to store an additional byte to indicate a positive or negative value, pass the absolute value).

	private static final short BASE_40_RADIX = 40;
	private static final short BASE_40_BASE = 48;
	private static final String BASE_40_ZERO = "0";

	public static long fromBase40(char[] cs) {
		return fromBase(cs, BASE_40_BASE, BASE_40_RADIX);
	}

	public static char[] toBase40(final long i) {
		return toBase(i, BASE_40_BASE, BASE_40_RADIX, BASE_40_ZERO);
	}

	private static final short BASE_75_RADIX = 75;
	private static final short BASE_75_BASE = 48;
	private static final String BASE_75_ZERO = "0";

	public static long fromBase75(char[] cs) {
		return fromBase(cs, BASE_75_BASE, BASE_75_RADIX);
	}

	public static char[] toBase75(final long i) {
		return toBase(i, BASE_75_BASE, BASE_75_RADIX, BASE_75_ZERO);
	}

	private static final short BASE_90_RADIX = 90;
	private static final short BASE_90_BASE = 33;
	private static final String BASE_90_ZERO = "!";

	public static long fromBase90(char[] cs) {
		return fromBase(cs, BASE_90_BASE, BASE_90_RADIX);
	}

	public static char[] toBase90(final long i) {
		return toBase(i, BASE_90_BASE, BASE_90_RADIX, BASE_90_ZERO);
	}

	private static char[] toBase(final long i, final short base, final short radix, String nullCharacter) {
		long value = i;

		if (value == 0) {
			return nullCharacter.toCharArray();
		}

		String result = "";
		while (value > 0) {
			long mod = value % radix;
			value -= mod;
			if (value > 0) value /= radix;
			result = (char)(mod + base) + result;
		}

		return result.toCharArray();
	}

	private static long fromBase(char[] cs, final short base, final short radix) {
		long value = 0;
		for (int i = cs.length - 1; i >= 0; i--) {
			int digit = ((int)cs[i]) - base;
			if (digit < 0 || digit >= radix) {
				throw new IllegalArgumentException("Invalid Base" + radix + " character: " + cs[i]);
			}
			long digitBase = (long)Math.pow(radix, (cs.length - 1) - i);
			value = (digit * digitBase) + value;
		}

		return value;
	}


And here are some tests:

	public void testFromBase40() {
		assertEquals(2559999, NumberUtils.fromBase40("WWWW".toCharArray()));
		assertEquals(1117580, NumberUtils.fromBase40("ABCD".toCharArray()));

		assertEquals(0, NumberUtils.fromBase40("0".toCharArray()));
		assertEquals(1, NumberUtils.fromBase40("1".toCharArray()));

		assertEquals(40, NumberUtils.fromBase40("10".toCharArray()));
		assertEquals(80, NumberUtils.fromBase40("20".toCharArray()));

		assertEquals(41, NumberUtils.fromBase40("11".toCharArray()));
		assertEquals(81, NumberUtils.fromBase40("21".toCharArray()));

		assertEquals(81, NumberUtils.fromBase40("0021".toCharArray()));

		assertEquals(3061560805L, NumberUtils.fromBase40("MSTSD5".toCharArray()));
	}

	public void testToBase40() {
		assertEquals("0", new String(NumberUtils.toBase40(0)));
		assertEquals("1", new String(NumberUtils.toBase40(1)));

		assertEquals("10", new String(NumberUtils.toBase40(40)));
		assertEquals("20", new String(NumberUtils.toBase40(80)));

		assertEquals("11", new String(NumberUtils.toBase40(41)));
		assertEquals("21", new String(NumberUtils.toBase40(81)));

		assertEquals("ABCD", new String(NumberUtils.toBase40(1117580)));
		assertEquals("WWWW", new String(NumberUtils.toBase40(2559999)));

		assertEquals("MSTSD5", new String(NumberUtils.toBase40(3061560805L)));

	}

	public void testToBase90() throws Exception {
		assertEquals("#Q&Y.`QY#", new String(NumberUtils.toBase90(10908158098650842L)));
	}

	public void testFromBase90() throws Exception {
		assertEquals(10908158098650842L, NumberUtils.fromBase90("#Q&Y.`QY#".toCharArray()));
	}

Convert a long to HEX value and the other way around

Handy to convert MD5 or SHA-1 hash values.

	public static long hexToLong(byte[] bytes) {

		if (bytes.length > 16) {
			throw new IllegalArgumentException("Byte array too long (max 16 elements)");
		}
		long v = 0;
		for (int i = 0; i < bytes.length; i += 2) {
			byte b1 = (byte) (bytes[i] & 0xFF);

			b1 -= 48;
			if (b1 > 9) b1 -= 39;

			if (b1 < 0 || b1 > 15) {
				throw new IllegalArgumentException("Illegal hex value: " + bytes[i]);
			}

			b1 <<=4;

			byte b2 = (byte) (bytes[i + 1] & 0xFF);
			b2 -= 48;
			if (b2 > 9) b2 -= 39;

			if (b2 < 0 || b2 > 15) {
				throw new IllegalArgumentException("Illegal hex value: " + bytes[i + 1]);
			}

			v |= (((b1 & 0xF0) | (b2 & 0x0F))) & 0x00000000000000FFL ;

			if (i + 2 < bytes.length) v <<= 8;
		}

		return v;
	}

	public static byte[] longToHex(final long l) {
		long v = l & 0xFFFFFFFFFFFFFFFFL;

		byte[] result = new byte[16];
		Arrays.fill(result, 0, result.length, (byte)0);

		for (int i = 0; i < result.length; i += 2) {
			byte b = (byte) ((v & 0xFF00000000000000L) >> 56);

			byte b2 = (byte) (b & 0x0F);
			byte b1 = (byte) ((b >> 4) & 0x0F);

			if (b1 > 9) b1 += 39;
			b1 += 48;

			if (b2 > 9) b2 += 39;
			b2 += 48;

			result[i] = (byte) (b1 & 0xFF);
			result[i + 1] = (byte) (b2 & 0xFF);

			v <<= 8;
		}

		return result;
	}



And tests:

	public void testHexToLong() throws Exception {
		assertEquals(-7057002501900618110L, NumberUtils.hexToLong("9e107d9d372bb682".getBytes()));
		assertEquals(-10908158098650842L, NumberUtils.hexToLong("ffd93f1687604926".getBytes()));
	}

	public void testLongToHex() throws Exception {
		assertEquals("9e107d9d372bb682", new String(NumberUtils.longToHex(-7057002501900618110L)));
		assertEquals("ffd93f1687604926", new String(NumberUtils.longToHex(-10908158098650842L)));
	}
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS