+ String stringify(Object obj) throws SQLException
+ {
+ if (obj instanceof java.sql.Blob) {
+ java.sql.Blob blob = null;
+
+ try {
+ blob = (java.sql.Blob)obj;
+ long length = blob.length();
+ if (length > BLOB_MAX) {
+ length = BLOB_MAX;
+ }
+ byte[] content = blob.getBytes(1, (int)length);
+
+ try {
+ // Assume that the BLOB is actually UTF-8 text, and attempt to return it that way.
+ // This happens to work well for the databases that I deal with regularly.
+ // Other users may want to change this assumption here...
+ return StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
+ .onUnmappableCharacter(CodingErrorAction.REPORT)
+ .decode(ByteBuffer.wrap(content))
+ .toString();
+ }
+ catch (CharacterCodingException exc) {
+ // If we get here, then the BLOB's content is not valid UTF-8.
+ // This may be because it's in a different character set, or because it's non-text
+ // (e.g., a bitmap image).
+ // We'll hex-dump it instead.
+ return DatatypeConverter.printHexBinary(content);
+ }
+ }
+ finally {
+ if (null != blob) {
+ try {
+ blob.free();
+ }
+ catch (UnsupportedOperationException | SQLFeatureNotSupportedException exc) {
+ // free() was only a hint; if the hint is not welcome, then that's OK.
+ }
+ }
+ }
+ }
+
+ return "" + obj;
+ }
+
+ int stringWidth(Object obj)
+ {
+ if (obj instanceof java.sql.Blob) {
+ return 1;
+ }
+ return ("" + obj).length();
+ }
+