/** * Return a character from the specified index. * * @param index the position from which to return a character * @return a character * @throws IndexOutOfBoundsException if the index argument is negative or not less than length() */ public char charAt(int index) { init(); if (index < 0) { throw new IndexOutOfBoundsException("index is less than zero"); } if (index >= length) { throw new IndexOutOfBoundsException("index is not less than length"); } int page = index / CLOB_PAGE_SIZE; String pageText = (String) results.get(page); return pageText.charAt(index - page * CLOB_PAGE_SIZE); }
/** * Initialises the state of this object. This is done lazily, because it requires the use of a * database connection to discover the length of the clob, and that cannot be done while inside * the ObjectStoreWriter while it has exclusive use of the connection. */ protected void init() { if (results == null) { Query q = new Query(); q.addToSelect(clob); results = os.executeSingleton(q, 20, false, false, true); int pageCount = results.size(); if (pageCount == 0) { length = 0; } else { String lastPage = (String) results.get(pageCount - 1); length = CLOB_PAGE_SIZE * (pageCount - 1) + lastPage.length(); } } }
/** * Sends the entire contents of the Clob into the given PrintStream. This method should be used in * preference to toString() in order to save memory with large Clobs. * * @param out a PrintStream to write the Clob value to */ public void drainToPrintStream(PrintStream out) { init(); int lowestPage = offset / CLOB_PAGE_SIZE; int highestPage = (offset + length - 1) / CLOB_PAGE_SIZE; for (int page = lowestPage; page <= highestPage; page++) { String pageText = (String) results.get(page); if (page == highestPage) { pageText = pageText.substring(0, offset + length - page * CLOB_PAGE_SIZE); } if (page == lowestPage) { pageText = pageText.substring(offset - page * CLOB_PAGE_SIZE, pageText.length()); } out.print(pageText); } }
/** * Converts the Clob into a String. Be careful that it can fit in memory! * * @return a String */ @Override public String toString() { init(); StringBuilder retval = new StringBuilder(); if (length > 0) { int lowestPage = offset / CLOB_PAGE_SIZE; int highestPage = (offset + length - 1) / CLOB_PAGE_SIZE; for (int page = lowestPage; page <= highestPage; page++) { String pageText = (String) results.get(page); if (page == highestPage) { pageText = pageText.substring(0, offset + length - page * CLOB_PAGE_SIZE); } if (page == lowestPage) { pageText = pageText.substring(offset - page * CLOB_PAGE_SIZE, pageText.length()); } retval.append(pageText); } } return retval.toString(); }