use of com.cubrid.jdbc.proxy.manage.CUBRIDProxyException 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;
}
Aggregations