| 1 | /* |
| 2 | * Copyright 2007 brunella ltd |
| 3 | * |
| 4 | * Licensed under the LGPL Version 3 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 17 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | */ |
| 19 | package sf.qof.adapter; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.Reader; |
| 23 | import java.io.StringWriter; |
| 24 | import java.sql.Clob; |
| 25 | import java.sql.SQLException; |
| 26 | |
| 27 | /** |
| 28 | * ClobReader is a helper class to read data from <code>Clob</code>s to a string. |
| 29 | * |
| 30 | * @see java.sql.Clob |
| 31 | */ |
| 32 | public class ClobReader { |
| 33 | |
| 34 | /** |
| 35 | * Reads data from a clob. |
| 36 | * |
| 37 | * @param clob the clob |
| 38 | * @return the clob data as string |
| 39 | * @throws SQLException an error occurred |
| 40 | * @see java.sql.Clob |
| 41 | */ |
| 42 | public static String readClob(Clob clob) throws SQLException { |
| 43 | Reader reader = clob.getCharacterStream(); |
| 44 | StringWriter writer = new StringWriter(); |
| 45 | char[] buf = new char[256]; |
| 46 | try { |
| 47 | for (int n = reader.read(buf); n != -1; n = reader.read(buf)) { |
| 48 | writer.write(buf, 0, n); |
| 49 | } |
| 50 | reader.close(); |
| 51 | writer.close(); |
| 52 | } catch (IOException e) { |
| 53 | throw new RuntimeException(e); |
| 54 | } |
| 55 | return writer.toString(); |
| 56 | } |
| 57 | |
| 58 | } |