Posts

Setting Java Socket Timeouts

Oddly, Java's Socket library doesn't allow you to set the socket timeout directly in the constructor. Its important to note that there is no default so the socket will last forever (ish). Note: The socket timeouts below apply to different situations: setSoTimeout applies to read timeouts. connect applies to connection establishment. At least that's how I read it. I found two options, setting the option on the socket after creating one simplest (for me at least): Socket s; try { s = new Socket( ip, port ); s.setSoTimeout( secs * 1000 ); // converts secs to ms // do other stuff like get input stream and read it } catch ( SocketException se ) { // catch timeout or other socket related error } catch ( Exception e ) { // any other exception } The other option is creating a Socket object without any parameters then calling the connect method with timeout. Here is a rough prototype since it seemed like pain to me but I was curious about the code: Socket s = new Socket...

A journey through whitespace cleanup - Perl5 and split

Running through some older Perl5 code, I found someone using grep with a split to cleanup white space on some incoming data. Found, this seemed confusing to me with lots of extra complexity: my @line = grep { s/\s*$// } split /\|/, $_; First Pass: my @line = split /\|\s*/, $_; But my guess is that all wrapping white space should be removed: my @line = split /\s*\|\s*/, $_; Even better (and more readable), use the Perl magic context: my @line = split /\s*\|\s*/; I would love to make that a cleaner regex but that will have to be another day...

Super Sweet CSV parsing with Perl

Today, everyday, every other day I need to pull in CSV files and write others out. After awhile, I was ready for a better solution. Lucky for me, the Perl community is amazing. For some amazing parsing options look no further than Text::CSV_XS , it provides a super sweet parsing interface. No talking just pure Perl code: use strict; use warnings; use Text::CSV_XS; my $csv = Text::CSV_XS->new( {eol => "\n"} ); # eol has nothing to do with parsing wait until later :) my $input = shift || die "Missing filename\n"; open my $fh, ' or die "Failed to open $input: $!\n"; # assume no file header while ( my $columns = $csv->getline($fh) ) { print $columns->[0], $columns->[1], "\n"; # or whatever you want to do } close $csv; That's for parsing. You can do some other super cool stuff too, with bind_columns . Lets' assume the file has the format of 'first name, last name, email address'. Here the parsing loop updated ...

unzip zip file with password

Never remember, its as simple as it seems it should be: unzip -P [password] file.zip Or for older versions of info-zip: unzip -[password] file.zip

YAHOO.util.Event.onDOMReady reminder

The callback argument signature wasn't obvious (or is it wrong?) on the YUI documentation page or very readable at the YUI API site. The callback has the following format: function init_for_ondomready(label, args, obj) { } label - string of "onDOMReady" args - empty array (from source doesn't look like its populated today) obj - object passed to onDOMReady event Example: YAHOO.util.Event.onDOMReady(init_for_ondomready, thingy); label = 'onDOMReady' args = [] obj = thingy Therefore you need to include a holder for the empty args array or process the arguments array manually.

Grabbing inputs from the DOM with my favorite javascript libraries

Grabbing input elements with my favorite JavaScript libraries What Prototype YUI All inputs $$('input') YAHOO.util.Selector.query('input'); All checkboxes $$('input[type=checkbox]') Sweet with .each YAHOO.util.Selector.query('input[type=checkbox]:checked'); All inputs in table with class .data_display $$('.data_display input') YAHOO.util.Selector.query('.data_display input');

JBuilder 8 losing Libraries

After not running JBuilder 8 for a bit, when I tried to rebuild an applet at $work, I received a very strange set of missing class errors. These classes were right there in the Jar libraries... I swear :) Well after a few hours of looking around I found that I need to remove the whole 'output path' option (under Run -> Configurations -> Path tab). Then it magically worked. Go Figure. I know JBuilder8 is ancient but one applet builds in it and nobody wants to fund it moving to Eclipse...