Class BinaryResult
- java.lang.Object
-
- com.google.gerrit.extensions.restapi.BinaryResult
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public abstract class BinaryResult extends Object implements Closeable
Wrapper around a non-JSON result from aRestView.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[]orInputSteam.
-
-
Constructor Summary
Constructors Constructor Description BinaryResult()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description StringasString()Return a copy of the result as a String.BinaryResultbase64()Wrap the binary data in base64 encoding.booleancanGzip()Returns true if this result can be gzip compressed to clients.voidclose()Close the result and release any resources it holds.static BinaryResultcreate(byte[] data)Produce anapplication/octet-streamresult from a byte array.static BinaryResultcreate(InputStream data)Produce anapplication/octet-streamof unknown length by copying the InputStream until EOF.static BinaryResultcreate(String data)Produce a UTF-8 encoded result from a string.BinaryResultdisableGzip()Disable gzip compression for already compressed responses.StringgetAttachmentName()Get the attachment file name; null if not set.CharsetgetCharacterEncoding()Get the character encoding; null if not known.longgetContentLength()Returns length in bytes of the result; -1 if not known.StringgetContentType()Returns the MIME type of the result, for HTTP clients.booleanisBase64()Returns true if the result must be base64 encoded.BinaryResultsetAttachmentName(String attachmentName)Set the attachment file name and returnthis.BinaryResultsetCharacterEncoding(Charset encoding)Set the character set used to encode text data and returnthis.BinaryResultsetContentLength(long len)Set the content length of the result; -1 if not known.BinaryResultsetContentType(String contentType)Set the MIME type of the result, and returnthis.StringtoString()abstract voidwriteTo(OutputStream os)Write or copy the result onto the specified output stream.
-
-
-
Method Detail
-
create
public static BinaryResult create(String data)
Produce a UTF-8 encoded result from a string.
-
create
public static BinaryResult create(byte[] data)
Produce anapplication/octet-streamresult from a byte array.
-
create
public static BinaryResult create(InputStream data)
Produce anapplication/octet-streamof unknown length by copying the InputStream until EOF. The server glue will automatically close this stream when copying is complete.
-
getContentType
public String getContentType()
Returns the MIME type of the result, for HTTP clients.
-
setContentType
public BinaryResult setContentType(String contentType)
Set the MIME type of the result, and returnthis.
-
getCharacterEncoding
public Charset getCharacterEncoding()
Get the character encoding; null if not known.
-
setCharacterEncoding
public BinaryResult setCharacterEncoding(Charset encoding)
Set the character set used to encode text data and returnthis.
-
getAttachmentName
public String getAttachmentName()
Get the attachment file name; null if not set.
-
setAttachmentName
public BinaryResult setAttachmentName(String attachmentName)
Set the attachment file name and returnthis.
-
getContentLength
public long getContentLength()
Returns length in bytes of the result; -1 if not known.
-
setContentLength
public BinaryResult setContentLength(long len)
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
public BinaryResult disableGzip()
Disable gzip compression for already compressed responses.
-
isBase64
public boolean isBase64()
Returns true if the result must be base64 encoded.
-
base64
public BinaryResult base64()
Wrap the binary data in base64 encoding.
-
writeTo
public abstract void writeTo(OutputStream os) throws IOException
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 OutputStreamosthrows any IOException during a write or flush call.
-
asString
public String asString() throws IOException
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
public void close() throws IOExceptionClose the result and release any resources it holds.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
-