Archive

Archive for the ‘Uncategorized’ Category

Please suggest idea to stop BP Oil Spill

May 29, 2010 Leave a comment

Java Safe Recursion

May 28, 2010 Leave a comment
Problem: Getting StackOverflowError in java recursion

Exception in thread "main" java.lang.StackOverflowError
at java.nio.Buffer.<init>(Buffer.java:176)
at java.nio.CharBuffer.<init>(CharBuffer.java:259)
at java.nio.HeapCharBuffer.<init>(HeapCharBuffer.java:52)
at java.nio.CharBuffer.wrap(CharBuffer.java:350)
at sun.nio.cs.StreamEncoder$CharsetSE.implWrite(StreamEncoder.java:378)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:136)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:191)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
at java.io.PrintStream.write(PrintStream.java:458)
at java.io.PrintStream.print(PrintStream.java:602)
at java.io.PrintStream.println(PrintStream.java:739)
at com.test.RecursiveLock.checkLogicOne(RecursiveLock.java:25)
at com.test.RecursiveLock.checkLogicOne(RecursiveLock.java:27)
at com.test.RecursiveLock.checkLogicOne(RecursiveLock.java:27)
at com.test.RecursiveLock.checkLogicOne(RecursiveLock.java:27)
at com.test.RecursiveLock.checkLogicOne(RecursiveLock.java:27)

Please check this code for better understanding 

package com.test;

public class RecursiveLock {

	public static void main(String[] args) {

		try {
			RecursiveLock rl = new RecursiveLock();
			 //rl.checkLogicOne(1);
			rl.checkLogicOneSafe(1);

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	/**
	 * This is not infinite. This fails with Stack Overflow error
	 *
	 * @param i
	 */
	private void checkLogicOne(long i) {
		try {
			System.out.println("Running " + i);
			// Thread.sleep(100);
			checkLogicOne(i + 1);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private static int recursionCounter = 0;
	private static int REC_LIMIT = 1000;

	/**
	 * Dont assume that data is available and system will come out smoothly.
	 * without safe check, it may enter into stack overflow.
	 *
	 * @param i
	 *
	 */
	private void checkLogicOneSafe(long i) {
		try {
			System.out.println("Running " + i);

			// This prevents Stack Over Flow
			if (recursionCounter > REC_LIMIT) {
				System.out.println("Exceeded Recursion Limit");
				return;
			} else
				recursionCounter++;

			checkLogicOneSafe(i + 1);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}

Inspecting HTTP data information

May 10, 2010 Leave a comment

Problem: How to inspect data communicated by browser for testing purpose?

Live HTTP Headers 0.16 https://addons.mozilla.org/en-US/firefox/addon/3829

Tamper Data 11.0.1 https://addons.mozilla.org/en-US/firefox/addon/966

Charles http://www.charlesproxy.com/

Fire Bug http://getfirebug.com/

Java Currency Format -1234.34 to -$1,234

Question: How to format currency. Example: -1234.34 to -$1,234

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

public class CurrencyTest {

	public static void main(String[] test) {
		double currency = -1234;
		NumberFormat dollarFormatter;
		dollarFormatter = NumberFormat.getCurrencyInstance(Locale.US);

		if (dollarFormatter instanceof DecimalFormat) {
			((DecimalFormat) dollarFormatter).setMaximumFractionDigits(0);
			((DecimalFormat) dollarFormatter).setNegativePrefix("-$");
			((DecimalFormat) dollarFormatter).setNegativeSuffix("");
		}

		String text = dollarFormatter.format(currency);
		System.out.println(text);
	}
}

Input: -1234.34
Output: -$1,234

Playing with WordPress

April 18, 2010 Leave a comment
Categories: Uncategorized, Wordpress Tags:
Follow

Get every new post delivered to your Inbox.