01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.components.persistency.type;
10:
11: import com.completex.objective.components.persistency.OdalRuntimePersistencyException;
12: import com.completex.objective.components.persistency.PersistentObject;
13: import com.completex.objective.components.persistency.core.DatabasePolicy;
14: import com.completex.objective.util.TypeUtil;
15:
16: import java.io.ByteArrayOutputStream;
17: import java.io.InputStream;
18: import java.io.ByteArrayInputStream;
19: import java.sql.PreparedStatement;
20: import java.sql.SQLException;
21:
22: /**
23: * @author Gennady Krizhevsky
24: */
25: public class BinaryTypeHandler extends DefaultTypeHandler {
26:
27: public Object transformRead(Object data) throws SQLException {
28: return TypeUtil.object2inputStream(data);
29: }
30:
31: /**
32: * @see TypeHandler#handleBind(java.sql.PreparedStatement, int, Object, com.completex.objective.components.persistency.core.DatabasePolicy, LobPostProcessings, com.completex.objective.components.persistency.PersistentObject, int)
33: */
34: public void handleBind(PreparedStatement statement, int index,
35: Object value, DatabasePolicy databasePolicy,
36: LobPostProcessings postProcessings,
37: PersistentObject persistentObject, int fieldIndex)
38: throws SQLException {
39: if (value instanceof InputStream) {
40: handleInputStream(statement, index, value, databasePolicy,
41: postProcessings, persistentObject, fieldIndex);
42: } else { // If this is not an InputStream - try it anyway,
43: // it can be byte[] and set will be successful:
44: handleBind(statement, index, value);
45: }
46: }
47:
48: protected void handleInputStream(PreparedStatement statement,
49: int index, Object value, DatabasePolicy databasePolicy,
50: LobPostProcessings postProcessings,
51: PersistentObject persistentObject, int fieldIndex)
52: throws SQLException {
53: InputStream in = (InputStream) value;
54: if (useTwoStepBinaryUpdate(databasePolicy)) {
55: // The update will be done in 2 steps:
56: // 1st - save a minimal size byte array
57: // 2nd - retrieve this binary object and update it through its stream
58: // Consider it to be a Blob column
59: //
60: handleBind(statement, index, MINIMAL_BYTE_ARRAY);
61: postProcessings.add(persistentObject, fieldIndex, in);
62: } else {
63: //
64: // This portion is for "normal" 1 step update
65: //
66: if (value instanceof BinaryInputStreamImpl) {
67: BinaryInputStreamImpl binaryInputStream = ((BinaryInputStreamImpl) value);
68: long length = binaryInputStream.length();
69: if (length > Integer.MAX_VALUE) {
70: throw new OdalRuntimePersistencyException(
71: "Cannot handle input stream of size"
72: + length + " > Integer.MAX_VALUE");
73: }
74: statement.setBinaryStream(index, binaryInputStream,
75: ((int) length));
76: } else if (value instanceof ByteArrayInputStream) {
77: ByteArrayInputStream binaryInputStream = ((ByteArrayInputStream) value);
78: int length = binaryInputStream.available();
79: statement.setBinaryStream(index, binaryInputStream,
80: length);
81: } else {
82: //
83: // Direct binding for InputStream is unlikely, then try
84: // to transform it into byte[]:
85: //
86: ByteArrayOutputStream out = new ByteArrayOutputStream();
87: BlobImpl.writeBlob(in, out);
88: byte[] buf = out.toByteArray();
89: ByteArrayInputStream inBytes = new ByteArrayInputStream(
90: buf);
91: statement.setBinaryStream(index, inBytes, buf.length);
92: }
93: }
94: }
95:
96: }
|