Class MessageBody
- All Implemented Interfaces:
Proxy
SoupMessageBody
represents the request or response body of a
Message
.
Note that while length
always reflects the full length of the
message body, data
is normally null
, and will only be filled in
after flatten()
is called. For client-side
messages, this automatically happens for the response body after it
has been fully read. Likewise, for server-side
messages, the request body is automatically filled in after being
read.
As an added bonus, when data
is filled in, it is always terminated
with a \\0
byte (which is not reflected in length
).
-
Constructor Summary
ConstructorDescriptionCreates a newSoupMessageBody
.MessageBody
(MemorySegment address) Create a MessageBody proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionvoid
append
(byte[] data) Appendslength
bytes fromdata
to this MessageBody.void
appendBytes
(Bytes buffer) Appends the data frombuffer
to this MessageBody.void
complete()
Tags this MessageBody as being complete.flatten()
Fills in this MessageBody's data field with a buffer containing all of the data in this MessageBody.boolean
Gets the accumulate flag on this MessageBody.getChunk
(long offset) Gets aGLib.Bytes
containing data from this MessageBody starting atoffset
.static MemoryLayout
The memory layout of the native struct.static Type
getType()
Get the GType of the MessageBody classvoid
Handles theSoupMessageBody
part of receiving a chunk of data from the network.byte[]
readData()
Read the value of the fielddata
.long
Read the value of the fieldlength
.ref()
Atomically increments the reference count of this MessageBody by one.void
setAccumulate
(boolean accumulate) Sets or clears the accumulate flag on this MessageBody.void
truncate()
Deletes all of the data in this MessageBody.void
unref()
Atomically decrements the reference count of this MessageBody by one.void
Write a value in the fielddata
.void
writeLength
(long length) Write a value in the fieldlength
.void
wroteChunk
(Bytes chunk) Handles theSoupMessageBody
part of writing a chunk of data to the network.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
MessageBody
Create a MessageBody proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
MessageBody
Creates a newSoupMessageBody
.Message
uses this internally; you will not normally need to call it yourself.- Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readData
public byte[] readData()Read the value of the fielddata
.- Returns:
- The value of the field
data
-
writeData
Write a value in the fielddata
.- Parameters:
data
- The new value for the fielddata
_arena
- to control the memory allocation scope
-
readLength
public long readLength()Read the value of the fieldlength
.- Returns:
- The value of the field
length
-
writeLength
public void writeLength(long length) Write a value in the fieldlength
.- Parameters:
length
- The new value for the fieldlength
-
appendBytes
Appends the data frombuffer
to this MessageBody.- Parameters:
buffer
- aGBytes
-
append
public void append(byte[] data) Appendslength
bytes fromdata
to this MessageBody.This function is exactly equivalent to
append(byte[])
withMemoryUse.TAKE
as second argument; it exists mainly for convenience and simplifying language bindings.- Parameters:
data
- data to append
-
complete
public void complete()Tags this MessageBody as being complete.Call this when using chunked encoding after you have appended the last chunk.
-
flatten
Fills in this MessageBody's data field with a buffer containing all of the data in this MessageBody.Adds an additional
\\0
byte not counted by this MessageBody's length field.- Returns:
- a
GBytes
containing the same data as this MessageBody. (You mustBytes.unref()
this if you do not want it.)
-
getAccumulate
public boolean getAccumulate()Gets the accumulate flag on this MessageBody.See [method
MessageBody
.set_accumulate. for details.- Returns:
- the accumulate flag for this MessageBody.
-
getChunk
Gets aGLib.Bytes
containing data from this MessageBody starting atoffset
.The size of the returned chunk is unspecified. You can iterate through the entire body by first calling
getChunk(long)
with an offset of 0, and then on each successive call, increment the offset by the length of the previously-returned chunk.If
offset
is greater than or equal to the total length of this MessageBody, then the return value depends on whether or notcomplete()
has been called or not; if it has, thengetChunk(long)
will return a 0-length chunk (indicating the end of this MessageBody). If it has not, thengetChunk(long)
will returnnull
(indicating that this MessageBody may still potentially have more data, but that data is not currently available).- Parameters:
offset
- an offset- Returns:
- a
GBytes
-
gotChunk
Handles theSoupMessageBody
part of receiving a chunk of data from the network.Normally this means appending
chunk
to this MessageBody, exactly as withappendBytes(org.gnome.glib.Bytes)
, but if you have set this MessageBody's accumulate flag tofalse
, then that will not happen.This is a low-level method which you should not normally need to use.
- Parameters:
chunk
- aGBytes
received from the network
-
ref
Atomically increments the reference count of this MessageBody by one.- Returns:
- the passed in
SoupMessageBody
-
setAccumulate
public void setAccumulate(boolean accumulate) Sets or clears the accumulate flag on this MessageBody.(The default value is
true
.) If set tofalse
, this MessageBody's data field will not be filled in after the body is fully sent/received, and the chunks that make up this MessageBody may be discarded when they are no longer needed.If you set the flag to
false
on theMessage
request_body of a client-side message, it will block the accumulation of chunks into this MessageBody's data field, but it will not normally cause the chunks to be discarded after being written like in the server-sideMessage
response_body case, because the request body needs to be kept around in case the request needs to be sent a second time due to redirection or authentication.- Parameters:
accumulate
- whether or not to accumulate body chunks in this MessageBody
-
truncate
public void truncate()Deletes all of the data in this MessageBody. -
unref
public void unref()Atomically decrements the reference count of this MessageBody by one.When the reference count reaches zero, the resources allocated by this MessageBody are freed
-
wroteChunk
Handles theSoupMessageBody
part of writing a chunk of data to the network.Normally this is a no-op, but if you have set this MessageBody's accumulate flag to
false
, then this will causechunk
to be discarded to free up memory.This is a low-level method which you should not need to use, and there are further restrictions on its proper use which are not documented here.
- Parameters:
chunk
- aGBytes
returned fromgetChunk(long)
-