<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cow Computing &#187; Java</title>
	<atom:link href="http://www.cowcomputing.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cowcomputing.com</link>
	<description>Share Info about Cloud Computing &#38; Programming</description>
	<lastBuildDate>Thu, 22 Jul 2010 17:10:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Finding Files with particular extension or Pattern in Java</title>
		<link>http://www.cowcomputing.com/2010/03/04/finding-files-with-particular-extension-or-pattern-in-java/</link>
		<comments>http://www.cowcomputing.com/2010/03/04/finding-files-with-particular-extension-or-pattern-in-java/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 09:16:53 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[Pattern]]></category>
		<category><![CDATA[RegExp]]></category>
		<category><![CDATA[Regular Expression]]></category>

		<guid isPermaLink="false">http://www.cowcomputing.com/?p=196</guid>
		<description><![CDATA[Often, to find a file with a particular extension or naming pattern within a directory, we might implement as below: // The directory which search would be conducted File directoryForSearch = new File("C:\folder"); // This may not be the best way to accomplish the task, please bear me with it File[] allFile = directoryForSearch.listFiles(); File[] [...]]]></description>
			<content:encoded><![CDATA[<p>Often, to find a file with a particular extension or naming pattern within a directory, we might implement as below:</p>
<pre class="brush:java">// The directory which search would be conducted
File directoryForSearch = new File("C:\folder");

// This may not be the best way to accomplish the task, please bear me with it
File[] allFile = directoryForSearch.listFiles();
File[] resultFile = new File[allFile.length];
int resultCount = 0;

// loop thru the list of files to find the required files
for(int i=0; i&lt;allFile.length; i++)
{
    if(allFile[i].getName().matches(".*\\.java")
    {
        resultFile[resultCount] = allFile[i];
        resultCount++;
    }
}</pre>
<p><span id="more-196"></span>In fact, there is a more convenient way to get the above task done. It&#8217;s not hard at all, however people might have often omitted it. We shall make use of &#8220;FilenameFilter&#8221; to filter the files instead of looping.</p>
<pre class="brush:java">// The directory which search would be conducted
File directoryForSearch = new File("C:\folder");

// Using FilenameFilter to do the filtering job
File[] resultFile = directoryForSearch.listFiles(new FilenameFilter()
{
    public boolean accept(File dir, String name)
    {
        // Here we try to match file with extension ".java"
        // Returning YES/TRUE indicate the acceptance of the file else ignore
        return name.matches(".*\\.java");
    }
}</pre>
<p>This code reduction and performance improvement would definitely help when one is trying to implement a complex file search.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2010/03/04/finding-files-with-particular-extension-or-pattern-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some Useful HotKey for Eclipse</title>
		<link>http://www.cowcomputing.com/2010/01/16/some-useful-hotkey-for-eclipse/</link>
		<comments>http://www.cowcomputing.com/2010/01/16/some-useful-hotkey-for-eclipse/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 06:44:24 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.cowcomputing.com/?p=155</guid>
		<description><![CDATA[If you develop heavily on Java, Eclipse definitely should be your primary programming tool (other than Text Editor). To be more effective in coding, one must master the tools he/she uses. In terms of Eclipse, hotkey is one of the chapter which should be learnt. After sometimes, the hotkey usage will eventually speed up your [...]]]></description>
			<content:encoded><![CDATA[<p>If you develop heavily on Java, Eclipse definitely should be your primary programming tool (other than Text Editor).<br />
To be more effective in coding, one must master the tools he/she uses. In terms of Eclipse, hotkey is one of the chapter which should be learnt. After sometimes, the hotkey usage will eventually speed up your coding.</p>
<p>Below are some common ones:</p>
<blockquote><p>Ctrl + Shift + P // Go to the corresponding ending brace<br />
Ctrl + Q // Go back to last edited location<br />
Alt + Left/Right Arrow // Go to previous or next editor<br />
Ctrl + I // Format code<br />
Ctrl + 1 // Generate try/catch or do/if/while/for<br />
Ctrl + / // Comment/Uncomment<br />
F3 // Find Method<br />
Ctrl + L // Go to line<br />
Ctrl + Space // Toggle code assist<br />
Ctrl + Shift + Space // Toggle arguments hints</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2010/01/16/some-useful-hotkey-for-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Java Mail API</title>
		<link>http://www.cowcomputing.com/2010/01/11/using-java-mail-api/</link>
		<comments>http://www.cowcomputing.com/2010/01/11/using-java-mail-api/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 04:55:08 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Mail]]></category>

		<guid isPermaLink="false">http://www.cowcomputing.com/?p=146</guid>
		<description><![CDATA[In order to have the ability of sending email from your java program, you would need Java Mail API. It could be obtained here. The following code illustrate how to use it. (Comment will guide you thru) // Obtain the system property and set inside the SMTP server Properties props = System.getProperties(); props.put("mail.smtp.host", "your isp [...]]]></description>
			<content:encoded><![CDATA[<p>In order to have the ability of sending email from your java program, you would need Java Mail API. It could be obtained <a href="http://java.sun.com/products/javamail/downloads/index.html">here</a>.</p>
<p>The following code illustrate how to use it. (Comment will guide you thru)</p>
<pre class="brush:java">// Obtain the system property and set inside the SMTP server
Properties props = System.getProperties();
props.put("mail.smtp.host", "your isp smtp");

// Obtain the mail session
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);

// Set the address for sender
message.setFrom(new InternetAddress("sender mail address"));

// Set the address for recipient
message.addRecipient(Message.RecipientType.TO, new InternetAddress("recipient mail address"));

// Set the Subject of the mail
message.setSubject("Subject");

// Set the message content of the mail
message.setContent("Message Body", "text/plain");

// Send the mail
Transport.send(message);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2010/01/11/using-java-mail-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SLF4J &#8211; Simple Logging Facade for Java</title>
		<link>http://www.cowcomputing.com/2010/01/05/slf4j-simple-logging-facade-for-java/</link>
		<comments>http://www.cowcomputing.com/2010/01/05/slf4j-simple-logging-facade-for-java/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 09:24:16 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Log4J]]></category>
		<category><![CDATA[Logging]]></category>
		<category><![CDATA[SLF4J]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.cowcomputing.com/?p=142</guid>
		<description><![CDATA[The logging tool come with the java.util.logging package is just so horrible to work with, and most people tend to use Log4J, while open source project tend to use common-logging. And here, i would like to introduce a better (IMHO) logging tool &#8212; SLF4J. SLF4J is a facade wrapper, while you could choose your own [...]]]></description>
			<content:encoded><![CDATA[<p>The logging tool come with the java.util.logging package is just so horrible to work with, and most people tend to use Log4J, while open source project tend to use common-logging. And here, i would like to introduce a better (IMHO) logging tool &#8212; SLF4J. SLF4J is a facade wrapper, while you could choose your own implementation to be run below, and that includes Log4J, Java.util.logging, SimpleLogger and Logback. I personally would recommend the use of Logback as the logging implementation as it&#8217;s a better successor of Log4J. So, to use SLF4J, setup the project as follows:</p>
<p>1. Put into your lib folder the following jars:</p>
<p style="padding-left: 30px;">slf4j-api.jar<br />
logback-core.jar<br />
logback-classic.jar</p>
<p>2. Then create a logback.xml or logback-test.xml in your classpath with the following content:</p>
<pre class="brush:xml">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;

&lt;configuration&gt;
 &lt;appender name="STDOUT"&gt;
   &lt;layout&gt;
     &lt;pattern&gt;%d{HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n&lt;/pattern&gt;
   &lt;/layout&gt;
 &lt;/appender&gt;
 &lt;root level="debug"&gt;
   &lt;appender-ref ref="STDOUT" /&gt;
 &lt;/root&gt;
&lt;/configuration&gt;</pre>
<p>3. In your class, make use the logger as simple as follows:</p>
<pre class="brush:java">// Simply get a logger from factory and start logging
Logger logger = LoggerFactory.getLogger("LogTest");
logger.debug("DEBUG MSG");</pre>
<p>There&#8217;s one thing i like SLF4J so much is that, it actually supports parameterized logging:</p>
<pre class="brush:java">// Parameterized logging reduce the hassle when constructing long log msg
Logger logger = LoggerFactory.getLogger("LogTest");
logger.debug("Error: {}, Reason: {}", error, reason);</pre>
<p>And one more tips, sometimes when you want to override the original logging tool used in certain open source framework or codebase (e.g Spring Framework), you could simply add the jcl-over-slf4j.jar to the lib/, this will automatically hook into the common-logging of spring and replace it with SLF4J.</p>
<p>Hope this is useful! Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2010/01/05/slf4j-simple-logging-facade-for-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java &#8211; Cloning an InputStream</title>
		<link>http://www.cowcomputing.com/2009/09/23/java-cloning-an-inputstream/</link>
		<comments>http://www.cowcomputing.com/2009/09/23/java-cloning-an-inputstream/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 02:38:04 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://steve.katyyeung.com/?p=42</guid>
		<description><![CDATA[There&#8217;s a moment when you have been given an InputStream, with its markSupported returned &#8220;False&#8221;. This means you could only read the stream one and only once, as you can&#8217;t call &#8220;reset&#8221; on it. So, what to do if you want to read the stream for more than one time and be able to re-read [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a moment when you have been given an InputStream, with its markSupported returned &#8220;False&#8221;. This means you could only read the stream one and only once, as you can&#8217;t call &#8220;reset&#8221; on it. So, what to do if you want to read the stream for more than one time and be able to re-read the previously read data. The answer is &#8220;CLONE&#8221;. My very own approach would be to use Apache Commons to turn the stream into string in which it acts as a base. Whenever i need that InputStream, i turns the string back into an stream without affecting the read position and data lost. OK, here&#8217;s the steps.</p>
<p>Suppose you got a request object with InputStream.</p>
<pre class="brush:java">// request is the Request Object
// Turns InputStream into string
String base = IOUtils.toString(request.getInputStream());

// Whenever you need the InputStream
// Turn the string back to InputStream this way
InputStream in = new ByteArrayInputStream(base.getBytes());</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2009/09/23/java-cloning-an-inputstream/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JAVA Substring Bug!</title>
		<link>http://www.cowcomputing.com/2009/09/06/java-substring-bug/</link>
		<comments>http://www.cowcomputing.com/2009/09/06/java-substring-bug/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 09:53:41 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Bug]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://steve.katyyeung.com/?p=24</guid>
		<description><![CDATA[Sometimes, when we are working with string in java, we are always tempted to use the substring method for extracting part of a string. It’s neat and efficient when you are working with small program or little string processing. However, this method is evil. It is actually causing you whole lots of problem. The substring [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, when we are working with string in java, we are always tempted to use the substring method for extracting part of a string. It’s neat and efficient when you are working with small program or little string processing. However, this method is evil. It is actually causing you whole lots of problem. The substring returns the part of the original string rather than a new one. Thus, GC wont collect the original string. So when the file or string get larger and larger, it’s actually taking up more and more memory, and thus OutOfMemory Error. To solve this, there’s nice workaround which is simple.</p>
<pre class="brush:java">String newString = new String( oldString.subString(0, 5) );</pre>
<p>Simple, Right?!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2009/09/06/java-substring-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Garbage Collection in Java</title>
		<link>http://www.cowcomputing.com/2009/09/06/garbage-collection-in-java/</link>
		<comments>http://www.cowcomputing.com/2009/09/06/garbage-collection-in-java/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 09:49:48 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Garbage Collection]]></category>

		<guid isPermaLink="false">http://steve.katyyeung.com/?p=19</guid>
		<description><![CDATA[Unlike C/C++ or older languages, Java helps user in managing their memory and auto release the unused memory. This process is named “Garbage Collection” (GC). In brief, GC consists of three steps: 1. Mark — Find all the unreachable memory, null-ed variables’ memory and mark. 2. Sweep — Clean-up all those marked memory. 3. Compact [...]]]></description>
			<content:encoded><![CDATA[<p>Unlike C/C++ or older languages, Java helps user in managing their memory and auto release the unused memory.<br />
This process is named “Garbage Collection” (GC). In brief, GC consists of three steps:</p>
<p style="padding-left: 30px;">1. Mark — Find all the unreachable memory, null-ed variables’ memory and mark.<br />
2. Sweep — Clean-up all those marked memory.<br />
3. Compact — Rearrange the data in the memory so as to provide more constiguous spaces.</p>
<p>In Java, there provides 6 algorithm of GC:</p>
<h3>For Young Sections:</h3>
<p style="padding-left: 30px;">1. Plain copying (DEFAULT)<br />
2. Parallel copying<br />
3. Parallel scavenging</p>
<h3>For Old Memory Sections:</h3>
<p style="padding-left: 30px;">1. Plain mark-sweep (DEFAULT)<br />
2. Incremental collector<br />
3. Concurrent mark-sweep</p>
<p>(*note that, the heap managed by java divides into three main area: Young — where they will be cleaned very soon; Old — where they will be kept for sometimes; and Perm — where they are not to be cleaned.</p>
<p>If ones’ program is very computational demanding or memory draining, one could inspect and pick the most suitable GC Algorithm.<br />
Profiling Tool</p>
<p>There are actually tools bundled with JDK to help us identify the memory usage and performance of a program.<br />
They are jStat and jConsole (Only bundled with JDK5 or above).</p>
<p style="padding-left: 30px;">1. To use jStat, you need to provide certain parameter to make it works.</p>
<p style="padding-left: 30px;">
<pre class="brush:bash">jStat -gc -t &lt;vmid&gt; &lt;interval&gt;</pre>
</p>
<p>vmid is the GUID for Virtual Machine, while interval states the time between each polling.<br />
*One could make use of jps to locate the JAVA program’s vmid.</p>
<p style="padding-left: 30px;">2. To use jConsole, it’s simple. Its GUI will lead you thru.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2009/09/06/garbage-collection-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
