Class HttpContent
- java.lang.Object
-
- net.lenni0451.commons.httpclient.content.HttpContent
-
- Direct Known Subclasses:
ByteArrayContent,DelegatingHttpContent,FileContent,InputStreamContent,MultiPartFormContent,URLEncodedFormContent
public abstract class HttpContent extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description HttpContent(ContentType contentType)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static HttpContentbytes(byte[] content)Create a new content from the given bytes.static HttpContentbytes(byte[] content, int offset, int length)Create a new content from the given bytes.abstract booleancanBeStreamedMultipleTimes()Get if the content can be streamed multiple times.
The return value is allowed to change between multiple calls depending on the implementation.protected voidclearCache()Clear the cached byte array if present.
This will force the content to be recomputed on the next access.
This should only be used ifcanBeStreamedMultipleTimes()returnstrue.protected abstract java.io.InputStreamcompute()Compute the content and return it as an input stream.
This method can be called multiple times ifcanBeStreamedMultipleTimes()returnstrue.static HttpContentfile(java.io.File file)Create a new content from the given file.static HttpContentform(java.lang.String key, java.lang.String value)Create a new content from the given form data.static HttpContentform(java.util.Map<java.lang.String,java.lang.String> form)Create a new content from the given form data.byte[]getAsBytes()java.io.InputStreamgetAsStream()Get the content as an input stream.
You are responsible for closing the input stream after use.java.lang.StringgetAsString()java.lang.StringgetAsString(java.nio.charset.Charset charset)Get the content as a string with the given charset.intgetBufferSize()intgetContentLength()Deprecated.ContentTypegetContentType()Deprecated.abstract intgetLength()Get the content length in bytes.
If the content length is unknown, return-1.
Streaming the content may require valid content length depending on the implementation.ContentTypegetType()static InputStreamContentinputStream(ContentType contentType, java.io.InputStream inputStream, int contentLength)Create a new input stream content from the given input stream.protected java.io.InputStreammodify(java.io.InputStream inputStream)Modify the given input stream before returning it to the user.
This can be used to wrap the input stream with another stream (e.g.static MultiPartFormContentmultiPartForm()Create a new multipart form content.HttpContentsetBufferSize(int bufferSize)Set the recommended buffer size for streaming the content.
This is only a recommendation and may be ignored by the implementation.static HttpContentstring(java.lang.String content)Create a new content from the given string.static HttpContentstring(java.lang.String content, java.nio.charset.Charset charset)Create a new content from the given string.voidtransferTo(java.io.OutputStream outputStream)Transfer the content to the given output stream.
The content stream will be closed after the transfer is complete.
-
-
-
Constructor Detail
-
HttpContent
public HttpContent(ContentType contentType)
-
-
Method Detail
-
bytes
public static HttpContent bytes(byte[] content)
Create a new content from the given bytes.- Parameters:
content- The bytes to send- Returns:
- The created content
-
bytes
public static HttpContent bytes(byte[] content, int offset, int length)
Create a new content from the given bytes.- Parameters:
content- The bytes to sendoffset- The offset to start reading fromlength- The length of the bytes to read- Returns:
- The created content
-
string
public static HttpContent string(java.lang.String content)
Create a new content from the given string.- Parameters:
content- The string to send- Returns:
- The created content
-
string
public static HttpContent string(java.lang.String content, java.nio.charset.Charset charset)
Create a new content from the given string.- Parameters:
content- The string to sendcharset- The charset to use- Returns:
- The created content
-
file
public static HttpContent file(java.io.File file)
Create a new content from the given file.- Parameters:
file- The file to send- Returns:
- The created content
-
form
public static HttpContent form(java.lang.String key, java.lang.String value)
Create a new content from the given form data.- Parameters:
key- The keyvalue- The value- Returns:
- The created content
-
form
public static HttpContent form(java.util.Map<java.lang.String,java.lang.String> form)
Create a new content from the given form data.- Parameters:
form- The form data- Returns:
- The created content
-
multiPartForm
public static MultiPartFormContent multiPartForm()
Create a new multipart form content.- Returns:
- The created content
-
inputStream
public static InputStreamContent inputStream(ContentType contentType, java.io.InputStream inputStream, int contentLength)
Create a new input stream content from the given input stream.- Parameters:
contentType- The content typeinputStream- The input streamcontentLength- The content length- Returns:
- The created content
-
getContentType
@Deprecated @ScheduledForRemoval public final ContentType getContentType()
Deprecated.Deprecated, usegetType()instead.
-
getType
public final ContentType getType()
- Returns:
- The type of this content
-
getBufferSize
public final int getBufferSize()
- Returns:
- The recommended buffer size for streaming the content
-
setBufferSize
public final HttpContent setBufferSize(int bufferSize)
Set the recommended buffer size for streaming the content.
This is only a recommendation and may be ignored by the implementation.- Parameters:
bufferSize- The buffer size- Returns:
- This instance for chaining
-
transferTo
public final void transferTo(@WillNotClose java.io.OutputStream outputStream) throws java.io.IOExceptionTransfer the content to the given output stream.
The content stream will be closed after the transfer is complete.- Parameters:
outputStream- The output stream to transfer to- Throws:
java.io.IOException- If an I/O error occurs
-
getAsStream
public final java.io.InputStream getAsStream() throws java.io.IOExceptionGet the content as an input stream.
You are responsible for closing the input stream after use.- Returns:
- The content as an input stream
- Throws:
java.io.IOException- If an I/O error occurs
-
getAsBytes
@Nonnull public final byte[] getAsBytes() throws java.io.IOException- Returns:
- The content as bytes
- Throws:
java.io.IOException- If an I/O error occurs
-
getAsString
@Nonnull public final java.lang.String getAsString() throws java.io.IOException- Returns:
- The content as a UTF-8 string
- Throws:
java.io.IOException- If an I/O error occurs
-
getAsString
@Nonnull public final java.lang.String getAsString(java.nio.charset.Charset charset) throws java.io.IOExceptionGet the content as a string with the given charset.- Parameters:
charset- The charset to use- Returns:
- The content as a string
- Throws:
java.io.IOException- If an I/O error occurs
-
clearCache
protected final void clearCache()
Clear the cached byte array if present.
This will force the content to be recomputed on the next access.
This should only be used ifcanBeStreamedMultipleTimes()returnstrue.
-
getContentLength
@Deprecated @ScheduledForRemoval public final int getContentLength()
Deprecated.Deprecated, usegetLength()instead.
-
canBeStreamedMultipleTimes
public abstract boolean canBeStreamedMultipleTimes()
Get if the content can be streamed multiple times.
The return value is allowed to change between multiple calls depending on the implementation.- Returns:
- If the content can be streamed multiple times
-
getLength
public abstract int getLength()
Get the content length in bytes.
If the content length is unknown, return-1.
Streaming the content may require valid content length depending on the implementation.- Returns:
- The content length
-
compute
@Nonnull protected abstract java.io.InputStream compute() throws java.io.IOExceptionCompute the content and return it as an input stream.
This method can be called multiple times ifcanBeStreamedMultipleTimes()returnstrue.- Returns:
- The content
- Throws:
java.io.IOException- If an I/O error occurs
-
modify
protected java.io.InputStream modify(java.io.InputStream inputStream) throws java.io.IOExceptionModify the given input stream before returning it to the user.
This can be used to wrap the input stream with another stream (e.g. for decompression).
The default implementation just returns the given input stream.- Parameters:
inputStream- The input stream to modify- Returns:
- The modified input stream
- Throws:
java.io.IOException- If an I/O error occurs
-
-