Class B2ContentWriter

  • All Implemented Interfaces:
    B2ContentSink
    Direct Known Subclasses:
    B2ContentFileWriter, B2ContentMemoryWriter, B2ContentOutputStreamWriter

    public abstract class B2ContentWriter
    extends java.lang.Object
    implements B2ContentSink
    B2ContentWriter is a base class for ContentHandlers which writes the content to a stream. Different subclasses support different types of output streams. If possible, this class will verify the SHA1 of the downloaded content. It is possible when: * the content is NOT a partial (ie "range") request. * the response include either a "X-Bz-Content-Sha1" or "X-Bz-Info-large_file_sha1" header. (Note that if the sha1 header's value starts with "unverified:", the remainder of the value will be used.) If possible (see above), this class will always verify the SHA1 of the content as it's downloaded. If possible (see above) and requested, this class will also re-read the content from its destination to verify that it has been stored to the destination with no errors. THREAD-SAFETY: this object is NOT thread-safe on its own. users should only use it from one thread at a time and do proper locking to ensure changes are visible to other threads as needed.
    • Method Detail

      • readContent

        public void readContent​(B2Headers responseHeaders,
                                java.io.InputStream rawIn)
                         throws B2Exception
        Description copied from interface: B2ContentSink
        Reads the data from the input stream. Does NOT need to close the stream. The caller of readContent() will take care of that. If you don't read to the end of the inputStream, the connection probably won't be re-usable, so read to the end unless you have a good reason not to.
        Specified by:
        readContent in interface B2ContentSink
        Parameters:
        responseHeaders - the headers from the response
        rawIn - an input stream to read the content of the response
        Throws:
        B2Exception - if there's trouble
      • createDestinationOutputStream

        protected abstract java.io.OutputStream createDestinationOutputStream()
                                                                       throws java.io.IOException
        Returns:
        an outputStream to write to the destination. calling this is allowed to destroy the existing output (if any) and make it impossible for a stream created by createDestinationInputStream() to be able to read the data, if any.
        Throws:
        java.io.IOException - if there's any trouble
        NOTE
        this may be called multiple times.
      • createDestinationInputStream

        protected abstract java.io.InputStream createDestinationInputStream()
                                                                     throws java.io.IOException
        Returns:
        a new inputStream to read from the destination. this might not contain what you think it should if readContents hasn't completed successfully.
        Throws:
        java.io.IOException - if there's any trouble
        NOTE
        this may be called multiple times.
      • succeeded

        protected void succeeded()
        Called when the download succeeded and, to the extent we're able to verify it, the SHA1 matched. Called at most once for each time that createDestinationOutputStream() is called.
      • failed

        protected void failed()
        Called after we've created an output stream, but then the download failed for some reason. That reason may include errors with the download, or, to the extent we can verify it, an error with the SHA1 of the written content. Called at most once for each time that createDestinationOutputStream() is called. Called before an attempt to retry the download (if any).
      • getHeadersOrNull

        public B2Headers getHeadersOrNull()
        Returns:
        the headers from the server, if any. returns null until readContent is called. may return non-null even if there's an exception during readContent().