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 String
asString()
Return a copy of the result as a String.BinaryResult
base64()
Wrap the binary data in base64 encoding.boolean
canGzip()
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
create(String data)
Produce a UTF-8 encoded result from a string.BinaryResult
disableGzip()
Disable gzip compression for already compressed responses.String
getAttachmentName()
Get the attachment file name; null if not set.Charset
getCharacterEncoding()
Get the character encoding; null if not known.long
getContentLength()
String
getContentType()
boolean
isBase64()
BinaryResult
setAttachmentName(String attachmentName)
Set the attachment file name and returnthis
.BinaryResult
setCharacterEncoding(Charset encoding)
Set the character set used to encode text data and returnthis
.BinaryResult
setContentLength(long len)
Set the content length of the result; -1 if not known.BinaryResult
setContentType(String contentType)
Set the MIME type of the result, and returnthis
.String
toString()
abstract void
writeTo(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-stream
result from a byte array.
-
create
public static BinaryResult create(InputStream data)
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
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 OutputStreamos
throws 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 IOException
Close the result and release any resources it holds.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
-