Class BinaryResult

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public abstract class BinaryResult
    extends Object
    implements Closeable
    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 Detail

      • BinaryResult

        public BinaryResult()
    • 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 an application/octet-stream result from a byte array.
      • create

        public static BinaryResult create​(InputStream data)
        Produce an application/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 return this.
      • 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 return this.
      • 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 return this.
      • 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 OutputStream os 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.