Wednesday, June 24, 2009

Don't use Integer keys with WeakHashMap

WeakHashMap relies on the key of an entry being garbage collected (due to being weakly referenced) so that the entire Map.Entry can be discarded.

However, if you use Integer as your map key, you can get suprising results when combined with autoboxing, due to a hidden cache of j.l.Integer objects embedded in java.lang.Integer itself.

This cache has been discussed in several other places (ie. http://www.owasp.org/index.php/Java_gotchas), particularly in respect to testing equality.

However, this cache of Integers from -128...127 will also mess with your WeakHashMap, pinning any elements in that range in memory, never to be released.

Here is a quick and dirty test demonstrating this issue:


public void testWeakHashMapWithIntKeys()
{
WeakHashMap<integer, stringbuffer> map = new WeakHashMap<integer, stringbuffer>();

map.put( 1, new StringBuffer("Test 1") );
map.put( 2, new StringBuffer("Test 2") );
map.put( 3, new StringBuffer("Test 3") );
map.put( 126, new StringBuffer("Test 126") );
map.put( 127, new StringBuffer("Test 127") );
map.put( 128, new StringBuffer("Test 128") );

System.out.println(map);

System.gc();

System.out.println(map);
}

You should get something like this:


{128=Test 128, 126=Test 126, 127=Test 127, 3=Test 3, 2=Test 2, 1=Test 1}
{126=Test 126, 127=Test 127, 3=Test 3, 2=Test 2, 1=Test 1}

Where the second line shows that all elements less than 128 have been retained.

This also applies to the other wrapper classes, java.lang.Byte, java.lang.Short, java.lang.Long, etc.

You can avoid this problem by not using autoboxing, and always invoking the new operator to create your keys. However, this seems to me to be error-prone. A more fool-proof strategy would be to wrap your Integer (or even a int!) in a custom class to avoid all this froo-fra.

Update: my google-foo found an example in "Pro Java Programming" (by Brett Spell), of how to not use WeakHashMap. His examples work by accident because the values he uses for the key are larger than 127.

Friday, May 08, 2009

Coolest thing ever

Customizable graph paper pdf generator

Friday, April 24, 2009

Phone bill fraud from "Internet Business Assocation" and ESBI (Enhanced Services Billing, Inc)

[update June 18, 2009]

It took a second call to Verizon (our phone company) to get the charges removed from our bill.

Doing a little more googling found this excelent write up:


[end update]

Today while going over the phone bill for my office, I noticed a section of the bill that Verizon prefixed with the text:

"This portion of your bill is provided as a service to Enhanced Services"

In the details buried in that section is a line item from "IBASERVICES MNTHLYFEE" for $39.95.

Googling "IBA Services" doesn't really come up with anything promising.

Calling the phone number for Enhanced Services (ESBI), 800-460-0078, I get to a helpful service rep named Eli, who tells me that IBA Services stands for Internet Business Association, and that IBA provides some kind of enhanced business websites. I immedately knew that this was a bogus charge. (the company I work for is a software consulting company... if anyone is going to make a website for us, it will be us)

I told Eli flat out that it was a fraudulent charge, and he quickly promised to cancel the monthly billing and refund the current charge, and put us on a "do not call" list.

I asked Eli a few questions about Internet Business Association. He answered my questions about IBA so quickly that it made me think that ESBI was just a front for IBA. When I asked him if he handled questions for any billing company other than IBA, he said "no", which confirmed my suspicions.

A co-worker, who overheard this conversation, googled "Internet Business Assocation", and found several hits with reports of bogus and fraudulent charges:



Also, the first hit, IBA's own website (which is pretty minimal), does tell you up front that they charge $39.95/month to their victims:

IBA also provides our customers with a unique auto-created web space on the Internet utilizing state-of-the-art technology and a vibrant and vast collection of artwork to improve their visual presence on the Internet, all for only $39.95 per month, conveniently billed on their local telephone bill so that there are not even additional checks to write for the small businesses we work with

The same co-worker was pretty pissed at the situation as he has had to field most of the annoying phone calls from scam companies like IBA Services that come into our office.

Beware, and keep an eye on your phone bill!


Thursday, March 05, 2009

Found bug in Sun's Java Swing implementation (?)

Previously, I had noticed a common mistake/bug in the code of the project I was working on at the time.

Today I noticed that same bug in Sun's code.  Its in javax.swing.AbstractAction, line 160 in the version I'm running (1.6 something):

       if (key == "enabled")
From the inline comments in the source, its a hack to synchronize the action's boolean enabled status with putValue("enabled", true/false) which stores the value in a map.

Kudos to Sun for including the source to Java's libraries.

Monday, September 08, 2008

Animating favicon the hard way

You can animate your page's favicon by using an animated gif instead of an ico file, but you can also do it by rotating the href value in the <link> tag.

Unfortunately, you have jump thru a few hoops. Just changing your link's href tag won't do anything:

$("link").get(0).href = "newicon.ico";


You also have to remove the <link> tag from the dom and re-add it:


function setFavIcon( src )
{
var link = $('link').get(0);
var linkparent = link.parentNode;
link.href = src;
$(link).remove();
$(linkparent).append(link);
}

Thursday, June 19, 2008

Finally!!!! I get my very own nigerian 419 buddy.

I put an item up on Craigslist. It was the second or third time I listed it, each time dropping the price a little.

Well, I finally got an email from someone that was interested! Yeah me!

From: "chuksbrown@ommail.com"
To: sale-722302282@craigslist.org
Subject: Sony 200 disc cd player --- CDP-CX70ES - $45 (New Market)
Date: Tue, 17 Jun 2008 03:38:07 -0500

** CRAIGSLIST ADVISORY --- AVOID SCAMS BY DEALING LOCALLY
** Avoid: wiring money, cross-border deals, work-at-home
** Beware: cashier checks, money orders, escrow, shipping
** More Info: http://www.craigslist.org/about/scams.html

I need to know your firm price,
kindly get back to me.
THANKS.


____________________________________________________
S Naidu Online Web Designing, Domain Name Registration, Web Hosting, Email Solutions,
Ecommerce website development, http://www.snaidu.com


I replied back that I wasn't willing to reduce the price anymore:

Date: Tue, 17 Jun 2008 09:45:03 -0400
From: "Trevor Harrison"
To: "chuksbrown@ommail.com"
Subject: Re: Sony 200 disc cd player --- CDP-CX70ES - $45 (New Market)

Sorry. I already specified my price... $45. If you're just a little
short of that I'll deal with you, but I've already reduced the price
on this quite a bit.

-Trevor


He replied:

Delivered-To: trevor@harrison.org
From: "chuksbrown@ommail.com"
To: "Trevor Harrison"
Subject: Re: Sony 200 disc cd player --- CDP-CX70ES - $45 (New Market)
Date: Tue, 17 Jun 2008 11:54:55 -0500

Alright, i will pay you the amount you said and i hope it
is in good condition.I'm in london presently but on a
busSiness trip,i would have love to come and pick it up
myself so as to check if it is in good shape,but since i'm
not in town, i will pay you through paypal or money
order,but you will have to do me a favour.I would like the
Sony 200 disc cd player sent to my only daughter as a
birthday gift.She is only 20 and works with the UNICEF in
west Africa. You will ship it to her after confirmation of
money is seen.Thanks.


Well, darn. He wants me to ship it to Africa for him. A old, kinda big cd player. My guess is that shipping would be much more than the listed price. But I'm sure I can trust that money order he says he'll send me.

Its kinda weird. This is my first 419 buddy. I feel happy to finally have one, but I'm wondering what I've been doing wrong that its taken 13 years to finally get one.

Thursday, May 29, 2008

Please wait....

Here are a smattering of please wait icons I found recently: