1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package com.sun.star.comp.sdbc;
22 
23 import java.io.InputStream;
24 
25 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.ResourceBasedEventLogger;
26 import org.apache.openoffice.comp.sdbc.dbtools.util.Resources;
27 import org.apache.openoffice.comp.sdbc.dbtools.util.SharedResources;
28 import org.apache.openoffice.comp.sdbc.dbtools.util.StandardSQLState;
29 
30 import com.sun.star.io.XInputStream;
31 import com.sun.star.lib.uno.adapter.InputStreamToXInputStreamAdapter;
32 import com.sun.star.lib.uno.helper.ComponentBase;
33 import com.sun.star.logging.LogLevel;
34 import com.sun.star.sdbc.SQLException;
35 import com.sun.star.sdbc.XBlob;
36 import com.sun.star.uno.Any;
37 
38 public class JavaSQLBlob extends ComponentBase implements XBlob {
39     private final ResourceBasedEventLogger logger;
40     private final java.sql.Blob jdbcBlob;
41 
JavaSQLBlob(ResourceBasedEventLogger logger, java.sql.Blob jdbcBlob)42     public JavaSQLBlob(ResourceBasedEventLogger logger, java.sql.Blob jdbcBlob) {
43         this.logger = logger;
44         this.jdbcBlob = jdbcBlob;
45     }
46 
47     @Override
postDisposing()48     protected void postDisposing() {
49         try {
50             jdbcBlob.free();
51         } catch (java.sql.SQLException jdbcSQLException) {
52             logger.log(LogLevel.WARNING, jdbcSQLException);
53         }
54     }
55 
56     @Override
getBinaryStream()57     public XInputStream getBinaryStream() throws SQLException {
58         try {
59             InputStream inputStream = jdbcBlob.getBinaryStream();
60             if (inputStream != null) {
61                 return new InputStreamToXInputStreamAdapter(inputStream);
62             } else {
63                 return null;
64             }
65         } catch (java.sql.SQLException jdbcSQLException) {
66             throw Tools.toUnoException(this, jdbcSQLException);
67         }
68     }
69 
70     @Override
getBytes(long pos, int length)71     public byte[] getBytes(long pos, int length) throws SQLException {
72         try {
73             return jdbcBlob.getBytes(pos, length);
74         } catch (java.sql.SQLException jdbcSQLException) {
75             throw Tools.toUnoException(this, jdbcSQLException);
76         }
77     }
78 
79     @Override
length()80     public long length() throws SQLException {
81         try {
82             return jdbcBlob.length();
83         } catch (java.sql.SQLException jdbcSQLException) {
84             throw Tools.toUnoException(this, jdbcSQLException);
85         }
86     }
87 
88     @Override
position(byte[] pattern, long start)89     public long position(byte[] pattern, long start) throws SQLException {
90         try {
91             return jdbcBlob.position(pattern, start);
92         } catch (java.sql.SQLException jdbcSQLException) {
93             throw Tools.toUnoException(this, jdbcSQLException);
94         }
95     }
96 
97     @Override
positionOfBlob(XBlob arg0, long arg1)98     public long positionOfBlob(XBlob arg0, long arg1) throws SQLException {
99         // this was put here in CWS warnings01. The previous implementation was defective, as it did ignore
100         // the pattern parameter. Since the effort for proper implementation is rather high - we would need
101         // to translated patter into a byte[] -, we defer this functionality for the moment (hey, it was
102         // unusable, anyway)
103         // 2005-11-15 / #i57457# / frank.schoenheit@sun.com
104         throw new SQLException(
105                 SharedResources.getInstance().getResourceStringWithSubstitution(
106                         Resources.STR_UNSUPPORTED_FEATURE, "$featurename$", "XBlob::positionOfBlob"),
107                 this, StandardSQLState.SQL_FEATURE_NOT_IMPLEMENTED.name(), 0, Any.VOID);
108     }
109 }
110