001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: RawContentStream.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf.dam.contentstores;
009:
010: import com.uwyn.rife.database.DbPreparedStatement;
011: import com.uwyn.rife.database.DbResultSet;
012: import com.uwyn.rife.database.exceptions.DatabaseException;
013: import com.uwyn.rife.database.queries.Select;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.sql.SQLException;
017:
018: public class RawContentStream extends InputStream {
019: protected DbPreparedStatement mStatement = null;
020: protected DbResultSet mResultSet = null;
021: protected byte[] mBuffer = null;
022: protected int mIndex = 0;
023: protected boolean mHasRow = false;
024:
025: protected RawContentStream(DbPreparedStatement statement) {
026: mStatement = statement;
027: mResultSet = mStatement.getResultSet();
028: mHasRow = true;
029:
030: assert mStatement != null;
031: assert mResultSet != null;
032: }
033:
034: public int read() throws IOException {
035: if (null == mResultSet) {
036: throw new IOException(
037: "Trying to read from a closed raw content stream.");
038: }
039:
040: int result = -1;
041: try {
042: if (null == mBuffer) {
043: if (!mHasRow) {
044: return -1;
045: }
046:
047: mBuffer = mResultSet.getBytes("chunk");
048: mIndex = 0;
049: }
050:
051: result = mBuffer[mIndex++];
052:
053: if (mIndex >= mBuffer.length) {
054: mBuffer = null;
055: mHasRow = mResultSet.next();
056: }
057: } catch (SQLException e) {
058: IOException e2 = new IOException(
059: "Unexpected error while reading the next bytes.");
060: e2.initCause(e);
061: throw e2;
062: }
063:
064: return result;
065: }
066:
067: public void close() throws IOException {
068: if (null == mStatement) {
069: return;
070: }
071:
072: try {
073: mStatement.close();
074: } catch (DatabaseException e) {
075: IOException e2 = new IOException(
076: "Unable to close prepared statement.");
077: e2.initCause(e);
078: throw e2;
079: } finally {
080: mStatement = null;
081: mResultSet = null;
082: mBuffer = null;
083: }
084: }
085:
086: protected static DbPreparedStatement prepareStatement(
087: DatabaseRawStore store, Select retrieveContentChunks, int id) {
088: DbPreparedStatement statement = store
089: .getStreamPreparedStatement(retrieveContentChunks,
090: store.getConnection());
091:
092: statement.setInt("contentId", id);
093: statement.executeQuery();
094: DbResultSet resultset = statement.getResultSet();
095: try {
096: if (!resultset.next()) {
097: statement.close();
098: return null;
099: }
100: } catch (SQLException e) {
101: statement.close();
102: return null;
103: }
104:
105: return statement;
106: }
107:
108: public static RawContentStream getInstance(DatabaseRawStore store,
109: Select retrieveContentChunks, int id) {
110: DbPreparedStatement statement = prepareStatement(store,
111: retrieveContentChunks, id);
112: if (null == statement) {
113: return null;
114: }
115:
116: return new RawContentStream(statement);
117: }
118: }
|