HSQLDBLOBPolicy.java :  » Content-Management-System » contelligent » de » finix » contelligent » persistence » lobs » Java Open Source

Java Open Source » Content Management System » contelligent 
contelligent » de » finix » contelligent » persistence » lobs » HSQLDBLOBPolicy.java
/*
 * Copyright 2001-2006 C:1 Financial Services GmbH
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License Version 2.1, as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
 */

package de.finix.contelligent.persistence.lobs;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import de.finix.contelligent.logging.LoggingService;

public class HSQLDBLOBPolicy implements LOBPolicy {
    final static org.apache.log4j.Logger log = LoggingService.getLogger(HSQLDBLOBPolicy.class);

    /**
     * Answer true is SELECT FOR UPDATE is necessary to for setting the blob
     */
    public boolean requiresSelectForUpdate() {
        return false;
    }

    public boolean supportsSelectIntoForBlobs() {
        return true;
    }

    /**
     * Answer the String to be included in an insert statement to create an
     * empty blob
     */
    public String getEmptyBlobFunction() {
        return "null";
    }

    /**
     * Returns the string representation of this LOB-policy.
     */
    public String toString() {
        return "HSQLDBLOBPolicy";
    }

    /**
     * Set the blob field in the way required by the underlying database.
     */
    public void setBlob(Blob blob, PreparedStatement statement, int index, byte[] bytes) throws SQLException {
        // InputStream in = new ByteArrayInputStream(bytes);
        statement.setBytes(index, bytes);
    }

    /**
     * Set the blob field in the way required by the underlying database.
     */
    public void setBlob(Blob blob, PreparedStatement statement, int index, InputStream stream, int length)
            throws SQLException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            de.finix.contelligent.util.FileUtils.copy(stream, out);
        } catch (IOException e) {
            log.error("setBlob() - problem copying inputStream to ByteArray", e);
            throw new SQLException("setBlob() - problem copying inputStream to ByteArray - see log file for details!");
        }
        byte[] bytes = out.toByteArray();
        statement.setBytes(index, bytes);
    }

    /**
     * Retrieve the blob field in the way required by the underlying database.
     */
    public InputStream getBinaryStream(ResultSet rs, int index) throws SQLException {
        byte bytes[] = rs.getBytes(index);

        if (bytes == null) {
            return null;
        } else {
            return new ByteArrayInputStream(bytes);
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.