Example #1
1
 @Override
 public Value createBlob(InputStream in, long maxLength) {
   init();
   int type = Value.BLOB;
   if (maxLength < 0) {
     maxLength = Long.MAX_VALUE;
   }
   int max = (int) Math.min(maxLength, database.getMaxLengthInplaceLob());
   try {
     if (max != 0 && max < Integer.MAX_VALUE) {
       BufferedInputStream b = new BufferedInputStream(in, max);
       b.mark(max);
       byte[] small = new byte[max];
       int len = IOUtils.readFully(b, small, max);
       if (len < max) {
         if (len < small.length) {
           small = Arrays.copyOf(small, len);
         }
         return ValueLobDb.createSmallLob(type, small);
       }
       b.reset();
       in = b;
     }
     if (maxLength != Long.MAX_VALUE) {
       in = new LimitInputStream(in, maxLength);
     }
     return createLob(in, type);
   } catch (IllegalStateException e) {
     throw DbException.get(ErrorCode.OBJECT_CLOSED, e);
   } catch (IOException e) {
     throw DbException.convertIOException(e, null);
   }
 }
Example #2
0
 /**
  * Create a temporary BLOB value from a stream.
  *
  * @param in the input stream
  * @param length the number of characters to read, or -1 for no limit
  * @param handler the data handler
  * @return the lob value
  */
 public static ValueLobDb createTempBlob(InputStream in, long length, DataHandler handler) {
   try {
     long remaining = Long.MAX_VALUE;
     boolean compress = handler.getLobCompressionAlgorithm(Value.BLOB) != null;
     if (length >= 0 && length < remaining) {
       remaining = length;
     }
     int len = getBufferSize(handler, compress, remaining);
     byte[] buff;
     if (len >= Integer.MAX_VALUE) {
       buff = IOUtils.readBytesAndClose(in, -1);
       len = buff.length;
     } else {
       buff = DataUtils.newBytes(len);
       len = IOUtils.readFully(in, buff, len);
     }
     if (len <= handler.getMaxLengthInplaceLob()) {
       byte[] small = DataUtils.newBytes(len);
       System.arraycopy(buff, 0, small, 0, len);
       return ValueLobDb.createSmallLob(Value.BLOB, small, small.length);
     }
     ValueLobDb lob = new ValueLobDb(handler, buff, len, in, remaining);
     return lob;
   } catch (IOException e) {
     throw DbException.convertIOException(e, null);
   }
 }
Example #3
0
 @Override
 public Value convertPrecision(long precision, boolean force) {
   if (this.precision <= precision) {
     return this;
   }
   ValueLobDb lob;
   if (type == CLOB) {
     if (handler == null) {
       try {
         int p = MathUtils.convertLongToInt(precision);
         String s = IOUtils.readStringAndClose(getReader(), p);
         byte[] data = s.getBytes(Constants.UTF8);
         lob = ValueLobDb.createSmallLob(type, data, s.length());
       } catch (IOException e) {
         throw DbException.convertIOException(e, null);
       }
     } else {
       lob = ValueLobDb.createTempClob(getReader(), precision, handler);
     }
   } else {
     if (handler == null) {
       try {
         int p = MathUtils.convertLongToInt(precision);
         byte[] data = IOUtils.readBytesAndClose(getInputStream(), p);
         lob = ValueLobDb.createSmallLob(type, data, data.length);
       } catch (IOException e) {
         throw DbException.convertIOException(e, null);
       }
     } else {
       lob = ValueLobDb.createTempBlob(getInputStream(), precision, handler);
     }
   }
   return lob;
 }
