/** * Obtains streams from the Clob and makes sure we can always read the last char in the Clob. * * <p>See DERBY-4060. * * @param id id of the Clob to use * @param length the length of the Clob * @param alphabet the alphabet used to create the content * @throws IOException if reading from a stream fails * @throws SQLException if something goes wrong */ private void getCharacterStreamLongLastChar(int id, int length, CharAlphabet alphabet) throws IOException, SQLException { // Get last char from the source stream. Reader cmpReader = new LoopingAlphabetReader(length, alphabet); cmpReader.skip(length - 1); char srcLastChar = (char) cmpReader.read(); assertTrue(cmpReader.read() == -1); PreparedStatement ps = prepareStatement("select CLOBDATA from BLOBCLOB where ID=?"); ps.setInt(1, id); // Read everything first. int charsToRead = length; ResultSet rs = ps.executeQuery(); rs.next(); Reader reader = rs.getClob(1).getCharacterStream(length - charsToRead + 1, charsToRead); // Drain the stream, and make sure we are able to read the last char. char lastCharRead = getLastCharInStream(reader, charsToRead); assertEquals(srcLastChar, lastCharRead); reader.close(); rs.close(); // Read a portion of the stream. charsToRead = length / 4; rs = ps.executeQuery(); rs.next(); reader = rs.getClob(1).getCharacterStream(length - charsToRead + 1, charsToRead); lastCharRead = getLastCharInStream(reader, charsToRead); assertEquals(srcLastChar, lastCharRead); reader.close(); rs.close(); // Read a very small portion of the stream. charsToRead = 1; rs = ps.executeQuery(); rs.next(); reader = rs.getClob(1).getCharacterStream(length - charsToRead + 1, charsToRead); lastCharRead = getLastCharInStream(reader, charsToRead); assertEquals(srcLastChar, lastCharRead); reader.close(); rs.close(); }
private StringBuffer getDataFromReader(Reader br) { StringBuffer xferData = null; char[] buf = new char[513]; int charsRead; try { do { charsRead = br.read(buf, 0, 512); if (charsRead != -1) { JConfig.log().logVerboseDebug("Read: " + charsRead + " characters."); if (xferData == null) { xferData = new StringBuffer(); } xferData.append(buf, 0, charsRead); } } while (charsRead != -1); br.close(); } catch (IOException e) { JConfig.log().logDebug("Caught an IO Exception trying to read the drag/drop data!"); return null; } return xferData; }
public void close() throws IOException { reader.close(); }