Wil Boayue

Software Engineer/Architect

Java Automatic Resource Management

You might have seen this example from The Java Tutorials.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    public class CopyBytes {
        public static void main(String[] args) throws IOException {

            FileInputStream in = null;
            FileOutputStream out = null;

            try {
                in = new FileInputStream("xanadu.txt");
                out = new FileOutputStream("outagain.txt");
                int c;

                while ((c = in.read()) != -1) {
                    out.write(c);
                }
            } finally {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            }
        }
    }

Unfortunately, this code has a bug. If an exception is thrown while closing the input stream on line #21, the output stream will not be closed on line #24.

Prior to Java 7 a clean version of this could be written with the Commons IO: IOUtils or Google Guava: Closer utility classes.

The following is a correct implementation using Google Guava.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    public class CopyBytes {
        public static void main(String[] args) throws IOException {

            Closer closer = Closer.create();

            try {
                InputStream in = closer.register(new FileInputStream("xanadu.txt"));
                OutputStream out = closer.register(new FileOutputStream("outagain.txt"));

                int c;

                while ((c = in.read()) != -1) {
                    out.write(c);
                }
            } catch (Throwable e) {
              throw closer.rethrow(e);
            } finally {
              closer.close();
            }
        }
    }

Language support for managing the common pattern makes it even easier in Java 7.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    public class CopyBytes {
        public static void main(String[] args) throws IOException {
            try (
              FileInputStream in = new FileInputStream("xanadu.txt");
              FileOutputStream out = new FileOutputStream("outagain.txt");

            ) {
                int c;

                while ((c = in.read()) != -1) {
                    out.write(c);
                }
            }
        }
    }

Get the details in this article

Using Npm Behind a Corporate Proxy

On a recent assignment, I needed to install npm behind a corporate proxy. I had already set the environment variables HTTP_PROXY and HTTPS_PROXY. Other command line utilities, like ruby gems, recognized these environment variables. Npm did not.

After some googling, I found the following way to configure the proxy for npm.

1
2
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080

If you need to specify credentials, they can be passed in the url using the following syntax.

http://user_name:password@proxy.company.com:8080

Further exploration of the npm config documentation showed that the npm config set command sets the proxy configuration in your .npmrc file. You can also set the proxy configuration as a command line argument or environment variable.

Configuration parameters can be specified using -- when executing npm. So the proxy could also be specified as follows.

1
npm --https-proxy=http://proxy.company.com:8080 -g install karma

To pass configurattion parameters to npm as environment variables, they must be prefixed with npm_config_. The proxy configuration could be set with environment variables as follows.

1
2
export npm_config_proxy http://proxy.company.com:8080
export npm_config_https_proxy http://proxy.company.com:8080

Ruby Performance Patches

There are several ruby performance patches, like the falcon patches, that may improve the performace of your application.

I use rvm and was pleased to learn that rvm supports compiling ruby with several of these performance patches. Using rvm with the railsexpress patch, I saw a 20% reduction in the time required to run the test suite for a large rails project.

Here is how you can install ruby with the railexpress patch using rvm.

First make sure you have the latest version of rvm.

1
rvm get head

Next install ruby 1.9.3 with the railsexpress patch. At this step rvm may have dependencies that require installation.

1
rvm install 1.9.3 --patch railsexpress

The railsexpress patch contains the following patches.

Setting Ubuntu Time Zone

You will eventually get tired of looking at UTC timestamps in your server log files.

https://help.ubuntu.com/community/UbuntuTime has a detailed guide for setting the time zone on your server and synchronizing it’s clock.

Here are the commands I used to set my time zone and synchronize my clock on Ubuntu 12.04.1 LTS.

Set time zone

1
sudo dpkg-reconfigure tzdata

Synchronize clock

1
sudo apt-get install ntp

WTF Javascript - Type Coercion

I recently came across the site wtfjs.com. It highlights some idiosyncrasies of the Javascript language. It’s worth a look, you’ll get a deeper understanding of the Javascript language.

This entry submitted by @diogobaeder held my attention. The solution was not immediately obvious to me so I did some digging. Once I found the correct type coercion rules the result were logical.

1
2
3
var foo = [0];
console.log(foo == foo);  // true
console.log(foo == !foo);  // true - ???

These examine this step by step.

1
foo == foo

The above was obvious. References to the same object evaluate to true.

1
foo == !foo

The expression above took a little longer to decipher.

First the precedence rules come into play.

1
2
![0];         // conversion to Boolean(), false
[0] == false; // true

If either operand is a number or a boolean, the operands are converted to numbers.

1
2
3
4
Number(false) == 0;
Number([0]) == 0;

0 == 0;