<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-12463714</id><updated>2012-01-02T06:16:10.598-05:00</updated><category term='design'/><category term='oop'/><category term='c c++'/><category term='ant'/><category term='Java'/><category term='debugging'/><category term='c++'/><category term='patterns'/><category term='jar'/><category term='decorator'/><category term='c'/><title type='text'>// Programming Notes</title><subtitle type='html'>Thoughts on programming</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://drpaulcarter.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://drpaulcarter.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Paul Carter</name><uri>http://www.blogger.com/profile/11141659329083855293</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-12463714.post-2276202125550033742</id><published>2010-02-28T12:48:00.003-05:00</published><updated>2010-02-28T13:16:13.195-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='oop'/><title type='text'>Keep It Local</title><content type='html'>Deciding whether a variable should be a member of a class or a local variable may not be clear to newer developers. I have always found the following rule of thumb to work best:&lt;br /&gt;     &lt;span style="font-weight: bold; font-style: italic;"&gt;If it can be local, make it local.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;That is, if at all possible, make the variable local. Making it a class member should be a last resort, not the default. Why should local be preferred over a class data member? For the same reason that local variables are preferred over global variables. Local variables are easier to understand. They only exist in the block they are defined in and can only directly influence code in this block. Data members exist for the entire lifetime of the object they are part of. Determining if they are initialized correctly and their effect requires one to look at every method of the class. In a multi-threaded environment, synchronization of access to data members is another complication that local variables do not have. Data members also increase the memory footprint of your objects.&lt;br /&gt;&lt;br /&gt;Variables that should be data members are ones that hold the persistent state of the object that must be maintained between method calls. Variables that hold transient values that are only needed temporarily should be local variables.&lt;br /&gt;&lt;br /&gt;So when reviewing a class, look carefully at each of its data members and be sure that you can justify why it's a data member and not a local variable. If you can't justify it, fix it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12463714-2276202125550033742?l=drpaulcarter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://drpaulcarter.blogspot.com/feeds/2276202125550033742/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12463714&amp;postID=2276202125550033742' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/2276202125550033742'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/2276202125550033742'/><link rel='alternate' type='text/html' href='http://drpaulcarter.blogspot.com/2010/02/keep-it-local.html' title='Keep It Local'/><author><name>Paul Carter</name><uri>http://www.blogger.com/profile/11141659329083855293</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12463714.post-4115575209499557108</id><published>2009-05-31T17:16:00.024-04:00</published><updated>2009-06-06T08:27:27.929-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='decorator'/><title type='text'>The Decorator Pattern</title><content type='html'>&lt;span style="font-style: italic;"&gt;I volunteered to do a presentation to the Java User's Group at my work and adapted an old lecture to discuss the Decorator design pattern. I thought this would also be a good post too. Source code is available from a link at the bottom of the post.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Patterns are techniques used to approach solving a common programming problem. They are object-oriented, but language independent. The &lt;span style="font-style: italic;"&gt;Decorator&lt;/span&gt; pattern is a very useful pattern, but not one that gets a lot of attention. The "Gang of Four" book (by Gamma, Helm, &lt;span style="font-style: italic;"&gt;et.al.&lt;/span&gt; see below) defines the purpose of this pattern as:&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-weight: bold;"&gt;Attach additional responsibiliti&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;es to an object dynamically. Decorators provide a flexible alte&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;rnative to subclassing for extending functionality.&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;A decorator class adds a new capability to an existing object by impersonating the original object and intercepting methods calls to it. By intercepting it can modify the normal result that the original method call would have. To impersonate the original, both the original class and the decorator class must implement the same interface.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_aWZdKhwZDT4/SiMWQQ8jEiI/AAAAAAAAABo/_Tpl18I_aaI/s1600-h/Decorator.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 259px; height: 198px;" src="http://2.bp.blogspot.com/_aWZdKhwZDT4/SiMWQQ8jEiI/AAAAAAAAABo/_Tpl18I_aaI/s320/Decorator.png" alt="" id="BLOGGER_PHOTO_ID_5342138051674182178" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Typically, the decorator object's methods will perform some additional operation and also call the original object's method. This enhances the result of the original operation in some way.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_aWZdKhwZDT4/SiWm6ueqzRI/AAAAAAAAABw/90q4u--5_Hk/s1600-h/Decorator-diagram.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 75px;" src="http://4.bp.blogspot.com/_aWZdKhwZDT4/SiWm6ueqzRI/AAAAAAAAABw/90q4u--5_Hk/s320/Decorator-diagram.png" alt="" id="BLOGGER_PHOTO_ID_5342860060784250130" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a name="books"&gt;The advantages of the decorator pattern are:&lt;br /&gt;&lt;/a&gt;&lt;ul&gt;&lt;li&gt;Decorators can be chained to add multiple behaviors&lt;/li&gt;&lt;li&gt;Very flexible reuse of code&lt;/li&gt;&lt;li&gt;Can be runtime decision (unlike inheritance)&lt;/li&gt;&lt;li&gt;Avoids huge inheritance trees&lt;a name="books"&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;a name="books"&gt;&lt;/a&gt;As an example of using the decorator pattern consider Java's &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html"&gt;InputStream&lt;/a&gt;/&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html"&gt;OutputStream&lt;/a&gt; classes. Java defines subclasses of these (&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterIntputStream.html"&gt;FilterInputStream&lt;/a&gt;/&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterOutputStream.html"&gt;FilterOutputStream&lt;/a&gt;) specifically designed to create decorators.&lt;br /&gt;&lt;br /&gt;Let's look at how to create a decorator for any &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html"&gt;OutputStream&lt;/a&gt; using &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterOutputStream.html"&gt;FilterOutputStream&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_aWZdKhwZDT4/SibkER462RI/AAAAAAAAAB4/7zm1lPLYuQc/s1600-h/OutputStream.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 276px; height: 320px;" src="http://4.bp.blogspot.com/_aWZdKhwZDT4/SibkER462RI/AAAAAAAAAB4/7zm1lPLYuQc/s320/OutputStream.png" alt="" id="BLOGGER_PHOTO_ID_5343208770094422290" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The &lt;span style="font-style: italic;"&gt;out&lt;/span&gt; attribute of the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterOutputStream.html"&gt;FilterOutputStream&lt;/a&gt; is a reference to the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html"&gt;OutputStream&lt;/a&gt; that the class is decoratoring. This attribute is initialized by the constructor of &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterOutputStream.html"&gt;FilterOutputStream&lt;/a&gt;. The default implementations of the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterOutputStream.html"&gt;FilterOutputStream&lt;/a&gt; methods just forward their call exactly to its &lt;span style="font-style: italic;"&gt;out&lt;/span&gt; attribute.&lt;br /&gt;&lt;br /&gt;We'll create a decorator named DebugOutputStream that traces the method calls on an &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html"&gt;OutputStream&lt;/a&gt;. First lets look at the beginning of its definition:&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;public class&lt;/span&gt; DebugOutputStream &lt;span style="color: rgb(51, 102, 255);"&gt;extends&lt;/span&gt; FilterOutputStream {&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;  private&lt;/span&gt; String name;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;  public&lt;/span&gt; DebugOutputStream(String name, OutputStream out) {&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;    super&lt;/span&gt;(out);&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;    this&lt;/span&gt;.name = name;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The &lt;span style="font-style: italic;"&gt;name&lt;/span&gt; parameter is used to label the output of this particular DebugOutputStream (useful if there is more than one). The &lt;span style="font-style: italic;"&gt;out&lt;/span&gt; parameter is passed up to the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterOutputStream.html"&gt;FilterOutputStream&lt;/a&gt; and is used to initialize it's protected out attribute.&lt;br /&gt;&lt;br /&gt;Let's look at what the &lt;span style="font-style: italic;"&gt;close&lt;/span&gt; method of DebugOutputStream:&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;public void&lt;/span&gt; close() &lt;span style="color: rgb(51, 102, 255);"&gt;throws&lt;/span&gt; IOException {&lt;br /&gt;&lt;br /&gt;  StringBuilder msg = &lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; StringBuilder(&lt;br /&gt;     &lt;span style="color: rgb(255, 0, 0);"&gt;"DebugOutputStream("&lt;/span&gt; + name + &lt;span style="color: rgb(255, 0, 0);"&gt;"): called void close()"&lt;/span&gt;);&lt;br /&gt;  &lt;span style="color: rgb(51, 102, 255);"&gt;try&lt;/span&gt; {&lt;br /&gt;&lt;br /&gt;    out.close();   &lt;span style="color: rgb(0, 102, 0);"&gt;// Call to decorated object&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  } &lt;span style="color: rgb(51, 102, 255);"&gt;catch&lt;/span&gt;( IOException e) {&lt;br /&gt;&lt;br /&gt;    msg.append( &lt;span style="color: rgb(255, 0, 0);"&gt;" Exception: "&lt;/span&gt; + e + &lt;span style="color: rgb(255, 0, 0);"&gt;" thrown"&lt;/span&gt;);&lt;br /&gt;    &lt;span style="color: rgb(51, 102, 255);"&gt;throw&lt;/span&gt; e;&lt;br /&gt;&lt;br /&gt;  } &lt;span style="color: rgb(51, 102, 255);"&gt;catch&lt;/span&gt;( RuntimeException e) {&lt;br /&gt;&lt;br /&gt;     msg.append( &lt;span style="color: rgb(255, 0, 0);"&gt;" Exception: "&lt;/span&gt; + e + &lt;span style="color: rgb(255, 0, 0);"&gt;" thrown"&lt;/span&gt;);&lt;br /&gt;    &lt;span style="color: rgb(51, 102, 255);"&gt;throw&lt;/span&gt; e;&lt;br /&gt;&lt;br /&gt;  } &lt;span style="color: rgb(51, 102, 255);"&gt;finally&lt;/span&gt; {&lt;br /&gt;&lt;br /&gt;     System.out.println( msg);&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Most of the code is composing a message to print out information about the call to &lt;span style="font-style: italic;"&gt;close&lt;/span&gt; on the decorated object (&lt;span style="font-style: italic;"&gt;i.e.&lt;/span&gt;, referenced in the &lt;span style="font-style: italic;"&gt;out&lt;/span&gt; attribute).&lt;br /&gt;&lt;br /&gt;Here's an example of using the DebugOutputStream to spy on how &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/GZIPOutputStream.html"&gt;GZIPOutputStream&lt;/a&gt; works.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;&lt;br /&gt;InputStream input = &lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; BufferedInputStream( &lt;br /&gt;  &lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; FileInputStream( args[0]));&lt;br /&gt;OutputStream output = &lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; DebugOutputStream( &lt;span style="color: rgb(255, 0, 0);"&gt;"GZIPOutputStream"&lt;/span&gt;,&lt;br /&gt;  &lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; GZIPOutputStream(&lt;br /&gt;  &lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; DebugOutputStream( &lt;span style="color: rgb(255, 0, 0);"&gt;"FileOutputStream"&lt;/span&gt;,&lt;br /&gt;  &lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; FileOutputStream( args[1]))));&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;byte&lt;/span&gt; [] buffer =&lt;span style="color: rgb(51, 102, 255);"&gt; new byte&lt;/span&gt;[4096];&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;int&lt;/span&gt; numRead;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;while&lt;/span&gt;( (numRead = input.read(buffer)) &gt; 0) {&lt;br /&gt;&lt;br /&gt;  output.write( buffer, 0, numRead);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;input.close();&lt;br /&gt;output.close();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Note that the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedInputStream.html"&gt;BufferedInputStream&lt;/a&gt; is acting as a decorator to the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileInputStream.html"&gt;FileInputStream&lt;/a&gt;. Two DebugOutputStreams are used to trace themethod calls at two places. The first one (given the name "GZIPOutputStream") will trace the calls made to the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/GZIPOutputStream.html"&gt;GZIPOutputStream&lt;/a&gt;. The second one will trace the calls made to the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileOutputStream.html"&gt;FileOutputStream&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The output of this program on a sample input file is:&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-size:78%;"&gt;DebugOutputStream(FileOutputStream): called void write( byte []) (10 bytes)&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void write(byte [], 0, 4096)&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void write(byte [], 0, 4096)&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void write(byte [], 0, 4096)&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void write(byte [], 0, 4096)&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void write(byte [], 0, 4096)&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void write(byte [], 0, 4096)&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void write(byte [], 0, 4096)&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void write(byte [], 0, 4096)&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void write(byte [], 0, 3424)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 512)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void write(byte [], 0, 253)&lt;br /&gt;DebugOutputStream(FileOutputStream): called void close()&lt;br /&gt;DebugOutputStream(GZIPOutputStream): called void close()&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;br /&gt;From this we can see that first a 10 byte header is written out to the file, then we see the data is being given to the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/GZIPOutputStream.html"&gt;GZIPOutputStream&lt;/a&gt; in 4096 byte chunks. But the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/GZIPOutputStream.html"&gt;GZIPOutputStream&lt;/a&gt; is not writing the data out immediately. Only after all the data from the input file has been exhausted is the compressed data written out to the file and in 512 byte chunks. For a bigger input file, compressed data would have been written out before the end. &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/GZIPOutputStream.html"&gt;GZIPOutputStream&lt;/a&gt; has some internal cache that is written out when it fills up or the stream is closed. The latter is happening here.&lt;br /&gt;&lt;br /&gt;I also used this same technique to help with layout problems with Swing panels. One can create a &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JComponent.html"&gt;JComponent&lt;/a&gt; or &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JPanel.html"&gt;JPanel&lt;/a&gt; decorator that prints out how the methods used by LayoutManagers (like getSize(), getPreferredSize(), etc.) to see how the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/LayoutManager.html"&gt;LayoutManager&lt;/a&gt; is interrogating your class to lay it out.&lt;br /&gt;&lt;p&gt;Next, let's look at another use of a decorator to add a more practical enhancement to an input/output stream, encryption. I am not an expert on cryptology at all, so please don't use this code for any information you really need to secure! To encrypt data, I &lt;a href="http://en.wikipedia.org/wiki/Exclusive_or"&gt;exclusive OR&lt;/a&gt; the data with a sequence of pseudo-random bytes as a mask. I then use the fact that:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;blockquote&gt;(x ^ mask) ^ mask == x&lt;/blockquote&gt;to decrypt the data (^ is the exclusive OR operator in Java, C and C++). By exclusive OR'ing the encrypted data with the same sequence of pseudo-random bytes I will get the original data back.&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;The abstract &lt;span style="font-style: italic;"&gt;EncryptingOutputStream&lt;/span&gt; class extends &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterOutputStream.html"&gt;FilterOutputStream&lt;/a&gt; and overrides the &lt;span style="font-style: italic;"&gt;write(int)&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;write( byte [], int, int)&lt;/span&gt; methods. The &lt;span style="font-style: italic;"&gt;write( byte [])&lt;/span&gt; method calls &lt;span style="font-style: italic;"&gt;write( byte[], int, int)&lt;/span&gt; so I don't need to override it. An abstract method &lt;span style="font-style: italic;"&gt;void encrypt( byte [])&lt;/span&gt; is defined that must be overridden to do the actual encryption of the data. (This is an example of the &lt;a href="http://en.wikipedia.org/wiki/Template_method_pattern"&gt;Template Method&lt;/a&gt; pattern.)&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Similarly, there is an abstract &lt;span style="font-style: italic;"&gt;DecryptingInputStream&lt;/span&gt; class that extends &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterIntputStream.html"&gt;FilterInputStream&lt;/a&gt; and overrides the &lt;span style="font-style: italic;"&gt;int read()&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;int read( byte [], int, int) &lt;/span&gt;methods. It defines an abstract method &lt;span style="font-style: italic;"&gt;void decrypt( byte [], int, int)&lt;/span&gt; that must decrypt the data in the specified section of the array passed to it.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Concrete classes named &lt;span style="font-style: italic;"&gt;XOREncryptingOutputStream&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;XORDecryptingInputStream&lt;/span&gt; are defined that define the appropriate abstract methods. Both classes are passed a &lt;span style="font-style: italic;"&gt;key&lt;/span&gt; to their constructors. This key is used as a seed to a random number generator. If the key is the same, the same pseudo-random numbers will be generated to encrypt/decrypt the data.&lt;/p&gt;&lt;p&gt;As an example, putting all this together I compressed then encrypted (with the key 1234567890) a web page and put it on my web site at &lt;span style="font-style: italic;"&gt;http://www.drpaulcarter.com/mystery&lt;/span&gt;. Let's look at how to use the classes discussed here to read the web page. We need to create a chain of InputStreams that read the web page, decrypt it and then uncompress it. We can do that with the following code:&lt;/p&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; GZIPInputStream(&lt;br /&gt;        &lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; XORDecryptingInputStream(&lt;br /&gt;        &lt;span style="color: rgb(51, 102, 255);"&gt;new&lt;/span&gt; URL(url).openStream(), key)));&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;The &lt;span style="font-style: italic;"&gt;url&lt;/span&gt; variable is string containing "http://www.drpaulcarter.com/mystery" Then we only need to read from the GZIPInputStream as we would any InputStream. Here's a sequence diagram showing what happens as data is read:&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_aWZdKhwZDT4/SihZYTQDiGI/AAAAAAAAACI/squGTkwI1Qw/s1600-h/sequence.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 145px;" src="http://3.bp.blogspot.com/_aWZdKhwZDT4/SihZYTQDiGI/AAAAAAAAACI/squGTkwI1Qw/s400/sequence.png" alt="" id="BLOGGER_PHOTO_ID_5343619231894440034" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;To download and display the web page type: &lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;java test.DecryptingBrowser http://www.drpaulcarter.com/mystery 1234567890&lt;/span&gt;&lt;/p&gt;&lt;p&gt;An important point to realize is the InputStream reading the data from the web server knows nothing about compression or encryption. However, we have dynamically added these capabilities by chaining it to together with decorators that add these capabilities. This is the power of the decorator pattern.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;I originally developed this example back in 1998 when I was teaching Java. This was in the Java 1.2 days. In creating this presentation, I discovered that Java 1.4 added encryption streams, &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/crypto/CipherInputStream.html"&gt;javax.crypto.CipherInputStream&lt;/a&gt; and &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/crypto/CipherOutputStream.html"&gt;javax.crypto.CipherOutputStream&lt;/a&gt;. They both use a &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/crypto/Cipher.html"&gt;javax.crypto.Cipher&lt;/a&gt; object to do the encryption and decryption. This is yet another pattern, the &lt;a href="http://en.wikipedia.org/wiki/Strategy_pattern"&gt;Strategy&lt;/a&gt; pattern.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;The full example code of the classes described here can be found &lt;a href="http://www.drpaulcarter.com/cs/decorator.zip"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;a name="books"&gt;Recommended books:&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=pcas-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=as1&amp;amp;asins=0201633612&amp;amp;md=10FE9736YVPPT7A0FBG2&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr" style="width: 120px; height: 240px;" marginwidth="0" marginheight="0" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=pcas-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=as1&amp;amp;asins=0321333020&amp;amp;md=10FE9736YVPPT7A0FBG2&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr" style="width: 120px; height: 240px;" marginwidth="0" marginheight="0" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=pcas-20&amp;o=1&amp;p=8&amp;l=as1&amp;asins=0596007124&amp;md=10FE9736YVPPT7A0FBG2&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"&gt;&lt;/iframe&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12463714-4115575209499557108?l=drpaulcarter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://drpaulcarter.blogspot.com/feeds/4115575209499557108/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12463714&amp;postID=4115575209499557108' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/4115575209499557108'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/4115575209499557108'/><link rel='alternate' type='text/html' href='http://drpaulcarter.blogspot.com/2009/05/decorator-pattern.html' title='The Decorator Pattern'/><author><name>Paul Carter</name><uri>http://www.blogger.com/profile/11141659329083855293</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_aWZdKhwZDT4/SiMWQQ8jEiI/AAAAAAAAABo/_Tpl18I_aaI/s72-c/Decorator.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12463714.post-2208636801567888068</id><published>2009-05-13T19:33:00.002-04:00</published><updated>2009-05-13T20:54:01.176-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c c++'/><title type='text'>typedef in C and C++</title><content type='html'>I like C++, but it is a complex language. After programming in it for 19 years, you'd think I would know it pretty well, but I'm still learning things even now.&lt;br /&gt;&lt;br /&gt;Recently, I discovered that &lt;span style="font-weight: bold;"&gt;typedef&lt;/span&gt; works differently in C++ than in C. In C, you can't repeat a &lt;span style="font-weight: bold;"&gt;typedef&lt;/span&gt; even if it's exactly the same as the original definition. For example:&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;&lt;br /&gt; typedef int T;&lt;br /&gt; typedef int T;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;will give you a compiler error in C. However, this is perfectly legal in C++. I'm pretty sure this wasn't the case in early C++. I wonder when this changed? Anybody know?&lt;br /&gt;&lt;br /&gt;I found a very good web site for the differences in C and C++ here:&lt;br /&gt;&lt;a href="http://david.tribble.com/text/cdiffs.htm"&gt;http://david.tribble.com/text/cdiffs.htm&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12463714-2208636801567888068?l=drpaulcarter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://drpaulcarter.blogspot.com/feeds/2208636801567888068/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12463714&amp;postID=2208636801567888068' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/2208636801567888068'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/2208636801567888068'/><link rel='alternate' type='text/html' href='http://drpaulcarter.blogspot.com/2009/05/typedef-in-c-and-c.html' title='typedef in C and C++'/><author><name>Paul Carter</name><uri>http://www.blogger.com/profile/11141659329083855293</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12463714.post-8581655116110825673</id><published>2009-03-21T13:21:00.000-04:00</published><updated>2009-03-22T09:59:34.128-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='debugging'/><category scheme='http://www.blogger.com/atom/ns#' term='c'/><title type='text'>Finding the source of an error in an inlined function using gdb</title><content type='html'>Yesterday at work, I was trying to find the source of a crash from a coredump using &lt;span style="font-style: italic;"&gt;gdb&lt;/span&gt; and discovered that if the crash was caused by inlined code, the stack trace that &lt;span style="font-style: italic;"&gt;gdb&lt;/span&gt; shows does not show you the information you might need to determine the exact location. In this case, &lt;span style="font-style: italic;"&gt;gdb&lt;/span&gt; told me that the crash occurred in a call to &lt;span style="font-style: italic;"&gt;std::string::size()&lt;/span&gt;. However, there was no call to this method in the next function up in the stack trace. The reason was that &lt;span style="font-style: italic;"&gt;std::string::size()&lt;/span&gt; was being called from another inlined function, but &lt;span style="font-style: italic;"&gt;gdb&lt;/span&gt; wasn't showing what that function that was. After fumbling around for about 30 minutes I was able to figure out that the crash was happening inside a &lt;span style="font-style: italic;"&gt;std::map::find()&lt;/span&gt; call (the&lt;span style="font-style: italic;"&gt; map&lt;/span&gt;'s key was a &lt;span style="font-style: italic;"&gt;std::string&lt;/span&gt;). I want to document the procedure that I used here.&lt;br /&gt;&lt;br /&gt;Let's use a simple program that illustrates the problem (in a file named &lt;span style="font-style: italic;"&gt;gdbtest.cpp&lt;/span&gt;):&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;&lt;br /&gt;inline void f1( int *p)&lt;br /&gt;{&lt;br /&gt;  (*p)++;   // Line 5&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;inline void f2( int * p)&lt;br /&gt;{&lt;br /&gt;  std::printf("%p\n", p);  // Line 10&lt;br /&gt;  f1(p);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  int i;&lt;br /&gt;&lt;br /&gt;  f2(&amp;amp;i);&lt;br /&gt;  std::printf( "%d\n", i);     // Line 19&lt;br /&gt;  f2(0);&lt;br /&gt;  std::printf( "%d\n", i);&lt;br /&gt;  f2(&amp;amp;i);&lt;br /&gt;  std::printf( "%d\n", i);&lt;br /&gt;&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here it's obvious which line is going to cause the segmentation fault, but the program is useful to demonstrate the general technique.&lt;br /&gt;&lt;br /&gt;Build the executable with:&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;g++ -g -finline -o gdbtest gdbtest.cpp&lt;/span&gt;&lt;br /&gt;The &lt;span style="font-style: italic;"&gt;-finline&lt;/span&gt; flag tells g++ to inline the functions even in debug mode.&lt;br /&gt;&lt;br /&gt;Make sure that core files are enabled by typing:&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;ulimit -c unlimited&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now run the program:&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;./gdbtest&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The output I get from a run (I'm running 64-bit Linux):&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;0x7fff2cdee1fc&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;(nil)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Segmentation fault (core dumped)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Start up &lt;span style="font-style: italic;"&gt;gdb&lt;/span&gt; with the corefile:&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;gdb gdbtest core&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;gdb&lt;/span&gt; displays it's header, etc. at the bottom appears:&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;#0  0x00000000004005fb in main () at gdbtest.cpp:5&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;5      (*p)++;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;(gdb) &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The hex address will probably be different for you. If you type &lt;span style="font-style: italic;"&gt;where&lt;/span&gt; (or &lt;span style="font-style: italic;"&gt;info stack)&lt;/span&gt; to see the stack trace you get (user input is in &lt;span style="font-style: italic;"&gt;italics&lt;/span&gt;):&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;(gdb) &lt;span style="font-style: italic;"&gt;where&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;#0  0x00000000004005fb in main () at gdbtest.cpp:5&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is not very useful, there is only one stack frame shown, &lt;span style="font-style: italic;"&gt;main&lt;/span&gt;'s. This does not show us which line in the actual body of the &lt;span style="font-style: italic;"&gt;main&lt;/span&gt; function is the source of the segmentation fault. Line 5 is the line in the inlined &lt;span style="font-style: italic;"&gt;f1&lt;/span&gt; function that is called multiple times.&lt;br /&gt;&lt;br /&gt;The key to determining the real location is the hex address listed. This is the address of the machine instruction that caused the fault. You can use the &lt;span style="font-style: italic;"&gt;info line&lt;/span&gt; gdb command to show the source line that maps to a code address.&lt;br /&gt;&lt;br /&gt;Typing:&lt;br /&gt;  &lt;span style="font-weight: bold; font-style: italic;"&gt;info line *0x4005fb&lt;/span&gt;&lt;br /&gt;displays:&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Line 5 of "gdbtest.cpp" starts at address 0x4005f7 &lt;main+111&gt; and ends at 0x400606 &lt;main+126&gt;.&lt;/main+126&gt;&lt;/main+111&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Again the actual address values will probably be different for you. Here &lt;span style="font-style: italic;"&gt;gdb&lt;/span&gt; is telling you that line 5 maps to the code from address 0x4005f7 to 0x400606. For ordinary code, a source line will map to only a single range of code. However, inline code will be mapped to multiple places. Every place that the code is inlined to will be mapped to the inline source. In the example above, line 5 will mapped to three different places since it is used it &lt;span style="font-style: italic;"&gt;f2&lt;/span&gt; which itself is inlined and used in three different places. The problem is to find which of these three places is causing the segmentation fault.&lt;br /&gt;&lt;br /&gt;The key is to look at the code around the faulting code to determine where we are in &lt;span style="font-style: italic;"&gt;main&lt;/span&gt;. From the above, we see that line 5 starts at address 0x4005f7, so the previous instruction will end at address 0x4005f6, we can use info line to see what line that is:&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;(gdb) &lt;span style="font-style: italic;"&gt;info line *0x4005f6&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Line 10 of "gdbtest.cpp" starts at address 0x4005dc &lt;main+84&gt; and ends at 0x4005f7 &lt;main+111&gt;.&lt;/main+111&gt;&lt;/main+84&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This tells us that the line executed before the crashing line was line 10. This is just what we expected, but doesn't tell us which call to &lt;span style="font-style: italic;"&gt;f2&lt;/span&gt; caused the crash. If we continue moving backward, using address 0x4005db (the address right before 0x4005dc where line 10 starts):&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;(gdb) info line *0x4005db&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Line 19 of "gdbtest.cpp" starts at address 0x4005c2 &lt;main+58&gt; and ends at 0x4005dc &lt;main+84&gt;.&lt;/main+84&gt;&lt;/main+58&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Line 19 is in the body of main and this tells us that it is second call to &lt;span style="font-style: italic;"&gt;f2&lt;/span&gt; (line 20) that caused the crash.&lt;br /&gt;&lt;br /&gt;It appears that &lt;span style="font-style: italic;"&gt;gdb &lt;/span&gt;has added a &lt;a href="http://sources.redhat.com/gdb/current/onlinedocs/gdb_9.html#SEC64"&gt;new option to the disassemble command, /m&lt;/a&gt; that would display the assembly code with addresses mixed in with the source code. This would allow one to easily determine the location. However, this option is not in the most recent version of &lt;span style="font-style: italic;"&gt;gdb&lt;/span&gt; I have access to (6.8)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12463714-8581655116110825673?l=drpaulcarter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://drpaulcarter.blogspot.com/feeds/8581655116110825673/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12463714&amp;postID=8581655116110825673' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/8581655116110825673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/8581655116110825673'/><link rel='alternate' type='text/html' href='http://drpaulcarter.blogspot.com/2009/03/finding-source-of-error-in-inlined.html' title='Finding the source of an error in an inlined function using gdb'/><author><name>Paul Carter</name><uri>http://www.blogger.com/profile/11141659329083855293</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12463714.post-1689778026061984715</id><published>2009-03-08T19:56:00.000-04:00</published><updated>2009-03-08T20:11:58.068-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='design'/><title type='text'>UML Considered Harmful</title><content type='html'>At work, we use &lt;a href="http://en.wikipedia.org/wiki/Unified_Modeling_Language"&gt;Unified Modeling Language&lt;/a&gt; (&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_0"&gt;UML&lt;/span&gt;) to document our designs. I think &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_1"&gt;UML&lt;/span&gt; has its place as &lt;span style="font-style: italic;"&gt;part&lt;/span&gt; of the design process and documentation, but I believe that &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_2"&gt;UML&lt;/span&gt; is not in itself sufficient to document a non-trivial design. I have been to numerous design reviews where a few &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_3"&gt;UML&lt;/span&gt; class and sequence diagrams are the only description of the design. I find these inadequate to communicate more than a vague idea of the overall design.&lt;br /&gt;&lt;br /&gt;For a complete description of the design, I feel that a full design document with words and &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_4"&gt;pseudocode&lt;/span&gt; is a requirement. I get the impression that &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_5"&gt;UML&lt;/span&gt; diagrams are being used as a way to say that a design is documented (for documentation requirements like &lt;a href="http://en.wikipedia.org/wiki/Capability_Maturity_Model_Integration"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_6"&gt;CMMI&lt;/span&gt;&lt;/a&gt;) without really providing any meaningful documentation.&lt;br /&gt;&lt;br /&gt;If anyone is out there, how is your company documenting design? Do you think your process works?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12463714-1689778026061984715?l=drpaulcarter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://drpaulcarter.blogspot.com/feeds/1689778026061984715/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12463714&amp;postID=1689778026061984715' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/1689778026061984715'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/1689778026061984715'/><link rel='alternate' type='text/html' href='http://drpaulcarter.blogspot.com/2009/03/uml-considered-harmful.html' title='UML Considered Harmful'/><author><name>Paul Carter</name><uri>http://www.blogger.com/profile/11141659329083855293</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12463714.post-4508108112745298886</id><published>2008-12-22T09:32:00.000-05:00</published><updated>2009-03-22T10:04:49.764-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='jar'/><category scheme='http://www.blogger.com/atom/ns#' term='ant'/><title type='text'>Jar files and indexing</title><content type='html'>Recently at work, I ran into an interesting "feature" of how jar files and indexing work with Java and &lt;a href="http://ant.apache.org/"&gt;ant&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;For background on indexing see:&lt;br /&gt;&lt;div&gt;&lt;a href="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#JAR%20Index"&gt;http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#JAR%20Index&lt;/a&gt;&lt;/div&gt; &lt;div&gt;&lt;a href="http://closingbraces.net/2007/05/13/jarclasspathandindex/"&gt;http://closingbraces.net/2007/05/13/jarclasspathandindex/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;On some other developer's machines, some java applications were failing with NoClassDefFoundError exceptions. However, I personally never saw this error. The jar's with the classes that were not being found were listed in the Class-Path entry of the manifest of the jar that used them. I was extremely puzzled  by this. I could fix the problem by explicitly adding the jar's to the classpath, but I didn't understand why I needed to do this when everything worked fine on my machine.&lt;br /&gt;&lt;br /&gt;From the title of this post you can probably guess that the problem involved indexing.  Our build process (using &lt;span style="font-style: italic;"&gt;ant&lt;/span&gt;), creates a jar (let's call in &lt;span style="font-style: italic;"&gt;myjar.jar&lt;/span&gt;) with the &lt;span style="font-style: italic;"&gt;&amp;lt;jar&amp;gt;&lt;/span&gt; task, then it runs "jar -i" on it to index it (using an &lt;span style="font-style: italic;"&gt;&lt;exec&gt;&lt;/exec&gt;&lt;/span&gt;). If all this happens everything works fine. However, there seems to be a race condition between when &lt;span style="font-style: italic;"&gt;ant&lt;/span&gt; closes the jar file that it created with the &lt;span style="font-style: italic;"&gt;&amp;lt;jar&amp;gt;&lt;/span&gt; task and when the "jar -i" command is run. When this happens, the "jar -i" command fails because the file is still locked and the index is not created.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;I will go into exactly why this caused the error in a minute, but first I want to look at how I first tried to fix this because it also brings up an important point on the problem. The &lt;span style="font-style: italic;"&gt;&amp;lt;jar&amp;gt;&lt;/span&gt; task has an optional attribute named "index" which can be used to have &lt;span style="font-style: italic;"&gt;ant&lt;/span&gt; index the file. I tried using this instead of calling "jar -i" on it, but it did not fix the problem. Why didn't it? Because setting &lt;span style="font-style: italic;"&gt;index="true"&lt;/span&gt; in the &lt;span style="font-style: italic;"&gt;&amp;lt;jar&amp;gt;&lt;/span&gt; task does &lt;span style="font-weight: bold;"&gt;not&lt;/span&gt; do the same thing as running "jar -i" on the jar! This is very uninituitive! So what is the difference? Using the &amp;lt;jar&amp;gt; task, on the classes in the jar itself are added to the index; however, using "jar -i" the classes in the jar &lt;span style="font-style: italic;"&gt;and all the classes used by the jar&lt;/span&gt; are added to the index.&lt;br /&gt;&lt;br /&gt;Why does this make a difference? The key is in the second &lt;a href="http://closingbraces.net/2007/05/13/jarclasspathandindex/"&gt;link&lt;/a&gt; above. Quoting:&lt;br /&gt;&lt;div&gt;&lt;blockquote&gt;The executable jar must either not have a META-INF/INDEX.LST file, or if it  does have such an INDEX.LST file this needs to list the contents of both the  executable jar and all of the other jars as well. Anything not in this list will  not be found on the class path, regardless of the “Class-Path” entry in the  manifest...&lt;/blockquote&gt;&lt;/div&gt;&lt;br /&gt;&lt;div dir="ltr"&gt;Two things to note, the filename I see is META-INF/INDEX.LIST (not  LST) and the link goes on to say:&lt;br /&gt;&lt;blockquote&gt;and regardless of any command-line “-classpath” (which is ignored anyway).&lt;/blockquote&gt;This did not apply in my case. We are using &lt;span style="font-style: italic;"&gt;java -cp file.jar file.MainClass&lt;/span&gt; to run our apps, not &lt;span style="font-style: italic;"&gt;java -jar file.jar&lt;/span&gt;. In a nutshell, if the jar has an index, it's Class-Path entry is ignored.&lt;br /&gt;&lt;br /&gt;In my case, our application uses &lt;span style="font-style: italic;"&gt;jacorb.jar&lt;/span&gt; (from &lt;a href="http://www.jacorb.org/"&gt;JacORB&lt;/a&gt;). It uses two other jars: &lt;em&gt;logkit-1.2.jar &lt;/em&gt;and &lt;em&gt;avalon-framework-4.1.5.jar&lt;/em&gt;. These are the two jars I had to add to the classpath to fix the problem. My jar has &lt;span style="font-style: italic;"&gt;jacorb.jar&lt;/span&gt; listed in its Class-Path entry.&lt;br /&gt;&lt;br /&gt;If everything builds properly and &lt;span style="font-style: italic;"&gt;myjar.jar&lt;/span&gt; is indexed using "jar -i", everything works because entries for all the classes used by &lt;span style="font-style: italic;"&gt;myjar.jar&lt;/span&gt; are included in the index (including the ones in &lt;em&gt;logkit-1.2.jar &lt;/em&gt;and &lt;em&gt;avalon-framework-4.1.5.jar&lt;/em&gt;. The Class-Path entry is ignored since the index is present.&lt;br /&gt;&lt;br /&gt;However,  if the "jar -i" command fails, then the Class-Path entry is in effect and classes in &lt;span style="font-style: italic;"&gt;jacorb.jar&lt;/span&gt; can be resolved, but not the classes that &lt;span style="font-style: italic;"&gt;jacorb.jar&lt;/span&gt; uses. Why not? Because &lt;span style="font-style: italic;"&gt;jacorb.jar&lt;/span&gt; was indexed using &lt;span style="font-style: italic;"&gt;ant&lt;/span&gt;. So, it has an index, but it only contains its own classes, not the ones in the other two jars.&lt;br /&gt;&lt;br /&gt;If you want to see this problem yourself. You can download the code I used to verify this from &lt;a href="http://www.drpaulcarter.com/cs/jartest.zip"&gt;here&lt;/a&gt;. It has 3 jars: &lt;em&gt;myjar.jar&lt;/em&gt;, &lt;em&gt;dependjar.jar &lt;/em&gt;and  &lt;em&gt;depend2jar.jar&lt;/em&gt;. myjar uses dependjar which uses depend2jar. This is  equivalent to &lt;span style="font-style: italic;"&gt;myjar.jar&lt;/span&gt; which uses &lt;em&gt;jacorb.jar &lt;/em&gt;which uses  &lt;em&gt;logkit-1.2.jar&lt;/em&gt;. &lt;div&gt;&lt;br /&gt;The ant script by default will build the jars without indexing and use  &lt;em&gt;jar -i&lt;/em&gt; to create indexed versions of the jars named  &lt;em&gt;imyjar.jar&lt;/em&gt;, &lt;em&gt;idependjar.jar &lt;/em&gt;and &lt;em&gt;idepend2jar.jar&lt;/em&gt;.&lt;/div&gt; &lt;div&gt; &lt;/div&gt; &lt;div&gt;If you run ant with -&lt;em&gt;Dindex="yes"&lt;/em&gt; it will index the plain jars  (&lt;em&gt;myjar.jar&lt;/em&gt;, &lt;em&gt;dependjar.jar &lt;/em&gt;and &lt;em&gt;depend2jar.jar&lt;/em&gt;) using  ant's index method.&lt;/div&gt; &lt;div&gt; &lt;/div&gt; &lt;div&gt;To recreate the problem I saw, type:&lt;/div&gt; &lt;blockquote dir="ltr" style="margin-right: 0px;"&gt; &lt;div&gt;ant&lt;/div&gt; &lt;div&gt;ant build.dependjar -Dindex="yes"&lt;/div&gt; &lt;div&gt;java -cp myjar.jar myjar.MyClass&lt;/div&gt;&lt;/blockquote&gt; &lt;div dir="ltr"&gt;The first line builds all the plain jars with no indexing. The  second line replaces &lt;em&gt;dependjar.jar &lt;/em&gt;with one using &lt;span style="font-style: italic;"&gt;ant&lt;/span&gt;'s index funciton  (just like &lt;em&gt;jacorb.jar&lt;/em&gt;). The last line demonstrates the error that  occurs. You can also verify that adding &lt;span style="font-style: italic;"&gt;dependjar.jar&lt;/span&gt; to the classpath doesn't fix the error either.&lt;br /&gt;&lt;br /&gt;In general I see several ways to fix this problem.&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Avoid using indexing&lt;/li&gt;&lt;li&gt;Put all the jars on the classpath&lt;/li&gt;&lt;li&gt;Fully index all jars with "jar -i"&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;Hopefully this post will help anyone else running into this problem.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12463714-4508108112745298886?l=drpaulcarter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://drpaulcarter.blogspot.com/feeds/4508108112745298886/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12463714&amp;postID=4508108112745298886' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/4508108112745298886'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/4508108112745298886'/><link rel='alternate' type='text/html' href='http://drpaulcarter.blogspot.com/2008/12/jar-files-and-indexing.html' title='Jar files and indexing'/><author><name>Paul Carter</name><uri>http://www.blogger.com/profile/11141659329083855293</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12463714.post-4619302414248781122</id><published>2008-12-21T13:07:00.001-05:00</published><updated>2008-12-21T13:14:59.249-05:00</updated><title type='text'>Time to start using this blog?</title><content type='html'>I guess it might be time to start using this blog.&lt;br /&gt;&lt;br /&gt;Here's some background. I'm a software developer and ex-CS professor. When I was teaching, I wrote a &lt;a href="http://www.drpaulcarter.com/pcasm"&gt;book&lt;/a&gt; on 32-bit x86 assembly language that I provide free on the web.&lt;br /&gt;&lt;br /&gt;In my current job, I mostly program in C++ with some Java. I'm also a big fan of Python. I expect that most of my posts with be dealing with these languages (and assembly of course).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12463714-4619302414248781122?l=drpaulcarter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://drpaulcarter.blogspot.com/feeds/4619302414248781122/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12463714&amp;postID=4619302414248781122' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/4619302414248781122'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12463714/posts/default/4619302414248781122'/><link rel='alternate' type='text/html' href='http://drpaulcarter.blogspot.com/2008/12/time-to-start-using-this-blog.html' title='Time to start using this blog?'/><author><name>Paul Carter</name><uri>http://www.blogger.com/profile/11141659329083855293</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry></feed>