Example #4
0
 /**
  * Returns a substring.
  *
  * @param pos the position (the first character is at position 1)
  * @param length the number of characters
  * @return the string
  */
 public String getSubString(long pos, int length) throws SQLException {
   try {
     if (isDebugEnabled()) {
       debugCode("getSubString(" + pos + ", " + length + ");");
     }
     checkClosed();
     if (pos < 1) {
       throw DbException.getInvalidValueException("pos", pos);
     }
     if (length < 0) {
       throw DbException.getInvalidValueException("length", length);
     }
     StringWriter writer = new StringWriter(Math.min(Constants.IO_BUFFER_SIZE, length));
     Reader reader = value.getReader();
     try {
       IOUtils.skipFully(reader, pos - 1);
       IOUtils.copyAndCloseInput(reader, writer, length);
     } finally {
       reader.close();
     }
     return writer.toString();
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Example #5
0
 /**
  * Create a temporary CLOB value from a stream.
  *
  * @param in the reader
  * @param length the number of characters to read, or -1 for no limit
  * @param handler the data handler
  * @return the lob value
  */
 public static ValueLobDb createTempClob(Reader in, long length, DataHandler handler) {
   BufferedReader reader;
   if (in instanceof BufferedReader) {
     reader = (BufferedReader) in;
   } else {
     reader = new BufferedReader(in, Constants.IO_BUFFER_SIZE);
   }
   try {
     boolean compress = handler.getLobCompressionAlgorithm(Value.CLOB) != null;
     long remaining = Long.MAX_VALUE;
     if (length >= 0 && length < remaining) {
       remaining = length;
     }
     int len = getBufferSize(handler, compress, remaining);
     char[] buff;
     if (len >= Integer.MAX_VALUE) {
       String data = IOUtils.readStringAndClose(reader, -1);
       buff = data.toCharArray();
       len = buff.length;
     } else {
       buff = new char[len];
       reader.mark(len);
       len = IOUtils.readFully(reader, buff, len);
     }
     if (len <= handler.getMaxLengthInplaceLob()) {
       byte[] small = new String(buff, 0, len).getBytes(Constants.UTF8);
       return ValueLobDb.createSmallLob(Value.CLOB, small, len);
     }
     reader.reset();
     ValueLobDb lob = new ValueLobDb(handler, reader, remaining);
     return lob;
   } catch (IOException e) {
     throw DbException.convertIOException(e, null);
   }
 }
 /**
  * Load a properties object from a file.
  *
  * @param fileName the name of the properties file
  * @return the properties object
  */
 public static synchronized SortedProperties loadProperties(String fileName) throws IOException {
   SortedProperties prop = new SortedProperties();
   if (IOUtils.exists(fileName)) {
     InputStream in = null;
     try {
       in = IOUtils.openFileInputStream(fileName);
       prop.load(in);
     } finally {
       if (in != null) {
         in.close();
       }
     }
   }
   return prop;
 }
Example #7
0
 /** Create a BLOB in a temporary file. */
 private ValueLobDb(DataHandler handler, byte[] buff, int len, InputStream in, long remaining)
     throws IOException {
   this.type = Value.BLOB;
   this.handler = handler;
   this.small = null;
   this.lobId = 0;
   this.hmac = null;
   this.fileName = createTempLobFileName(handler);
   this.tempFile = this.handler.openFile(fileName, "rw", false);
   this.tempFile.autoDelete();
   FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null, null);
   long tmpPrecision = 0;
   boolean compress = this.handler.getLobCompressionAlgorithm(Value.BLOB) != null;
   try {
     while (true) {
       tmpPrecision += len;
       out.write(buff, 0, len);
       remaining -= len;
       if (remaining <= 0) {
         break;
       }
       len = getBufferSize(this.handler, compress, remaining);
       len = IOUtils.readFully(in, buff, len);
       if (len <= 0) {
         break;
       }
     }
   } finally {
     out.close();
   }
   this.precision = tmpPrecision;
 }
Example #8
0
 private void testTransfer() throws Exception {
   Server server = new Server();
   server.setOut(new PrintStream(new ByteArrayOutputStream()));
   server.runTool("-web", "-webPort", "8182", "-properties", "null");
   File transfer = new File("transfer");
   transfer.mkdirs();
   try {
     FileOutputStream f = new FileOutputStream("transfer/test.txt");
     f.write("Hello World".getBytes());
     f.close();
     WebClient client = new WebClient();
     String url = "http://localhost:8182";
     String result = client.get(url);
     client.readSessionId(result);
     String test = client.get(url, "transfer/test.txt");
     assertEquals("Hello World", test);
     new File("transfer/testUpload.txt").delete();
     client.upload(
         url + "/transfer/testUpload.txt",
         "testUpload.txt",
         new ByteArrayInputStream("Hallo Welt".getBytes()));
     byte[] d = IOUtils.readBytesAndClose(new FileInputStream("transfer/testUpload.txt"), -1);
     assertEquals("Hallo Welt", new String(d));
     new File("transfer/testUpload.txt").delete();
   } finally {
     server.shutdown();
     FileUtils.deleteRecursive("transfer", true);
   }
 }
Example #9
0
 public void test() throws Exception {
   if (getBaseDir().indexOf(':') > 0) {
     return;
   }
   test(getBaseDir());
   IOUtils.delete(getBaseDir() + "/test");
 }
Example #10
0
 /** Create a CLOB in a temporary file. */
 private ValueLobDb(DataHandler handler, Reader in, long remaining) throws IOException {
   this.type = Value.CLOB;
   this.handler = handler;
   this.small = null;
   this.lobId = 0;
   this.hmac = null;
   this.fileName = createTempLobFileName(handler);
   this.tempFile = this.handler.openFile(fileName, "rw", false);
   this.tempFile.autoDelete();
   FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null, null);
   long tmpPrecision = 0;
   try {
     char[] buff = new char[Constants.IO_BUFFER_SIZE];
     while (true) {
       int len = getBufferSize(this.handler, false, remaining);
       len = IOUtils.readFully(in, buff, len);
       if (len == 0) {
         break;
       }
       byte[] data = new String(buff, 0, len).getBytes("UTF-8");
       out.write(data);
       tmpPrecision += len;
     }
   } finally {
     out.close();
   }
   this.precision = tmpPrecision;
 }
 private void listTop(String title, int[] list, int max) throws IOException {
   printLine('-');
   int total = 0;
   int totalLines = 0;
   for (int j = 0; j < maxIndex; j++) {
     int l = list[j];
     if (l > 0) {
       total += list[j];
       totalLines++;
     }
   }
   if (max == 0) {
     max = totalLines;
   }
   print(title);
   print("Total: " + total);
   printLine('-');
   String[] text = new String[max];
   int[] index = new int[max];
   for (int i = 0; i < max; i++) {
     int big = list[0];
     int bigIndex = 0;
     for (int j = 1; j < maxIndex; j++) {
       int l = list[j];
       if (l > big) {
         big = l;
         bigIndex = j;
       }
     }
     list[bigIndex] = -(big + 1);
     index[i] = bigIndex;
   }
   LineNumberReader r = null;
   try {
     r = new LineNumberReader(new FileReader("profile.txt"));
     for (int i = 0; i < maxIndex; i++) {
       String line = r.readLine();
       int k = list[i];
       if (k < 0) {
         k = -(k + 1);
         list[i] = k;
         for (int j = 0; j < max; j++) {
           if (index[j] == i) {
             int percent = 100 * k / total;
             text[j] = k + " " + percent + "%: " + line;
           }
         }
       }
     }
     for (int i = 0; i < max; i++) {
       print(text[i]);
     }
   } finally {
     IOUtils.closeSilently(r);
   }
 }
