Interface Converter
- All Superinterfaces:
Proxy
- All Known Implementing Classes:
CharsetConverter,Converter.Converter$Impl,ZlibCompressor,ZlibDecompressor
GConverter is an interface for streaming conversions.
GConverter is implemented by objects that convert
binary data in various ways. The conversion can be
stateful and may fail at any place.
Some example conversions are: character set conversion, compression, decompression and regular expression replace.
- Since:
- 2.24
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classThe Converter$Impl type represents a native instance of the Converter interface.static classProvides an interface for converting data from one type to another type. -
Method Summary
Modifier and TypeMethodDescriptiondefault ConverterResultconvert(@org.jspecify.annotations.Nullable byte @Nullable [] inbuf, @org.jspecify.annotations.Nullable byte @Nullable [] outbuf, Set<ConverterFlags> flags, Out<Long> bytesRead, Out<Long> bytesWritten) This is the main operation used when converting data.default ConverterResultconvert(@org.jspecify.annotations.Nullable byte @Nullable [] inbuf, @org.jspecify.annotations.Nullable byte @Nullable [] outbuf, ConverterFlags flags, Out<Long> bytesRead, Out<Long> bytesWritten) This is the main operation used when converting data.default byte[]convertBytes(byte[] bytes) Applies this Converter to the data inbytes.static @Nullable TypegetType()Get the GType of the Converter classdefault voidreset()Resets all internal state in the converter, making it behave as if it was just created.
-
Method Details
-
getType
-
convert
default ConverterResult convert(@org.jspecify.annotations.Nullable byte @Nullable [] inbuf, @org.jspecify.annotations.Nullable byte @Nullable [] outbuf, Set<ConverterFlags> flags, Out<Long> bytesRead, Out<Long> bytesWritten) throws GErrorException This is the main operation used when converting data. It is to be called multiple times in a loop, and each time it will do some work, i.e. producing some output (inoutbuf)or consuming some input (frominbuf)or both. If its not possible to do any work an error is returned.Note that a single call may not consume all input (or any input at all). Also a call may produce output even if given no input, due to state stored in the converter producing output.
If any data was either produced or consumed, and then an error happens, then only the successful conversion is reported and the error is returned on the next call.
A full conversion loop involves calling this method repeatedly, each time giving it new input and space output space. When there is no more input data after the data in
inbuf,the flagConverterFlags.INPUT_AT_ENDmust be set. The loop will be (unless some error happens) returningConverterResult.CONVERTEDeach time until all data is consumed and all output is produced, thenConverterResult.FINISHEDis returned instead. Note, thatConverterResult.FINISHEDmay be returned even ifConverterFlags.INPUT_AT_ENDis not set, for instance in a decompression converter where the end of data is detectable from the data (and there might even be other data after the end of the compressed data).When some data has successfully been converted
bytesReadand is set to the number of bytes read frominbuf,andbytesWrittenis set to indicate how many bytes was written tooutbuf.If there are more data to output or consume (i.e. unless theConverterFlags.INPUT_AT_ENDis specified) thenConverterResult.CONVERTEDis returned, and if no more data is to be output thenConverterResult.FINISHEDis returned.On error
ConverterResult.ERRORis returned anderroris set accordingly. Some errors need special handling:IOErrorEnum.NO_SPACEis returned if there is not enough space to write the resulting converted data, the application should call the function again with a largeroutbufto continue.IOErrorEnum.PARTIAL_INPUTis returned if there is not enough input to fully determine what the conversion should produce, and theConverterFlags.INPUT_AT_ENDflag is not set. This happens for example with an incomplete multibyte sequence when converting text, or when a regexp matches up to the end of the input (and may match further input). It may also happen wheninbufSizeis zero and there is no more data to produce.When this happens the application should read more input and then call the function again. If further input shows that there is no more data call the function again with the same data but with the
ConverterFlags.INPUT_AT_ENDflag set. This may cause the conversion to finish as e.g. in the regexp match case (or, to fail again withIOErrorEnum.PARTIAL_INPUTin e.g. a charset conversion where the input is actually partial).After g_converter_convert() has returned
ConverterResult.FINISHEDthe converter object is in an invalid state where its not allowed to call g_converter_convert() anymore. At this time you can only free the object or call g_converter_reset() to reset it to the initial state.If the flag
ConverterFlags.FLUSHis set then conversion is modified to try to write out all internal state to the output. The application has to call the function multiple times with the flag set, and when the available input has been consumed and all internal state has been produced thenConverterResult.FLUSHED(orConverterResult.FINISHEDif really at the end) is returned instead ofConverterResult.CONVERTED. This is somewhat similar to what happens at the end of the input stream, but done in the middle of the data.This has different meanings for different conversions. For instance in a compression converter it would mean that we flush all the compression state into output such that if you uncompress the compressed data you get back all the input data. Doing this may make the final file larger due to padding though. Another example is a regexp conversion, where if you at the end of the flushed data have a match, but there is also a potential longer match. In the non-flushed case we would ask for more input, but when flushing we treat this as the end of input and do the match.
Flushing is not always possible (like if a charset converter flushes at a partial multibyte sequence). Converters are supposed to try to produce as much output as possible and then return an error (typically
IOErrorEnum.PARTIAL_INPUT).- Parameters:
inbuf- the buffer containing the data to convert.outbuf- a buffer to write converted data in.flags- aGConverterFlagscontrolling the conversion detailsbytesRead- will be set to the number of bytes read frominbufon successbytesWritten- will be set to the number of bytes written tooutbufon success- Returns:
- a
GConverterResult,ConverterResult.ERRORon error. - Throws:
GErrorException- seeGError- Since:
- 2.24
-
convert
default ConverterResult convert(@org.jspecify.annotations.Nullable byte @Nullable [] inbuf, @org.jspecify.annotations.Nullable byte @Nullable [] outbuf, ConverterFlags flags, Out<Long> bytesRead, Out<Long> bytesWritten) throws GErrorException This is the main operation used when converting data. It is to be called multiple times in a loop, and each time it will do some work, i.e. producing some output (inoutbuf)or consuming some input (frominbuf)or both. If its not possible to do any work an error is returned.Note that a single call may not consume all input (or any input at all). Also a call may produce output even if given no input, due to state stored in the converter producing output.
If any data was either produced or consumed, and then an error happens, then only the successful conversion is reported and the error is returned on the next call.
A full conversion loop involves calling this method repeatedly, each time giving it new input and space output space. When there is no more input data after the data in
inbuf,the flagConverterFlags.INPUT_AT_ENDmust be set. The loop will be (unless some error happens) returningConverterResult.CONVERTEDeach time until all data is consumed and all output is produced, thenConverterResult.FINISHEDis returned instead. Note, thatConverterResult.FINISHEDmay be returned even ifConverterFlags.INPUT_AT_ENDis not set, for instance in a decompression converter where the end of data is detectable from the data (and there might even be other data after the end of the compressed data).When some data has successfully been converted
bytesReadand is set to the number of bytes read frominbuf,andbytesWrittenis set to indicate how many bytes was written tooutbuf.If there are more data to output or consume (i.e. unless theConverterFlags.INPUT_AT_ENDis specified) thenConverterResult.CONVERTEDis returned, and if no more data is to be output thenConverterResult.FINISHEDis returned.On error
ConverterResult.ERRORis returned anderroris set accordingly. Some errors need special handling:IOErrorEnum.NO_SPACEis returned if there is not enough space to write the resulting converted data, the application should call the function again with a largeroutbufto continue.IOErrorEnum.PARTIAL_INPUTis returned if there is not enough input to fully determine what the conversion should produce, and theConverterFlags.INPUT_AT_ENDflag is not set. This happens for example with an incomplete multibyte sequence when converting text, or when a regexp matches up to the end of the input (and may match further input). It may also happen wheninbufSizeis zero and there is no more data to produce.When this happens the application should read more input and then call the function again. If further input shows that there is no more data call the function again with the same data but with the
ConverterFlags.INPUT_AT_ENDflag set. This may cause the conversion to finish as e.g. in the regexp match case (or, to fail again withIOErrorEnum.PARTIAL_INPUTin e.g. a charset conversion where the input is actually partial).After g_converter_convert() has returned
ConverterResult.FINISHEDthe converter object is in an invalid state where its not allowed to call g_converter_convert() anymore. At this time you can only free the object or call g_converter_reset() to reset it to the initial state.If the flag
ConverterFlags.FLUSHis set then conversion is modified to try to write out all internal state to the output. The application has to call the function multiple times with the flag set, and when the available input has been consumed and all internal state has been produced thenConverterResult.FLUSHED(orConverterResult.FINISHEDif really at the end) is returned instead ofConverterResult.CONVERTED. This is somewhat similar to what happens at the end of the input stream, but done in the middle of the data.This has different meanings for different conversions. For instance in a compression converter it would mean that we flush all the compression state into output such that if you uncompress the compressed data you get back all the input data. Doing this may make the final file larger due to padding though. Another example is a regexp conversion, where if you at the end of the flushed data have a match, but there is also a potential longer match. In the non-flushed case we would ask for more input, but when flushing we treat this as the end of input and do the match.
Flushing is not always possible (like if a charset converter flushes at a partial multibyte sequence). Converters are supposed to try to produce as much output as possible and then return an error (typically
IOErrorEnum.PARTIAL_INPUT).- Parameters:
inbuf- the buffer containing the data to convert.outbuf- a buffer to write converted data in.flags- aGConverterFlagscontrolling the conversion detailsbytesRead- will be set to the number of bytes read frominbufon successbytesWritten- will be set to the number of bytes written tooutbufon success- Returns:
- a
GConverterResult,ConverterResult.ERRORon error. - Throws:
GErrorException- seeGError- Since:
- 2.24
-
convertBytes
Applies this Converter to the data inbytes.- Parameters:
bytes- the data to convert- Returns:
- A newly-allocated
GByteswith the converted data, orNULLif an error occurred - Throws:
GErrorException- seeGError- Since:
- 2.82
-
reset
default void reset()Resets all internal state in the converter, making it behave as if it was just created. If the converter has any internal state that would produce output then that output is lost.- Since:
- 2.24
-