use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.
the class ContentEditorInput method prepareContent.
private void prepareContent(DBRProgressMonitor monitor) throws DBException {
DBDContent content = getContent();
DBDContentStorage storage = content.getContents(monitor);
if (contentDetached) {
release(monitor);
contentDetached = false;
}
if (storage instanceof DBDContentStorageLocal) {
// User content's storage directly
contentFile = ((DBDContentStorageLocal) storage).getDataFile();
contentDetached = true;
} else {
// Copy content to local file
try {
// Create file
if (contentFile == null) {
String valueId;
if (valueController instanceof IAttributeController) {
valueId = ((IAttributeController) valueController).getColumnId();
} else {
valueId = valueController.getValueName();
}
contentFile = ContentUtils.createTempContentFile(monitor, DBeaverCore.getInstance(), valueId);
}
// Write value to file
copyContentToFile(content, monitor);
} catch (IOException e) {
// Delete temp file
if (contentFile != null && contentFile.exists()) {
if (!contentFile.delete()) {
log.warn("Can't delete temporary content file '" + contentFile.getAbsolutePath() + "'");
}
}
throw new DBException("Can't delete content file", e);
}
}
// Mark file as readonly
if (valueController.isReadOnly()) {
markReadOnly(true);
}
}
use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.
the class DialogUtils method saveToFile.
public static void saveToFile(IValueController controller) {
if (!(controller.getValue() instanceof DBDContent)) {
log.error(CoreMessages.model_jdbc_bad_content_value_ + controller.getValue());
return;
}
Shell shell = UIUtils.getShell(controller.getValueSite());
final File saveFile = selectFileForSave(shell, controller.getValueName());
if (saveFile == null) {
return;
}
final DBDContent value = (DBDContent) controller.getValue();
try {
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
DBDContentStorage storage = value.getContents(monitor);
if (ContentUtils.isTextContent(value)) {
try (Reader cr = storage.getContentReader()) {
ContentUtils.saveContentToFile(cr, saveFile, GeneralUtils.UTF8_ENCODING, monitor);
}
} else {
try (InputStream cs = storage.getContentStream()) {
ContentUtils.saveContentToFile(cs, saveFile, monitor);
}
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InvocationTargetException e) {
UIUtils.showErrorDialog(shell, CoreMessages.model_jdbc_could_not_save_content, //$NON-NLS-2$
CoreMessages.model_jdbc_could_not_save_content_to_file_ + saveFile.getAbsolutePath() + "'", e.getTargetException());
} catch (InterruptedException e) {
// do nothing
}
}
use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.
the class ContentUtils method getContentStringValue.
@NotNull
public static String getContentStringValue(@NotNull DBRProgressMonitor monitor, @NotNull DBDContent object) throws DBCException {
DBDContentStorage data = object.getContents(monitor);
if (data != null) {
if (data instanceof DBDContentCached) {
Object cachedValue = ((DBDContentCached) data).getCachedValue();
if (cachedValue instanceof String) {
return (String) cachedValue;
}
}
try {
Reader contentReader = data.getContentReader();
if (contentReader != null) {
try {
StringWriter buf = new StringWriter();
ContentUtils.copyStreams(contentReader, object.getContentLength(), buf, monitor);
return buf.toString();
} finally {
IOUtils.close(contentReader);
}
}
} catch (IOException e) {
log.debug("Can't extract string from content", e);
}
}
return object.toString();
}
use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.
the class ContentUtils method getContentBinaryValue.
@NotNull
public static byte[] getContentBinaryValue(@NotNull DBRProgressMonitor monitor, @NotNull DBDContent object) throws DBCException {
DBDContentStorage data = object.getContents(monitor);
if (data != null) {
if (data instanceof DBDContentCached) {
Object cachedValue = ((DBDContentCached) data).getCachedValue();
if (cachedValue instanceof byte[]) {
return (byte[]) cachedValue;
}
}
try {
InputStream contentStream = data.getContentStream();
if (contentStream != null) {
try {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ContentUtils.copyStreams(contentStream, object.getContentLength(), buf, monitor);
return buf.toByteArray();
} finally {
IOUtils.close(contentStream);
}
}
} catch (IOException e) {
log.debug("Can't extract string from content", e);
}
}
return null;
}
use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.
the class JDBCContentLOB method cloneValue.
@Override
public DBDValueCloneable cloneValue(DBRProgressMonitor monitor) throws DBCException {
JDBCContentLOB copy = createNewContent();
DBDContentStorage storage = getContents(monitor);
if (storage != null) {
try {
copy.updateContents(monitor, storage.cloneStorage(monitor));
} catch (IOException e) {
throw new DBCException("IO error while clone content", e);
}
}
return copy;
}
Aggregations