use of org.jkiss.dbeaver.model.data.DBDContentCached in project dbeaver by dbeaver.
the class ContentUtils method getContentStringValue.
@Nullable
public static String getContentStringValue(@NotNull DBRProgressMonitor monitor, @NotNull DBDContent object) throws DBCException {
if (object.isNull()) {
return null;
}
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.DBDContentCached in project dbeaver by dbeaver.
the class TextViewDialog method primeEditorValue.
@Override
public void primeEditorValue(@Nullable Object value) {
if (value instanceof DBDContentCached) {
value = ((DBDContentCached) value).getCachedValue();
}
if (value instanceof byte[]) {
// Binary
byte[] bytes = (byte[]) value;
textEdit.setText(GeneralUtils.convertToString(bytes, 0, bytes.length));
if (hexEditorService != null) {
hexEditorService.setHexContent(hexEditControl, bytes, getDefaultCharset());
}
} else {
// Should be string
final IValueController valueController = getValueController();
final String strValue = valueController.getValueHandler().getValueDisplayString(valueController.getValueType(), value, DBDDisplayFormat.EDIT);
textEdit.setText(strValue);
if (hexEditControl != null) {
setBinaryContent(strValue);
}
}
}
use of org.jkiss.dbeaver.model.data.DBDContentCached in project dbeaver by serge-rider.
the class ContentEditorInput method updateContentFromFile.
public void updateContentFromFile(IProgressMonitor monitor) throws DBException {
if (valueController.isReadOnly()) {
throw new DBCException("Can't update read-only value");
}
DBRProgressMonitor localMonitor = RuntimeUtils.makeMonitor(monitor);
DBDContent content = getContent();
DBDContentStorage storage = content.getContents(localMonitor);
if (storage instanceof DBDContentStorageLocal) {
// Nothing to update - we user content's storage
contentDetached = true;
} else if (storage instanceof DBDContentCached) {
// Create new storage and pass it to content
try (FileInputStream is = new FileInputStream(contentFile)) {
if (storage instanceof StringContentStorage) {
try (Reader reader = new InputStreamReader(is, fileCharset)) {
storage = StringContentStorage.createFromReader(reader);
}
} else {
storage = BytesContentStorage.createFromStream(is, contentFile.length(), fileCharset);
}
//StringContentStorage.
contentDetached = content.updateContents(localMonitor, storage);
} catch (IOException e) {
throw new DBException("Error reading content from file", e);
}
} else {
// Create new storage and pass it to content
storage = new TemporaryContentStorage(DBeaverCore.getInstance(), contentFile, fileCharset);
contentDetached = content.updateContents(localMonitor, storage);
}
}
use of org.jkiss.dbeaver.model.data.DBDContentCached in project dbeaver by serge-rider.
the class TextViewDialog method primeEditorValue.
@Override
public void primeEditorValue(@Nullable Object value) {
if (value instanceof DBDContentCached) {
value = ((DBDContentCached) value).getCachedValue();
}
if (value instanceof byte[]) {
// Binary
byte[] bytes = (byte[]) value;
textEdit.setText(GeneralUtils.convertToString(bytes, 0, bytes.length));
if (hexEditorService != null) {
hexEditorService.setHexContent(hexEditControl, bytes, getDefaultCharset());
}
} else {
// Should be string
final IValueController valueController = getValueController();
final String strValue = valueController.getValueHandler().getValueDisplayString(valueController.getValueType(), value, DBDDisplayFormat.EDIT);
textEdit.setText(strValue);
if (hexEditControl != null) {
setBinaryContent(strValue);
}
}
}
use of org.jkiss.dbeaver.model.data.DBDContentCached 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;
}
Aggregations