Skip to content

Commit

Permalink
Fixed parsing of floating point values using getInt, getLong...
Browse files Browse the repository at this point in the history
Most JDBC drivers would not complain if getInt, getLong, getShort or
getByte was called on a floating point value and would simply truncate
the floating value instead (e.g. "1.9" would become "1", "-1.9" would
return "-1").
  • Loading branch information
misambart authored and Marcus Eriksson committed Dec 20, 2012
1 parent c3c19e8 commit 3516b9c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/org/drizzle/jdbc/internal/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ public static byte byteArrayToByte(byte [] b) {
public static int byteArrayToInt(byte [] b) {
int sum = 0;
int len = b.length;

for(int i = 0; i < b.length; i++) {
if(b[i] == '.'){
len = i;
break;
}
}

int limit = -Integer.MAX_VALUE;
int startIdx = 0;
Expand All @@ -430,7 +437,7 @@ public static int byteArrayToInt(byte [] b) {

}
int factor = (int) Math.pow(10, len-1-startIdx);
for(int i = startIdx; i < b.length; i++) {
for(int i = startIdx; i < len; i++) {
byte x = (byte) (b[i] - '0');
if(x < 0 || x > 9) throw new NumberFormatException("Could not parse as int");
int oldSum = sum;
Expand All @@ -448,6 +455,13 @@ public static int byteArrayToInt(byte [] b) {
public static long byteArrayToLong(byte [] b) {
long sum = 0;
int len = b.length;

for(int i = 0; i < b.length; i++) {
if(b[i] == '.'){
len = i;
break;
}
}

long limit = -Long.MAX_VALUE;
int startIdx = 0;
Expand All @@ -468,7 +482,7 @@ public static long byteArrayToLong(byte [] b) {

}
long factor = (long) Math.pow(10, len-1-startIdx);
for(int i = startIdx; i < b.length; i++) {
for(int i = startIdx; i < len; i++) {
byte x = (byte) (b[i] - '0');
if(x < 0 || x > 9) throw new NumberFormatException("Could not parse as long");
long oldSum = sum;
Expand Down

0 comments on commit 3516b9c

Please sign in to comment.