<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Almighty Bus Error</title>
 <link href="http://almightybuserror.com/atom.xml" rel="self"/>
 <link href="http://almightybuserror.com/"/>
 <updated>2010-01-22T19:27:28+00:00</updated>
 <id>http://almightybuserror.com/</id>
 <author>
   <name>Fernando Alexandre</name>
   <email>kernl@almightybuserror.com</email>
 </author>

 
 <entry>
   <title>Snippet.Java: Using FilenameFilter</title>
   <link href="http://almightybuserror.com/2009/12/15/using-filenamefilter.html"/>
   <updated>2009-12-15T10:47:00+00:00</updated>
   <id>http://almightybuserror.com/2009/12/15/using-filenamefilter</id>
   <content type="html">&lt;p&gt;To use a &lt;em&gt;FilenameFilter&lt;/em&gt; it is needed to implement a method &lt;em&gt;accept&lt;/em&gt; which has as arguments the directory where the file exists and the name of the file. It is normally used in conjuction with the &lt;em&gt;list&lt;/em&gt; method of the &lt;em&gt;File&lt;/em&gt; object.&lt;/p&gt;
&lt;p&gt;The following example prints all the files with extension &amp;#8220;jpg&amp;#8221; in a certain directory:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public static void main(String[] args) {
    FilenameFilter filter = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(&quot;.jpg&quot;);
        }
    };

    for(String c : args) {
        File dir = new File(c);

        if(dir.isDirectory()) {
            System.out.println(&quot;Directory: &quot; + c);
            for(String curr : dir.list(filter))
                System.out.println(curr);
        }
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;More information can be found &lt;a href=&quot;http://java.sun.com/j2se/1.4.2/docs/api/java/io/FilenameFilter.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thank you for reading!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Drawing Splines in openGL</title>
   <link href="http://almightybuserror.com/2009/12/04/drawing-splines-in-opengl.html"/>
   <updated>2009-12-04T19:04:00+00:00</updated>
   <id>http://almightybuserror.com/2009/12/04/drawing-splines-in-opengl</id>
   <content type="html">&lt;p&gt;This article will cover the basics of drawing bezier curves in openGL and how to convert from other curves to Bezier, such as Bspline and Catmull-Rom curves.&lt;/p&gt;
&lt;h4&gt;Declaring control Points&lt;/h4&gt;
&lt;p&gt;The way to declare a list of control points for openGL usage is an one dimensional matrix. Since a curve segment only has in account 4 control points:&lt;/p&gt;
&lt;div style=&quot;float:right; margin-left:auto; margin: 0px; padding:1em&quot;&gt;
&lt;table style=&quot;color:#000&quot;&gt;&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;cntlPoints[]&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;=&lt;/td&gt;
&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0px style=&quot;border-left:1px solid #000; border-right:1px solid #000; color:#000&quot;&gt;&lt;tr&gt;
&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0 style=&quot;color:#000;&quot;&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p0x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p0y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p0z&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p1x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p1y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p1z&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p2x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p2y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p2z&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p3x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p3y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p3z&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;&lt;p&gt;A Bezier curve only needs the last point of the previous curve to be continuous. Catmull-Rom and Bspline need to know the last 3 points from the previous curve to be continuous.&lt;/p&gt;
&lt;p&gt;In the case you have more than 4 points, lets say 7, you would draw 2 curves from p0 to p3 and p3 to p6 in case of a Bezier curve and 5 curves in Catmull-Rom and Bspline going from p0 to p3, p1 to p4, p2 to p5 and p3 to p6 for them to become continous.&lt;/p&gt;
&lt;h4&gt;Drawing a Bezier curve in openGL&lt;/h4&gt;
&lt;div style=&quot;float:right; margin-left:auto; margin: 0px; padding:1em&quot;&gt;
&lt;table style=&quot;color:#000&quot;&gt;&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;mBezier[]&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;=&lt;/td&gt;
&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0px style=&quot;border-left:1px solid #000; border-right:1px solid #000; color:#000&quot;&gt;&lt;tr&gt;
&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0 style=&quot;color:#000;&quot;&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;-1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;-3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;-6&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;-3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;&lt;p&gt;OpenGL has the ability to draw Bezier curves almost directly using the &lt;em&gt;map&lt;/em&gt; function but for completeness sake the matrix for a bezier curve is the following:&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;
The following code in &lt;span class=&quot;caps&quot;&gt;JOGL&lt;/span&gt; will draw a Bezier line using the &lt;em&gt;map&lt;/em&gt; procedure:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public void drawLine(GL gl, double[] controlPoints) {
	DoubleBuffer ctrlPointBuffer = BufferUtil.newDoubleBuffer(3*controlPoints.length);

	for(int i = 0; i &amp;lt; controlPoints.length; i++)
		ctrlPointBuffer.put(controlPoints[i]);
	ctrlPointBuffer.rewind();

	gl.glMap1d(GL.GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, ctrlPointBuffer);

	gl.glEnable(GL.GL_MAP1_VERTEX_3);
	gl.glBegin(GL.GL_LINE_STRIP);
	for (int i = 0; i &amp;lt;= 30; i++) {
		gl.glEvalCoord1f(i / 30.0f);
	}
	gl.glEnd();
	gl.glFlush();
}&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Converting other curves to Bezier&lt;/h4&gt;
&lt;p&gt;To convert the control points of another type of curve to a Bezier control points you need to:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;mCustom * mInverseBezier * customCntlPoints = bezierCntlPoints&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;float:right; margin-left:auto; margin: 0px; padding:1em&quot;&gt;
&lt;table style=&quot;color:#000&quot;&gt;&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;mBezier[]&lt;/td&gt;
&lt;td align=&quot;center&quot; width=15&gt;&lt;sup&gt;-1&lt;/sup&gt;&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;=&lt;/td&gt;
&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0px style=&quot;border-left:1px solid #000; border-right:1px solid #000; color:#000&quot;&gt;&lt;tr&gt;
&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0 style=&quot;color:#000;&quot;&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1/3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1/3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;2/3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;&lt;p&gt;The inverse of the Bezier matrix used for the conversion of the control points is:&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h4&gt;Example of a conversion from Catmull-Rom to Bezier&lt;/h4&gt;
&lt;div style=&quot;float:right; margin-left:auto; margin: 0px; padding:0.5em&quot;&gt;
&lt;table style=&quot;color:#000&quot;&gt;&lt;tr&gt;
&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0px style=&quot;border-left:1px solid #000; border-right:1px solid #000; color:#000&quot;&gt;&lt;tr&gt;
&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0 style=&quot;color:#000;&quot;&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;-1/2&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;3/2&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;-3/2&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1/2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;-5/2&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;2&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;-1/2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;-1/2&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1/2&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;
&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0px style=&quot;margin: 0px 0px 0px 1ex; border-left:1px solid #000; border-right:1px solid #000; color:#000&quot;&gt;&lt;tr&gt;
&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0 style=&quot;color:#000;&quot;&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1/3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1/3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;2/3&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;
&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0px style=&quot;margin: 0px 0px 0px 1ex; border-left:1px solid #000; border-right:1px solid #000; color:#000&quot;&gt;&lt;tr&gt;
&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0 style=&quot;color:#000;&quot;&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p0x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p0y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p0z&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p1x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p1y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p1z&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p2x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p2y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p2z&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p3x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p3y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p3z&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;=&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;bezierCntlPoints[]&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;&lt;h4&gt;Example of a conversion from B-Spline to Bezier&lt;/h4&gt;
&lt;div style=&quot;float:right; margin-left:auto; margin: 0px; padding:0.5em; padding-bottom:2em;&quot;&gt;
&lt;table style=&quot;color:#000&quot;&gt;&lt;tr&gt;
&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0px style=&quot;border-left:1px solid #000; border-right:1px solid #000; color:#000&quot;&gt;&lt;tr&gt;
&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0 style=&quot;color:#000;&quot;&gt;
    &lt;tr&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;-1/6&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1/2&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;-1/2&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1/6&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1/2&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;-1&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1/2&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;-1/2&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;0&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1/2&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1/6&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;4/6&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;1/6&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=35&gt;0&lt;/td&gt;
    &lt;/tr&gt;

&lt;/table&gt;&lt;/td&gt;&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;
&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0px style=&quot;margin: 0px 0px 0px 1ex; border-left:1px solid #000; border-right:1px solid #000; color:#000&quot;&gt;&lt;tr&gt;
&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0 style=&quot;color:#000;&quot;&gt;
    &lt;tr&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1/3&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;0&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1/3&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;2/3&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;1&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;
&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0px style=&quot;margin: 0px 0px 0px 1ex; border-left:1px solid #000; border-right:1px solid #000; color:#000&quot;&gt;&lt;tr&gt;
&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;table border=0 cellpadding=0 cellspacing=0 style=&quot;color:#000;&quot;&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p0x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p0y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p0z&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p1x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p1y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p1z&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p2x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p2y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p2z&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p3x&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p3y&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;p3z&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;&lt;td style =&quot;border-top:1px solid #000; border-bottom:1px solid #000;&quot;&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;=&lt;/td&gt;
&lt;td align=&quot;center&quot; valign=&quot;center&quot; width=30&gt;bezierCntlPoints[]&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;&lt;p&gt;That was it, thank you for reading.&lt;/p&gt;
&lt;p&gt;Comments are welcome!&lt;/p&gt;
&lt;p&gt;Note: I used &lt;a href=&quot;http://metamerist.com/excanvas/example34.htm&quot;&gt;Matrices in &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt;&lt;/a&gt; to present the matrices in this post. It uses css and tables to present them correctly.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>LaTex: Using Tables</title>
   <link href="http://almightybuserror.com/2009/11/26/LaTex-Tables.html"/>
   <updated>2009-11-26T11:29:00+00:00</updated>
   <id>http://almightybuserror.com/2009/11/26/LaTex-Tables</id>
   <content type="html">&lt;p&gt;To create a table in LaTex you can just use a tabular block, or use a tabular block and a table wrapper block.&lt;/p&gt;
&lt;p&gt;The table wrapper is used for positioning and additional presentation.&lt;/p&gt;
&lt;h3&gt;Table&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;\begin{table}[h,t,b,p]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Where each optional means:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&amp;#8216;h&amp;#8217; &amp;rarr; &amp;#8220;here&amp;#8221;. Will appear where the table is declared.&lt;/li&gt;
	&lt;li&gt;&amp;#8216;t&amp;#8217;/&amp;#8216;b&amp;#8217; &amp;rarr; &amp;#8220;top&amp;#8221;/&amp;#8220;bottom&amp;#8221;. Will appear at the top/bottom of the page where it is declared.&lt;/li&gt;
	&lt;li&gt;&amp;#8216;p&amp;#8217; &amp;rarr; &amp;#8220;page&amp;#8221; of floats. Will group all the table in one page.&lt;br /&gt;
&lt;br/&gt;
A caption can be added to the table by doing:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;\caption{Text}&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Tabular&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;\begin{tabular}{width}{cols}&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
	&lt;li&gt;&amp;#8216;width&amp;#8217; is the fixed width of the table.&lt;/li&gt;
	&lt;li&gt;&amp;#8216;cols&amp;#8217; define the way columns are presented.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Defining a tabular&amp;#8217;s presentation&lt;/h3&gt;
&lt;h4&gt;Column presentation&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;{|l|c|r|}&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
	&lt;li&gt;&amp;#8216;l&amp;#8217;/&amp;#8216;r&amp;#8217; means the cell&amp;#8217;s content will be aligned to the left/right.&lt;/li&gt;
	&lt;li&gt;&amp;#8216;c&amp;#8217; will center the cells&amp;#8217;s content.&lt;/li&gt;
	&lt;li&gt;&amp;#8216;|&amp;#8217; will insert an column seperator. &amp;#8216;||&amp;#8217; will use a double seperation between two columns.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Line presentation&lt;/h4&gt;
&lt;ul&gt;
	&lt;li&gt;&amp;#8216;&amp;amp;&amp;#8217; marks the end of a cell and the placeholder of the seperator of a column.&lt;/li&gt;
	&lt;li&gt;&amp;#8216;\hline&amp;#8217; draws an horizontal line to seperate two rows. &amp;#8216;\hline\hline&amp;#8217; will use a double seperation between two rows.&lt;/li&gt;
	&lt;li&gt;&amp;#8216;\\&amp;#8217; marks the end of a row.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Example&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;/images/posts/latex_table.png&quot; class=&quot;lightbox&quot; id=&quot;img&quot;&gt;&lt;img src=&quot;/images/posts/latex_table.png&quot; style=&quot;width: 560px;&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Is the result of:&lt;br /&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;\begin{table}[h]
\begin{tabular}{|l|c|r|}
    \hline
    Column 1 With long title &amp;amp; Column2 looooooong &amp;amp; Column3 longlong \\
    \hline
    Value1/1 &amp;amp; Value 1/2 &amp;amp; Value 1/3 \\
    \hline
    \hline
    Value2/2 &amp;amp; Value 2/2 &amp;amp; Value 2/3 \\
    \hline
\end{tabular}
\caption{Alot of stuff}
\end{table}&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;More information about LaTex tables can be found &lt;a href=&quot;http://en.wikibooks.org/wiki/LaTeX/Tables#Alternate_Row_Colors_in_Tables&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Java: Remote Method Invocation</title>
   <link href="http://almightybuserror.com/2009/10/14/java-rmi.html"/>
   <updated>2009-10-14T23:42:00+01:00</updated>
   <id>http://almightybuserror.com/2009/10/14/java-rmi</id>
   <content type="html">&lt;p&gt;The Java &lt;acronym title=&quot;Remote Method Invocation&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RMI&lt;/span&gt;&lt;/acronym&gt; is a way to create distributed services. It is composed of RMIRegistry which is a name registry which saves the resources available (servers) in a &lt;acronym title=&quot;Java Virtual Machine&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&lt;/acronym&gt;. The registry is, however, accessible through different &lt;acronym title=&quot;Java Virtual Machine&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&lt;/acronym&gt;&amp;#8217;s when configured.&lt;/p&gt;
&lt;p&gt;Since all the invocations are remote, most arguments are passed by value. &lt;br /&gt;
The exception is when an object the implements an interface that extends &lt;em&gt;Remote&lt;/em&gt;. A server is passed by reference, since its concept requires that the information must to be consistent between all the clients accessing it.&lt;/p&gt;
&lt;p&gt;Considering that the server object would be passed by value like any other argument. This means that for the server to be consistent between all clients, it would need for each client to relay the local changes of each individual server to the central server, this creates alot of useless workload since its easier to just change the central server directly, it would save &lt;acronym title=&quot;Central Processing Unit&quot;&gt;&lt;span class=&quot;caps&quot;&gt;CPU&lt;/span&gt;&lt;/acronym&gt; cycles and network traffic on the client side used to calculate the changes and the central server load doesn&amp;#8217;t change.&lt;/p&gt;
&lt;h5&gt;Interface&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public interface IStuffServer extends Remote
{
    public void doStuff() throws RemoteException;
}&lt;/code&gt;&lt;/pre&gt;
&lt;h5&gt;Class&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public class StuffServer extends UnicodeRemoteObject implements IInfoReceiver 
{
    StuffServer() throws RemoteException { super(); }
    public void doStuff() { System.out.println(&quot;Stuff done.&quot;); }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every method of a class which extends Remote might throw a RemoteException (network connectivity).&lt;/p&gt;
&lt;h4&gt;Registering a resource&lt;/h4&gt;
&lt;p&gt;To create a registry there is a static method in the class &lt;em&gt;LocateRegistry&lt;/em&gt; named &lt;em&gt;createRegistry&lt;/em&gt; which receives an integer that specifies the port in which the registry will listen for requests. For the registry to be able to communicate with other &lt;acronym title=&quot;Java Virtual Machine&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;&lt;/acronym&gt;&amp;#8217;s, it is needed to enable it by defining a &lt;em&gt;policy&lt;/em&gt;.&lt;/p&gt;
&lt;h5&gt;policy.all&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;grant {
    permission java.security.AllPermission &quot;&quot;, &quot;&quot;;
};&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Please bear in mind this might be insecure for critical applications.&lt;/p&gt;
&lt;h5&gt;Creating a &amp;#8220;public&amp;#8221; registry&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;System.getProperties().put(&quot;java.security.policy&quot;, &quot;policy.all&quot;);
if (System.getSecurityManager() == null) {
    System.setSecurityManager(new RMISecurityManager());
}
try { // start rmiregistry
    LocateRegistry.createRegistry(1099);
} catch (RemoteException e) 
{   /* registry already created*/   }
InfoServerImpl server = new InfoServerImpl();
Naming.rebind(&quot;/StuffServer&quot;, server);
System.out.println(&quot;StuffServer bound in registry&quot;);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After creating the registry, the static class &lt;em&gt;Naming&lt;/em&gt; is used to bind a name to the class using the method &lt;em&gt;rebind&lt;/em&gt;, which in the example is &amp;#8220;/StuffServer&amp;#8221;. This name is only known by the registry in the current &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;A registry can be accessed by a simple netbios-like link, for example &lt;br /&gt;
&amp;#8220;&lt;em&gt;//localhost/StuffServer&lt;/em&gt;&amp;#8221;.&lt;/p&gt;
&lt;h4&gt;Obtaining Server Reference&lt;/h4&gt;
&lt;p&gt;To obtain the server object reference the static &lt;em&gt;Naming&lt;/em&gt; class is used again, this time with the &lt;em&gt;lookup&lt;/em&gt; method.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;IInfoServer server = null;
try {
    server = (IInfoServer) Naming.lookup(&quot;//localhost/StuffServer&quot;);
} catch(RemoteException e) 
    { System.out.println(&quot;Server not found&quot;); }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After the server reference is obtained all the interaction to it is the same of a local object, except it might fail/be slower because of network connectivity issues.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;server.doStuff();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The previous will print &amp;#8220;Stuff done.&amp;#8221; at the server&amp;#8217;s console.&lt;/p&gt;
&lt;p&gt;That was it about Java&amp;#8217;s &lt;acronym title=&quot;Remote Method Invocation&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RMI&lt;/span&gt;&lt;/acronym&gt;. For more information go &lt;a href=&quot;http://java.sun.com/javase/technologies/core/basic/rmi/index.jsp&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thank you for reading!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>FTP in Python</title>
   <link href="http://almightybuserror.com/2009/08/25/ftp-in-python.html"/>
   <updated>2009-08-25T20:06:00+01:00</updated>
   <id>http://almightybuserror.com/2009/08/25/ftp-in-python</id>
   <content type="html">&lt;p&gt;In Python, to create and maintain a &lt;acronym title=&quot;File Transfer Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;FTP&lt;/span&gt;&lt;/acronym&gt; communication, the &lt;a href=&quot;http://docs.python.org/library/ftplib.html&quot;&gt;ftplib&lt;/a&gt; or &lt;a href=&quot;http://docs.python.org/library/urllib.html&quot;&gt;urllib&lt;/a&gt; (explained in a future post) can be used.&lt;/p&gt;
&lt;h3&gt;Creating a &lt;acronym title=&quot;File Transfer Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;FTP&lt;/span&gt;&lt;/acronym&gt; connection&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-python&quot;&gt;from ftplib import FTP
try:
    conn = FTP('almightybuserror.com')
except:
    print &quot;Could not connect to almightybuserror.com.&quot;
    return&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Logging in&amp;#8230;&lt;/h3&gt;
&lt;p&gt;For anonymous log in:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-python&quot;&gt;try:
    conn.login()
except:
    print &quot;No anonymous connections allowed.&quot;
    return
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Otherwise:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-python&quot;&gt;try:
    conn.login(user, passwd)
    print conn.getwelcome()
except:
    print &quot;Wrong username or password.&quot;
    return
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Sending commands and handling response.&lt;/h3&gt;
&lt;p&gt;To send commands (or requests) to the server there is a method for each command which can be consulted &lt;a href=&quot;http://docs.python.org/library/ftplib.html#ftp-objects&quot;&gt;here&lt;/a&gt;, however I will show an example of an binary file download request.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-python&quot;&gt;try:
    conn.cwd('/stuff/more/misc/stuff/')
except:
    print &quot;No such folder!&quot;
    return
file = open('stuff', 'w')
try: # file.write acts as a function pointer
    conn.retrbinary('RETR stuff', file.write) 
except:
    print &quot;Couldn't download stuff...&quot;
finally:
    file.close()
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Check &lt;em&gt;downloader.py&lt;/em&gt; in &lt;a href=&quot;http://github.com/kernl/Scripts-and-Tools/tree/master&quot;&gt;github&lt;/a&gt; for a more complete example.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>HTTP in Python</title>
   <link href="http://almightybuserror.com/2009/08/22/http-in-python.html"/>
   <updated>2009-08-22T17:51:00+01:00</updated>
   <id>http://almightybuserror.com/2009/08/22/http-in-python</id>
   <content type="html">&lt;p&gt;To create and &lt;acronym title=&quot;Hyper Text Transfer Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;&lt;/acronym&gt; connection we use the &lt;a href=&quot;http://docs.python.org/library/httplib.html&quot;&gt;httplib&lt;/a&gt; module which provides some simple yet effective methods to create a connection and send requests to an &lt;acronym title=&quot;Hyper Text Transfer Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;&lt;/acronym&gt; server. The module &lt;a href=&quot;http://docs.python.org/library/urllib.html&quot;&gt;urllib&lt;/a&gt; can also be used and is easier, does, however only perform &lt;strong&gt;&lt;span class=&quot;caps&quot;&gt;GET&lt;/span&gt;&lt;/strong&gt; requests (explained in a future post).&lt;/p&gt;
&lt;h3&gt;Creating a connection&lt;/h3&gt;
&lt;p&gt;To create a connection, the method &lt;em&gt;HTTPConnection&lt;/em&gt; does the job. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-python&quot;&gt;import httplib
try:
    http = httplib.HTTPConnection('almightybuserror.com')
except:
    print 'Could not connect...'
    return&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Sending a request&lt;/h3&gt;
&lt;p&gt;To send a request, a connection object is needed (we&amp;#8217;ll use &lt;em&gt;http&lt;/em&gt; from the previous example):&lt;/p&gt;
&lt;pre&gt;&lt;code class='prettyprint lang-python'&gt;http.request(&quot;GET&quot;, &quot;/&quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Getting a response&lt;/h3&gt;
&lt;p&gt;To obtain a response, the &lt;em&gt;getResponse()&lt;/em&gt; method is used. From this method we get a &lt;a href=&quot;http://docs.python.org/library/httplib.html#httpresponse-objects&quot;&gt;HTTPResponse&lt;/a&gt; object.&lt;/p&gt;
&lt;pre&gt;&lt;code class='prettyprint lang-python'&gt;response = http.getResponse()
if response.status == httplib.OK:
    file = open('example.html', 'w')
    file.write(response.read())
    file.close()
else:
    print 'Error: %s %s' % (response.status, response.reason)
    return&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For a more complete example check &lt;em&gt;downloader.py&lt;/em&gt; in &lt;a href=&quot;http://github.com/kernl/Scripts-and-Tools/tree/master&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>The Comic Massacre</title>
   <link href="http://almightybuserror.com/2009/08/04/The-Comic-Massacre.html"/>
   <updated>2009-08-04T15:12:00+01:00</updated>
   <id>http://almightybuserror.com/2009/08/04/The-Comic-Massacre</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://2.media.tumblr.com/ESJyzYLhpqq97odqlLDGib3co1_500.jpg&quot; class=&quot;lightbox&quot; id=&quot;img&quot;&gt;&lt;img src=&quot;http://2.media.tumblr.com/ESJyzYLhpqq97odqlLDGib3co1_500.jpg&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;By Shello, original can be found &lt;a href=&quot;http://www.flickr.com/photos/ptshello/3788850280/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Snippet.Py: Getting an image resolution</title>
   <link href="http://almightybuserror.com/2009/08/03/python-image-resolution.html"/>
   <updated>2009-08-03T21:49:00+01:00</updated>
   <id>http://almightybuserror.com/2009/08/03/python-image-resolution</id>
   <content type="html">&lt;p&gt;Using &lt;a href=&quot;http://www.pythonware.com/products/pil/&quot; title=&quot;Python Image Library&quot;&gt;&lt;span class=&quot;caps&quot;&gt;PIL&lt;/span&gt;&lt;/a&gt; it is possible to get the width and height of a image using the property &lt;em&gt;size&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The following example will take an undefined number of arguments and print all the resolutions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-py&quot;&gt;import Image
import sys
def main():
    argv = sys.argv[1:]
    if (len(argv) == 0):
        print &quot;No file given!&quot;
    else:
        for name in argv:
            img = Image.open(name)
            print name + &quot;: %d %d&quot; % img.size
if __name__ == &quot;__main__&quot;:
    main()
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Credit for the snippet goes to &lt;a href=&quot;http://scarybox.net&quot;&gt;meqif&lt;/a&gt;.&lt;br /&gt;
The code can also be found at &lt;a href=&quot;http://gist.github.com/160844&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>To the rescue!</title>
   <link href="http://almightybuserror.com/2009/06/05/To-The-Rescue.html"/>
   <updated>2009-06-05T23:05:00+01:00</updated>
   <id>http://almightybuserror.com/2009/06/05/To-The-Rescue</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://4.media.tumblr.com/ESJyzYLhpoce9r3npvae8FYwo1_500.jpg&quot; class=&quot;lightbox&quot; id=&quot;img&quot;&gt;&lt;img src=&quot;http://4.media.tumblr.com/ESJyzYLhpoce9r3npvae8FYwo1_500.jpg&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Source:  &lt;a href=&quot;http://draken413o.deviantart.com/art/To-the-rescue-121441303&quot;&gt;~Draken413o&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Stop that fridge!</title>
   <link href="http://almightybuserror.com/2009/05/31/Stop-That-Fridge.html"/>
   <updated>2009-05-31T23:05:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/31/Stop-That-Fridge</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://20.media.tumblr.com/ESJyzYLhpo5uhh66Ez0T913No1_400.jpg&quot; class=&quot;lightbox&quot; id=&quot;img&quot;&gt;&lt;img src=&quot;http://20.media.tumblr.com/ESJyzYLhpo5uhh66Ez0T913No1_400.jpg&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Source: &lt;a href=&quot;http://digg.com/comedy/Stop_That_Fridge_Pic&quot;&gt;digg&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>The connectionless networking protocol</title>
   <link href="http://almightybuserror.com/2009/05/30/udp.html"/>
   <updated>2009-05-30T18:05:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/30/udp</id>
   <content type="html">&lt;p&gt;Also known as &lt;acronym title=&quot;User Datagram Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt;&lt;/acronym&gt;, like the title suggests, it does not have a logical idea of connection.&lt;/p&gt;
&lt;p&gt;The way it communicates is by &lt;em&gt;datagrams&lt;/em&gt; (or &lt;em&gt;messages&lt;/em&gt;). The protocol is not able assert any type of confirmation at a transport layer level if a message got thru to the destination. A collateral effect of this is that &lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt; is a very lightweight protocol, perfect for scenarios where the loss of some information does not break the intended application.&lt;/p&gt;
&lt;p&gt;In Java, to implement &lt;acronym title=&quot;User Datagram Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt;&lt;/acronym&gt; communication it&amp;#8217;s mostly used the following two objects from the java.net.* package: &lt;em&gt;DatagramPacket&lt;/em&gt; and &lt;em&gt;DatagramSocket&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The DatagramPacket is used to create a datagram that will be received or sent and configure destination/port in the latter:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;DatagramPacket packet = new DatagramPacket(
someString.getBytes(), someString.length() );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To configure the destination of the datagram the following methods are used:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;packet.setAddress( InetAddress.getByName( someServerName ) );
packet.setPort( port );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;InetAddress.&lt;em&gt;getByName&lt;/em&gt; is an static method that given a server name like &amp;#8220;www.google.com&amp;#8221; returns the IP address.&lt;/p&gt;
&lt;p&gt;A DatagramPacket can be used to send any type of message that has been converted to an array of &lt;em&gt;byte&lt;/em&gt;. There is, however, an &lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt; protocol &lt;strong&gt;65,507&lt;/strong&gt; byte limit to the size of the array.&lt;/p&gt;
&lt;p&gt;To extract the information from a received packet the following methods are used:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;byte[] info = packet.getData();
int infoLength = packet.getLength();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A DatagramSocket is initialized using the constructor that receives a port which will be used for sending and receiving datagrams:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;DatagramSocket socket = new DatagramSocket( port );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The methods used by this object are:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;socket.send( DatagramPacket x );
socket.receive( DatagramPacket x );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The receive method is blocking, it will block the current execution flow until it has received a message. &lt;br /&gt;
To receive a packet, it doesn&amp;#8217;t make sense to configure a destination and a port, only a buffer to where the data will be stored like in the declaration of the packet object. &lt;br /&gt;
Since all the complex ( i.e. Object ) argument passing is by reference, the received packet will overwrite all the information in x.&lt;/p&gt;
&lt;p&gt;For more information on &lt;acronym title=&quot;User Datagram Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt;&lt;/acronym&gt; go &lt;a href=&quot;http://en.wikipedia.org/wiki/User_Datagram_Protocol&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Hope this has been useful.&lt;/p&gt;
&lt;p&gt;Thank you for reading!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Multicast: An UDP strong point</title>
   <link href="http://almightybuserror.com/2009/05/30/multicast.html"/>
   <updated>2009-05-30T18:05:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/30/multicast</id>
   <content type="html">&lt;p&gt;Multicast is a way to distribute &lt;acronym title=&quot;User Datagram Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt;&lt;/acronym&gt; messages to multiple hosts in an automated fashion. It works by making every host join a group which is composed of an IP address in the range &lt;em&gt;224.0.0.0 to 239.255.255.255&lt;/em&gt; and a &lt;em&gt;port&lt;/em&gt;. Every message sent to a group will be delivered to every host that has joined it.&lt;br /&gt;
Since it can generate alot of traffic, multicast is only used at a &lt;span class=&quot;caps&quot;&gt;LAN&lt;/span&gt; or intranet level.&lt;/p&gt;
&lt;p&gt;In Java, to use Multicast there is an object named &lt;i&gt;MulticastSocket&lt;/i&gt;. It has mostly the same usage as the &lt;i&gt;DatagramSocket&lt;/i&gt; but has an &lt;i&gt;joinGroup&lt;/i&gt; method and makes the process join a multicast group:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;MulticastSocket mso = new MulticastSocket( 9001 );
mso.joinGroup( InetAddress.getByName(&quot;224.0.5.8&quot;) );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For more information about receiving and sending messages in &lt;acronym title=&quot;User Datagram Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt;&lt;/acronym&gt; go &lt;a href=&quot;http://almightybuserror.com/post/92359462/the-connectionless-networking-protocol&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>I shot the seriff</title>
   <link href="http://almightybuserror.com/2009/05/28/I-shot-the-seriff.html"/>
   <updated>2009-05-28T17:46:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/28/I-shot-the-seriff</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://1.media.tumblr.com/ESJyzYLhpo18s7rcm8Ybs5eXo1_500.jpg&quot; class=&quot;lightbox&quot; id=&quot;img&quot;&gt;&lt;img src=&quot;http://1.media.tumblr.com/ESJyzYLhpo18s7rcm8Ybs5eXo1_500.jpg&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Source: &lt;a href=&quot;http://digg.com/design/I_Shot_The_Serif_PIC&quot;&gt;gamebittk&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>HTTP Proxy: Implementation</title>
   <link href="http://almightybuserror.com/2009/05/27/proxy-implementation.html"/>
   <updated>2009-05-27T21:08:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/27/proxy-implementation</id>
   <content type="html">&lt;p&gt;Continuing from &amp;#8220;&lt;a href=&quot;http://almightybuserror.com/post/107781290/http-proxy-concept-introduction&quot;&gt;Concept Introduction&lt;/a&gt;&amp;#8221; we will now see how a possible implementation is done.&lt;/p&gt;
&lt;p&gt;To start implementing a proxy we must first create a simple server application which accepts a connection from a client to be redirected.&lt;/p&gt;
&lt;h3&gt;Creating Server&lt;/h3&gt;
&lt;p&gt;We start by creating a ServerSocket (for more information go &lt;a href=&quot;http://almightybuserror.com/post/95514996/the-connection-oriented-network-protocol&quot;&gt;here&lt;/a&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;doIt( new ServerSocket( serverPort ) );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we need to implement the method &lt;em&gt;doIt&lt;/em&gt;() which simply uses a non-stopping cycle to take requests.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public static void doIt( ServerSocket server )
{
    for(;;)
    {
        try
        {
            Socket request = server.accept();
            new RequestHandler( request ).start();
        }
        catch( IOException e )
        {   e.printStackTrace(); continue;  }
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Creating a RequestHandler&lt;/h3&gt;
&lt;p&gt;The object &lt;em&gt;RequestHandler&lt;/em&gt; is the one that actually does all the work for &lt;em&gt;one&lt;/em&gt; request. Because of that we thread it (more information &lt;a href=&quot;http://almightybuserror.com/post/104704332/threading-in-java&quot;&gt;here&lt;/a&gt;) so that the proxy can handle multiple requests at once. The utility functions used here are located at the end of the post.&lt;/p&gt;
&lt;p&gt;The code to convert the request to &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.0 is as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;InputStream srcIs = source.getInputStream();
String req = readLine(srcIs);
Scanner head = new Scanner(req);
String request = head.next();
String[] link = parseUrl(head.next());
if(link[0].compareTo(&quot;http&quot;) != 0)
    throw new IOException(&quot;Incompatible Protocol&quot;);

request += String.format(&quot; %s HTTP/1.0\r\n&quot;, link[3]);
String[] currentProperty = parseHttpHeader( readLine(srcIs) );
while(currentProperty != null)
{
    if(isBanned(currentProperty))
    {
        currentProperty = parseHttpHeader( readLine(srcIs) );
        continue;
    }
    request += String.format(&quot;%s:%s\r\n&quot;, currentProperty[0],
                                                    currentProperty[1]);
    currentProperty = parseHttpHeader( readLine(srcIs) );
}
request += &quot;\r\n&quot;; // Add last CRLF&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function &lt;em&gt;isBanned&lt;/em&gt; is the filter for properties from &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.1:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;private boolean isBanned(String[] property)
{
    return property[0].compareTo(&quot;Connection&quot;) == 0 ||
        property[0].compareTo(&quot;Keep-Alive&quot;) == 0 ||
        property[0].compareTo(&quot;Proxy-Connection&quot;) == 0;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that the request is ready, the only thing needed to be done, is to send the request and then dump the whole response from the server to the client.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;InetAddress host = InetAddress.getByName(link[1]);
Socket destination = new Socket(host, Integer.parseInt(link[2]) );
OutputStream destOs = destination.getOutputStream();
InputStream destIs = destination.getInputStream();
destOs.write(request.getBytes());
dumpStream(destIs, source.getOutputStream());
destination.close();
source.close();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note: &lt;em&gt;source&lt;/em&gt; is a socket that is passed in the &lt;em&gt;RequestHandler&lt;/em&gt; constructor.&lt;/p&gt;
&lt;h4&gt;Utility Functions&lt;/h4&gt;
&lt;p&gt;The &lt;em&gt;parseHttpHeader&lt;/em&gt; function parses the properties that a normal &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; request has so we can easily remove the properties that are exclusive to HTTP1.1.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public static String[] parseHttpHeader( String header )
{
    String[] result = new String[2];
    int pos0 = header.indexOf( ':' );
    if( pos0 == -1 )
        return null;
    result[0] = header.substring( 0, pos0 ).trim();
    result[1] = header.substring( pos0 + 1 ).trim();
    return result;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;readLine&lt;/em&gt; function keeps reading from a InputStream until it reaches a       &amp;#8220;\r\n&amp;#8221;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public static String readLine( InputStream is ) throws IOException
{
    StringBuffer sb = new StringBuffer();
    int c;
    while( (c = is.read() ) &amp;gt;= 0 ) {
        if( c == '\r' ) continue;
        if( c == '\n' ) break ;
        sb.append( new Character( (char)c ) );
    }
    return sb.toString();
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;parseUrl&lt;/em&gt; function parses a header, for example &amp;#8220;http://almightybuserror.com:5000/stuff/here/&amp;#8221; will be transformed to:&lt;br /&gt;
[&amp;#8220;http&amp;#8221;, &amp;#8220;almightybuserror.com&amp;#8221;, &amp;#8220;5000&amp;#8221;, &amp;#8220;/stuff/here/&amp;#8221;].&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public static String[] parseUrl(String url)
{
    String result[] = new String[4];
    int i = url.indexOf(':');
    result[0] = url.substring(0, i); // Protocol
    result[1] = url.substring(i+3);
    i = result[1].indexOf(':'); // Get possibly a port number
    int j = result[1].indexOf('/');
    // To parse the rest of the request
    result[2] = (i &amp;gt; 0 ? result[1].substring(i+1, j) : &quot;80&quot;);
    result[3] = result[1].substring(j); // Request
    result[1] = (i &amp;gt; 0 ? result[1].substring(0, i) :
                                result[1].substring(0, j));
    //Concat so only the host remains
    return result;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;dumpStream&lt;/em&gt; function will also be used, you can find it &lt;a href=&quot;http://almightybuserror.com/post/109924240/snippet-java-dump-inputstream-outputstream&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thank you for reading. Comments are welcome!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Credits for the functions &lt;em&gt;readLine&lt;/em&gt; and &lt;em&gt;parseHttpHeader&lt;/em&gt; go to &lt;a href=&quot;http://asc.di.fct.unl.pt/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;ASC&lt;/span&gt; Department&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Snippet.Cpp: Comparing Types of Classes</title>
   <link href="http://almightybuserror.com/2009/05/20/cpp-typeid.html"/>
   <updated>2009-05-20T10:53:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/20/cpp-typeid</id>
   <content type="html">&lt;p&gt;This snippet shows how to differentiate between two classes that have heritage from the same class.&lt;/p&gt;
&lt;p&gt;Using the following classes:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-cpp&quot;&gt;class A {}
class B: public A { B(); }
class C: public A { C(); }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the following variables:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-cpp&quot;&gt;A a;
A *b = new B();
A *c = new C();
if(typeid(b) == typeid(a))
    cout &amp;lt;&amp;lt; &quot;First is true&quot; &amp;lt;&amp;lt; endl; // A* != A
if(typeid(*b) == typeid(B))
    cout &amp;lt;&amp;lt; &quot;Second is true&quot; &amp;lt;&amp;lt; endl; // B = B
if(typeid(*b) == typeid(*c))
    cout &amp;lt;&amp;lt; &quot;Third is true&quot; &amp;lt;&amp;lt; endl; // B != C
if(typeid(b) == typeid(c))
    cout &amp;lt;&amp;lt; &quot;Fourth is true&quot; &amp;lt;&amp;lt; endl; // A* = A*
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The pointer usage is important when using the &lt;strong&gt;typeid&lt;/strong&gt;() in this situation.&lt;/p&gt;
&lt;p&gt;As you might notice you can actually use the class name to compare like in the second example and not just objects.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: There is a &lt;em&gt;name&lt;/em&gt;() method that can be used to print the class name. You might notice that there sometimes are pseudo-random numbers/letters before the actual name.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Snippet.Java: Dump InputStream &rarr; OutputStream</title>
   <link href="http://almightybuserror.com/2009/05/19/java-dumpstream.html"/>
   <updated>2009-05-19T11:16:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/19/java-dumpstream</id>
   <content type="html">&lt;p&gt;This piece of code keeps reading 1024 bytes from the &lt;em&gt;InputStream&lt;/em&gt; and writing to the &lt;em&gt;OutStream&lt;/em&gt; until the &lt;em&gt;read&lt;/em&gt; function returns an error value (-1) which means there is not anything else to read.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-Java&quot;&gt;protected void dumpStream( InputStream in, OutputStream out )
throws IOException {
	byte[] arr = new byte[1024];
	int n;
	do {
		n = in.read( arr );
		out.write( arr, 0, n );
	} while (n != -1);
}&lt;/code&gt;&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>HTTP Proxy: Concept introduction</title>
   <link href="http://almightybuserror.com/2009/05/14/proxy-concept.html"/>
   <updated>2009-05-14T18:50:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/14/proxy-concept</id>
   <content type="html">&lt;p&gt;An &lt;acronym title=&quot;Hyper Text Transfer Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;&lt;/acronym&gt; proxy is a program that acts as a seamless server (since the user does not notice its existence under normal circumstances) and forwards &lt;acronym title=&quot;Hyper Text Transfer Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;&lt;/acronym&gt; traffic.&lt;/p&gt;
&lt;p&gt;A problem for people that are creating a proxy for learning purposes is that most browsers nowadays days use &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.1 which is far more complex than its &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.0 counterpart implementation-wise. For this very reason we will convert every &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.1 requests to &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.0.&lt;/p&gt;
&lt;p&gt;For a browser using &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.0 to fetch a webpage, it first needs to establish a connection with the webserver and use a &amp;#8220;&lt;span class=&quot;caps&quot;&gt;GET&lt;/span&gt;&amp;#8221; command on the index (or another page if specified). When it finishes receiving and processing that page, the browser identifies every resource the page uses (be it images, scripts or &lt;em&gt;Cascading Style Sheets&lt;/em&gt;). For each resource it needs, the browser creates a connection and sends a request. The webserver closes the connection after it finishes sending the resource.&lt;/p&gt;
&lt;p&gt;The proxy will work like the following:&lt;br /&gt;
&lt;a href=&quot;/images/posts/proxy_sequence.png&quot; class=&quot;lightbox&quot; id=&quot;img&quot;&gt;&lt;img src=&quot;/images/posts/proxy_sequence.png&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A browser using &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.1 does, however use &lt;em&gt;pipelining&lt;/em&gt; and persistent connections, which after requesting and receiving the webpage the server does not close the connection, and can keeps receiving requests from the browser for resources avaiable locally until the client closes the connection (by using the &lt;em&gt;Connection&lt;/em&gt; property with &lt;strong&gt;keep-alive&lt;/strong&gt; value).&lt;/p&gt;
&lt;p&gt;For the proxy to convert the request headers to &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.0 it is needed to read and modify them, removing some fields from the &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.1 request.&lt;/p&gt;
&lt;p&gt;For our purposes we will remove &amp;#8220;&lt;em&gt;Connection&lt;/em&gt;&amp;#8221; and the non-obligatory &amp;#8220;&lt;em&gt;Keep-Alive&lt;/em&gt;&amp;#8221; and &amp;#8220;&lt;em&gt;Proxy-Connection&lt;/em&gt;&amp;#8221; properties since they are unique to &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;/1.1 and change the protocol version from the header. Every line of a &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; request and response ends using two special characters &amp;#8220;\r\n&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;GET http://almightybuserror.com/ HTTP/1.1
Connection: keep-alive&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And this last request gets converted and sent to the server &amp;#8220;http://almightybuserror.com&amp;#8221; as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;GET / HTTP/1.0&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: There needs to exist an extra &amp;#8220;\r\n&amp;#8221; at the end of the request since it is the only way the server involved knows that the request has ended.&lt;/p&gt;
&lt;p&gt;Thank for reading. Comments are welcome.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Threading in Java</title>
   <link href="http://almightybuserror.com/2009/05/07/threading-java.html"/>
   <updated>2009-05-07T21:19:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/07/threading-java</id>
   <content type="html">&lt;p&gt;A thread is a lightweight process that will act as the child of the process that created it. If the parent process is interrupted the child will be inherited by the kernel process, however, most of the times when the parent dies and child loses its purpose.&lt;br /&gt;
A thread will seemingly run at the same time as the other processes which raises concurrency issues but this won&amp;#8217;t be discussed in this article.&lt;/p&gt;
&lt;p&gt;Threading in a Java implementation can be made by two ways: extending the &lt;em&gt;Thread&lt;/em&gt; object and implementing the &lt;em&gt;Runnable&lt;/em&gt; interface.&lt;/p&gt;
&lt;p&gt;To create a thread in either way it is needed to implement the &lt;em&gt;run&lt;/em&gt;() method. Run will be the entry execution point when a thread is created.&lt;/p&gt;
&lt;h3&gt;Extending Thread object&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public class DummyThreadable extends Thread 
{
    DummyThreadable() { ; } //dummy constructor.
    public void Run()
    {
        //do some threaded stuff.
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To create a thread and run it use the &lt;strong&gt;start&lt;/strong&gt; method like the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;new DummyThreadable().start();&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Implementing Runnable interface&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;public class DummyThreadable implements Runnable 
{
    DummyThreadable() { ; } //dummy constructor.
    public void Run()
    {
        //do some threaded stuff.
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To create a thread using a class that implements Runnable use the following example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;DummyThreadable t = new DummyThreadable();
new Thread(t).start();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A very useful method for thread programing is the sleep method. It receives a time in milliseconds (and optionally nanoseconds). It makes a thread stop its execution flow for the defined time.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;long ms = 9001;
int ns = 901;
Thread.sleep(ms, ns);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thank you for reading!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Snippet.C: Getting the length of an array.</title>
   <link href="http://almightybuserror.com/2009/05/03/c-array-length.html"/>
   <updated>2009-05-03T15:36:00+01:00</updated>
   <id>http://almightybuserror.com/2009/05/03/c-array-length</id>
   <content type="html">&lt;p&gt;The following C macro gets the amount of memory the array uses and divides it by the size of the first position:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-c&quot;&gt;#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x)[0])&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Credits go to &lt;a href=&quot;http://scarybox.net/&quot;&gt;meqif&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Snippet.Py: Showing all the filenames in a directory</title>
   <link href="http://almightybuserror.com/2009/04/17/python-find.html"/>
   <updated>2009-04-17T20:26:00+01:00</updated>
   <id>http://almightybuserror.com/2009/04/17/python-find</id>
   <content type="html">&lt;p&gt;The following Python code prints the name of the chosen directory and all the files in it and then proceeds to print all the files in the other nested directories one nested directory at a time. &lt;br /&gt;
This method is recursive for nested directories.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-python&quot;&gt;def print_dir(loc):
    print loc + &quot;\n&quot;
    for root, dirs, files in os.walk(loc):
        for f in files:
           print f + &quot;\n&quot;
        for d in dirs:        
           print_dir(os.path.join(loc, d))
    return&lt;/code&gt;&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>The connection-oriented network protocol</title>
   <link href="http://almightybuserror.com/2009/04/12/tcp.html"/>
   <updated>2009-04-12T21:10:00+01:00</updated>
   <id>http://almightybuserror.com/2009/04/12/tcp</id>
   <content type="html">&lt;p&gt;The &lt;acronym title=&quot;Transmission Control Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;TCP&lt;/span&gt;&lt;/acronym&gt; and unlike &lt;acronym title=&quot;User Datagram Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt;&lt;/acronym&gt; it does have a connection definition within the protocol based on Acknowledge-type packets.&lt;/p&gt;
&lt;p&gt;In &lt;acronym title=&quot;Transmission Control Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;TCP&lt;/span&gt;&lt;/acronym&gt; every sent information packet needs to be acknowledged by the destination and if it is missing, the packet is resent until there is confirmation thus creating a &amp;#8220;connection&amp;#8221; between two peers. If a packet is corrupted a request is to the source for a replacement. Because there is a need to acknowledge all sent packets and error correction, &lt;acronym title=&quot;Transmission Control Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;TCP&lt;/span&gt;&lt;/acronym&gt; becomes bulkier and slower than &lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt; but extremely reliable.&lt;/p&gt;
&lt;p&gt;In Java, to implement &lt;acronym title=&quot;Transmission Control Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;TCP&lt;/span&gt;&lt;/acronym&gt; communication the following objects from the java.net.* package are used: &lt;em&gt;Socket&lt;/em&gt; and &lt;em&gt;ServerSocket&lt;/em&gt;. Other needed objects, from the java.io.* package: &lt;em&gt;InputStream&lt;/em&gt; and &lt;em&gt;OutputStream&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;As you can see in Java there is such a thing as a Server and Client. A ServerSocket can be looked at as a &lt;em&gt;Socket generator&lt;/em&gt;. Its &lt;em&gt;accept&lt;/em&gt; method accepts an incoming connection from a client creating and returning a &lt;em&gt;Socket&lt;/em&gt; object.&lt;/p&gt;
&lt;h5&gt;How to use a &lt;em&gt;Socket&lt;/em&gt;&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;Socket aSocket = new Socket( server, port );&lt;/code&gt;&lt;/pre&gt;
&lt;h5&gt;Reading from the socket&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;InputStream is = aSocket.getInputStream();
Scanner in = new Scanner( is );
( ... )&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For more information on how to use a Scanner please refer to &lt;a href=&quot;http://java.sun.com/developer/JDCTechTips/2004/tt1201.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h5&gt;Writing to the socket&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;OuputStream os = aSocket.getOutputStream();
byte[] message = new String( &quot;Hello World!&quot; ).getBytes();
os.write( message );&lt;/code&gt;&lt;/pre&gt;
&lt;h5&gt;Closing a socket&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;socket.close();&lt;/code&gt;&lt;/pre&gt;
&lt;h5&gt;How to use a &lt;em&gt;ServerSocket&lt;/em&gt;&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;prettyprint lang-java&quot;&gt;ServerSocket server = new ServerSocket( port );
Socket aSocket = server.accept();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The accept method is blocking, it will block the current execution flow. If there is need to process multiple clients at the same time the program must be multithreaded.&lt;/p&gt;
&lt;p&gt;As we can observe, opposing &lt;acronym title=&quot;User Datagram Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;UDP&lt;/span&gt;&lt;/acronym&gt;, the &lt;acronym title=&quot;Transmission Control Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;TCP&lt;/span&gt;&lt;/acronym&gt; approach, at least in Java, does not need a crafting of every single packet, there is an abstraction layer that makes it possible to just dump information and let the underlying implementation handle the low-level management.&lt;/p&gt;
&lt;p&gt;For more information on &lt;acronym title=&quot;Transmission Control Protocol&quot;&gt;&lt;span class=&quot;caps&quot;&gt;TCP&lt;/span&gt;&lt;/acronym&gt; go &lt;a href=&quot;http://en.wikipedia.org/wiki/Transmission_Control_Protocol&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thank you for reading!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Yes we kern</title>
   <link href="http://almightybuserror.com/2009/04/05/yes-we-kern.html"/>
   <updated>2009-04-05T19:47:00+01:00</updated>
   <id>http://almightybuserror.com/2009/04/05/yes-we-kern</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://19.media.tumblr.com/ESJyzYLhplxmrlulK5SgYfJLo1_500.png&quot; class=&quot;lightbox&quot; id=&quot;img&quot;&gt;&lt;img src=&quot;http://19.media.tumblr.com/ESJyzYLhplxmrlulK5SgYfJLo1_500.png&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Source:  &lt;a href=&quot;http://ilovetypography.com/2009/04/05/yes-we-kern-the-week-in-typography-and-fonts/&quot;&gt;ilovetypography&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 

</feed>
