Module bus.http

Class Downloads

java.lang.Object
org.miaixz.bus.http.plugin.httpv.Downloads

public class Downloads extends Object
Manages the process of downloading content from an InputStream to a File. This class provides controls for pausing, resuming, and canceling the download, and supports callbacks for success and failure events.
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • nextOnIO

      protected boolean nextOnIO
      A temporary flag to indicate that the next callback should be executed on an I/O thread.
  • Constructor Details

    • Downloads

      public Downloads(File file, InputStream input, CoverTasks.Executor executor, long skipBytes)
      Constructs a new download handler.
      Parameters:
      file - The destination file.
      input - The source input stream.
      executor - The task executor.
      skipBytes - The number of bytes to skip from the beginning of the input stream (used for resuming).
  • Method Details

    • setBuffSize

      public Downloads setBuffSize(int buffSize)
      Sets the buffer size for reading from the input stream. The default size is 2048 bytes.
      Parameters:
      buffSize - The buffer size in bytes.
      Returns:
      this Downloads instance for chaining.
    • setAppended

      public Downloads setAppended()
      Enables append mode. When set, the download will append data to the file, which is useful for resuming downloads or chunked downloading.
      Returns:
      this Downloads instance for chaining.
    • setFilePointer

      public Downloads setFilePointer(long seekBytes)
      Sets the file pointer to a specific position. Writing will start from this byte offset.
      Parameters:
      seekBytes - The number of bytes to skip from the beginning of the file.
      Returns:
      this Downloads instance for chaining.
    • nextOnIO

      public Downloads nextOnIO()
      Specifies that the next callback to be set (e.g., onSuccess, onFailure) should be executed on an I/O thread.
      Returns:
      this Downloads instance for chaining.
    • setOnSuccess

      public Downloads setOnSuccess(Callback<File> onSuccess)
      Sets the callback to be executed when the download completes successfully.
      Parameters:
      onSuccess - The success callback.
      Returns:
      this Downloads instance for chaining.
    • setOnFailure

      public Downloads setOnFailure(Callback<Downloads.Failure> onFailure)
      Sets the callback to be executed when the download fails.
      Parameters:
      onFailure - The failure callback.
      Returns:
      this Downloads instance for chaining.
    • start

      public Downloads.Control start()
      Starts the download process asynchronously on an I/O thread.
      Returns:
      A Downloads.Control object to manage the download (pause, resume, cancel).
    • getCtrl

      public Downloads.Control getCtrl()
      Gets the controller for this download task.
      Returns:
      The Downloads.Control object.