Search in sources :

Example 1 with ConcurrentReadException

use of org.apache.sis.storage.ConcurrentReadException in project sis by apache.

the class Store method write.

/**
 * Replaces the content of this GPX file by the given metadata and features.
 *
 * @param  metadata  the metadata to write, or {@code null} if none.
 * @param  features  the features to write, or {@code null} if none.
 * @throws ConcurrentReadException if the {@code features} stream was provided by this data store.
 * @throws DataStoreException if an error occurred while writing the data.
 */
public synchronized void write(final Metadata metadata, final Stream<? extends AbstractFeature> features) throws DataStoreException {
    try {
        /*
             * If we created a reader for reading metadata, we need to close that reader now otherwise the call
             * to 'new Writer(…)' will fail.  Note that if that reader was in use by someone else, the 'reader'
             * field would be null and the 'new Writer(…)' call should detect that a reader is in use somewhere.
             */
        final Reader r = reader;
        if (r != null) {
            reader = null;
            r.close();
        }
        /*
             * Get the writer if no read or other write operation is in progress, then write the data.
             */
        try (Writer writer = new Writer(this, org.apache.sis.internal.storage.gpx.Metadata.castOrCopy(metadata, locale))) {
            writer.writeStartDocument();
            if (features != null) {
                features.forEachOrdered(writer);
            }
            writer.writeEndDocument();
        }
    } catch (BackingStoreException e) {
        final Throwable cause = e.getCause();
        if (cause instanceof DataStoreException) {
            throw (DataStoreException) cause;
        }
        throw new DataStoreException(e.getLocalizedMessage(), cause);
    } catch (Exception e) {
        if (e instanceof UncheckedIOException) {
            e = ((UncheckedIOException) e).getCause();
        }
        throw new DataStoreException(e);
    }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) UncheckedIOException(java.io.UncheckedIOException) URISyntaxException(java.net.URISyntaxException) IllegalNameException(org.apache.sis.storage.IllegalNameException) DataStoreContentException(org.apache.sis.storage.DataStoreContentException) DataStoreException(org.apache.sis.storage.DataStoreException) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) UncheckedIOException(java.io.UncheckedIOException) ConcurrentReadException(org.apache.sis.storage.ConcurrentReadException) FactoryException(org.opengis.util.FactoryException)

Example 2 with ConcurrentReadException

use of org.apache.sis.storage.ConcurrentReadException in project sis by apache.

the class StaxDataStore method createWriter.

/**
 * Creates a new XML stream writer for writing the XML document.
 * If another {@code XMLStreamWriter} has already been created before this method call,
 * whether this method will succeed in creating a new writer depends on the storage type
 * (e.g. file or output stream).
 *
 * @param  target  the writer which will store the {@code XMLStreamWriter} reference.
 * @return a new writer for writing the XML data.
 * @throws DataStoreException if the output type is not recognized or the data store is closed.
 * @throws XMLStreamException if an error occurred while opening the XML file.
 * @throws IOException if an error occurred while preparing the output stream.
 */
final synchronized XMLStreamWriter createWriter(final StaxStreamWriter target) throws DataStoreException, XMLStreamException, IOException {
    Object outputOrFile = storage;
    if (outputOrFile == null) {
        throw new DataStoreClosedException(getLocale(), getFormatName(), StandardOpenOption.WRITE);
    }
    switch(state) {
        default:
            throw new AssertionError(state);
        case READING:
            throw new ConcurrentReadException(getLocale(), getDisplayName());
        case WRITING:
            throw new ConcurrentWriteException(getLocale(), getDisplayName());
        // Stream already at the data start; nothing to do.
        case START:
            break;
        case FINISHED:
            {
                if (reset())
                    break;
                throw new ForwardOnlyStorageException(getLocale(), getDisplayName(), StandardOpenOption.WRITE);
            }
    }
    /*
         * If the storage given by the user was not one of OutputStream, Writer or other type recognized
         * by OutputType, then maybe that storage was a Path, File or URL, in which case the constructor
         * should have opened an InputStream (not an OutputStream) for it. In some cases (e.g. reading a
         * channel opened on a file), the input stream can be converted to an output stream.
         */
    AutoCloseable output = stream;
    OutputType type = storageToWriter;
    if (type == null) {
        type = OutputType.STREAM;
        output = IOUtilities.toOutputStream(output);
        if (output == null) {
            throw new UnsupportedStorageException(getLocale(), getFormatName(), storage, StandardOpenOption.WRITE);
        }
        outputOrFile = output;
        if (output != stream) {
            stream = output;
            mark();
        }
    }
    XMLStreamWriter writer = type.create(this, outputOrFile);
    if (indentation >= 0) {
        writer = new FormattedWriter(writer, indentation);
    }
    target.stream = output;
    state = WRITING;
    return writer;
}
Also used : ConcurrentWriteException(org.apache.sis.storage.ConcurrentWriteException) DataStoreClosedException(org.apache.sis.storage.DataStoreClosedException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ForwardOnlyStorageException(org.apache.sis.storage.ForwardOnlyStorageException) UnsupportedStorageException(org.apache.sis.storage.UnsupportedStorageException) ConcurrentReadException(org.apache.sis.storage.ConcurrentReadException)

Aggregations

ConcurrentReadException (org.apache.sis.storage.ConcurrentReadException)2 UncheckedIOException (java.io.UncheckedIOException)1 URISyntaxException (java.net.URISyntaxException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 ConcurrentWriteException (org.apache.sis.storage.ConcurrentWriteException)1 DataStoreClosedException (org.apache.sis.storage.DataStoreClosedException)1 DataStoreContentException (org.apache.sis.storage.DataStoreContentException)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 ForwardOnlyStorageException (org.apache.sis.storage.ForwardOnlyStorageException)1 IllegalNameException (org.apache.sis.storage.IllegalNameException)1 UnsupportedStorageException (org.apache.sis.storage.UnsupportedStorageException)1 BackingStoreException (org.apache.sis.util.collection.BackingStoreException)1 FactoryException (org.opengis.util.FactoryException)1