Example #12
0
 public void copy(String original, String copy) {
   try {
     OutputStream out = openFileOutputStream(copy, false);
     InputStream in = openFileInputStream(original);
     IOUtils.copyAndClose(in, out);
   } catch (IOException e) {
     rollback();
     throw DbException.convertIOException(e, "Can not copy " + original + " to " + copy);
   }
 }
Example #13
0
 /**
  * Returns the input stream.
  *
  * @return the input stream
  */
 public InputStream getAsciiStream() throws SQLException {
   try {
     debugCodeCall("getAsciiStream");
     checkClosed();
     String s = value.getString();
     return IOUtils.getInputStreamFromString(s);
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
 @Override
 public FileChannel open(String mode) throws IOException {
   InputStream in = newInputStream();
   FilePath copy = FilePath.get(getBase().toString() + ".copy");
   OutputStream out = copy.newOutputStream(false);
   IOUtils.copy(in, out);
   FileChannel base = getBase().open(mode);
   FileChannel readBase = copy.open(mode);
   return new FileReorderWrites(this, base, readBase);
 }
 private void listUnvisited() throws IOException {
   printLine('=');
   print("NOT COVERED");
   printLine('-');
   LineNumberReader r = null;
   BufferedWriter writer = null;
   try {
     r = new LineNumberReader(new FileReader("profile.txt"));
     writer = new BufferedWriter(new FileWriter("notCovered.txt"));
     int unvisited = 0;
     int unvisitedThrow = 0;
     for (int i = 0; i < maxIndex; i++) {
       String line = r.readLine();
       if (count[i] == 0) {
         if (!line.endsWith("throw")) {
           writer.write(line + "\r\n");
           if (LIST_UNVISITED) {
             print(line + "\r\n");
           }
           unvisited++;
         } else {
           unvisitedThrow++;
         }
       }
     }
     int percent = 100 * unvisited / maxIndex;
     print(
         "Not covered: "
             + percent
             + " % "
             + " ("
             + unvisited
             + " of "
             + maxIndex
             + "; throw="
             + unvisitedThrow
             + ")");
   } finally {
     IOUtils.closeSilently(writer);
     IOUtils.closeSilently(r);
   }
 }
Example #16
0
 private void loadFragments() throws IOException {
   File dir = new File(sourceDir, "html");
   for (File f : dir.listFiles()) {
     if (f.getName().startsWith("fragments")) {
       FileInputStream in = new FileInputStream(f);
       byte[] bytes = IOUtils.readBytesAndClose(in, 0);
       String page = new String(bytes, "UTF-8");
       fragments.put(f.getName(), page);
     }
   }
 }
Example #17
0
 @Override
 public String getString() {
   int len = precision > Integer.MAX_VALUE || precision == 0 ? Integer.MAX_VALUE : (int) precision;
   try {
     if (type == Value.CLOB) {
       if (small != null) {
         return new String(small, Constants.UTF8);
       }
       return IOUtils.readStringAndClose(getReader(), len);
     }
     byte[] buff;
     if (small != null) {
       buff = small;
     } else {
       buff = IOUtils.readBytesAndClose(getInputStream(), len);
     }
     return StringUtils.convertBytesToHex(buff);
   } catch (IOException e) {
     throw DbException.convertIOException(e, toString());
   }
 }
Example #18
0
 private void copy(File source, File target, boolean replaceFragments, boolean web)
     throws IOException {
   if (source.isDirectory()) {
     target.mkdirs();
     for (File f : source.listFiles()) {
       copy(f, new File(target, f.getName()), replaceFragments, web);
     }
   } else {
     String name = source.getName();
     if (name.endsWith("onePage.html") || name.startsWith("fragments")) {
       return;
     }
     if (web) {
       if (name.endsWith("main.html") || name.endsWith("main_ja.html")) {
         return;
       }
     } else {
       if (name.endsWith("mainWeb.html") || name.endsWith("mainWeb_ja.html")) {
         return;
       }
     }
     FileInputStream in = new FileInputStream(source);
     byte[] bytes = IOUtils.readBytesAndClose(in, 0);
     if (name.endsWith(".html")) {
       String page = new String(bytes, "UTF-8");
       if (web) {
         page = StringUtils.replaceAll(page, ANALYTICS_TAG, ANALYTICS_SCRIPT);
       }
       if (replaceFragments) {
         page = replaceFragments(name, page);
         page = StringUtils.replaceAll(page, "<a href=\"frame", "<a href=\"main");
         page = StringUtils.replaceAll(page, "html/frame.html", "html/main.html");
       }
       if (web) {
         page = StringUtils.replaceAll(page, TRANSLATE_START, "");
         page = StringUtils.replaceAll(page, TRANSLATE_END, "");
         page = StringUtils.replaceAll(page, "<pre>", "<pre class=\"notranslate\">");
         page = StringUtils.replaceAll(page, "<code>", "<code class=\"notranslate\">");
       }
       bytes = page.getBytes("UTF-8");
     }
     FileOutputStream out = new FileOutputStream(target);
     out.write(bytes);
     out.close();
     if (web) {
       if (name.endsWith("mainWeb.html")) {
         target.renameTo(new File(target.getParentFile(), "main.html"));
       } else if (name.endsWith("mainWeb_ja.html")) {
         target.renameTo(new File(target.getParentFile(), "main_ja.html"));
       }
     }
   }
 }
  @Override
  public void test() throws Exception {
    if (!SysProperties.MODIFY_ON_WRITE) {
      return;
    }
    deleteDb("modifyOnWrite");
    String dbFile = getBaseDir() + "/modifyOnWrite.h2.db";
    assertFalse(FileUtils.exists(dbFile));
    Connection conn = getConnection("modifyOnWrite");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int)");
    conn.close();
    byte[] test = IOUtils.readBytesAndClose(FileUtils.newInputStream(dbFile), -1);

    conn = getConnection("modifyOnWrite");
    stat = conn.createStatement();
    ResultSet rs;
    rs = stat.executeQuery("select * from test");
    assertFalse(rs.next());
    conn.close();
    assertTrue(FileUtils.exists(dbFile));
    byte[] test2 = IOUtils.readBytesAndClose(FileUtils.newInputStream(dbFile), -1);
    assertEquals(test, test2);

    conn = getConnection("modifyOnWrite");
    stat = conn.createStatement();
    stat.execute("insert into test values(1)");
    conn.close();

    conn = getConnection("modifyOnWrite");
    stat = conn.createStatement();
    rs = stat.executeQuery("select * from test");
    assertTrue(rs.next());
    conn.close();

    test2 = IOUtils.readBytesAndClose(FileUtils.newInputStream(dbFile), -1);
    assertFalse(Utils.compareSecure(test, test2));
  }
Example #20
0
 @Override
 public void run() {
   try {
     input = new BufferedInputStream(socket.getInputStream());
     output = new BufferedOutputStream(socket.getOutputStream());
     while (!stop) {
       if (!process()) {
         break;
       }
     }
   } catch (Exception e) {
     TraceSystem.traceThrowable(e);
   }
   IOUtils.closeSilently(output);
   IOUtils.closeSilently(input);
   try {
     socket.close();
   } catch (IOException e) {
     // ignore
   } finally {
     server.remove(this);
   }
 }
Example #21
0
 /**
  * Returns the length.
  *
  * @return the length
  */
 public long length() throws SQLException {
   try {
     debugCodeCall("length");
     checkClosed();
     if (value.getType() == Value.CLOB) {
       long precision = value.getPrecision();
       if (precision > 0) {
         return precision;
       }
     }
     return IOUtils.copyAndCloseInput(value.getReader(), null, Long.MAX_VALUE);
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Example #22
0
 @Override
 public byte[] getBytesNoCopy() {
   if (type == CLOB) {
     // convert hex to string
     return super.getBytesNoCopy();
   }
   if (small != null) {
     return small;
   }
   try {
     return IOUtils.readBytesAndClose(getInputStream(), Integer.MAX_VALUE);
   } catch (IOException e) {
     throw DbException.convertIOException(e, toString());
   }
 }
 private boolean openWriter() {
   if (printWriter == null) {
     try {
       FileUtils.createDirectories(FileUtils.getParent(fileName));
       if (FileUtils.exists(fileName) && !FileUtils.canWrite(fileName)) {
         // read only database: don't log error if the trace file
         // can't be opened
         return false;
       }
       fileWriter = IOUtils.getBufferedWriter(FileUtils.newOutputStream(fileName, true));
       printWriter = new PrintWriter(fileWriter, true);
     } catch (Exception e) {
       logWritingError(e);
       return false;
     }
   }
   return true;
 }
Example #24
0
 /**
  * Print a trace message to the trace file.
  *
  * @param s the message
  * @param e the exception or null
  */
 protected void traceOperation(String s, Exception e) {
   FileWriter writer = null;
   try {
     File f = new File(getBaseDir() + "/" + TRACE_FILE_NAME);
     f.getParentFile().mkdirs();
     writer = new FileWriter(f, true);
     PrintWriter w = new PrintWriter(writer);
     s = dateFormat.format(new Date()) + ": " + s;
     w.println(s);
     if (e != null) {
       e.printStackTrace(w);
     }
   } catch (IOException e2) {
     e2.printStackTrace();
   } finally {
     IOUtils.closeSilently(writer);
   }
 }
Example #25
0
 public FileObject openFileObject(String fileName, String mode) throws IOException {
   try {
     long id = getId(fileName, false);
     PreparedStatement prep = prepare("SELECT DATA FROM FILEDATA WHERE ID=?");
     prep.setLong(1, id);
     ResultSet rs = prep.executeQuery();
     if (rs.next()) {
       InputStream in = rs.getBinaryStream(1);
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       IOUtils.copyAndClose(in, out);
       byte[] data = out.toByteArray();
       return new FileObjectDatabase(this, fileName, data, false);
     }
     return new FileObjectDatabase(this, fileName, new byte[0], true);
   } catch (SQLException e) {
     throw convert(e);
   }
 }
Example #26
0
 private void processHtml(String fileName) throws Exception {
   String source = "src/tools/org/h2/jcr/";
   String target = "docs/html/";
   byte[] s = BuildBase.readFile(new File(source + "stylesheet.css"));
   BuildBase.writeFile(new File(target + "stylesheet.css"), s);
   String inFile = source + fileName;
   String outFile = target + fileName;
   new File(outFile).getParentFile().mkdirs();
   FileOutputStream out = new FileOutputStream(outFile);
   FileInputStream in = new FileInputStream(inFile);
   byte[] bytes = IOUtils.readBytesAndClose(in, 0);
   if (fileName.endsWith(".html")) {
     String page = new String(bytes);
     page = PageParser.parse(page, session);
     bytes = page.getBytes();
   }
   out.write(bytes);
   out.close();
 }
 private Profile() {
   LineNumberReader r = null;
   try {
     r = new LineNumberReader(new FileReader("profile.txt"));
     while (r.readLine() != null) {
       // nothing - just count lines
     }
     maxIndex = r.getLineNumber();
     count = new int[maxIndex];
     time = new int[maxIndex];
     lastTime = System.currentTimeMillis();
     Runtime.getRuntime().addShutdownHook(this);
   } catch (Exception e) {
     e.printStackTrace();
     System.exit(1);
   } finally {
     IOUtils.closeSilently(r);
   }
 }
  // download arquivo
  @RequestMapping(value = "/{id}/download", method = RequestMethod.GET)
  public void getFile(@PathVariable("id") Long id, HttpServletResponse response) {
    try {
      Documento documento = serviceDocumento.find(Documento.class, id);
      if (documento != null) {

        InputStream is = new ByteArrayInputStream(documento.getArquivo());
        response.setContentType(documento.getTipo());
        response.setHeader(
            "Content-Disposition",
            "attachment; filename=" + documento.getNomeOriginal().replace(" ", "_"));
        IOUtils.copy(is, response.getOutputStream());

        response.flushBuffer();
      }
    } catch (IOException ex) {
      throw new RuntimeException("IOError writing file to output stream");
    }
  }
Example #29
0
 /**
  * Get a writer to update the Clob. This is only supported for new, empty Clob objects that were
  * created with Connection.createClob() or createNClob(). The Clob is created in a separate
  * thread, and the object is only updated when Writer.close() is called. The position must be 1,
  * meaning the whole Clob data is set.
  *
  * @param pos where to start writing (the first character is at position 1)
  * @return a writer
  */
 public Writer setCharacterStream(long pos) throws SQLException {
   try {
     if (isDebugEnabled()) {
       debugCodeCall("setCharacterStream(" + pos + ");");
     }
     checkClosed();
     if (pos != 1) {
       throw DbException.getInvalidValueException("pos", pos);
     }
     if (value.getPrecision() != 0) {
       throw DbException.getInvalidValueException("length", value.getPrecision());
     }
     final JdbcConnection c = conn;
     // PipedReader / PipedWriter are a lot slower
     // than PipedInputStream / PipedOutputStream
     // (Sun/Oracle Java 1.6.0_20)
     final PipedInputStream in = new PipedInputStream();
     final Task task =
         new Task() {
           public void call() {
             value = c.createClob(IOUtils.getReader(in), -1);
           }
         };
     PipedOutputStream out =
         new PipedOutputStream(in) {
           public void close() throws IOException {
             super.close();
             try {
               task.get();
             } catch (Exception e) {
               throw DbException.convertToIOException(e);
             }
           }
         };
     task.execute();
     return IOUtils.getBufferedWriter(out);
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Example #30
0
 public static Object convert(Object o, Class<?> targetType) {
   if (o == null) {
     return null;
   }
   Class<?> currentType = o.getClass();
   if (targetType.isAssignableFrom(currentType)) {
     return o;
   }
   if (targetType == String.class) {
     if (Clob.class.isAssignableFrom(currentType)) {
       Clob c = (Clob) o;
       try {
         Reader r = c.getCharacterStream();
         return IOUtils.readStringAndClose(r, -1);
       } catch (Exception e) {
         throw new RuntimeException("Error converting CLOB to String: " + e.toString(), e);
       }
     }
     return o.toString();
   }
   if (Number.class.isAssignableFrom(currentType)) {
     Number n = (Number) o;
     if (targetType == Byte.class) {
       return n.byteValue();
     } else if (targetType == Short.class) {
       return n.shortValue();
     } else if (targetType == Integer.class) {
       return n.intValue();
     } else if (targetType == Long.class) {
       return n.longValue();
     } else if (targetType == Double.class) {
       return n.doubleValue();
     } else if (targetType == Float.class) {
       return n.floatValue();
     }
   }
   throw new RuntimeException(
       "Can not convert the value " + o + " from " + currentType + " to " + targetType);
 }