org.deshang.content.indexing.util.jdbc.AbstractRowMapper.java Source code

Java tutorial

Introduction

Here is the source code for org.deshang.content.indexing.util.jdbc.AbstractRowMapper.java

Source

/*
 * Copyright 2014 Deshang group.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.deshang.content.indexing.util.jdbc;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;

public abstract class AbstractRowMapper<T> implements RowMapper<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRowMapper.class);

    protected String getBlobContent(Blob blob) throws SQLException {

        LOGGER.debug("Enter getBlobContent(Blob)");
        String blobContent = null;
        try {
            InputStream in = blob.getBinaryStream();
            BufferedInputStream bis = new BufferedInputStream(in);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            int data = -1;
            while ((data = bis.read()) != -1) {
                baos.write(data);
            }
            blobContent = baos.toString("UTF-8");
        } catch (IOException e) {
            throw new SQLException("Can't read blob conent", e);
        }

        LOGGER.debug("Exit getBlobContent(Blob)");
        return blobContent;
    }
}