security.checkPermission(Backupable.BACKUP_RESTORE_PERMISSION);
}
int dialect = DialectDetecter.detect(jdbcConn.getMetaData()).hashCode();
ObjectZipWriterImpl contentWriter = null;
ObjectZipWriterImpl contentLenWriter = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try
{
File contentFile = new File(storageDir, tableName + CONTENT_FILE_SUFFIX);
contentWriter = new ObjectZipWriterImpl(PrivilegedFileHelper.zipOutputStream(contentFile));
contentWriter.putNextEntry(new ZipEntry(tableName));
File contentLenFile = new File(storageDir, tableName + CONTENT_LEN_FILE_SUFFIX);
contentLenWriter = new ObjectZipWriterImpl(PrivilegedFileHelper.zipOutputStream(contentLenFile));
contentLenWriter.putNextEntry(new ZipEntry(tableName));
stmt = jdbcConn.prepareStatement(script);
rs = stmt.executeQuery();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
int[] columnType = new int[columnCount];
contentWriter.writeInt(columnCount);
for (int i = 0; i < columnCount; i++)
{
columnType[i] = metaData.getColumnType(i + 1);
contentWriter.writeInt(columnType[i]);
contentWriter.writeString(metaData.getColumnName(i + 1));
}
// Now we can output the actual data
while (rs.next())
{
for (int i = 0; i < columnCount; i++)
{
InputStream value;
if (columnType[i] == Types.VARBINARY || columnType[i] == Types.LONGVARBINARY
|| columnType[i] == Types.BLOB || columnType[i] == Types.BINARY)
{
value = rs.getBinaryStream(i + 1);
}
else
{
String str = rs.getString(i + 1);
value = str == null ? null : new ByteArrayInputStream(str.getBytes(Constants.DEFAULT_ENCODING));
}
if (value == null)
{
contentLenWriter.writeLong(-1);
}
else
{
long len = 0;
int read = 0;
byte[] tmpBuff = new byte[2048];
while ((read = value.read(tmpBuff)) >= 0)
{
contentWriter.write(tmpBuff, 0, read);
len += read;
}
contentLenWriter.writeLong(len);
}
}
}
}
finally
{
if (contentWriter != null)
{
contentWriter.closeEntry();
contentWriter.close();
}
if (contentLenWriter != null)
{
contentLenWriter.closeEntry();
contentLenWriter.close();
}
if (rs != null)
{
rs.close();