001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on Jan 26, 2005 4:42:44 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.dao.oracle;
044:
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.sql.Blob;
049: import java.sql.PreparedStatement;
050: import java.sql.ResultSet;
051: import java.sql.SQLException;
052:
053: import net.jforum.JForumExecutionContext;
054: import net.jforum.exceptions.DatabaseException;
055: import net.jforum.util.DbUtils;
056: import oracle.sql.BLOB;
057:
058: /**
059: * @author Dmitriy Kiriy
060: * @version $Id: OracleUtils.java,v 1.14 2007/08/31 22:56:40 rafaelsteil Exp $
061: */
062: public class OracleUtils {
063: public static String readBlobUTF16BinaryStream(ResultSet rs,
064: String fieldName) throws SQLException {
065: try {
066: Blob clob = rs.getBlob(fieldName);
067:
068: InputStream is = clob.getBinaryStream();
069: StringBuffer sb = new StringBuffer();
070:
071: int readedBytes;
072: int bufferSize = 4096;
073:
074: do {
075: byte[] bytes = new byte[bufferSize];
076:
077: readedBytes = is.read(bytes);
078:
079: if (readedBytes > 0) {
080: String readed = new String(bytes, 0, readedBytes,
081: "UTF-16");
082: sb.append(readed);
083: }
084: } while (readedBytes == bufferSize);
085:
086: is.close();
087: return sb.toString();
088: } catch (IOException e) {
089: throw new DatabaseException(e);
090: }
091: }
092:
093: /**
094: * The query should look like:
095: *
096: * SELECT blob_field from any_table WHERE id = ? FOR UPDATE
097: *
098: * BUT KEEP IN MIND:
099: *
100: * When you insert record in previous step, it should go with empty_blob() like:
101: *
102: * INSERT INTO jforum_posts_text ( post_text ) VALUES (EMPTY_BLOB())
103: *
104: * @param query String
105: * @param idForQuery int
106: * @param value String
107: * @throws SQLException
108: */
109: public static void writeBlobUTF16BinaryStream(String query,
110: int idForQuery, String value) throws Exception {
111: PreparedStatement p = null;
112: ResultSet rs = null;
113: OutputStream blobWriter = null;
114:
115: try {
116: p = JForumExecutionContext.getConnection()
117: .prepareStatement(query);
118: p.setInt(1, idForQuery);
119:
120: rs = p.executeQuery();
121: rs.next();
122: Blob text = rs.getBlob(1);
123:
124: if (text instanceof BLOB) {
125: blobWriter = ((BLOB) text).getBinaryOutputStream();
126: } else {
127: blobWriter = text.setBinaryStream(0);
128: }
129:
130: blobWriter.write(value.getBytes("UTF-16"));
131:
132: blobWriter.close();
133: } catch (IOException e) {
134: throw new DatabaseException(e);
135: } finally {
136: if (blobWriter != null) {
137: blobWriter.close();
138: }
139:
140: DbUtils.close(rs, p);
141: }
142: }
143: }
|