01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: RawContentStreamOracle.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.cmf.dam.contentstores.rawstoredrivers;
09:
10: import com.uwyn.rife.cmf.dam.contentstores.DatabaseRawStore;
11: import com.uwyn.rife.cmf.dam.contentstores.RawContentStream;
12: import com.uwyn.rife.database.DbPreparedStatement;
13: import com.uwyn.rife.database.queries.Select;
14: import java.io.IOException;
15: import java.io.InputStream;
16: import java.sql.SQLException;
17: import oracle.sql.BLOB;
18:
19: public class RawContentStreamOracle extends RawContentStream {
20: private InputStream mInputStream = null;
21:
22: protected RawContentStreamOracle(DbPreparedStatement statement) {
23: super (statement);
24: }
25:
26: public int read() throws IOException {
27: BLOB blob = null;
28:
29: int result = -1;
30: try {
31: while (true) {
32: if (null == mInputStream) {
33: if (!mHasRow) {
34: return -1;
35: }
36:
37: blob = (BLOB) mResultSet.getBlob("chunk");
38: mInputStream = blob.getBinaryStream();
39: }
40:
41: result = mInputStream.read();
42:
43: if (-1 == result) {
44: mInputStream.close();
45: mInputStream = null;
46: mHasRow = mResultSet.next();
47: continue;
48: }
49:
50: break;
51: }
52: } catch (SQLException e) {
53: IOException e2 = new IOException(
54: "Unexpected error while reading the next bytes.");
55: e2.initCause(e);
56: throw e2;
57: }
58:
59: return result;
60: }
61:
62: public void close() throws IOException {
63: if (mInputStream != null) {
64: mInputStream.close();
65:
66: mInputStream = null;
67: }
68: }
69:
70: public static RawContentStream getInstance(DatabaseRawStore store,
71: Select retrieveContentChunks, int id) {
72: DbPreparedStatement statement = prepareStatement(store,
73: retrieveContentChunks, id);
74: if (null == statement) {
75: return null;
76: }
77:
78: return new RawContentStreamOracle(statement);
79: }
80: }
|