Package com.linecorp.armeria.common
Interface HttpData
- All Superinterfaces:
AutoCloseable
,HttpObject
,SafeCloseable
public interface HttpData extends HttpObject, SafeCloseable
HTTP/2 data that contains a chunk of bytes.
-
Method Summary
Modifier and Type Method Description byte[]
array()
Returns the underlying byte array of this data.default ByteBuf
byteBuf()
(Advanced users only) Returns a new duplicate of the underlyingByteBuf
of this data.ByteBuf
byteBuf(int offset, int length, ByteBufAccessMode mode)
(Advanced users only) Returns a new slice, retained slice or direct copy of the underlyingByteBuf
of this data based on the specifiedByteBufAccessMode
.ByteBuf
byteBuf(ByteBufAccessMode mode)
(Advanced users only) Returns a new duplicate, retained duplicate or direct copy of the underlyingByteBuf
of this data based on the specifiedByteBufAccessMode
.void
close()
Releases the underlyingByteBuf
if this data was created viawrap(ByteBuf)
.static HttpData
copyOf(byte[] data)
Creates a new instance from the specified byte array by first copying it.static HttpData
copyOf(byte[] data, int offset, int length)
Creates a new instance from the specified byte array,offset
andlength
by first copying it.static HttpData
copyOf(ByteBuf data)
Creates a new instance from the specifiedByteBuf
by first copying its content.static HttpData
empty()
Returns an emptyHttpData
.default boolean
isEmpty()
Returns whether thelength()
of this data is 0.boolean
isPooled()
(Advanced users only) Returns whether this data is pooled.int
length()
Returns the length of this data.static HttpData
of(Charset charset, CharSequence text)
Converts the specifiedtext
into anHttpData
.static HttpData
of(Charset charset, String text)
Converts the specifiedtext
into anHttpData
.static HttpData
of(Charset charset, String format, Object... args)
Converts the specified formatted string into anHttpData
.static HttpData
ofAscii(CharSequence text)
Converts the specifiedtext
into a US-ASCIIHttpData
.static HttpData
ofAscii(String text)
Converts the specifiedtext
into a US-ASCIIHttpData
.static HttpData
ofAscii(String format, Object... args)
Converts the specified formatted string into a US-ASCIIHttpData
.static HttpData
ofUtf8(CharSequence text)
Converts the specifiedtext
into a UTF-8HttpData
.static HttpData
ofUtf8(String text)
Converts the specifiedtext
into a UTF-8HttpData
.static HttpData
ofUtf8(String format, Object... args)
Converts the specified formatted string into a UTF-8HttpData
.InputStream
toInputStream()
Returns a newInputStream
that is sourced from this data.default Reader
toReader(Charset charset)
default Reader
toReaderAscii()
Returns a newReader
that is sourced from this data and decoded usingStandardCharsets.US_ASCII
.default Reader
toReaderUtf8()
Returns a newReader
that is sourced from this data and decoded usingStandardCharsets.UTF_8
.String
toString(Charset charset)
Decodes this data into aString
.default String
toStringAscii()
Decodes this data into aString
using US-ASCII encoding.default String
toStringUtf8()
Decodes this data into aString
using UTF-8 encoding.default void
touch(Object hint)
(Advanced users only) Records the current access location of this data for debugging purposes.default HttpData
withEndOfStream()
Returns theHttpData
that has the same content with this data and its HTTP/2endOfStream
flag set.HttpData
withEndOfStream(boolean endOfStream)
Returns theHttpData
that has the same content with this data and its HTTP/2endOfStream
flag set with the specified value.static HttpData
wrap(byte[] data)
Creates a new instance from the specified byte array.static HttpData
wrap(byte[] data, int offset, int length)
Creates a new instance from the specified byte array,offset
andlength
.static HttpData
wrap(ByteBuf buf)
-
Method Details
-
empty
Returns an emptyHttpData
. -
wrap
Creates a new instance from the specified byte array. The array is not copied; any changes made in the array later will be visible toHttpData
. -
wrap
Creates a new instance from the specified byte array,offset
andlength
. The array is not copied; any changes made in the array later will be visible toHttpData
.- Returns:
- a new
HttpData
.empty()
iflength
is 0. - Throws:
IndexOutOfBoundsException
- ifoffset
andlength
are out of bounds
-
wrap
(Advanced users only) Converts the specified NettyByteBuf
into a pooledHttpData
. The buffer is not copied; any changes made to it will be visible toHttpData
. The ownership of the buffer is transferred to theHttpData
. If you still need to use it after calling this method, make sure to callByteBuf.retain()
first.- Returns:
- a new
HttpData
.empty()
if the readable bytes ofbuf
is 0. - See Also:
PooledObjects
-
copyOf
Creates a new instance from the specified byte array by first copying it. -
copyOf
Creates a new instance from the specified byte array,offset
andlength
by first copying it.- Returns:
- a new
HttpData
.empty()
iflength
is 0. - Throws:
ArrayIndexOutOfBoundsException
- ifoffset
andlength
are out of bounds
-
copyOf
-
of
Converts the specifiedtext
into anHttpData
. -
of
Converts the specifiedtext
into anHttpData
. -
of
Converts the specified formatted string into anHttpData
. The string is formatted byString.format(Locale, String, Object...)
with English locale.- Parameters:
charset
- theCharset
to use for encoding stringformat
- the format string of the response contentargs
- the arguments referenced by the format specifiers in the format string- Returns:
- a new
HttpData
.empty()
ifformat
is empty.
-
ofUtf8
Converts the specifiedtext
into a UTF-8HttpData
. -
ofUtf8
Converts the specifiedtext
into a UTF-8HttpData
. -
ofUtf8
Converts the specified formatted string into a UTF-8HttpData
. The string is formatted byString.format(Locale, String, Object...)
with English locale.- Parameters:
format
- the format string of the response contentargs
- the arguments referenced by the format specifiers in the format string- Returns:
- a new
HttpData
.empty()
ifformat
is empty.
-
ofAscii
Converts the specifiedtext
into a US-ASCIIHttpData
. -
ofAscii
Converts the specifiedtext
into a US-ASCIIHttpData
. -
ofAscii
Converts the specified formatted string into a US-ASCIIHttpData
. The string is formatted byString.format(Locale, String, Object...)
with English locale.- Parameters:
format
- the format string of the response contentargs
- the arguments referenced by the format specifiers in the format string- Returns:
- a new
HttpData
.empty()
ifformat
is empty.
-
array
byte[] array()Returns the underlying byte array of this data. Any changes made in the returned array affects the content of this data. -
length
int length()Returns the length of this data. -
isEmpty
default boolean isEmpty()Returns whether thelength()
of this data is 0. -
toString
Decodes this data into aString
. -
toStringUtf8
Decodes this data into aString
using UTF-8 encoding.- Returns:
- the decoded
String
-
toStringAscii
Decodes this data into aString
using US-ASCII encoding.- Returns:
- the decoded
String
-
toInputStream
InputStream toInputStream()Returns a newInputStream
that is sourced from this data. -
toReader
-
toReaderUtf8
Returns a newReader
that is sourced from this data and decoded usingStandardCharsets.UTF_8
. -
toReaderAscii
Returns a newReader
that is sourced from this data and decoded usingStandardCharsets.US_ASCII
. -
withEndOfStream
Returns theHttpData
that has the same content with this data and its HTTP/2endOfStream
flag set. If this data already hasendOfStream
set,this
will be returned. -
withEndOfStream
Returns theHttpData
that has the same content with this data and its HTTP/2endOfStream
flag set with the specified value. If this data already has the sameendOfStream
value set,this
will be returned. -
isPooled
(Advanced users only) Returns whether this data is pooled. Note, if this method returnstrue
, you must callclose()
once you no longer need this data, because its underlyingByteBuf
will not be released automatically.- See Also:
PooledObjects
-
byteBuf
(Advanced users only) Returns a new duplicate of the underlyingByteBuf
of this data. This method does not transfer the ownership of the underlyingByteBuf
, i.e. the reference count of theByteBuf
does not change. If this data is not pooled, the returnedByteBuf
is not pooled, either, which means you need to worry about releasing it only when you created this data withwrap(ByteBuf)
. Any changes made in the content of the returnedByteBuf
affects the content of this data.- See Also:
PooledObjects
-
byteBuf
(Advanced users only) Returns a new duplicate, retained duplicate or direct copy of the underlyingByteBuf
of this data based on the specifiedByteBufAccessMode
. This method does not transfer the ownership of the underlyingByteBuf
, i.e. the reference count of theByteBuf
does not change. If this data is not pooled, the returnedByteBuf
is not pooled, either, which means you need to worry about releasing it only when you created this data withwrap(ByteBuf)
. Any changes made in the content of the returnedByteBuf
affects the content of this data.- See Also:
PooledObjects
-
byteBuf
(Advanced users only) Returns a new slice, retained slice or direct copy of the underlyingByteBuf
of this data based on the specifiedByteBufAccessMode
. This method does not transfer the ownership of the underlyingByteBuf
, i.e. the reference count of theByteBuf
does not change. If this data is not pooled, the returnedByteBuf
is not pooled, either, which means you need to worry about releasing it only when you created this data withwrap(ByteBuf)
. Any changes made in the content of the returnedByteBuf
affects the content of this data.- See Also:
PooledObjects
-
touch
(Advanced users only) Records the current access location of this data for debugging purposes. If this data is determined to be leaked, the information recorded by this operation will be provided to you viaResourceLeakDetector
. -
close
void close()Releases the underlyingByteBuf
if this data was created viawrap(ByteBuf)
. Otherwise, this method does nothing. You may want to call this method to reclaim the underlyingByteBuf
when using operations that return pooled objects, such as: If you don't use such operations, you don't need to call this method.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceSafeCloseable
- See Also:
PooledObjects
-