Class InputStream

All Implemented Interfaces:
AutoCloseable, Proxy, AutoCloseable
Direct Known Subclasses:
FileInputStream, FilterInputStream, InputStream.InputStream$Impl, MemoryInputStream, PollableInputStream.PollableInputStream$Impl

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

GInputStream is a base class for implementing streaming input.

It has functions to read from a stream (read(Out, Cancellable)), to close a stream (close(Cancellable)) and to skip some content (skip(long, Cancellable)).

To copy the content of an input stream to an output stream without manually handling the reads and writes, use OutputStream.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.

  • Constructor Details

    • InputStream

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

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

    • getType

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

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

      protected InputStream 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.

      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.

      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 some streams can use a faster close that doesn't block to e.g. check errors.

      Specified by:
      close in interface AutoCloseable
      Parameters:
      cancellable - optional GCancellable object, null to ignore.
      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 closes of the stream, releasing resources related to it. When the operation is finished callback will be called. You can then call g_input_stream_close_finish() to get the result of the operation.

      For behaviour details see g_input_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 I/O 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
      Finishes closing a stream asynchronously, started from g_input_stream_close_async().
      Parameters:
      result - a GAsyncResult.
      Returns:
      true if the stream was closed successfully.
      Throws:
      GErrorException - see GError
    • hasPending

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

      public boolean isClosed()
      Checks if an input stream is closed.
      Returns:
      true if the stream is closed.
    • read

      public long read(@Nullable Out<byte[]> buffer, @Nullable Cancellable cancellable) throws GErrorException

      Tries to read count bytes from the stream into the buffer starting at buffer. Will block during this read.

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

      On success, the number of bytes read into the buffer is returned. It is not an error if this is not the same as the requested size, as it can happen e.g. near the end of a file. Zero is returned on end of file (or if count is zero), but never otherwise.

      The returned buffer is not a nul-terminated string, it can contain nul bytes at any position, and this function doesn't nul-terminate the buffer.

      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 - a buffer to read data into (which should be at least count bytes long).
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      Number of bytes read, or -1 on error, or 0 on end of file.
      Throws:
      GErrorException - see GError
    • readAll

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

      Tries to read count bytes from the stream into the buffer starting at buffer. Will block during this read.

      This function is similar to g_input_stream_read(), except it tries to read as many bytes as requested, only stopping on an error or end of stream.

      On a successful read of count bytes, or if we reached the end of the stream, true is returned, and bytesRead is set to the number of bytes read into buffer.

      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 bytesRead will be set to the number of bytes that were successfully read 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_input_stream_read().

      Parameters:
      buffer - a buffer to read data into (which should be at least count bytes long).
      bytesRead - location to store the number of bytes that was read from the stream
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      true on success, false if there was an error
      Throws:
      GErrorException - see GError
    • readAllAsync

      public void readAllAsync(@Nullable Out<byte[]> buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Request an asynchronous read of count bytes from the stream into the buffer starting at buffer.

      This is the asynchronous equivalent of readAll(Out, Out, Cancellable).

      Call readAllFinish(AsyncResult, Out) 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.

      Parameters:
      buffer - a buffer to read data into (which should be at least count bytes long)
      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.44
    • readAllFinish

      public boolean readAllFinish(AsyncResult result, Out<Long> bytesRead) throws GErrorException

      Finishes an asynchronous stream read operation started with readAllAsync(Out, int, Cancellable, AsyncReadyCallback).

      As a special exception to the normal conventions for functions that use GError, if this function returns false (and sets error) then bytesRead will be set to the number of bytes that were successfully read 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_input_stream_read_async().

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

      public void readAsync(@Nullable Out<byte[]> buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

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

      During an async request no other sync and async calls are allowed on stream, 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 read into the buffer 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. near the end of a file, but generally we try to read as many bytes as requested. Zero is returned on end of file (or if count is zero), but never otherwise.

      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.

      Parameters:
      buffer - a buffer to read data into (which should be at least count bytes long).
      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
    • readBytes

      public byte[] readBytes(long count, @Nullable Cancellable cancellable) throws GErrorException

      Like g_input_stream_read(), this tries to read count bytes from the stream in a blocking fashion. However, rather than reading into a user-supplied buffer, this will create a new GBytes containing the data that was read. This may be easier to use from language bindings.

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

      On success, a new GBytes is returned. It is not an error if the size of this object is not the same as the requested size, as it can happen e.g. near the end of a file. A zero-length GBytes is returned on end of file (or if count is zero), but never otherwise.

      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 null is returned and error is set accordingly.

      Parameters:
      count - maximum number of bytes that will be read from the stream. Common values include 4096 and 8192.
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      a new GBytes, or null on error
      Throws:
      GErrorException - see GError
      Since:
      2.34
    • readBytesAsync

      public void readBytesAsync(long count, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Request an asynchronous read of count bytes from the stream into a new GBytes. When the operation is finished callback will be called. You can then call g_input_stream_read_bytes_finish() to get the result of the operation.

      During an async request no other sync and async calls are allowed on stream, 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 new GBytes will be passed to the callback. It is not an error if this is smaller than the requested size, as it can happen e.g. near the end of a file, but generally we try to read as many bytes as requested. Zero is returned on end of file (or if count is zero), but never otherwise.

      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.

      Parameters:
      count - the number of bytes that will be read from the stream
      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.34
    • readBytesFinish

      public byte[] readBytesFinish(AsyncResult result) throws GErrorException
      Finishes an asynchronous stream read-into-GBytes operation.
      Parameters:
      result - a GAsyncResult.
      Returns:
      the newly-allocated GBytes, or null on error
      Throws:
      GErrorException - see GError
      Since:
      2.34
    • readFinish

      public long readFinish(AsyncResult result) throws GErrorException
      Finishes an asynchronous stream read operation.
      Parameters:
      result - a GAsyncResult.
      Returns:
      number of bytes read in, or -1 on error, or 0 on end of file.
      Throws:
      GErrorException - see GError
    • setPending

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

      public long skip(long count, @Nullable Cancellable cancellable) throws GErrorException

      Tries to skip count bytes from the stream. Will block during the operation.

      This is identical to g_input_stream_read(), from a behaviour standpoint, but the bytes that are skipped are not returned to the user. Some streams have an implementation that is more efficient than reading the data.

      This function is optional for inherited classes, as the default implementation emulates it using read.

      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.

      Parameters:
      count - the number of bytes that will be skipped from the stream
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      Number of bytes skipped, or -1 on error
      Throws:
      GErrorException - see GError
    • skipAsync

      public void skipAsync(long count, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Request an asynchronous skip of count bytes from the stream. When the operation is finished callback will be called. You can then call g_input_stream_skip_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 skipped 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. near the end of a file, but generally we try to skip as many bytes as requested. Zero is returned on end of file (or if count is zero), but never otherwise.

      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.

      Parameters:
      count - the number of bytes that will be skipped from the stream
      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
    • skipFinish

      public long skipFinish(AsyncResult result) throws GErrorException
      Finishes a stream skip operation.
      Parameters:
      result - a GAsyncResult.
      Returns:
      the size of the bytes skipped, or -1 on error.
      Throws:
      GErrorException - see GError
    • closeFn

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

      protected long readFn(@Nullable MemorySegment buffer, long count, @Nullable Cancellable cancellable) throws GErrorException
      Throws:
      GErrorException