Search in sources :

Example 1 with IPersistentStorage

use of org.jkiss.dbeaver.ui.editors.IPersistentStorage in project dbeaver by serge-rider.

the class FileRefDocumentProvider method doSaveDocument.

@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
    try {
        IStorage storage = EditorUtils.getStorageFromInput(element);
        File localFile = null;
        if (storage == null) {
            localFile = EditorUtils.getLocalFileFromInput(element);
            if (localFile == null) {
                throw new DBException("Can't obtain file from editor input");
            }
        }
        String encoding = (storage instanceof IEncodedStorage ? ((IEncodedStorage) storage).getCharset() : GeneralUtils.UTF8_ENCODING);
        Charset charset = Charset.forName(encoding);
        CharsetEncoder encoder = charset.newEncoder();
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
        byte[] bytes;
        ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(document.get()));
        if (byteBuffer.hasArray()) {
            bytes = byteBuffer.array();
        } else {
            bytes = new byte[byteBuffer.limit()];
            byteBuffer.get(bytes);
        }
        InputStream stream = new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
        if (storage instanceof IFile) {
            IFile file = (IFile) storage;
            if (file.exists()) {
                // inform about the upcoming content change
                fireElementStateChanging(element);
                try {
                    file.setContents(stream, true, true, monitor);
                } catch (CoreException x) {
                    // inform about failure
                    fireElementStateChangeFailed(element);
                    throw x;
                } catch (RuntimeException x) {
                    // inform about failure
                    fireElementStateChangeFailed(element);
                    throw x;
                }
            } else {
                try {
                    monitor.beginTask("Save file '" + file.getName() + "'", 2000);
                    //ContainerCreator creator = new ContainerCreator(file.getWorkspace(), file.getParent().getFullPath());
                    //creator.createContainer(new SubProgressMonitor(monitor, 1000));
                    file.create(stream, false, monitor);
                } finally {
                    monitor.done();
                }
            }
        } else if (storage instanceof IPersistentStorage) {
            monitor.beginTask("Save document", 1);
            ((IPersistentStorage) storage).setContents(monitor, stream);
        } else if (localFile != null) {
            try (OutputStream os = new FileOutputStream(localFile)) {
                IOUtils.copyStream(stream, os);
            }
        } else {
            throw new DBException("Storage [" + storage + "] doesn't support save");
        }
    } catch (Exception e) {
        if (e instanceof CoreException) {
            throw (CoreException) e;
        } else {
            throw new CoreException(GeneralUtils.makeExceptionStatus(e));
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Charset(java.nio.charset.Charset) IPersistentStorage(org.jkiss.dbeaver.ui.editors.IPersistentStorage) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) DBException(org.jkiss.dbeaver.DBException)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 Charset (java.nio.charset.Charset)1 CharsetEncoder (java.nio.charset.CharsetEncoder)1 DBException (org.jkiss.dbeaver.DBException)1 IPersistentStorage (org.jkiss.dbeaver.ui.editors.IPersistentStorage)1