Class OutputStream

All Implemented Interfaces:
AutoCloseable, Proxy, AutoCloseable
Direct Known Subclasses:
FileOutputStream, FilterOutputStream, MemoryOutputStream, OutputStream.OutputStream$Impl, PollableOutputStream.PollableOutputStream$Impl

@Generated("org.javagi.JavaGI") public abstract class OutputStream extends GObject implements AutoCloseable

GOutputStream is a base class for implementing streaming output.

It has functions to write to a stream (write(byte[], Cancellable)), to close a stream (close(Cancellable)) and to flush pending writes (flush(Cancellable)).

To copy the content of an input stream to an output stream without manually handling the reads and writes, use splice(InputStream, Set, Cancellable).

See the documentation for IOStream for details of thread safety of streaming APIs.

All of these functions have async variants too.

All classes derived from GOutputStream should implement synchronous writing, splicing, flushing and closing streams, but may implement asynchronous versions.

  • Constructor Details

    • OutputStream

      public OutputStream(MemorySegment address)
      Create a OutputStream instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • OutputStream

      public OutputStream()
      Create a new OutputStream.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the OutputStream class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected OutputStream asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • clearPending

      public void clearPending()
      Clears the pending flag on stream.
    • close

      public boolean close(@Nullable Cancellable cancellable) throws GErrorException

      Closes the stream, releasing resources related to it.

      Once the stream is closed, all other operations will return IOErrorEnum.CLOSED. Closing a stream multiple times will not return an error.

      Closing a stream will automatically flush any outstanding buffers in the stream.

      Streams will be automatically closed when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible.

      Some streams might keep the backing store of the stream (e.g. a file descriptor) open after the stream is closed. See the documentation for the individual stream for details.

      On failure the first error that happened will be reported, but the close operation will finish as much as possible. A stream that failed to close will still return IOErrorEnum.CLOSED for all operations. Still, it is important to check and report the error to the user, otherwise there might be a loss of data as all data might not be written.

      If cancellable is not null, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IOErrorEnum.CANCELLED will be returned. Cancelling a close will still leave the stream closed, but there some streams can use a faster close that doesn't block to e.g. check errors. On cancellation (as with any error) there is no guarantee that all written data will reach the target.

      Specified by:
      close in interface AutoCloseable
      Parameters:
      cancellable - optional cancellable object
      Returns:
      true on success, false on failure
      Throws:
      GErrorException - see GError
    • closeAsync

      public void closeAsync(int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Requests an asynchronous close of the stream, releasing resources related to it. When the operation is finished callback will be called. You can then call g_output_stream_close_finish() to get the result of the operation.

      For behaviour details see g_output_stream_close().

      The asynchronous methods have a default fallback that uses threads to implement asynchronicity, so they are optional for inheriting classes. However, if you override one you must override all.

      Parameters:
      ioPriority - the io priority of the request.
      cancellable - optional cancellable object
      callback - a GAsyncReadyCallback to call when the request is satisfied
    • closeFinish

      public boolean closeFinish(AsyncResult result) throws GErrorException
      Closes an output stream.
      Parameters:
      result - a GAsyncResult.
      Returns:
      true if stream was successfully closed, false otherwise.
      Throws:
      GErrorException - see GError
    • flush

      public boolean flush(@Nullable Cancellable cancellable) throws GErrorException

      Forces a write of all user-space buffered data for the given stream. Will block during the operation. Closing the stream will implicitly cause a flush.

      This function is optional for inherited classes.

      If cancellable is not null, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IOErrorEnum.CANCELLED will be returned.

      Parameters:
      cancellable - optional cancellable object
      Returns:
      true on success, false on error
      Throws:
      GErrorException - see GError
    • flushAsync

      public void flushAsync(int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Forces an asynchronous write of all user-space buffered data for the given stream. For behaviour details see g_output_stream_flush().

      When the operation is finished callback will be called. You can then call g_output_stream_flush_finish() to get the result of the operation.

      Parameters:
      ioPriority - the io priority of the request.
      cancellable - optional GCancellable object, null to ignore.
      callback - a GAsyncReadyCallback to call when the request is satisfied
    • flushFinish

      public boolean flushFinish(AsyncResult result) throws GErrorException
      Finishes flushing an output stream.
      Parameters:
      result - a GAsyncResult.
      Returns:
      true if flush operation succeeded, false otherwise.
      Throws:
      GErrorException - see GError
    • hasPending

      public boolean hasPending()
      Checks if an output stream has pending actions.
      Returns:
      true if this OutputStream has pending actions.
    • isClosed

      public boolean isClosed()
      Checks if an output stream has already been closed.
      Returns:
      true if this OutputStream is closed. false otherwise.
    • isClosing

      public boolean isClosing()
      Checks if an output stream is being closed. This can be used inside e.g. a flush implementation to see if the flush (or other i/o operation) is called from within the closing operation.
      Returns:
      true if this OutputStream is being closed. false otherwise.
      Since:
      2.24
    • printf

      public boolean printf(@Nullable Out<Long> bytesWritten, @Nullable Cancellable cancellable, GError[] error, String format, Object... varargs)

      This is a utility function around g_output_stream_write_all(). It uses g_strdup_vprintf() to turn format and ... into a string that is then written to stream.

      See the documentation of g_output_stream_write_all() about the behavior of the actual write operation.

      Note that partial writes cannot be properly checked with this function due to the variable length of the written string, if you need precise control over partial write failures, you need to create you own printf()-like wrapper around g_output_stream_write() or g_output_stream_write_all().

      Parameters:
      bytesWritten - location to store the number of bytes that was written to the stream
      cancellable - optional GCancellable object, null to ignore.
      error - location to store the error occurring, or null to ignore
      format - the format string. See the printf() documentation
      varargs - the parameters to insert into the format string
      Returns:
      true on success, false if there was an error
      Since:
      2.40
    • setPending

      public boolean setPending() throws GErrorException
      Sets this OutputStream to have actions pending. If the pending flag is already set or this OutputStream is closed, it will return false and set error.
      Returns:
      true if pending was previously unset and is now set.
      Throws:
      GErrorException - see GError
    • splice

      public long splice(InputStream source, Set<OutputStreamSpliceFlags> flags, @Nullable Cancellable cancellable) throws GErrorException
      Splices an input stream into an output stream.
      Parameters:
      source - a GInputStream.
      flags - a set of GOutputStreamSpliceFlags.
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      a gssize containing the size of the data spliced, or -1 if an error occurred. Note that if the number of bytes spliced is greater than G_MAXSSIZE, then that will be returned, and there is no way to determine the actual number of bytes spliced.
      Throws:
      GErrorException - see GError
    • splice

      public long splice(InputStream source, OutputStreamSpliceFlags flags, @Nullable Cancellable cancellable) throws GErrorException
      Splices an input stream into an output stream.
      Parameters:
      source - a GInputStream.
      flags - a set of GOutputStreamSpliceFlags.
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      a gssize containing the size of the data spliced, or -1 if an error occurred. Note that if the number of bytes spliced is greater than G_MAXSSIZE, then that will be returned, and there is no way to determine the actual number of bytes spliced.
      Throws:
      GErrorException - see GError
    • spliceAsync

      public void spliceAsync(InputStream source, Set<OutputStreamSpliceFlags> flags, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Splices a stream asynchronously. When the operation is finished callback will be called. You can then call g_output_stream_splice_finish() to get the result of the operation.

      For the synchronous, blocking version of this function, see g_output_stream_splice().

      Parameters:
      source - a GInputStream.
      flags - a set of GOutputStreamSpliceFlags.
      ioPriority - the io priority of the request.
      cancellable - optional GCancellable object, null to ignore.
      callback - a GAsyncReadyCallback to call when the request is satisfied
    • spliceAsync

      public void spliceAsync(InputStream source, OutputStreamSpliceFlags flags, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Splices a stream asynchronously. When the operation is finished callback will be called. You can then call g_output_stream_splice_finish() to get the result of the operation.

      For the synchronous, blocking version of this function, see g_output_stream_splice().

      Parameters:
      source - a GInputStream.
      flags - a set of GOutputStreamSpliceFlags.
      ioPriority - the io priority of the request.
      cancellable - optional GCancellable object, null to ignore.
      callback - a GAsyncReadyCallback to call when the request is satisfied
    • spliceFinish

      public long spliceFinish(AsyncResult result) throws GErrorException
      Finishes an asynchronous stream splice operation.
      Parameters:
      result - a GAsyncResult.
      Returns:
      a gssize of the number of bytes spliced. Note that if the number of bytes spliced is greater than G_MAXSSIZE, then that will be returned, and there is no way to determine the actual number of bytes spliced.
      Throws:
      GErrorException - see GError
    • write

      public long write(@Nullable byte @Nullable [] buffer, @Nullable Cancellable cancellable) throws GErrorException

      Tries to write count bytes from buffer into the stream. Will block during the operation.

      If count is 0, returns 0 and does nothing. A value of count larger than G_MAXSSIZE will cause a IOErrorEnum.INVALID_ARGUMENT error.

      On success, the number of bytes written to the stream is returned. It is not an error if this is not the same as the requested size, as it can happen e.g. on a partial I/O error, or if there is not enough storage in the stream. All writes block until at least one byte is written or an error occurs; 0 is never returned (unless count is 0).

      If cancellable is not null, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IOErrorEnum.CANCELLED will be returned. If an operation was partially finished when the operation was cancelled the partial result will be returned, without an error.

      On error -1 is returned and error is set accordingly.

      Parameters:
      buffer - the buffer containing the data to write.
      cancellable - optional cancellable object
      Returns:
      Number of bytes written, or -1 on error
      Throws:
      GErrorException - see GError
    • writeAll

      public boolean writeAll(@Nullable byte @Nullable [] buffer, @Nullable Out<Long> bytesWritten, @Nullable Cancellable cancellable) throws GErrorException

      Tries to write count bytes from buffer into the stream. Will block during the operation.

      This function is similar to g_output_stream_write(), except it tries to write as many bytes as requested, only stopping on an error.

      On a successful write of count bytes, true is returned, and bytesWritten is set to count.

      If there is an error during the operation false is returned and error is set to indicate the error status.

      As a special exception to the normal conventions for functions that use GError, if this function returns false (and sets error) then bytesWritten will be set to the number of bytes that were successfully written before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_output_stream_write().

      Parameters:
      buffer - the buffer containing the data to write.
      bytesWritten - location to store the number of bytes that was written to the stream
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      true on success, false if there was an error
      Throws:
      GErrorException - see GError
    • writeAllAsync

      public void writeAllAsync(@Nullable byte @Nullable [] buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Request an asynchronous write of count bytes from buffer into the stream. When the operation is finished callback will be called. You can then call g_output_stream_write_all_finish() to get the result of the operation.

      This is the asynchronous version of g_output_stream_write_all().

      Call g_output_stream_write_all_finish() to collect the result.

      Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is G_PRIORITY_DEFAULT.

      Note that no copy of buffer will be made, so it must stay valid until callback is called.

      Parameters:
      buffer - the buffer containing the data to write
      ioPriority - the io priority of the request
      cancellable - optional GCancellable object, null to ignore
      callback - a GAsyncReadyCallback to call when the request is satisfied
      Since:
      2.44
    • writeAllFinish

      public boolean writeAllFinish(AsyncResult result, @Nullable Out<Long> bytesWritten) throws GErrorException

      Finishes an asynchronous stream write operation started with g_output_stream_write_all_async().

      As a special exception to the normal conventions for functions that use GError, if this function returns false (and sets error) then bytesWritten will be set to the number of bytes that were successfully written before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_output_stream_write_async().

      Parameters:
      result - a GAsyncResult
      bytesWritten - location to store the number of bytes that was written to the stream
      Returns:
      true on success, false if there was an error
      Throws:
      GErrorException - see GError
      Since:
      2.44
    • writeAsync

      public void writeAsync(@Nullable byte @Nullable [] buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Request an asynchronous write of count bytes from buffer into the stream. When the operation is finished callback will be called. You can then call g_output_stream_write_finish() to get the result of the operation.

      During an async request no other sync and async calls are allowed, and will result in IOErrorEnum.PENDING errors.

      A value of count larger than G_MAXSSIZE will cause a IOErrorEnum.INVALID_ARGUMENT error.

      On success, the number of bytes written will be passed to the callback. It is not an error if this is not the same as the requested size, as it can happen e.g. on a partial I/O error, but generally we try to write as many bytes as requested.

      You are guaranteed that this method will never fail with IOErrorEnum.WOULD_BLOCK - if this OutputStream can't accept more data, the method will just wait until this changes.

      Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is G_PRIORITY_DEFAULT.

      The asynchronous methods have a default fallback that uses threads to implement asynchronicity, so they are optional for inheriting classes. However, if you override one you must override all.

      For the synchronous, blocking version of this function, see g_output_stream_write().

      Note that no copy of buffer will be made, so it must stay valid until callback is called. See g_output_stream_write_bytes_async() for a GBytes version that will automatically hold a reference to the contents (without copying) for the duration of the call.

      Parameters:
      buffer - the buffer containing the data to write.
      ioPriority - the io priority of the request.
      cancellable - optional GCancellable object, null to ignore.
      callback - a GAsyncReadyCallback to call when the request is satisfied
    • writeBytes

      public long writeBytes(byte[] bytes, @Nullable Cancellable cancellable) throws GErrorException

      A wrapper function for g_output_stream_write() which takes a GBytes as input. This can be more convenient for use by language bindings or in other cases where the refcounted nature of GBytes is helpful over a bare pointer interface.

      However, note that this function may still perform partial writes, just like g_output_stream_write(). If that occurs, to continue writing, you will need to create a new GBytes containing just the remaining bytes, using g_bytes_new_from_bytes(). Passing the same GBytes instance multiple times potentially can result in duplicated data in the output stream.

      Parameters:
      bytes - the GBytes to write
      cancellable - optional cancellable object
      Returns:
      Number of bytes written, or -1 on error
      Throws:
      GErrorException - see GError
    • writeBytesAsync

      public void writeBytesAsync(byte[] bytes, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      This function is similar to g_output_stream_write_async(), but takes a GBytes as input. Due to the refcounted nature of GBytes, this allows the stream to avoid taking a copy of the data.

      However, note that this function may still perform partial writes, just like g_output_stream_write_async(). If that occurs, to continue writing, you will need to create a new GBytes containing just the remaining bytes, using g_bytes_new_from_bytes(). Passing the same GBytes instance multiple times potentially can result in duplicated data in the output stream.

      For the synchronous, blocking version of this function, see g_output_stream_write_bytes().

      Parameters:
      bytes - The bytes to write
      ioPriority - the io priority of the request.
      cancellable - optional GCancellable object, null to ignore.
      callback - a GAsyncReadyCallback to call when the request is satisfied
    • writeBytesFinish

      public long writeBytesFinish(AsyncResult result) throws GErrorException
      Finishes a stream write-from-GBytes operation.
      Parameters:
      result - a GAsyncResult.
      Returns:
      a gssize containing the number of bytes written to the stream.
      Throws:
      GErrorException - see GError
    • writeFinish

      public long writeFinish(AsyncResult result) throws GErrorException
      Finishes a stream write operation.
      Parameters:
      result - a GAsyncResult.
      Returns:
      a gssize containing the number of bytes written to the stream.
      Throws:
      GErrorException - see GError
    • writev

      public boolean writev(@Nullable OutputVector @Nullable [] vectors, @Nullable Out<Long> bytesWritten, @Nullable Cancellable cancellable) throws GErrorException

      Tries to write the bytes contained in the nVectors vectors into the stream. Will block during the operation.

      If nVectors is 0 or the sum of all bytes in vectors is 0, returns 0 and does nothing.

      On success, the number of bytes written to the stream is returned. It is not an error if this is not the same as the requested size, as it can happen e.g. on a partial I/O error, or if there is not enough storage in the stream. All writes block until at least one byte is written or an error occurs; 0 is never returned (unless nVectors is 0 or the sum of all bytes in vectors is 0).

      If cancellable is not null, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IOErrorEnum.CANCELLED will be returned. If an operation was partially finished when the operation was cancelled the partial result will be returned, without an error.

      Some implementations of g_output_stream_writev() may have limitations on the aggregate buffer size, and will return IOErrorEnum.INVALID_ARGUMENT if these are exceeded. For example, when writing to a local file on UNIX platforms, the aggregate buffer size must not exceed G_MAXSSIZE bytes.

      Parameters:
      vectors - the buffer containing the GOutputVectors to write.
      bytesWritten - location to store the number of bytes that were written to the stream
      cancellable - optional cancellable object
      Returns:
      true on success, false if there was an error
      Throws:
      GErrorException - see GError
      Since:
      2.60
    • writevAll

      public boolean writevAll(@Nullable OutputVector @Nullable [] vectors, @Nullable Out<Long> bytesWritten, @Nullable Cancellable cancellable) throws GErrorException

      Tries to write the bytes contained in the nVectors vectors into the stream. Will block during the operation.

      This function is similar to g_output_stream_writev(), except it tries to write as many bytes as requested, only stopping on an error.

      On a successful write of all nVectors vectors, true is returned, and bytesWritten is set to the sum of all the sizes of vectors.

      If there is an error during the operation false is returned and error is set to indicate the error status.

      As a special exception to the normal conventions for functions that use GError, if this function returns false (and sets error) then bytesWritten will be set to the number of bytes that were successfully written before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_output_stream_write().

      The content of the individual elements of vectors might be changed by this function.

      Parameters:
      vectors - the buffer containing the GOutputVectors to write.
      bytesWritten - location to store the number of bytes that were written to the stream
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      true on success, false if there was an error
      Throws:
      GErrorException - see GError
      Since:
      2.60
    • writevAllAsync

      public void writevAllAsync(@Nullable OutputVector @Nullable [] vectors, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Request an asynchronous write of the bytes contained in the nVectors vectors into the stream. When the operation is finished callback will be called. You can then call g_output_stream_writev_all_finish() to get the result of the operation.

      This is the asynchronous version of g_output_stream_writev_all().

      Call g_output_stream_writev_all_finish() to collect the result.

      Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is G_PRIORITY_DEFAULT.

      Note that no copy of vectors will be made, so it must stay valid until callback is called. The content of the individual elements of vectors might be changed by this function.

      Parameters:
      vectors - the buffer containing the GOutputVectors to write.
      ioPriority - the I/O priority of the request
      cancellable - optional GCancellable object, null to ignore
      callback - a GAsyncReadyCallback to call when the request is satisfied
      Since:
      2.60
    • writevAllFinish

      public boolean writevAllFinish(AsyncResult result, @Nullable Out<Long> bytesWritten) throws GErrorException

      Finishes an asynchronous stream write operation started with g_output_stream_writev_all_async().

      As a special exception to the normal conventions for functions that use GError, if this function returns false (and sets error) then bytesWritten will be set to the number of bytes that were successfully written before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_output_stream_writev_async().

      Parameters:
      result - a GAsyncResult
      bytesWritten - location to store the number of bytes that were written to the stream
      Returns:
      true on success, false if there was an error
      Throws:
      GErrorException - see GError
      Since:
      2.60
    • writevAsync

      public void writevAsync(@Nullable OutputVector @Nullable [] vectors, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Request an asynchronous write of the bytes contained in nVectors vectors into the stream. When the operation is finished callback will be called. You can then call g_output_stream_writev_finish() to get the result of the operation.

      During an async request no other sync and async calls are allowed, and will result in IOErrorEnum.PENDING errors.

      On success, the number of bytes written will be passed to the callback. It is not an error if this is not the same as the requested size, as it can happen e.g. on a partial I/O error, but generally we try to write as many bytes as requested.

      You are guaranteed that this method will never fail with IOErrorEnum.WOULD_BLOCK — if this OutputStream can't accept more data, the method will just wait until this changes.

      Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is G_PRIORITY_DEFAULT.

      The asynchronous methods have a default fallback that uses threads to implement asynchronicity, so they are optional for inheriting classes. However, if you override one you must override all.

      For the synchronous, blocking version of this function, see g_output_stream_writev().

      Note that no copy of vectors will be made, so it must stay valid until callback is called.

      Parameters:
      vectors - the buffer containing the GOutputVectors to write.
      ioPriority - the I/O priority of the request.
      cancellable - optional GCancellable object, null to ignore.
      callback - a GAsyncReadyCallback to call when the request is satisfied
      Since:
      2.60
    • writevFinish

      public boolean writevFinish(AsyncResult result, @Nullable Out<Long> bytesWritten) throws GErrorException
      Finishes a stream writev operation.
      Parameters:
      result - a GAsyncResult.
      bytesWritten - location to store the number of bytes that were written to the stream
      Returns:
      true on success, false if there was an error
      Throws:
      GErrorException - see GError
      Since:
      2.60
    • closeFn

      protected boolean closeFn(@Nullable Cancellable cancellable) throws GErrorException
      Throws:
      GErrorException