pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Eberhard Schulte <esc@pixelboxx.de>
To: pgsql-jdbc@postgresql.org
Subject: Enhancement: toBoolean() and empty String
Date: Fri, 03 Mar 2006 10:15:32 +0100
Message-ID: <44080934.4030605@pixelboxx.de> (raw)

Hello,

I habe two enhancements. I found the problems even in older versions.

1. Encoding.decode() creates for each empty String a new String object. Memory is wasted unnecessary.

inline patch 1: Encoding.java, Encoding.java 3 Mar 2006 08:45:12 -0000 (+4/-0 lines) download inline-1.patchIndex: Encoding.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/Encoding.java,v retrieving revision 1.21 diff -u -r1.21 Encoding.java --- Encoding.java 4 Jul 2005 02:18:32 -0000 1.21 +++ Encoding.java 3 Mar 2006 08:45:12 -0000 @@ -175,6 +175,10 @@ */ public String decode(byte[] encodedString, int offset, int length) throws IOException { + if(encodedString == null || encodedString.length == 0) { + return ""; // do not create new String() use constant String + } + if (encoding == null) return new String(encodedString, offset, length);
2. AbstractJdbc2ResultSet.toBoolean(): there is no explicite false check. A false is produced by an NumberFormatException.
inline patch 2: AbstractJdbc2ResultSet.java, AbstractJdbc2ResultSet.java 3 Mar 2006 08:54:53 -0000 (+10/-4 lines) download inline-2.patchIndex: AbstractJdbc2ResultSet.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v retrieving revision 1.83 diff -u -r1.83 AbstractJdbc2ResultSet.java --- AbstractJdbc2ResultSet.java 4 Dec 2005 21:40:33 -0000 1.83 +++ AbstractJdbc2ResultSet.java 3 Mar 2006 08:54:53 -0000 @@ -2434,22 +2434,28 @@ //----------------- Formatting Methods ------------------- - public static boolean toBoolean(String s) + public static boolean toBoolean(String s) throws PSQLException { if (s != null) { s = s.trim(); - if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t")) - return true; - + if (s.equalsIgnoreCase("t") || s.equalsIgnoreCase("true")) { // examine the most frequent case first + return true; + } else if (s.equalsIgnoreCase("f") || s.equalsIgnoreCase("false")) { // explicite false check + return false; + } + try { if (Double.valueOf(s).doubleValue() == 1) return true; + // TODO / FIXME: explicite false check } catch (NumberFormatException e) { + // throw exception if occurs + throw new PSQLException ("postgresql.res.baddouble", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, e); } } return false; // SQL NULL
Schönen Gruß Eberhard Schulte Patches: download all (2 files, .tar.gz)

view thread (2+ messages)  latest in thread

Message-ID: <44080934.4030605@pixelboxx.de>
Permalink:  ../44080934.4030605@pixelboxx.de/
Also on:    postgresql.org/message-id/44080934.4030605@pixelboxx.de

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: esc@pixelboxx.de
  Subject: Re: Enhancement: toBoolean() and empty String
  In-Reply-To: <44080934.4030605@pixelboxx.de>
  References: <44080934.4030605@pixelboxx.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox