<?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>Software Evolved &#187; Uncategorized</title>
	<atom:link href="http://www.peterfranza.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.peterfranza.com</link>
	<description></description>
	<lastBuildDate>Wed, 29 Sep 2010 17:04:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>GWT ScrollPanel for Touch Screens</title>
		<link>http://www.peterfranza.com/2010/07/15/gwt-scrollpanel-for-touch-screens/</link>
		<comments>http://www.peterfranza.com/2010/07/15/gwt-scrollpanel-for-touch-screens/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 18:27:37 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[gwt]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=508</guid>
		<description><![CDATA[Getting this working is easier than you think. Basically the only tricky bit is that the touch object allows for multiple fingers they are represented in the array &#8216;touches[]&#8216;. Since we are only scrolling we only need to worry about the first finger which is why I use touches[0].screenY. Enjoy. 1 2 3 4 5 [...]]]></description>
			<content:encoded><![CDATA[<p>Getting this working is easier than you think.  Basically the only tricky bit is that the touch object allows for multiple fingers they are represented in the array &#8216;touches[]&#8216;. Since we are only scrolling we only need to worry about the first finger which is why I use touches[0].screenY.  Enjoy.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
</pre></td><td class="code"><pre class="" style="font-family:monospace;">package com.peterfranza.gwt.mobileui.client.widgets;
&nbsp;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.ui.ScrollPanel;
&nbsp;
/**
 * 
 * @author peter.franza
 *
 */
public class TouchScrollPanel extends ScrollPanel <span class="br0">&#123;</span>
&nbsp;
	private int initialTouchX = -<span style="">1</span>;
	private int initialTouchY = -<span style="">1</span>;
	private int initialHorizontalOffset;
	private int initialVerticalOffset;
	private boolean moved = false;
&nbsp;
	<span class="br0">&#123;</span>
		attachTouch<span class="br0">&#40;</span>getElement<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>
&nbsp;
	public TouchScrollPanel<span class="br0">&#40;</span>VerticalPanel body<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		super<span class="br0">&#40;</span>body<span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>
&nbsp;
	private native void attachTouch<span class="br0">&#40;</span>JavaScriptObject ele<span class="br0">&#41;</span> /*-<span class="br0">&#123;</span>	
		var ref = this;
		ele.ontouchstart = function<span class="br0">&#40;</span>evt<span class="br0">&#41;</span><span class="br0">&#123;</span>
	  		evt.preventDefault<span class="br0">&#40;</span><span class="br0">&#41;</span>;	
	  		ref.@com.peterfranza.gwt.mobileui.client.widgets.TouchScrollPanel::setInitialTouch<span class="br0">&#40;</span>II<span class="br0">&#41;</span><span class="br0">&#40;</span>evt.touches<span class="br0">&#91;</span><span style="">0</span><span class="br0">&#93;</span>.screenX, evt.touches<span class="br0">&#91;</span><span style="">0</span><span class="br0">&#93;</span>.screenY<span class="br0">&#41;</span>;
		<span class="br0">&#125;</span>	
		ele.ontouchmove = function<span class="br0">&#40;</span>evt<span class="br0">&#41;</span><span class="br0">&#123;</span>
	  		evt.preventDefault<span class="br0">&#40;</span><span class="br0">&#41;</span>;	
	  		ref.@com.peterfranza.gwt.mobileui.client.widgets.TouchScrollPanel::doScroll<span class="br0">&#40;</span>II<span class="br0">&#41;</span><span class="br0">&#40;</span>evt.touches<span class="br0">&#91;</span><span style="">0</span><span class="br0">&#93;</span>.screenX, evt.touches<span class="br0">&#91;</span><span style="">0</span><span class="br0">&#93;</span>.screenY<span class="br0">&#41;</span>;
		<span class="br0">&#125;</span>		
		ele.ontouchend = function<span class="br0">&#40;</span>evt<span class="br0">&#41;</span><span class="br0">&#123;</span>
			evt.preventDefault<span class="br0">&#40;</span><span class="br0">&#41;</span>;
			ref.@com.peterfranza.gwt.mobileui.client.widgets.TouchScrollPanel::setEndTouch<span class="br0">&#40;</span>II<span class="br0">&#41;</span><span class="br0">&#40;</span>evt.pageX, evt.pageY<span class="br0">&#41;</span>;
		<span class="br0">&#125;</span>		
	<span class="br0">&#125;</span>-*/;
&nbsp;
	private native void fireClick<span class="br0">&#40;</span>int x, int y<span class="br0">&#41;</span> /*-<span class="br0">&#123;</span>	
		var theTarget = $doc.elementFromPoint<span class="br0">&#40;</span>x, y<span class="br0">&#41;</span>;
		if <span class="br0">&#40;</span>theTarget.nodeType == <span style="">3</span><span class="br0">&#41;</span> theTarget = theTarget.parentNode;
&nbsp;
		var theEvent = $doc.createEvent<span class="br0">&#40;</span>'MouseEvents'<span class="br0">&#41;</span>;
		theEvent.initEvent<span class="br0">&#40;</span>'click', true, true<span class="br0">&#41;</span>;
		theTarget.dispatchEvent<span class="br0">&#40;</span>theEvent<span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>-*/;
&nbsp;
&nbsp;
	@SuppressWarnings<span class="br0">&#40;</span>&quot;unused&quot;<span class="br0">&#41;</span>
	private void setInitialTouch<span class="br0">&#40;</span>int x, int y<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		initialVerticalOffset = getScrollPosition<span class="br0">&#40;</span><span class="br0">&#41;</span>;
		initialHorizontalOffset = getHorizontalScrollPosition<span class="br0">&#40;</span><span class="br0">&#41;</span>;
&nbsp;
		initialTouchX = x;
		initialTouchY = y;
		moved = false;
&nbsp;
	<span class="br0">&#125;</span>
&nbsp;
	@SuppressWarnings<span class="br0">&#40;</span>&quot;unused&quot;<span class="br0">&#41;</span>
	private void doScroll<span class="br0">&#40;</span>int x, int y<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		if <span class="br0">&#40;</span>initialTouchY != -<span style="">1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
			moved = true;
			int vDelta = initialTouchY - y;
			int hDelta = initialTouchX - x;
			setScrollPosition<span class="br0">&#40;</span>vDelta + initialVerticalOffset<span class="br0">&#41;</span>;
			setHorizontalScrollPosition<span class="br0">&#40;</span>hDelta + initialHorizontalOffset<span class="br0">&#41;</span>;
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
&nbsp;
	@SuppressWarnings<span class="br0">&#40;</span>&quot;unused&quot;<span class="br0">&#41;</span>
	private void setEndTouch<span class="br0">&#40;</span>int x, int y<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		if <span class="br0">&#40;</span>!moved<span class="br0">&#41;</span> <span class="br0">&#123;</span>
			fireClick<span class="br0">&#40;</span>x, y<span class="br0">&#41;</span>;
		<span class="br0">&#125;</span>
		initialTouchX = -<span style="">1</span>;
		initialTouchY = -<span style="">1</span>;
	<span class="br0">&#125;</span> 
<span class="br0">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2010/07/15/gwt-scrollpanel-for-touch-screens/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>GWT loading remote JSONP XSS</title>
		<link>http://www.peterfranza.com/2010/06/09/gwt-loading-remote-jsonp-xss/</link>
		<comments>http://www.peterfranza.com/2010/06/09/gwt-loading-remote-jsonp-xss/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 13:18:39 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[gwt]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=502</guid>
		<description><![CDATA[In almost all browsers there is one thing that everybody wants to do, but you just can&#8217;t, Load cross site data. That is you can&#8217;t unless you are using JSONP. Basically you load the data into its own script tag tell the server you are loading it from to wrap the data in a callback [...]]]></description>
			<content:encoded><![CDATA[<p>In almost all browsers there is one thing that everybody wants to do, but you just can&#8217;t, Load cross site data.  That is you can&#8217;t unless you are using JSONP.  Basically you load the data into its own script tag tell the server you are loading it from to wrap the data in a callback that you have defined.  This will fire an event after the data has loaded.  Perfect.</p>
<p>I wanted to use it in GWT &#8230; now I can.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="" style="font-family:monospace;">package com.peterfranza.gwt.cle.client;
&nbsp;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.RootPanel;
&nbsp;
public class JSONPLoader <span class="br0">&#123;</span>
&nbsp;
	private LoaderCallback callback;
&nbsp;
	public void load<span class="br0">&#40;</span>String jsonUrl, LoaderCallback callback<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		this.callback = callback;
		String callbackString = &quot;jsonLoad_&quot; + 
			DOM.createUniqueId<span class="br0">&#40;</span><span class="br0">&#41;</span>.replace<span class="br0">&#40;</span>&quot;-&quot;, &quot;_&quot;<span class="br0">&#41;</span> + &quot;_callback&quot;;
		String url = jsonUrl + <span class="br0">&#40;</span>jsonUrl.contains<span class="br0">&#40;</span>&quot;?&quot;<span class="br0">&#41;</span> ? &quot;&amp;&quot; : &quot;?&quot;<span class="br0">&#41;</span> 
			+ &quot;_callback=&quot; + callbackString;
		publishCallbackMethod<span class="br0">&#40;</span>callbackString<span class="br0">&#41;</span>;
		Element fr1 = DOM.createElement<span class="br0">&#40;</span>&quot;script&quot;<span class="br0">&#41;</span>;		
		fr1.setAttribute<span class="br0">&#40;</span>&quot;src&quot;, url<span class="br0">&#41;</span>;
		RootPanel.get<span class="br0">&#40;</span><span class="br0">&#41;</span>.getElement<span class="br0">&#40;</span><span class="br0">&#41;</span>.appendChild<span class="br0">&#40;</span>fr1<span class="br0">&#41;</span>;
&nbsp;
	<span class="br0">&#125;</span>
&nbsp;
	private native void publishCallbackMethod<span class="br0">&#40;</span>String callback<span class="br0">&#41;</span> /*-<span class="br0">&#123;</span>
		var ptr = this;
  		$wnd<span class="br0">&#91;</span>callback<span class="br0">&#93;</span> = function<span class="br0">&#40;</span>obj<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  			ptr.@com.peterfranza.gwt.cle.client.JSONPLoader::loadremotedata<span class="br0">&#40;</span>Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;<span class="br0">&#41;</span><span class="br0">&#40;</span>callback, obj<span class="br0">&#41;</span>;
  		<span class="br0">&#125;</span>;
	<span class="br0">&#125;</span>-*/;
&nbsp;
	@SuppressWarnings<span class="br0">&#40;</span>&quot;unused&quot;<span class="br0">&#41;</span>
	private void loadremotedata<span class="br0">&#40;</span>String cbp, JavaScriptObject obj<span class="br0">&#41;</span> <span class="br0">&#123;</span>
	  callback.onLoad<span class="br0">&#40;</span>new JSONObject<span class="br0">&#40;</span>obj<span class="br0">&#41;</span><span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>
&nbsp;
	public interface LoaderCallback <span class="br0">&#123;</span>
		void onLoad<span class="br0">&#40;</span>JSONObject object<span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p><strong>Usage:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="" style="font-family:monospace;">JSONPLoader loader = new JSONPLoader<span class="br0">&#40;</span><span class="br0">&#41;</span>;
loader.load<span class="br0">&#40;</span>&quot;http://pipes.yahoo.com/pipes/pipe.run?_id=CPreCvz42xG4eaOWouNLYQ&amp;_render=json&amp;location=madison&amp;section=mcy&quot;, new LoaderCallback<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
	@Override
	public void onLoad<span class="br0">&#40;</span>JSONObject object<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		Window.alert<span class="br0">&#40;</span>&quot;loaded &quot; + object.size<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span>;</pre></td></tr></table></div>

<p>Notice that the json url does not specify the _callback parameter, this is because it is added by the JSONPLoader class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2010/06/09/gwt-loading-remote-jsonp-xss/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Connecting Apache (httpd) To Active Directory</title>
		<link>http://www.peterfranza.com/2010/04/30/connecting-apache-httpd-to-active-directory/</link>
		<comments>http://www.peterfranza.com/2010/04/30/connecting-apache-httpd-to-active-directory/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 14:18:22 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[active directory]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[ldap]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=496</guid>
		<description><![CDATA[Recently I went through a small effort to connect a subversion repository to active directory. This is a good thing because it means that you no longer will need to manage the usernames and password using the old htpasswd format. The htpasswd is fine for very controlled environments but the passwords it allows you to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I went through a small effort to connect a subversion repository to active directory.  This is a good thing because it means that you no longer will need to manage the usernames and password using the old htpasswd format.  The htpasswd is fine for very controlled environments but the passwords it allows you to use are pretty weak and the encryption of the passwords is fairly weak also, so allowing the connection to happen using active directory as the authority is a good thing.  Also it keeps you from having to maintain more passwords, and I like that idea.</p>
<p><strong>In your /etc/httpd/conf.d/<em>filename</em>.conf</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="" style="font-family:monospace;">LoadModule dav_svn_module modules/mod_dav_svn.so
LoadModule authz_svn_module modules/mod_authz_svn.so
&nbsp;
LDAPConnectionTimeout <span style="">15</span>
&nbsp;
LDAPSharedCacheSize <span style="">200000</span>
LDAPCacheEntries <span style="">1024</span>
LDAPCacheTTL <span style="">600</span>
LDAPOpCacheEntries <span style="">1024</span>
LDAPOpCacheTTL <span style="">600</span>
&nbsp;
  &lt;Location /server/cache-info&gt;
   SetHandler ldap-status
  &lt;/Location&gt;
&nbsp;
  &lt;Location /svn&gt;
    DAV svn
    SVNPath /subversion/repos
    SVNListParentPath on
&nbsp;
    AuthzSVNAccessFile /subversion/svnauthorz
    Satisfy Any
    AuthType Basic
    AuthName &quot;Members Only&quot;
    AuthzLDAPAuthoritative off
    AuthBasicProvider ldap
    AuthLDAPBindDN &quot;svn.user@domain.com&quot;
    AuthLDAPBindPassword &quot;svn.user.password&quot;
    AuthLDAPURL &quot;ldap://&lt;ldapserverip&gt;/DC=domain,DC=com?sAMAccountName?sub?<span class="br0">&#40;</span>objectClass=user<span class="br0">&#41;</span>&quot;
    Require valid-user
 &lt;/Location&gt;</pre></td></tr></table></div>

<p>However every couple of transactions I would get an error and this message would appear in my error log</p>
<p><strong>/var/log/httpd/error_log</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="" style="font-family:monospace;"><span class="br0">&#91;</span>Fri Apr <span style="">30</span> 09:<span style="">21</span>:<span style="">46</span> <span style="">2010</span><span class="br0">&#93;</span> <span class="br0">&#91;</span>warn<span class="br0">&#93;</span> <span class="br0">&#91;</span>client 192.168.100.105<span class="br0">&#93;</span> <span class="br0">&#91;</span><span style="">22578</span><span class="br0">&#93;</span> auth_ldap authenticate: user peter.franza authentication failed; URI /svnad/projects/ <span class="br0">&#91;</span>ldap_search_ext_s<span class="br0">&#40;</span><span class="br0">&#41;</span> for user failed<span class="br0">&#93;</span><span class="br0">&#91;</span>Operations error<span class="br0">&#93;</span>, referer: http://svn/svnad/projects/</pre></td></tr></table></div>

<p>The solution was to disable following referers</p>
<p><strong>/etc/openldap/ldap.conf</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="" style="font-family:monospace;">REFERRALS off</pre></td></tr></table></div>

<p>*Note this is in <em>/etc/openldap/</em> not the ldap.conf in <em>/etc</em> that file is used from pam authentication and not for mod_ldap</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2010/04/30/connecting-apache-httpd-to-active-directory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Announcing &#8211; &#8220;Announce&#8221; for Android</title>
		<link>http://www.peterfranza.com/2010/02/03/announcing-announce-for-android/</link>
		<comments>http://www.peterfranza.com/2010/02/03/announcing-announce-for-android/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 03:29:53 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=479</guid>
		<description><![CDATA[Announce is an application for Android 2.0 and above.  It intercepts incoming calls and and reads the name of the caller aloud.  You can also record custom voice tags to use instead of the text to speech engine. Project Page: http://www.peterfranza.com/projects/announce-for-android/ Price: Free (That&#8217;s a good value) Enjoy.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.peterfranza.com/wp-content/uploads/2010/02/announce_qr.png" rel="lightbox[479]"><img class="size-full wp-image-473 alignright" title="announce_qr" src="http://www.peterfranza.com/wp-content/uploads/2010/02/announce_qr.png" alt="" width="135" height="135" /></a><a href="http://www.peterfranza.com/announce-for-android/">Announce</a> is an application for Android 2.0 and above.  It intercepts incoming calls and and reads the name of the caller aloud.  You can also record custom voice tags to use instead of the text to speech engine.</p>
<p><strong>Project Page: <a href="http://www.peterfranza.com/projects/announce-for-android/">http://www.peterfranza.com/projects/announce-for-android/</a></strong></p>
<p><strong>Price: Free </strong>(That&#8217;s a good value)<strong><br />
</strong></p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2010/02/03/announcing-announce-for-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BitArrayInputStream</title>
		<link>http://www.peterfranza.com/2009/10/09/bitarrayinputstream/</link>
		<comments>http://www.peterfranza.com/2009/10/09/bitarrayinputstream/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 14:43:31 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[inputstream]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=449</guid>
		<description><![CDATA[Sometime you just have to read the bits one by one at least now you don&#8217;t need to write your own class to do it. This class works for &#8216;Little Endian&#8217; or &#8216;Big Endian&#8217;. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [...]]]></description>
			<content:encoded><![CDATA[<p>Sometime you just have to read the bits one by one at least now you don&#8217;t need to write your own class to do it. This class works for &#8216;Little Endian&#8217; or &#8216;Big Endian&#8217;.<br />
<br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">import <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
import <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
&nbsp;
public class BitArrayInputStream <span style="color: #009900;">&#123;</span>
&nbsp;
	private final BitDirection direction<span style="color: #339933;">;</span>
	private final InputStream inputStream<span style="color: #339933;">;</span>
	private int currentByte<span style="color: #339933;">;</span>
	private int currentPosition <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
	public BitArrayInputStream<span style="color: #009900;">&#40;</span>InputStream inputStream<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		this<span style="color: #009900;">&#40;</span>BitDirection.<span style="color: #006633;">HIGHLOW</span>, inputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	public BitArrayInputStream<span style="color: #009900;">&#40;</span>BitDirection direction,
			InputStream inputStream<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		this.<span style="color: #006633;">inputStream</span> <span style="color: #339933;">=</span> inputStream<span style="color: #339933;">;</span>
		this.<span style="color: #006633;">direction</span> <span style="color: #339933;">=</span> direction<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	public int readBit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		if<span style="color: #009900;">&#40;</span>currentPosition <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			currentByte <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			if<span style="color: #009900;">&#40;</span>currentByte <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				return <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			currentPosition <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		int c <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
		if<span style="color: #009900;">&#40;</span>direction.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>BitDirection.<span style="color: #006633;">HIGHLOW</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			c <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>currentByte <span style="color: #339933;">&lt;&lt;</span> currentPosition <span style="color: #339933;">&amp;</span> 0x80<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> else <span style="color: #009900;">&#123;</span>
			c <span style="color: #339933;">=</span> currentByte <span style="color: #339933;">&gt;&gt;</span> currentPosition <span style="color: #339933;">&amp;</span> 0x1<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		currentPosition <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		if<span style="color: #009900;">&#40;</span>currentPosition <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			currentPosition <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		return c<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	public static enum BitDirection <span style="color: #009900;">&#123;</span>HIGHLOW, LOWHIGH<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And here is a unit test for it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">public class BitArrayInputStreamTest extends TestCase <span style="color: #009900;">&#123;</span>
&nbsp;
	public void testInit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws Exception <span style="color: #009900;">&#123;</span>
		assertNotNull<span style="color: #009900;">&#40;</span>new BitArrayInputStream<span style="color: #009900;">&#40;</span>null<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	public void testReadBits<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws Exception <span style="color: #009900;">&#123;</span>
		BitArrayInputStream i <span style="color: #339933;">=</span> new BitArrayInputStream<span style="color: #009900;">&#40;</span>
				new ByteArrayInputStream<span style="color: #009900;">&#40;</span>new byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#40;</span>byte<span style="color: #009900;">&#41;</span> 0xA3<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	public void testReadBitsLowHigh<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws Exception <span style="color: #009900;">&#123;</span>
		BitArrayInputStream i <span style="color: #339933;">=</span> new BitArrayInputStream<span style="color: #009900;">&#40;</span>BitDirection.<span style="color: #006633;">LOWHIGH</span>,
				new ByteArrayInputStream<span style="color: #009900;">&#40;</span>new byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#40;</span>byte<span style="color: #009900;">&#41;</span> 0xA3<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	public void testReadBitsEnd<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws Exception <span style="color: #009900;">&#123;</span>
		BitArrayInputStream i <span style="color: #339933;">=</span> new BitArrayInputStream<span style="color: #009900;">&#40;</span>
				new ByteArrayInputStream<span style="color: #009900;">&#40;</span>new byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#40;</span>byte<span style="color: #009900;">&#41;</span> 0xA3<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	public void testReadBitsMultiByte<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws Exception <span style="color: #009900;">&#123;</span>
		BitArrayInputStream i <span style="color: #339933;">=</span> new BitArrayInputStream<span style="color: #009900;">&#40;</span>
				new ByteArrayInputStream<span style="color: #009900;">&#40;</span>
                                  new byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#40;</span>byte<span style="color: #009900;">&#41;</span> 0xA3, <span style="color: #009900;">&#40;</span>byte<span style="color: #009900;">&#41;</span> 0xB6<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, i.<span style="color: #006633;">readBit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2009/10/09/bitarrayinputstream/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Look Mom, No Typing</title>
		<link>http://www.peterfranza.com/2009/06/27/look-mom-no-typing/</link>
		<comments>http://www.peterfranza.com/2009/06/27/look-mom-no-typing/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 20:25:57 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=406</guid>
		<description><![CDATA[Always wanted to leave a comment about something you&#8217;ve read on my site, but couldn&#8217;t be bothered to type a response. Well now you can using my new Google Voice Call widget. Now you don&#8217;t have to worry about carpel tunnel or any of that fancy spelling stuff. Really all you need is a mouth [...]]]></description>
			<content:encoded><![CDATA[<p>Always wanted to leave a comment about something you&#8217;ve read on my site, but couldn&#8217;t be bothered to type a response.  Well now you can using my new Google Voice Call widget.  Now you don&#8217;t have to worry about carpel tunnel or any of that fancy spelling stuff.  Really all you need is a mouth and a phone.<br />
<center><br />
<object type="application/x-shockwave-flash" data="https://clients4.google.com/voice/embed/webCallButton" width="230" height="85"><param name="movie" value="https://clients4.google.com/voice/embed/webCallButton" /><param name="wmode" value="transparent" /><param name="FlashVars" value="id=90835e93b32d9b8cb28b4b61c54ed3bea8b8b923&#038;style=0" /></object><br />
</center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2009/06/27/look-mom-no-typing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ignore Compiler Warnings</title>
		<link>http://www.peterfranza.com/2009/05/11/ignore-compiler-warnings/</link>
		<comments>http://www.peterfranza.com/2009/05/11/ignore-compiler-warnings/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:55:58 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=381</guid>
		<description><![CDATA[Let me be upfront, I will be the first person to tell you that you should treat a warning as if it were an error. The compiler is trying to tell you something, it wants to help you. The very smart people who are designing our compiler aren&#8217;t identifying code patterns and exposing them as [...]]]></description>
			<content:encoded><![CDATA[<p>Let me be upfront, I will be the first person to tell you that you should treat a warning as if it were an error.  The compiler is trying to tell you something, it wants to help you. The very smart people who are designing our compiler aren&#8217;t identifying code patterns and exposing them as warnings for their health, so come on people &#8230; <strong>FIX YOUR WARNINGS</strong>!</p>
<p>Ok, now that I got that out of the way, we come to the special case.  Third party source code generation tools, I actually love generated source, don&#8217;t ask me why but I find it much easier to deal with than all the mucking around you could do in meta-code etc. Just give me a plain &#8216;ol class that I can use.  The drawback to generated source is some of the generators are pretty rough when it comes to warnings. They might have unneeded imports, referenced untyped classes, who knows.  We all can agree that it would be optimal if the generators would just produce warning free code, and if it is in your power join up with some of these open-source projects and actually fix them (you&#8217;d be my hero).  </p>
<p>When that isn&#8217;t an option I&#8217;d just ignore them, I&#8217;m adding an ant task that can read in a fileset and append warning suppression annotations to the class headings.  Just remember you have not fixed anything, but to be perfectly practical you may never fix these things.</p>
<p><strong>Download Jar File:</strong> <a href="http://www.peterfranza.com/wp-content/uploads/2009/05/suppresswarnings-05.jar">suppresswarnings-05.jar</a></p>
<p><strong>Usage:</strong><br />
<code lang="xml"><taskdef resource="suppresswarnings.properties" classpath="${suppresswarnings.jarfile}"/></p>
<p><suppressWarnings><br />
  <fileset dir="${src.dir}"><br />
     <include name="**/*.java"/><br />
  </fileset><br />
</suppressWarnings></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2009/05/11/ignore-compiler-warnings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copying Files Using NIO</title>
		<link>http://www.peterfranza.com/2008/10/25/copying-files-using-nio/</link>
		<comments>http://www.peterfranza.com/2008/10/25/copying-files-using-nio/#comments</comments>
		<pubDate>Sun, 26 Oct 2008 02:11:09 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=275</guid>
		<description><![CDATA[Prior to the JDK 1.4 introduction of the NIO package a tipical file copy routine would look something like: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static void copyFile&#40;File in, File out&#41; throws Exception &#123; FileInputStream fis = new FileInputStream&#40;in&#41;; FileOutputStream fos = new FileOutputStream&#40;out&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>Prior to the JDK 1.4 introduction of the NIO package a tipical file copy routine would look something like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">public static void copyFile<span style="color: #009900;">&#40;</span>File in, File out<span style="color: #009900;">&#41;</span> throws Exception <span style="color: #009900;">&#123;</span>
    FileInputStream fis  <span style="color: #339933;">=</span> new FileInputStream<span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    FileOutputStream fos <span style="color: #339933;">=</span> new FileOutputStream<span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    try <span style="color: #009900;">&#123;</span>
        byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> buf <span style="color: #339933;">=</span> new byte<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        int i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        while <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> fis.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            fos.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>buf, <span style="color: #cc66cc;">0</span>, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        throw e<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> finally <span style="color: #009900;">&#123;</span>
        if <span style="color: #009900;">&#40;</span>fis <span style="color: #339933;">!=</span> null<span style="color: #009900;">&#41;</span> fis.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        if <span style="color: #009900;">&#40;</span>fos <span style="color: #339933;">!=</span> null<span style="color: #009900;">&#41;</span> fos.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>With the introduction of the NIO package&#8217;s conecpt of channels we can rewrite the fileCopy routine as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">public static void copyFile<span style="color: #009900;">&#40;</span>File in, File out<span style="color: #009900;">&#41;</span> 
        throws IOException  <span style="color: #009900;">&#123;</span>
        FileChannel inChannel <span style="color: #339933;">=</span> new
            FileInputStream<span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getChannel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        FileChannel outChannel <span style="color: #339933;">=</span> new
            FileOutputStream<span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getChannel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        try <span style="color: #009900;">&#123;</span>
            inChannel.<span style="color: #006633;">transferTo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, inChannel.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
                    outChannel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>IOException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            throw e<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> finally <span style="color: #009900;">&#123;</span>
            if <span style="color: #009900;">&#40;</span>inChannel <span style="color: #339933;">!=</span> null<span style="color: #009900;">&#41;</span> inChannel.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            if <span style="color: #009900;">&#40;</span>outChannel <span style="color: #339933;">!=</span> null<span style="color: #009900;">&#41;</span> outChannel.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>However on windows platforms, you may have to replace</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
            inChannel.<span style="color: #006633;">transferTo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, inChannel.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
                    outChannel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>with</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
           <span style="color: #666666; font-style: italic;">// magic number for Windows, 64Mb - 32Kb)</span>
           int maxCount <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">64</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">32</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           long size <span style="color: #339933;">=</span> inChannel.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           long position <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
           while <span style="color: #009900;">&#40;</span>position <span style="color: #339933;">&lt;</span> size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              position <span style="color: #339933;">+=</span> 
                inChannel.<span style="color: #006633;">transferTo</span><span style="color: #009900;">&#40;</span>position, maxCount, outChannel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>when attempting to copy a file in excess of 64Mb.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2008/10/25/copying-files-using-nio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dependency Injection Basics</title>
		<link>http://www.peterfranza.com/2008/10/06/dependency-injection-basics/</link>
		<comments>http://www.peterfranza.com/2008/10/06/dependency-injection-basics/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 02:39:08 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Inversion Of Control]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=230</guid>
		<description><![CDATA[Dependency Injection is a process of supplying external dependencies to componenets, changing the flow of control of the system to be inverted in comparison to the traditional architecture of software libraries. Dependency Injection builds of the concept that you should favor composition over inheritance [Effective Java Programming Language Guide, chapter 4, item 14], but extending [...]]]></description>
			<content:encoded><![CDATA[<p>Dependency Injection is a  process of supplying external dependencies to componenets, changing the flow of control of the system to be inverted in comparison to the traditional architecture of software libraries. Dependency Injection builds of the concept that you should favor composition over inheritance <em><span style="font-size: xx-small;">[Effective Java Programming Language Guide, chapter 4, item 14]</span></em>, but extending it to say that the specific implementations of the subcomponenets that you are using to compose your class should be fully decoupled from the class itself.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">public interface BrakeSystem <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
public interface Engine <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
public class Car <span style="color: #009900;">&#123;</span>
&nbsp;
 private Transmission transmission <span style="color: #339933;">=</span> new AutomaticTransmission<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 private Engine brakes <span style="color: #339933;">=</span> new GasEngine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We can see that in class Car we&#8217;ve coupled the implementation of AutomaticTransmission and DiscBrakeSystem to the implementation of Car.  What do we do when we want to create an instance of Car that has a manual Transmission?  The answer is dependency injection.  In this example we find it trivial to remove the coupling of subcomponent implementations by passing in the subcomponents at time of construction rather than at the time of compiliation.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">public class Car <span style="color: #009900;">&#123;</span>
&nbsp;
 private Transmission transmission<span style="color: #339933;">;</span>
 private Engine engine<span style="color: #339933;">;</span>
&nbsp;
 public Car<span style="color: #009900;">&#40;</span>Transmission transmission, Engine engine<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    this.<span style="color: #006633;">transmission</span> <span style="color: #339933;">=</span> transmission<span style="color: #339933;">;</span>
    this.<span style="color: #006633;">engine</span> <span style="color: #339933;">=</span> engine<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now we can use the constructor to create variations of the cars behaviour.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">public class CarFactory <span style="color: #009900;">&#123;</span>
&nbsp;
 public static final Car createAutomaticCar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 return new Car<span style="color: #009900;">&#40;</span>new AutomaticTransmission<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
 new GasEngine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 public static final Car createManualCar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 return new Car<span style="color: #009900;">&#40;</span>new ManualTransmission<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
 new GasEngine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Using this method we can vary the components of car without subclassing it, allowing the freedom to provide numerious unique representations of cars without changing anything in the car class itself. Its all left to wiring the classes together in an appropriate way.</p>
<p><strong>Can&#8217;t I just use the Service Locator Pattern?</strong></p>
<p>Well it is true that dependency injection does aim to solve the same problem as the Service Locator Pattern, usage of the pattern requires you to build in logic that preforms the dependency resolution and construction.  This introduces cross cutting behaviour that disrupts the cohesion and singular responsibility of the class.  By removing the service location logic and relegating it into a factory or some other framework level function, a high level of cohesion can be maintained. This will be true for both the object and its factory, as the single responsibility of the factory is to construct instances of the class, and the class just has to be itself.</p>
<p><strong>Usage of Dependency Injection Frameworks</strong></p>
<p>I sit firmly on the fence about the usage of frameworks for dependency injection, becuase it seems that they all have a dark side to them that in one way or another reduces readability/traceability of the classes.  Some like <a href="http://code.google.com/p/google-guice/" target="_blank">Google Guice</a> use method annotations to define where the injection points are, and while this seems to be be a relativly consice way to inform the  framework it adds a lot of extranious information to your code that doesn&#8217;t really make sense within the context of the class.</p>
<p>This is where I actually like the use of factories that construct the objects and do the injection for you.  Its generally clean, and it localizes the couplings to a factory which is natually where they should be.  The downside is sometimes dependency injection can get several layers deep (you have a class that requires classes which requre classes etc) and the notion that you would have a factory method for each one of the permutations is quite excessive.  However if you only add a method that builds the specific combination that you are looking for this might strike a nice middle ground.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2008/10/06/dependency-injection-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Little Endian Input Stream</title>
		<link>http://www.peterfranza.com/2008/09/26/little-endian-input-stream/</link>
		<comments>http://www.peterfranza.com/2008/09/26/little-endian-input-stream/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 18:18:50 +0000</pubDate>
		<dc:creator>pfranza</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://www.peterfranza.com/?p=208</guid>
		<description><![CDATA[As everybody probably knows, Java only supports Big Endian streams but every once and a while you find yourself in the predicament of wanting to interface with a legacy system that wants to use little endian encoding. So I&#8217;m attaching a class that will allow you to use the nice DataInputStream API but that will [...]]]></description>
			<content:encoded><![CDATA[<p>As everybody probably knows, Java only supports Big Endian streams but every once and a while you find yourself in the predicament of wanting to interface with a legacy system that wants to use little endian encoding.  So I&#8217;m attaching a class that will allow you to use the nice DataInputStream API but that will read the data in little endian format.</p>
<p><em>*On a side note if you don&#8217;t know the difference between big and little endian or what the etymology of the term is.  Endianess is basically to do start reading from the left most or right most bit (assuming bits were arranged in this fashion) you can find more information <a href="http://en.wikipedia.org/wiki/Endianness#Examples_of_storing_the_value_0x0A0B0C0D_in_memory">here</a>. The term comes from Jonathan Swift&#8217;s satirical novel Gulliver’s Travels where there was a large disagreement about which side of one&#8217;s soft-boiled egg you should open.</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">public class LittleEndianDataInputStream extends InputStream implements DataInput <span style="color: #009900;">&#123;</span>
&nbsp;
	public LittleEndianDataInputStream<span style="color: #009900;">&#40;</span>InputStream in<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		this.<span style="color: #006633;">in</span> <span style="color: #339933;">=</span> in<span style="color: #339933;">;</span>
		this.<span style="color: #006633;">d</span> <span style="color: #339933;">=</span> new DataInputStream<span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		w <span style="color: #339933;">=</span> new byte<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	public int available<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		return d.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	public final short readShort<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException
	<span style="color: #009900;">&#123;</span>
		d.<span style="color: #006633;">readFully</span><span style="color: #009900;">&#40;</span>w, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		return <span style="color: #009900;">&#40;</span>short<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>
				<span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">8</span> <span style="color: #339933;">|</span>
				<span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Note, returns int even though it reads a short.
	 */</span>
	 public final int readUnsignedShort<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException
	 <span style="color: #009900;">&#123;</span>
		 d.<span style="color: #006633;">readFully</span><span style="color: #009900;">&#40;</span>w, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 return <span style="color: #009900;">&#40;</span>
				 <span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">8</span> <span style="color: #339933;">|</span>
				 <span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 <span style="color: #008000; font-style: italic; font-weight: bold;">/**
	  * like DataInputStream.readChar except little endian.
	  */</span>
	 public final char readChar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException
	 <span style="color: #009900;">&#123;</span>
		 d.<span style="color: #006633;">readFully</span><span style="color: #009900;">&#40;</span>w, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 return <span style="color: #009900;">&#40;</span>char<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>
				 <span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">8</span> <span style="color: #339933;">|</span>
				 <span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 <span style="color: #008000; font-style: italic; font-weight: bold;">/**
	  * like DataInputStream.readInt except little endian.
	  */</span>
	 public final int readInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException
	 <span style="color: #009900;">&#123;</span>
		 d.<span style="color: #006633;">readFully</span><span style="color: #009900;">&#40;</span>w, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 return
		 <span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>      <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">|</span>
		 <span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">16</span> <span style="color: #339933;">|</span>
		 <span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span>  <span style="color: #cc66cc;">8</span> <span style="color: #339933;">|</span>
		 <span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 <span style="color: #008000; font-style: italic; font-weight: bold;">/**
	  * like DataInputStream.readLong except little endian.
	  */</span>
	 public final long readLong<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException
	 <span style="color: #009900;">&#123;</span>
		 d.<span style="color: #006633;">readFully</span><span style="color: #009900;">&#40;</span>w, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 return
		 <span style="color: #009900;">&#40;</span>long<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>      <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">56</span> <span style="color: #339933;">|</span> 
		 <span style="color: #009900;">&#40;</span>long<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">48</span> <span style="color: #339933;">|</span>
		 <span style="color: #009900;">&#40;</span>long<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">40</span> <span style="color: #339933;">|</span>
		 <span style="color: #009900;">&#40;</span>long<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">32</span> <span style="color: #339933;">|</span>
		 <span style="color: #009900;">&#40;</span>long<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">|</span>
		 <span style="color: #009900;">&#40;</span>long<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">16</span> <span style="color: #339933;">|</span>
		 <span style="color: #009900;">&#40;</span>long<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span>  <span style="color: #cc66cc;">8</span> <span style="color: #339933;">|</span>
		 <span style="color: #009900;">&#40;</span>long<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>w<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span>0xff<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final float readFloat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return Float.<span style="color: #006633;">intBitsToFloat</span><span style="color: #009900;">&#40;</span>readInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final double readDouble<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return Double.<span style="color: #006633;">longBitsToDouble</span><span style="color: #009900;">&#40;</span>readLong<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final int read<span style="color: #009900;">&#40;</span>byte b<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, int off, int len<span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return in.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>b, off, len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final void readFully<span style="color: #009900;">&#40;</span>byte b<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 d.<span style="color: #006633;">readFully</span><span style="color: #009900;">&#40;</span>b, <span style="color: #cc66cc;">0</span>, b.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final void readFully<span style="color: #009900;">&#40;</span>byte b<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, int off, int len<span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 d.<span style="color: #006633;">readFully</span><span style="color: #009900;">&#40;</span>b, off, len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final int skipBytes<span style="color: #009900;">&#40;</span>int n<span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return d.<span style="color: #006633;">skipBytes</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final boolean readBoolean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return d.<span style="color: #006633;">readBoolean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final byte readByte<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return d.<span style="color: #006633;">readByte</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public int read<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return in.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final int readUnsignedByte<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return d.<span style="color: #006633;">readUnsignedByte</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 @Deprecated
	 public final String readLine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return d.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final String readUTF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 return d.<span style="color: #006633;">readUTF</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 public final void close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws IOException <span style="color: #009900;">&#123;</span>
		 d.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 private DataInputStream d<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// to get at high level readFully methods of</span>
	 <span style="color: #666666; font-style: italic;">// DataInputStream</span>
	 private InputStream in<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// to get at the low-level read methods of</span>
	 <span style="color: #666666; font-style: italic;">// InputStream</span>
	 private byte w<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// work array for buffering input</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Download:</strong> <a href="http://www.peterfranza.com/wp-content/uploads/2008/09/littleendiandatainputstream.java">LittleEndianDataInputStream.java</a></p>
<p>enjoy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfranza.com/2008/09/26/little-endian-input-stream/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

