Class UploadReader

java.lang.Object
io.milton.zsync.UploadReader

public class UploadReader extends Object
An object that performs the server side operations needed to assemble the file from a ZSync PUT.

These operations consist of copying byte ranges into the new file. The moveBlocks(byte[], java.util.List<io.milton.zsync.RelocateRange>, int, byte[]) method copies ranges from the previous file according to a list of RelocateRanges, while the sendRanges(byte[], java.util.List<io.milton.http.Range>, byte[]) method copies the new data received in the upload. Both of these methods are overloaded with versions that accept File rather than byte[] arguments for dealing with large files that should not be loaded into memory all at once.

To assemble the file from a ZSync upload, the server should construct an UploadReader, passing to the constructor the file to be updated and an InputStream containing the upload data. It should then invoke the assemble() method, which will return a temporary file that was created.

Author:
Nick
  • Constructor Details

    • UploadReader

      public UploadReader(File serverFile, InputStream uploadIn) throws IOException
      Constructor that parses the InputStream into an Upload object and initializes a temporary file that will contain the assembled upload
      Parameters:
      serverFile - The server file to be updated
      uploadIn - A stream containing the ZSync PUT data
      Throws:
      IOException
  • Method Details

    • moveBlocks

      public static void moveBlocks(byte[] in, List<RelocateRange> rlist, int blockSize, byte[] out)
      Copies blocks of data from the in array to the out array.
      Parameters:
      in - The byte array containing the server's file being replaced
      rlist - The List of RelocateRanges received from the upload
      blockSize - The block size used in rlist
      out - The byte array of the file being assembled
    • moveBlocks

      public static void moveBlocks(File inFile, Enumeration<RelocateRange> relocRanges, int blocksize, File outFile) throws IOException
      Copies blocks of data from the input File to the output File. For each RelocateRange A-B/C in relocRanges, the block starting at A and ending at B-1 is copied from inFile and written to byte C of outFile.
      Parameters:
      inFile - The server's File being replaced
      relocRanges - The Enumeration of RelocateRanges parsed from the Upload's relocStream
      blocksize - The block size used in relocRanges
      outFile - The File being assembled
      Throws:
      IOException
    • sendRanges

      public static void sendRanges(byte[] in, List<Range> ranges, byte[] out)
      Copies bytes from the in array into Ranges of the out array. The in array is expected to contain the queued bytes in the same order as the ranges List.
      Parameters:
      in - An array containing the queued bytes corresponding to the ranges List
      ranges - The List of target Ranges
      out - The byte array for the file being assembled
    • sendRanges

      public static void sendRanges(Enumeration<ByteRange> byteRanges, File outFile) throws IOException
      Inserts the data from each DataRange into the output File, at the appropriate offset
      Parameters:
      byteRanges - The Enumeration of Range/InputStream pairs parsed from the Upload's dataStream
      outFile - The output File being assembled
      Throws:
      IOException
    • assemble

      public File assemble() throws IOException
      Invokes the methods to put together the uploaded file.
      Returns:
      The assembled File
      Throws:
      IOException
    • getChecksum

      public String getChecksum()
      Returns the expected SHA1 checksum String received in the upload
      Returns:
      A SHA1 checksum