<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.2" -->
<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/"
	>

<channel>
	<title>Oliver Kaljuvee</title>
	<link>http://oliver.kaljuvee.info/blog</link>
	<description>Weblog by Oliver Kaljuvee about software, technology, finance, and life.</description>
	<pubDate>Sat, 03 Jul 2010 01:16:50 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>
	<language>en</language>
			<item>
		<title>Java 4 Ever</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2010/07/02/java-4-ever/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2010/07/02/java-4-ever/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 01:12:43 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>humor</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2010/07/02/java-4-ever/</guid>
		<description><![CDATA[Pretty much the story my life&#8230;





]]></description>
			<content:encoded><![CDATA[<p>Pretty much the story my life&#8230;<br />
<object width="560" height="336"><br />
<param name="movie" value="http://www.youtube.com/v/A1zySeNpW20&amp;hl=en_US&amp;fs=1"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/A1zySeNpW20&amp;hl=en_US&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="336"></embed></object>
</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2010/07/02/java-4-ever/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Medical Ethics by Specialty</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2010/01/16/medical-ethics-by-specialty/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2010/01/16/medical-ethics-by-specialty/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 23:04:51 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>humor</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2010/01/16/medical-ethics-by-specialty/</guid>
		<description><![CDATA[Four doctors went duck hunting together: a family practitioner, a gynecologist, a surgeon, and a pathologist.  As a bird flew overhead, the family practitioner started tho shoot but decided not to because he wasn&#8217;t absolutely sure it was a duck.  The gynecologist also prepared to shoot, but lowered his gun when he realized [...]]]></description>
			<content:encoded><![CDATA[<p>Four doctors went duck hunting together: a family practitioner, a gynecologist, a surgeon, and a pathologist.  As a bird flew overhead, the family practitioner started tho shoot but decided not to because he wasn&#8217;t absolutely sure it was a duck.  The gynecologist also prepared to shoot, but lowered his gun when he realized that he didn&#8217;t know whether it was a male or female duck.  The surgeon, meanwhile, blew the bird out of the sky, turned to the pathologist and said: &#8220;Go see if that was a duck.&#8221;
</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2010/01/16/medical-ethics-by-specialty/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Lawyer Ethics</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2010/01/16/lawyer-ethics-via-humor/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2010/01/16/lawyer-ethics-via-humor/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 23:01:37 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>humor</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2010/01/16/lawyer-ethics-via-humor/</guid>
		<description><![CDATA[Even lawyers have ethics.  If a client mistakenly gives a lawyer $400 to pay a $300 bill, the ethical question that naturally arises is whether the lawyer should tell his partner.

]]></description>
			<content:encoded><![CDATA[<p>Even lawyers have ethics.  If a client mistakenly gives a lawyer $400 to pay a $300 bill, the ethical question that naturally arises is whether the lawyer should tell his partner.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2010/01/16/lawyer-ethics-via-humor/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Motivation through Fear</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2009/12/27/motivation-through-fear/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2009/12/27/motivation-through-fear/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 18:02:11 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>humor</category>
	<category>finance</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2009/12/27/motivation-through-fear/</guid>
		<description><![CDATA[This is very appropriate given the times and given the fact that I too once worked for Lehman Brothers:



]]></description>
			<content:encoded><![CDATA[<p>This is very appropriate given the times and given the fact that I too once worked for Lehman Brothers:<br />
<br/><br />
<img id="image45" src="http://oliver.kaljuvee.info/blog/wp-content/uploads/2009/12/dilbert-lb.jpg">
</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2009/12/27/motivation-through-fear/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>The Argument from Analogy</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2009/09/23/the-argument-from-analogy/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2009/09/23/the-argument-from-analogy/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 03:48:15 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>random</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2009/09/23/the-argument-from-analogy/</guid>
		<description><![CDATA[The ninety-year-old man went to the doctor and said, &#8220;Doctor, my eighteen-year-old wife is expecting a baby.&#8221;  The doctor said, &#8220;Well, let me tell you a story.  A man went hunting, but instead of a gun, he picked up an umbrella by mistake.  When a bear suddenly charged at his wife who [...]]]></description>
			<content:encoded><![CDATA[<p>The ninety-year-old man went to the doctor and said, &#8220;Doctor, my eighteen-year-old wife is expecting a baby.&#8221;  The doctor said, &#8220;Well, let me tell you a story.  A man went hunting, but instead of a gun, he picked up an umbrella by mistake.  When a bear suddenly charged at his wife who was accompanying him in the woods, the man aimed the umbrella, shot the bear, and killed it!&#8221;  The main said, &#8220;Impossible!  Someone else must have shot the bear!&#8221;  The doctor said, &#8220;My point exactly.&#8221;
</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2009/09/23/the-argument-from-analogy/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Semantic Paradox</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2009/09/23/semantic-paradox/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2009/09/23/semantic-paradox/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 03:43:56 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>random</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2009/09/23/semantic-paradox/</guid>
		<description><![CDATA[There is a town in which the only barber, a man, shaves all the townsmen who do not shave themselves.  Does the barber shave himself?  If he does, he doesn&#8217;t.  If he doesn&#8217;t, he does.  Makes sense?

]]></description>
			<content:encoded><![CDATA[<p>There is a town in which the only barber, a man, shaves all the townsmen who do not shave themselves.  Does the barber shave himself?  If he does, he doesn&#8217;t.  If he doesn&#8217;t, he does.  Makes sense?
</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2009/09/23/semantic-paradox/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>&#8220;Ma&#8217;, look no streams!&#8221;</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2009/03/12/ma-look-no-streams/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2009/03/12/ma-look-no-streams/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 06:20:27 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>random</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2009/03/12/ma-look-no-streams/</guid>
		<description><![CDATA[
Looking for efficient ways of loading files that run in the GB realm, in Java?


I recently played around with Java&#8217;s NIO API, which was introduced in JDK 1.4.  As you may know, the traditional IO classes deal with streams, whereas the NIO API deals with block-oriented approach, which makes it considerably faster because the [...]]]></description>
			<content:encoded><![CDATA[<p>
Looking for efficient ways of loading files that run in the GB realm, in Java?
</p>
<p>
I recently played around with Java&#8217;s NIO API, which was introduced in JDK 1.4.  As you may know, the traditional IO classes deal with streams, whereas the NIO API deals with block-oriented approach, which makes it considerably faster because the filling and draining of buffers is handled by OS, not JVM.
</p>
<p>
Anyway, what really caught my attention in the API, was the java.nio.MappedByteBuffer class.  Memory-mapped IO means that data in a file can directly mapped to physical memory.  Nothing new in the OS world, but this was previously not possible in Java.  Just be aware that when dealing with writing, you are dealing directly with the disk memory - there is no separation between modifying the data and saving it to the disk (as in the traditional IO).
</p>
<p>
I&#8217;ve written a sample code snippet that uses MappedByteBuffer.  The sample code maps the file in increments of 1 kB (block size), just for the <i>demonstrative purposes</i>.  If you are dealing with files less than ~1.6GB on Windows or up to 2GB files on the Unix-based OS, you can comfortably make the block size equal to the file size and the entire file would get mapped at once.  (Read more about the <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6416884">limit-related issues</a> which will be addressed in JDK 7).  I was trying to process a 10GB file, so I had to map the file in &#8220;sliding&#8221; blocks.
</p>
<p>
Note, however, that mapping file channels directly into memory makes sense only when dealing with very large files.  There is no significant performance improvement when dealing with small files.  Since the release of JDK 1.4, the java.io classes are actually implemented by the java.nio classes.
</p>
<p>Below is a sample method that maps 1 kB at a time (again, this does not make sense in practice; you can comfortably map 1 GB files at once on any platform).  The while-loop executes as long as there is still areas of the file that need to be mapped.  The map method is the key, which returns the MappedByteBuffer.  In order to make the contents into human-readable format, the buffer gets wrapped by a CharBuffer.  Each line in the file, get stored into a class variable lines, which is of type List (see full source for details).
</p>
<pre name="code" class="java">
public void load() throws IOException {
	FileInputStream fis = new FileInputStream(fileName);
	FileChannel fc = fis.getChannel();
	MappedByteBuffer mbb = null;
	Charset cs = Charset.forName("ISO-8859-1");
	CharsetDecoder decoder = cs.newDecoder();
	StringBuilder sb = new StringBuilder();

	long bs = 1024L; // block size
	long fs = fc.size(); // file size
	long t = 0L; // total size

	if (fs != 0) {
		while (t < fs) {
			if (t + bs > fs) {
				bs = fs - t;
			}
			mbb = fc.map(FileChannel.MapMode.READ_ONLY, t, bs);
			CharBuffer cb = decoder.decode(mbb);

			while (cb.hasRemaining()) {
				char c = cb.get();

				if (c == &#8216;n&#8217;) {
					lines.add(sb.toString());
					sb = new StringBuilder();
				} else if (c == &#8216;r&#8217;) { // Windows
					continue;
				} else {
					sb.append(c);
				}
			}
			t += bs;
		}
		// The last line may not have ended with n
		if (sb.length() > 0) {
			lines.add(sb.toString());
		}
	}
}
</pre>
<p>The complete source code of the sample class: <a href="http://oliver.kaljuvee.info/blog/wp-content/uploads/2009/MemoryMappingReader.java">MemoryMappingReader.java</a>.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2009/03/12/ma-look-no-streams/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Infinite Humor</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2009/02/12/infinite-humor/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2009/02/12/infinite-humor/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 06:00:37 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>random</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2009/02/12/infinite-humor/</guid>
		<description><![CDATA[Two cows are standing in the pasture. After a while, one turns to the other and says, &#8220;Do you realize that although pi is usually abbreviated to four decimal places, it actually goes to infinity?&#8221; And the other cow replies, &#8220;Moo.&#8221;

]]></description>
			<content:encoded><![CDATA[<p>Two cows are standing in the pasture. After a while, one turns to the other and says, &#8220;Do you realize that although pi is usually abbreviated to four decimal places, it actually goes to infinity?&#8221; And the other cow replies, &#8220;Moo.&#8221;
</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2009/02/12/infinite-humor/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Infinite Regress</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2009/01/24/infinite-regress/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2009/01/24/infinite-regress/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 05:41:31 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>random</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2009/01/24/infinite-regress/</guid>
		<description><![CDATA[When debating the beginning of the universe, many often hide behind the infamous infinite regress idea.  In other words, in order to explain the existence of the world by positing a &#8220;maker&#8221; raises the question of how to explain the existence of the maker. If another maker is posited, the question becomes, &#8220;Who made [...]]]></description>
			<content:encoded><![CDATA[<p>When debating the beginning of the universe, many often hide behind the infamous infinite regress idea.  In other words, in order to explain the existence of the world by positing a &#8220;maker&#8221; raises the question of how to explain the existence of the maker. If another maker is posited, the question becomes, &#8220;Who made the maker?&#8221; And so on, or &#8220;ad nauseam,&#8221; whichever comes first.</p>
<p>My response to the very first cause of the cosmos? Just pull Lennon&#8217;s <i>creatio ex nihilo</i> (creation out of nothing) argument: </p>
<p>&#8220;Before Elvis, there was nothing.&#8221; &#8212; John Lennon</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2009/01/24/infinite-regress/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Rational Humor</title>
		<link>http://oliver.kaljuvee.info/blog/index.php/2009/01/24/rational-humor/</link>
		<comments>http://oliver.kaljuvee.info/blog/index.php/2009/01/24/rational-humor/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 05:26:39 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
		
	<category>random</category>
		<guid isPermaLink="false">http://oliver.kaljuvee.info/blog/index.php/2009/01/24/rational-humor/</guid>
		<description><![CDATA[The anecdote is motivated by Leibniz&#8217;s ideas&#8211;he was neither an optimist nor a pessimist, but merely a neutral rationalist.
Optimist: The glass is half full.
Pessimist: The glass is half empty.
Rationalist: The glass is twice as big as it needs to be.

]]></description>
			<content:encoded><![CDATA[<p>The anecdote is motivated by Leibniz&#8217;s ideas&#8211;he was neither an optimist nor a pessimist, but merely a neutral rationalist.</p>
<p>Optimist: The glass is half full.<br />
Pessimist: The glass is half empty.<br />
Rationalist: The glass is twice as big as it needs to be.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://oliver.kaljuvee.info/blog/index.php/2009/01/24/rational-humor/feed/</wfw:commentRSS>
		</item>
	</channel>
</rss>
