10000 - Updated how byte arrays are represented in the toString() method i… · javaxt-project/javaxt-core@522a7d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 522a7d5

Browse files
committed
- Updated how byte arrays are represented in the toString() method in the JSONObject. Byte arrays are now Base64 encoded.
- Fixed NPE when dealing with null dates in the update() method in the Recordset class. - Updated the toByteArray() method in the Value class to check for Base64 encoded strings. git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@1570 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 952ef97 commit 522a7d5

File tree

3 files changed

+75
-46
lines changed

3 files changed

+75
-46
lines changed

src/javaxt/json/JSONObject.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ public String toString(int indentFactor) {
286286
//** writeValue
287287
//**************************************************************************
288288
protected static final Writer writeValue(Writer writer, Object value,
289-
int indentFactor, int indent) throws JSONException, IOException {
289+
int indentFactor, int indent) throws JSONException, IOException {
290+
290291
if (value == null || value.equals(null)) {
291292
writer.write("null");
292293
}
@@ -317,6 +318,9 @@ else if (value instanceof java.util.Date) {
317318
else if (value instanceof java.util.Calendar) {
318319
writer.write(quote(new javaxt.utils.Date(((java.util.Calendar) value)).toISOString()));
319320
}
321+
else if (value instanceof byte[]){
322+
writer.write(quote(javaxt.utils.Base64.encode((byte[]) value)));
323+
}
320324
else if (value instanceof java.sql.Clob){
321325
//Special case: Use javaxt.sql.Value to stringify Clobs
322326
writer.write(quote(new javaxt.sql.Value(value).toString()));

src/javaxt/sql/Recordset.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ public class Recordset implements AutoCloseable {
7575
new ConcurrentHashMap<>();
7676

7777

78-
// private static final AtomicLong openStatements = new AtomicLong(0);
79-
// private static final AtomicLong openRecordsets = new AtomicLong(0);
80-
// private static final AtomicLong openCalls = new AtomicLong(0);
81-
// private static final AtomicLong closeCalls = new AtomicLong(0);
78+
private static final AtomicLong openStatements = new AtomicLong(0);
79+
private static final AtomicLong openRecordsets = new AtomicLong(0);
80+
private static final AtomicLong openCalls = new AtomicLong(0);
81+
private static final AtomicLong closeCalls = new AtomicLong(0);
82+
private static boolean debugClose = false;
8283

8384

8485
//**************************************************************************
@@ -185,7 +186,7 @@ public java.sql.ResultSet open(String sqlString, Connection connection, boolean
185186
if (shuttingDown.get()) throw new IllegalStateException("JVM shutting down");
186187
if (connection==null) throw new SQLException("Connection is null.");
187188
if (connection.isClosed()) throw new SQLException("Connection is closed.");
188-
//openCalls.incrementAndGet();
189+
if (debugClose) openCalls.incrementAndGet();
189190

190191
rs = null;
191192
stmt = null;
@@ -253,11 +254,11 @@ else if (driver.equals("PostgreSQL")){
253254
stmt = Conn.createStatement();
254255
}
255256
}
256-
//openStatements.incrementAndGet();
257+
if (debugClose) openStatements.incrementAndGet();
257258

258259
if (fetchSize!=null) stmt.setFetchSize(fetchSize);
259260
rs = stmt.executeQuery(sqlString);
260-
//openRecordsets.incrementAndGet();
261+
if (debugClose) openRecordsets.incrementAndGet();
261262
State = 1;
262263
}
263264
catch(SQLException e){
@@ -343,8 +344,8 @@ else if (driver.equals("DB2")){
343344
}
344345

345346

346-
//if (stmt!=null) openStatements.incrementAndGet();
347-
//if (rs!=null) openRecordsets.incrementAndGet();
347+
if (stmt!=null && debugClose) openStatements.incrementAndGet();
348+
if (rs!=null && debugClose) openRecordsets.incrementAndGet();
348349

349350
}
350351
catch(SQLException e){
@@ -375,7 +376,7 @@ else if (driver.equals("DB2")){
375376
*/
376377
public void open(java.sql.ResultSet resultSet){
377378
if (shuttingDown.get()) throw new IllegalStateException("JVM shutting down");
378-
//openCalls.incrementAndGet();
379+
if (debugClose) openCalls.incrementAndGet();
379380
startTime = System.currentTimeMillis();
380381
queryResponseTime = ellapsedTime = metadataQueryTime = endTime = 0;
381382
EOF = true;
@@ -443,15 +444,15 @@ private void init(){
443444
/** Closes the Recordset freeing up database and jdbc resources.
444445
*/
445446
public void close(){
446-
//closeCalls.incrementAndGet();
447+
if (debugClose) closeCalls.incrementAndGet();
447448

448449
//Close recordset
449450
try{
450451
if (State==1) executeBatch();
451452
if (!isReadOnly) commit();
452453
if (rs!=null){
453454
rs.close();
454-
//openRecordsets.decrementAndGet();
455+
if (debugClose) openRecordsets.decrementAndGet();
455456
}
456457
if (stmt!=null){
457458

@@ -468,7 +469,7 @@ public void close(){
468469

469470
try{
470471
stmt.close();
471-
//openStatements.decrementAndGet();
472+
if (debugClose) openStatements.decrementAndGet();
472473
}
473474
catch(Exception e){
474475
//e.printStackTrace();
@@ -509,8 +510,10 @@ record = null;
509510
endTime = System.currentTimeMillis();
510511
ellapsedTime = endTime-startTime;
511512

512-
//javaxt.utils.Console.console.log(openRecordsets + " openRecordsets, " + openStatements + " openStatements, " + (openCalls.get()-closeCalls.get()));
513-
513+
if (debugClose) javaxt.utils.Console.console.log(
514+
openRecordsets + " openRecordsets, " +
515+
openStatements + " openStatements, " +
516+
(openCalls.get()-closeCalls.get()));
514517
}
515518

516519

@@ -941,8 +944,11 @@ else if (FieldType.indexOf("decimal")>=0)
941944
else if (FieldType.indexOf("timestamp")>=0)
942945
stmt.setTimestamp(id, FieldValue.toTimeStamp());
943946

944-
else if (FieldType.indexOf("date")>=0)
945-
stmt.setDate(id, new java.sql.Date(FieldValue.toDate().getTime()));
947+
else if (FieldType.indexOf("date")>=0){
948+
javaxt.utils.Date d = FieldValue.toDate();
949+
if (d==null) stmt.setNull(id, java.sql.Types.DATE);
950+
else stmt.setDate(id, new java.sql.Date(d.getTime()));
951+
}
946952

947953
else if (FieldType.indexOf("object")>=0)
948954
stmt.setObject(id, FieldValue.toObject());

src/javaxt/utils/Value.java

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,38 @@ else if (value instanceof java.util.Calendar){
201201
//**************************************************************************
202202
//** toByteArray
203203
//**************************************************************************
204-
/** Returns the value as a byte array.
204+
/** Returns the value as a byte array. Returns a null if the value is null
205+
* or if there was a problem converting the value to a byte array. Note
206+
* that if the underlying object is a String, and if the String appears to
207+
* represent a Base64 encoded byte array, then an attempt is made to decode
208+
* the Base64 String. Otherwise, this method will simply write the value to
209+
* a ByteArrayOutputStream and return the raw bytes.
205210
*/
206211
public byte[] toByteArray(){
207212
if (value==null) return null;
208213
if (value instanceof byte[]) return (byte[]) value;
209214

215+
216+
//Check if string is Base64 encoded
217+
if (value instanceof String){
218+
String data = (String) value;
219+
if (data.startsWith("data:") && data.contains(";base64,")){
220+
String type = data.substring(data.indexOf(":")+1, data.indexOf(";"));
221+
data = data.substring(("data:" + type + ";base64,").length());
222+
byte[] b = Base64.decode(data);
223+
if (b!=null) return b;
224+
}
225+
}
226+
227+
210228
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
211-
try {
212-
java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(bos);
229+
try (java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(bos)){
213230
out.writeObject(value);
214231
out.flush();
215-
out.close();
216232
bos.close();
217233
return bos.toByteArray();
218234
}
219-
catch (java.io.IOException ex) {
235+
catch (Exception ex) {
220236
return null;
221237
}
222238
}
@@ -225,37 +241,40 @@ public byte[] toByteArray(){
225241
//**************************************************************************
226242
//** toBoolean
227243
//**************************************************************************
228-
/** Returns a boolean value for the field. */
229-
244+
/** Returns the value as a Boolean. Returns null if the value is null or
245+
* cannot be converted to a Boolean.
246+
*/
230247
public Boolean toBoolean(){
231-
if (value!=null){
232-
String value = this.value.toString().toLowerCase().trim();
248+
if (value==null) return null;
249+
if (value instanceof Boolean) return (Boolean) value;
233250

234-
if (value.equals("true")) return true;
235-
if (value.equals("false")) return false;
236251

237-
if (value.equals("yes")) return true;
238-
if (value.equals("no")) return false;
252+
String value = this.value.toString().toLowerCase().trim();
239253

240-
if (value.equals("y")) return true;
241-
if (value.equals("n")) return false;
254+
if (value.equals("true")) return true;
255+
if (value.equals("false")) return false;
242256

243-
if (value.equals("t")) return true;
244-
if (value.equals("f")) return false;
257+
if (value.equals("yes")) return true;
258+
if (value.equals("no")) return false;
245259

246-
if (value.equals("1")) return true;
247-
if (value.equals("0")) return false;
260+
if (value.equals("y")) return true;
261+
if (value.equals("n")) return false;
262+
263+
if (value.equals("t")) return true;
264+
if (value.equals("f")) return false;
265+
266+
if (value.equals("1")) return true;
267+
if (value.equals("0")) return false;
248268

249-
}
250269
return null;
251270
}
252271

253272

254273
//**************************************************************************
255274
//** isNumeric
256275
//**************************************************************************
257-
/** Used to determine if the value is numeric. */
258-
276+
/** Returns true if the value is numeric.
277+
*/
259278
public boolean isNumeric(){
260279
return (toDouble()!=null);
261280
}
@@ -264,8 +283,8 @@ public boolean isNumeric(){
264283
//**************************************************************************
265284
//** isArray
266285
//**************************************************************************
267-
/** Used to determine whether the value is an array. */
268-
286+
/** Returns true is the value is an array.
287+
*/
269288
public boolean isArray(){
270289
return value!=null && value.getClass().isArray();
271290
}
@@ -274,8 +293,8 @@ public boolean isArray(){
274293
//**************************************************************************
275294
//** isNull
276295
//**************************************************************************
277-
/** Used to determine whether the value is null. */
278-
296+
/** Returns true if the value is null.
297+
*/
279298
public boolean isNull(){
280299
return value==null;
281300
}
@@ -296,8 +315,8 @@ public String toString(){
296315
//**************************************************************************
297316
//** equals
298317
//**************************************************************************
299-
/** Used to compare values. Accepts any object. */
300-
318+
/** Used to compare values. Accepts any object.
319+
*/
301320
public boolean equals(Object obj){
302321
if (obj instanceof Value) obj = ((Value) obj).toObject();
303322
if (obj==null) {

0 commit comments

Comments
 (0)
0