Class DataInputStream

All Implemented Interfaces:
AutoCloseable, Seekable, Proxy, AutoCloseable

@Generated("org.javagi.JavaGI") public class DataInputStream extends BufferedInputStream implements Seekable
Data input stream implements InputStream and includes functions for reading structured data directly from a binary input stream.
  • Constructor Details

    • DataInputStream

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

      public DataInputStream(InputStream baseStream)
      Creates a new data input stream for the baseStream.
      Parameters:
      baseStream - a GInputStream.
    • DataInputStream

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

    • getType

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

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

      protected DataInputStream 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 BufferedInputStream
      Returns:
      the instance as if it were its parent type
    • getByteOrder

      public DataStreamByteOrder getByteOrder()
      Gets the byte order for the data input stream.
      Returns:
      the stream's current GDataStreamByteOrder.
    • getNewlineType

      public DataStreamNewlineType getNewlineType()
      Gets the current newline type for the stream.
      Returns:
      GDataStreamNewlineType for the given stream.
    • readByte

      public byte readByte(@Nullable Cancellable cancellable) throws GErrorException
      Reads an unsigned 8-bit/1-byte value from stream.
      Parameters:
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      an unsigned 8-bit/1-byte value read from the this DataInputStream or 0 if an error occurred.
      Throws:
      GErrorException - see GError
    • readInt16

      public short readInt16(@Nullable Cancellable cancellable) throws GErrorException

      Reads a 16-bit/2-byte value from stream.

      In order to get the correct byte order for this read operation, see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().

      Parameters:
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      a signed 16-bit/2-byte value read from this DataInputStream or 0 if an error occurred.
      Throws:
      GErrorException - see GError
    • readInt32

      public int readInt32(@Nullable Cancellable cancellable) throws GErrorException

      Reads a signed 32-bit/4-byte value from stream.

      In order to get the correct byte order for this read operation, see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().

      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 GCancellable object, null to ignore.
      Returns:
      a signed 32-bit/4-byte value read from the this DataInputStream or 0 if an error occurred.
      Throws:
      GErrorException - see GError
    • readInt64

      public long readInt64(@Nullable Cancellable cancellable) throws GErrorException

      Reads a 64-bit/8-byte value from stream.

      In order to get the correct byte order for this read operation, see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().

      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 GCancellable object, null to ignore.
      Returns:
      a signed 64-bit/8-byte value read from this DataInputStream or 0 if an error occurred.
      Throws:
      GErrorException - see GError
    • readLine

      public @Nullable byte @Nullable [] readLine(@Nullable Out<Long> length, @Nullable Cancellable cancellable) throws GErrorException

      Reads a line from the data input stream. Note that no encoding checks or conversion is performed; the input is not guaranteed to be UTF-8, and may in fact have embedded NUL characters.

      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:
      length - a gsize to get the length of the data read in.
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      a NUL terminated byte array with the line that was read in (without the newlines). Set length to a gsize to get the length of the read line. On an error, it will return null and error will be set. If there's no content to read, it will still return null, but error won't be set.
      Throws:
      GErrorException - see GError
    • readLineAsync

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

      The asynchronous version of g_data_input_stream_read_line(). It is an error to have two outstanding calls to this function.

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

      Parameters:
      ioPriority - the I/O priority of the request
      cancellable - optional GCancellable object, null to ignore.
      callback - callback to call when the request is satisfied.
      Since:
      2.20
    • readLineFinish

      public @Nullable byte @Nullable [] readLineFinish(AsyncResult result, @Nullable Out<Long> length) throws GErrorException
      Finish an asynchronous call started by g_data_input_stream_read_line_async(). Note the warning about string encoding in g_data_input_stream_read_line() applies here as well.
      Parameters:
      result - the GAsyncResult that was provided to the callback.
      length - a gsize to get the length of the data read in.
      Returns:
      a NUL-terminated byte array with the line that was read in (without the newlines). Set length to a gsize to get the length of the read line. On an error, it will return null and error will be set. If there's no content to read, it will still return null, but error won't be set.
      Throws:
      GErrorException - see GError
      Since:
      2.20
    • readLineFinishUtf8

      public @Nullable String readLineFinishUtf8(AsyncResult result, @Nullable Out<Long> length) throws GErrorException
      Finish an asynchronous call started by g_data_input_stream_read_line_async().
      Parameters:
      result - the GAsyncResult that was provided to the callback.
      length - a gsize to get the length of the data read in.
      Returns:
      a string with the line that was read in (without the newlines). Set length to a gsize to get the length of the read line. On an error, it will return null and error will be set. For UTF-8 conversion errors, the set error domain is G_CONVERT_ERROR. If there's no content to read, it will still return null, but error won't be set.
      Throws:
      GErrorException - see GError
      Since:
      2.30
    • readLineUtf8

      public @Nullable String readLineUtf8(@Nullable Out<Long> length, @Nullable Cancellable cancellable) throws GErrorException

      Reads a UTF-8 encoded line from the data input stream.

      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:
      length - a gsize to get the length of the data read in.
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      a NUL terminated UTF-8 string with the line that was read in (without the newlines). Set length to a gsize to get the length of the read line. On an error, it will return null and error will be set. For UTF-8 conversion errors, the set error domain is G_CONVERT_ERROR. If there's no content to read, it will still return null, but error won't be set.
      Throws:
      GErrorException - see GError
      Since:
      2.30
    • readUint16

      public short readUint16(@Nullable Cancellable cancellable) throws GErrorException

      Reads an unsigned 16-bit/2-byte value from stream.

      In order to get the correct byte order for this read operation, see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().

      Parameters:
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      an unsigned 16-bit/2-byte value read from the this DataInputStream or 0 if an error occurred.
      Throws:
      GErrorException - see GError
    • readUint32

      public int readUint32(@Nullable Cancellable cancellable) throws GErrorException

      Reads an unsigned 32-bit/4-byte value from stream.

      In order to get the correct byte order for this read operation, see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().

      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 GCancellable object, null to ignore.
      Returns:
      an unsigned 32-bit/4-byte value read from the this DataInputStream or 0 if an error occurred.
      Throws:
      GErrorException - see GError
    • readUint64

      public long readUint64(@Nullable Cancellable cancellable) throws GErrorException

      Reads an unsigned 64-bit/8-byte value from stream.

      In order to get the correct byte order for this read operation, see g_data_input_stream_get_byte_order().

      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 GCancellable object, null to ignore.
      Returns:
      an unsigned 64-bit/8-byte read from this DataInputStream or 0 if an error occurred.
      Throws:
      GErrorException - see GError
    • readUntil

      @Deprecated public String readUntil(String stopChars, @Nullable Out<Long> length, @Nullable Cancellable cancellable) throws GErrorException
      Deprecated.
      Use g_data_input_stream_read_upto() instead, which has more consistent behaviour regarding the stop character.

      Reads a string from the data input stream, up to the first occurrence of any of the stop characters.

      Note that, in contrast to g_data_input_stream_read_until_async(), this function consumes the stop character that it finds.

      Don't use this function in new code. Its functionality is inconsistent with g_data_input_stream_read_until_async(). Both functions will be marked as deprecated in a future release. Use g_data_input_stream_read_upto() instead, but note that that function does not consume the stop character.

      Parameters:
      stopChars - characters to terminate the read.
      length - a gsize to get the length of the data read in.
      cancellable - optional GCancellable object, null to ignore.
      Returns:
      a string with the data that was read before encountering any of the stop characters. Set length to a gsize to get the length of the string. This function will return null on an error.
      Throws:
      GErrorException - see GError
    • readUntilAsync

      @Deprecated public void readUntilAsync(String stopChars, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)
      Deprecated.
      Use g_data_input_stream_read_upto_async() instead, which has more consistent behaviour regarding the stop character.

      The asynchronous version of g_data_input_stream_read_until(). It is an error to have two outstanding calls to this function.

      Note that, in contrast to g_data_input_stream_read_until(), this function does not consume the stop character that it finds. You must read it for yourself.

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

      Don't use this function in new code. Its functionality is inconsistent with g_data_input_stream_read_until(). Both functions will be marked as deprecated in a future release. Use g_data_input_stream_read_upto_async() instead.

      Parameters:
      stopChars - characters to terminate the read.
      ioPriority - the I/O priority of the request
      cancellable - optional GCancellable object, null to ignore.
      callback - callback to call when the request is satisfied.
      Since:
      2.20
    • readUntilFinish

      @Deprecated public String readUntilFinish(AsyncResult result, @Nullable Out<Long> length) throws GErrorException
      Deprecated.
      Use g_data_input_stream_read_upto_finish() instead, which has more consistent behaviour regarding the stop character.
      Finish an asynchronous call started by g_data_input_stream_read_until_async().
      Parameters:
      result - the GAsyncResult that was provided to the callback.
      length - a gsize to get the length of the data read in.
      Returns:
      a string with the data that was read before encountering any of the stop characters. Set length to a gsize to get the length of the string. This function will return null on an error.
      Throws:
      GErrorException - see GError
      Since:
      2.20
    • readUpto

      public String readUpto(String stopChars, long stopCharsLen, @Nullable Out<Long> length, @Nullable Cancellable cancellable) throws GErrorException

      Reads a string from the data input stream, up to the first occurrence of any of the stop characters.

      In contrast to g_data_input_stream_read_until(), this function does not consume the stop character. You have to use g_data_input_stream_read_byte() to get it before calling g_data_input_stream_read_upto() again.

      Note that stopChars may contain '\0' if stopCharsLen is specified.

      The returned string will always be nul-terminated on success.

      Parameters:
      stopChars - characters to terminate the read
      stopCharsLen - length of stopChars. May be -1 if stopChars is nul-terminated
      length - a gsize to get the length of the data read in
      cancellable - optional GCancellable object, null to ignore
      Returns:
      a string with the data that was read before encountering any of the stop characters. Set length to a gsize to get the length of the string. This function will return null on an error
      Throws:
      GErrorException - see GError
      Since:
      2.26
    • readUptoAsync

      public void readUptoAsync(String stopChars, long stopCharsLen, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      The asynchronous version of g_data_input_stream_read_upto(). It is an error to have two outstanding calls to this function.

      In contrast to g_data_input_stream_read_until(), this function does not consume the stop character. You have to use g_data_input_stream_read_byte() to get it before calling g_data_input_stream_read_upto() again.

      Note that stopChars may contain '\0' if stopCharsLen is specified.

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

      Parameters:
      stopChars - characters to terminate the read
      stopCharsLen - length of stopChars. May be -1 if stopChars is nul-terminated
      ioPriority - the I/O priority of the request
      cancellable - optional GCancellable object, null to ignore
      callback - callback to call when the request is satisfied
      Since:
      2.26
    • readUptoFinish

      public String readUptoFinish(AsyncResult result, @Nullable Out<Long> length) throws GErrorException

      Finish an asynchronous call started by g_data_input_stream_read_upto_async().

      Note that this function does not consume the stop character. You have to use g_data_input_stream_read_byte() to get it before calling g_data_input_stream_read_upto_async() again.

      The returned string will always be nul-terminated on success.

      Parameters:
      result - the GAsyncResult that was provided to the callback
      length - a gsize to get the length of the data read in
      Returns:
      a string with the data that was read before encountering any of the stop characters. Set length to a gsize to get the length of the string. This function will return null on an error.
      Throws:
      GErrorException - see GError
      Since:
      2.24
    • setByteOrder

      public void setByteOrder(DataStreamByteOrder order)
      This function sets the byte order for the given stream. All subsequent reads from the this DataInputStream will be read in the given order.
      Parameters:
      order - a GDataStreamByteOrder to set.
    • setNewlineType

      public void setNewlineType(DataStreamNewlineType type)

      Sets the newline type for the stream.

      Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read chunk ends in "CR" we must read an additional byte to know if this is "CR" or "CR LF", and this might block if there is no more data available.

      Parameters:
      type - the type of new line return as GDataStreamNewlineType.
    • builder

      public static DataInputStream.Builder<? extends DataInputStream.Builder> builder()
      A DataInputStream.Builder object constructs a DataInputStream with the specified properties. Use the various set...() methods to set properties, and finish construction with DataInputStream.Builder.build().
      Returns:
      the builder object