Package com.morpheusdata.core.util.image
Class StreamingQcow2Writer
java.lang.Object
com.morpheusdata.core.util.image.StreamingQcow2Writer
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAn iterable that converts byte ranges into cluster indices.static classStreamingQcow2Writer.Range<T extends Comparable<? super T>>Represents a range with a start and end value. -
Field Summary
FieldsModifier and TypeFieldDescription -
Constructor Summary
ConstructorsConstructorDescriptionStreamingQcow2Writer(long inputSize, Iterable<StreamingQcow2Writer.Range<Long>> ranges) Constructs a StreamingQcow2Writer for generating QCOW2 format disk images. -
Method Summary
Modifier and TypeMethodDescriptionvoidcopyData(InputStream inputStream, OutputStream writer) Copies data clusters from an input stream to an output stream.voidcopyData(InputStream inputStream, RandomAccessFile writer) Copies data clusters from an input stream to a RandomAccessFile using two independent FlushingBuffers â one for L2 table entries, one for data clusters â both writing sequentially forward with no backward seeks.voidcopyData(RandomAccessFile reader, OutputStream writer) Copies data clusters from a random access file to an output stream.longfileSize()Calculates the total file size of the QCOW2 image.longCalculates the total number of clusters needed to represent the guest virtual disk.voidwriteHeader(OutputStream outputStream) Writes the complete QCOW2 header, refcount table, and mapping tables to an output stream.voidwriteHeader(RandomAccessFile randomAccessFile) Writes the complete QCOW2 header, refcount table, and mapping tables to a random access file.
-
Field Details
-
dataClusters
-
dataClusterIterable
-
-
Constructor Details
-
StreamingQcow2Writer
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 bytesranges- 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
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
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
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 fromwriter- the output stream to write data to- Throws:
IOException- if an I/O error occurs during copying
-
copyData
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 fromwriter- the output stream to write data to (will be buffered)- Throws:
IOException- if an I/O error occurs during copying
-
copyData
Copies data clusters from an input stream to a RandomAccessFile using two independent FlushingBuffers â one for L2 table entries, one for data clusters â both writing sequentially forward with no backward seeks. L2 entries are written for every guest cluster: 0 for zero-filled/sparse clusters, and the actual data file offset for non-zero clusters. Data clusters are coalesced into 1MB writes for efficiency on high-latency storage.- Parameters:
inputStream- the input stream to read data fromwriter- the RandomAccessFile wrapping the destination QCOW2 file- Throws:
IOException- if an I/O error occurs
-