Class BinaryResult
java.lang.Object
com.google.gerrit.extensions.restapi.BinaryResult
- All Implemented Interfaces:
Closeable
,AutoCloseable
Wrapper around a non-JSON result from a
RestView
.
Views may return this type to signal they want the server glue to write raw data to the
client, instead of attempting automatic conversion to JSON. The create form is overloaded to
handle plain text from a String, or binary data from a byte[]
or InputSteam
.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasString()
Return a copy of the result as a String.base64()
Wrap the binary data in base64 encoding.boolean
canGzip()
Returns true if this result can be gzip compressed to clients.void
close()
Close the result and release any resources it holds.static BinaryResult
create
(byte[] data) Produce anapplication/octet-stream
result from a byte array.static BinaryResult
create
(InputStream data) Produce anapplication/octet-stream
of unknown length by copying the InputStream until EOF.static BinaryResult
Produce a UTF-8 encoded result from a string.Disable gzip compression for already compressed responses.Get the attachment file name; null if not set.Get the character encoding; null if not known.long
Returns length in bytes of the result; -1 if not known.Returns the MIME type of the result, for HTTP clients.boolean
isBase64()
Returns true if the result must be base64 encoded.setAttachmentName
(String attachmentName) Set the attachment file name and returnthis
.setCharacterEncoding
(Charset encoding) Set the character set used to encode text data and returnthis
.setContentLength
(long len) Set the content length of the result; -1 if not known.setContentType
(String contentType) Set the MIME type of the result, and returnthis
.toString()
abstract void
writeTo
(OutputStream os) Write or copy the result onto the specified output stream.
-
Constructor Details
-
BinaryResult
public BinaryResult()
-
-
Method Details
-
create
Produce a UTF-8 encoded result from a string. -
create
Produce anapplication/octet-stream
result from a byte array. -
create
Produce anapplication/octet-stream
of unknown length by copying the InputStream until EOF. The server glue will automatically close this stream when copying is complete. -
getContentType
Returns the MIME type of the result, for HTTP clients. -
setContentType
Set the MIME type of the result, and returnthis
. -
getCharacterEncoding
Get the character encoding; null if not known. -
setCharacterEncoding
Set the character set used to encode text data and returnthis
. -
getAttachmentName
Get the attachment file name; null if not set. -
setAttachmentName
Set the attachment file name and returnthis
. -
getContentLength
public long getContentLength()Returns length in bytes of the result; -1 if not known. -
setContentLength
Set the content length of the result; -1 if not known. -
canGzip
public boolean canGzip()Returns true if this result can be gzip compressed to clients. -
disableGzip
Disable gzip compression for already compressed responses. -
isBase64
public boolean isBase64()Returns true if the result must be base64 encoded. -
base64
Wrap the binary data in base64 encoding. -
writeTo
Write or copy the result onto the specified output stream.- Parameters:
os
- stream to write result data onto. This stream will be closed by the caller after this method returns.- Throws:
IOException
- if the data cannot be produced, or the OutputStreamos
throws any IOException during a write or flush call.
-
asString
Return a copy of the result as a String.The default version of this method copies the result into a temporary byte array and then tries to decode it using the configured encoding.
- Returns:
- string version of the result.
- Throws:
IOException
- if the data cannot be produced or could not be decoded to a String.
-
close
Close the result and release any resources it holds.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
toString
-