Search in sources :

Example 1 with CUBRIDBlobProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy in project cubrid-manager by CUBRID.

the class DBAttrTypeFormatter method formatMuchValue.

/**
	 * Format much value to JDBC object, this value probably is from file, if it
	 * is file, convert it for JDBC object according to JDBC type
	 * 
	 * @param str The much value
	 * @param type The JDBC type
	 * @param conn The Connection
	 * @param dbCharSet The database charset
	 * @param fileCharSet The file charset
	 * @param isUseNULLValueSetting
	 * @return the real JDBC object
	 */
public static Object formatMuchValue(String str, String type, Connection conn, String dbCharSet, String fileCharSet, boolean isUseNULLValueSetting) {
    String upperType = type.trim().toUpperCase();
    boolean isString = upperType.startsWith(DataType.DATATYPE_VARCHAR) || upperType.startsWith(DataType.DATATYPE_CHAR) || upperType.startsWith(DataType.DATATYPE_STRING);
    boolean isByte = upperType.startsWith(DataType.DATATYPE_BIT_VARYING) || upperType.startsWith(DataType.DATATYPE_BIT) || upperType.startsWith(DataType.DATATYPE_NCHAR) || upperType.startsWith(DataType.DATATYPE_NCHAR_VARYING);
    int size = 0;
    if (isString || isByte) {
        size = DataType.getSize(type);
    }
    Object realObj = null;
    String errorMsg = null;
    File file = new File(str.replaceFirst(FILE_URL_PREFIX, ""));
    if (upperType.startsWith(DataType.DATATYPE_BLOB)) {
        try {
            CUBRIDBlobProxy blob = new CUBRIDBlobProxy((CUBRIDConnectionProxy) conn);
            if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
                InputStream fin = null;
                OutputStream out = null;
                try {
                    fin = new FileInputStream(file);
                    out = blob.setBinaryStream(1);
                    byte[] data = new byte[512];
                    int count = -1;
                    while ((count = fin.read(data)) != -1) {
                        out.write(data, 0, count);
                    }
                    realObj = blob.getProxyObj();
                } catch (IOException e) {
                    errorMsg = e.getMessage();
                    LOGGER.error("", e);
                } finally {
                    try {
                        fin.close();
                    } catch (IOException e) {
                        LOGGER.error("", e);
                    }
                    try {
                        out.close();
                    } catch (IOException e) {
                        LOGGER.error("", e);
                    }
                }
            } else {
                try {
                    byte[] byteArr = str.getBytes(dbCharSet);
                    blob.setBytes(1, byteArr);
                    realObj = blob.getProxyObj();
                } catch (UnsupportedEncodingException e) {
                    errorMsg = e.getMessage();
                    LOGGER.error("", e);
                }
            }
        } catch (CUBRIDProxyException e) {
            errorMsg = e.getMessage();
            LOGGER.error("", e);
        } catch (SQLException e) {
            errorMsg = e.getMessage();
            LOGGER.error("", e);
        }
    } else if (upperType.startsWith(DataType.DATATYPE_CLOB)) {
        try {
            CUBRIDClobProxy clob = new CUBRIDClobProxy((CUBRIDConnectionProxy) conn, dbCharSet);
            if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
                BufferedReader reader = null;
                Writer writer = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), fileCharSet));
                    writer = clob.setCharacterStream(1);
                    char[] charArr = new char[512];
                    int count = reader.read(charArr);
                    while (count > 0) {
                        writer.write(charArr, 0, count);
                        count = reader.read(charArr);
                    }
                } catch (IOException e) {
                    errorMsg = e.getMessage();
                    LOGGER.error("", e);
                } finally {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        LOGGER.error("", e);
                    }
                    try {
                        writer.close();
                    } catch (IOException e) {
                        LOGGER.error("", e);
                    }
                }
            } else {
                clob.setString(1, str);
            }
            realObj = clob.getProxyObj();
        } catch (CUBRIDProxyException e) {
            errorMsg = e.getMessage();
            LOGGER.error("", e);
        } catch (SQLException e) {
            errorMsg = e.getMessage();
            LOGGER.error("", e);
        }
    } else if (isString) {
        if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), fileCharSet));
                StringBuffer strBuffer = new StringBuffer();
                char[] charArr = new char[512];
                int count = reader.read(charArr);
                int totalCount = count;
                while (count > 0) {
                    if (count == 512) {
                        strBuffer.append(charArr);
                    } else {
                        char[] tmpChar = new char[count];
                        System.arraycopy(charArr, 0, tmpChar, 0, count);
                        strBuffer.append(tmpChar);
                    }
                    count = reader.read(charArr);
                    totalCount += count;
                }
                if (totalCount > size) {
                    errorMsg = Messages.bind(Messages.fileTooLongMsg, new String[] { str, type });
                } else {
                    realObj = strBuffer.toString();
                }
            } catch (IOException e) {
                errorMsg = e.getMessage();
                LOGGER.error("", e);
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    LOGGER.error("", e);
                }
            }
        } else {
            realObj = str;
        }
    } else if (isByte) {
        if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
            InputStream fin = null;
            try {
                fin = new FileInputStream(file);
                int length = fin.available();
                if (length * 8 > size) {
                    errorMsg = Messages.bind(Messages.fileTooLongMsg, new String[] { str, type });
                } else {
                    byte[] data = new byte[length];
                    if (fin.read(data) != -1) {
                        realObj = data;
                    }
                }
            } catch (IOException e) {
                errorMsg = e.getMessage();
                LOGGER.error("", e);
            } finally {
                try {
                    fin.close();
                } catch (IOException e) {
                    LOGGER.error("", e);
                }
            }
        } else {
            FormatDataResult result = DBAttrTypeFormatter.format(type, str, false, dbCharSet, isUseNULLValueSetting);
            byte[] byteArr = (byte[]) result.getFormatedJavaObj();
            realObj = byteArr;
        }
    }
    if (errorMsg != null) {
        realObj = new Exception(errorMsg);
    }
    return realObj;
}
Also used : InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) CUBRIDConnectionProxy(com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy) FileInputStream(java.io.FileInputStream) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) CUBRIDProxyException(com.cubrid.jdbc.proxy.manage.CUBRIDProxyException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CUBRIDProxyException(com.cubrid.jdbc.proxy.manage.CUBRIDProxyException) CUBRIDBlobProxy(com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy) BufferedReader(java.io.BufferedReader) File(java.io.File) CUBRIDClobProxy(com.cubrid.jdbc.proxy.driver.CUBRIDClobProxy) Writer(java.io.Writer)

Example 2 with CUBRIDBlobProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy in project cubrid-manager by CUBRID.

the class AbsExportDataHandler method exportBlobData.

/**
	 * Export blob data to file
	 *
	 * @param folderName
	 * @param blob
	 * @return
	 * @throws SQLException
	 */
protected String exportBlobData(String folderName, Blob blob) throws SQLException {
    if (!exportConfig.isExportLob() || blob == null) {
        // FIXME move this logic to core module
        return null;
    }
    if (blob instanceof CUBRIDBlobProxy) {
        CUBRIDBlobProxy cbp = (CUBRIDBlobProxy) blob;
        if (cbp.getProxyObj() == null) {
            return null;
        }
    }
    String fileName = null;
    OutputStream writer = null;
    InputStream reader = null;
    try {
        File folder = new File(exportConfig.getDataFileFolder() + File.separator + folderName + BLOB_FOLDER_POSTFIX);
        if (!folder.exists()) {
            folder.mkdir();
        }
        File file = createLobFile(folder.getAbsolutePath());
        fileName = file.getName();
        writer = new FileOutputStream(file);
        if (blob != null) {
            reader = blob.getBinaryStream();
            byte[] buf = new byte[1024];
            int len = reader.read(buf);
            while (len != -1) {
                writer.write(buf);
                len = reader.read(buf);
            }
        }
    } catch (IOException e) {
        LOGGER.error(e.getLocalizedMessage());
    } finally {
        Closer.close(writer);
        Closer.close(reader);
    }
    return fileName;
}
Also used : CUBRIDBlobProxy(com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 3 with CUBRIDBlobProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy in project cubrid-manager by CUBRID.

the class ResultSetDataCache method free.

public void free() {
    for (ArrayList<Object> rowData : datas) {
        for (Object value : rowData) {
            if (value instanceof Blob) {
                Blob blob = (Blob) value;
                try {
                    if (blob != null && ((CUBRIDBlobProxy) blob).getProxyObj() != null) {
                        blob.free();
                        blob = null;
                    }
                } catch (SQLException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            } else if (value instanceof Clob) {
                Clob clob = (Clob) value;
                try {
                    if (clob != null && ((CUBRIDClobProxy) clob).getProxyObj() != null) {
                        clob.free();
                        clob = null;
                    }
                } catch (SQLException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
        }
    }
}
Also used : Blob(java.sql.Blob) CUBRIDBlobProxy(com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy) SQLException(java.sql.SQLException) Clob(java.sql.Clob)

Aggregations

CUBRIDBlobProxy (com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy)3 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 SQLException (java.sql.SQLException)2 CUBRIDClobProxy (com.cubrid.jdbc.proxy.driver.CUBRIDClobProxy)1 CUBRIDConnectionProxy (com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy)1 CUBRIDProxyException (com.cubrid.jdbc.proxy.manage.CUBRIDProxyException)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Writer (java.io.Writer)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 ParseException (java.text.ParseException)1