001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.jdbc;
017
018import java.io.InputStream;
019import java.io.OutputStream;
020import java.sql.Blob;
021import java.sql.SQLException;
022import java.sql.SQLFeatureNotSupportedException;
023import javax.jcr.Binary;
024import javax.jcr.RepositoryException;
025import javax.jcr.Value;
026
027/**
028 * The JDBC {@link Blob} wrapper around a JCR binary {@link Value} object.
029 */
030public class JcrBlob implements Blob {
031
032    private final Binary binary;
033
034    /**
035     * Creates a new {@link java.sql.Blob} by wrapping a JCR binary
036     * 
037     * @param binary a {@link javax.jcr.Binary} instance; may not be null
038     */
039    public JcrBlob( Binary binary ) {
040        this.binary = binary;
041        assert this.binary != null;
042    }
043
044    @Override
045    public void free() {
046        binary.dispose();
047    }
048
049    @Override
050    public InputStream getBinaryStream() throws SQLException {
051        try {
052            return binary.getStream();
053        } catch (RepositoryException e) {
054            throw new SQLException(e);
055        }
056    }
057
058    @Override
059    public InputStream getBinaryStream( long pos,
060                                        long length ) throws SQLException {
061        throw new SQLFeatureNotSupportedException();
062    }
063
064    @Override
065    public byte[] getBytes( long pos,
066                            int length ) throws SQLException {
067        try {
068            byte[] data = new byte[length];
069            int numRead = 0;
070            try {
071                numRead = binary.read(data, pos);
072            } finally {
073                binary.dispose();
074            }
075
076            // We may have read less than the desired length ...
077            if (numRead < length) {
078                // create a shortened array ...
079                byte[] shortData = new byte[numRead];
080                System.arraycopy(data, 0, shortData, 0, numRead);
081                data = shortData;
082            }
083            return data;
084        } catch (Exception e) {
085            throw new SQLException(e);
086        }
087    }
088
089    @Override
090    public long length() throws SQLException {
091        try {
092            return binary.getSize();
093        } catch (RepositoryException e) {
094            throw new SQLException(e);
095        }
096    }
097
098    @Override
099    public long position( byte[] pattern,
100                          long start ) throws SQLException {
101        throw new SQLFeatureNotSupportedException();
102    }
103
104    @Override
105    public long position( Blob pattern,
106                          long start ) throws SQLException {
107        throw new SQLFeatureNotSupportedException();
108    }
109
110    @Override
111    public OutputStream setBinaryStream( long pos ) throws SQLException {
112        throw new SQLFeatureNotSupportedException();
113    }
114
115    @Override
116    public int setBytes( long pos,
117                         byte[] bytes ) throws SQLException {
118        throw new SQLFeatureNotSupportedException();
119    }
120
121    @Override
122    public int setBytes( long pos,
123                         byte[] bytes,
124                         int offset,
125                         int len ) throws SQLException {
126        throw new SQLFeatureNotSupportedException();
127    }
128
129    @Override
130    public void truncate( long len ) throws SQLException {
131        throw new SQLFeatureNotSupportedException();
132    }
133
134}