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
ConstructorsConstructorDescriptionCreates a newSoupMessageBody.MessageBody(MemorySegment address) Create a MessageBody proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionvoidappend(@org.jspecify.annotations.Nullable byte @Nullable [] data) Appendslengthbytes fromdatatobody.voidappendBytes(byte[] buffer) Appends the data frombuffertobody.voidcomplete()Tags this MessageBody as being complete.byte[]flatten()Fills inbody'sdata field with a buffer containing all of the data inbody.booleanGets the accumulate flag onbody.byte @Nullable []getChunk(long offset) Gets aGLib.Bytescontaining data from this MessageBody starting atoffset.static MemoryLayoutThe memory layout of the native struct.static @Nullable TypegetType()Get the GType of the MessageBody classvoidgotChunk(byte[] chunk) Handles theSoupMessageBodypart of receiving a chunk of data from the network.@org.jspecify.annotations.Nullable byte @Nullable []readData()Read the value of the fielddata.longRead the value of the fieldlength.ref()Atomically increments the reference count of this MessageBody by one.voidsetAccumulate(boolean accumulate) Sets or clears the accumulate flag onbody.voidtruncate()Deletes all of the data inbody.voidunref()Atomically decrements the reference count of this MessageBody by one.voidWrite a value in the fielddata.voidwriteLength(long length) Write a value in the fieldlength.voidwroteChunk(byte[] chunk) Handles theSoupMessageBodypart of writing a chunk of data to the network.Methods inherited from class org.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
public MessageBody()Creates a newSoupMessageBody.Messageuses this internally; you will not normally need to call it yourself.
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readData
public @org.jspecify.annotations.Nullable byte @Nullable [] 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
public void appendBytes(byte[] buffer) Appends the data frombuffertobody.- Parameters:
buffer- aGBytes
-
append
public void append(@org.jspecify.annotations.Nullable byte @Nullable [] data) Appendslengthbytes fromdatatobody.This function is exactly equivalent to
append(byte[])withMemoryUse.TAKEas 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
public byte[] flatten()Fills inbody'sdata field with a buffer containing all of the data inbody.Adds an additional
\\0byte not counted bybody'slength field.- Returns:
- a
GBytescontaining the same data asbody.(You mustorg.gnome.glib.Bytes#unrefthis if you do not want it.)
-
getAccumulate
public boolean getAccumulate()Gets the accumulate flag onbody.See [method
MessageBody.setAccumulate.for details.- Returns:
- the accumulate flag for
body.
-
getChunk
public byte @Nullable [] getChunk(long offset) Gets aGLib.Bytescontaining 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
offsetis greater than or equal to the total length ofbody,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 ofbody).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
public void gotChunk(byte[] chunk) Handles theSoupMessageBodypart of receiving a chunk of data from the network.Normally this means appending
chunktobody,exactly as withappendBytes(byte[]), but if you have setbody'saccumulate flag tofalse, then that will not happen.This is a low-level method which you should not normally need to use.
- Parameters:
chunk- aGBytesreceived 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 onbody.(The default value is
true.) If set tofalse,body'sdata 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
falseon theMessagerequest_body of a client-side message, it will block the accumulation of chunks intobody'sdata field, but it will not normally cause the chunks to be discarded after being written like in the server-sideMessageresponse_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 inbody. -
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
public void wroteChunk(byte[] chunk) Handles theSoupMessageBodypart of writing a chunk of data to the network.Normally this is a no-op, but if you have set
body'saccumulate flag tofalse, then this will causechunkto 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- aGBytesreturned fromgetChunk(long)
-