Class StreamingQcow2Writer

java.lang.Object
com.morpheusdata.core.util.image.StreamingQcow2Writer

public class StreamingQcow2Writer extends Object
A class for generating QCOW2 format disk images in a streaming manner. This class constructs the QCOW2 header, refcount tables, and mapping tables based on the specified virtual disk size and data ranges. It supports writing the QCOW2 structures to output streams or random access files, and copying data clusters from input streams or random access files while skipping sparse regions.
  • Field Details

  • Constructor Details

    • StreamingQcow2Writer

      public StreamingQcow2Writer(long inputSize, Iterable<StreamingQcow2Writer.Range<Long>> ranges)
      Constructs a StreamingQcow2Writer for generating QCOW2 format disk images. Initializes the QCOW2 header structure, calculates cluster mappings, and sets up the refcount tables based on the provided input size and data ranges.
      Parameters:
      inputSize - the virtual disk size in bytes
      ranges - an iterable collection of byte ranges that contain actual data (non-sparse regions)
      Throws:
      IllegalArgumentException - if the data clusters in ranges are not sorted
  • Method Details

    • fileSize

      public long fileSize()
      Calculates the total file size of the QCOW2 image.
      Returns:
      the total file size in bytes (total clusters * cluster size)
    • totalGuestClusters

      public long totalGuestClusters()
      Calculates the total number of clusters needed to represent the guest virtual disk.
      Returns:
      the number of clusters in the virtual disk
    • writeHeader

      public void writeHeader(OutputStream outputStream) throws IOException
      Writes the complete QCOW2 header, refcount table, and mapping tables to an output stream. This includes the QCOW2 magic bytes, version 3 format header fields, L1/L2 mapping tables, and refcount structures necessary for a valid QCOW2 image.
      Parameters:
      outputStream - the output stream to write the QCOW2 header to
      Throws:
      IOException - if an I/O error occurs during writing
    • writeHeader

      public void writeHeader(RandomAccessFile randomAccessFile) throws IOException
      Writes the complete QCOW2 header, refcount table, and mapping tables to a random access file. This method seeks to the beginning of the file and writes the QCOW2 magic bytes, version 3 format header fields, L1/L2 mapping tables, and refcount structures necessary for a valid QCOW2 image.
      Parameters:
      randomAccessFile - the random access file to write the QCOW2 header to (will seek to position 0)
      Throws:
      IOException - if an I/O error occurs during writing
    • copyData

      public void copyData(RandomAccessFile reader, OutputStream writer) throws IOException
      Copies data clusters from a random access file to an output stream. Only copies clusters identified in the data ranges provided during construction, skipping sparse/empty regions. Progress is reported to stderr at regular intervals.
      Parameters:
      reader - the random access file to read data from
      writer - the output stream to write data to
      Throws:
      IOException - if an I/O error occurs during copying
    • copyData

      public void copyData(InputStream inputStream, OutputStream writer) throws IOException
      Copies data clusters from an input stream to an output stream. Only copies clusters identified in the data ranges provided during construction, skipping sparse/empty regions by advancing the input stream position. Progress is reported to stderr at regular intervals.
      Parameters:
      inputStream - the input stream to read data from
      writer - the output stream to write data to (will be buffered)
      Throws:
      IOException - if an I/O error occurs during copying
    • copyData

      public void copyData(InputStream inputStream, RandomAccessFile writer) throws IOException
      Copies data clusters from an input stream to a random access file. Only copies clusters identified in the data ranges provided during construction, skipping sparse/empty regions by advancing the input stream position. Detects zero-filled clusters and marks them as empty in the L2 table to create a thin QCOW2 image. If the input stream ends before all clusters are processed, all remaining clusters are marked as empty in the L2 table. Progress is reported to stderr at regular intervals.
      Parameters:
      inputStream - the input stream to read data from
      writer - the random access file to write data to
      Throws:
      IOException - if an I/O error occurs during copying