Class UploadMaker

java.lang.Object
io.milton.zsync.UploadMaker

public class UploadMaker extends Object
An object that performs the client-side operations needed to generate ZSync PUT data.

In order to update a file on a server, the client first needs to download the appropriate .zsync containing the metadata of the server file. The client should then instantiate an UploadMaker, passing to the constructor this .zsync file as well as the local file to be uploaded. On construction, the UploadMaker will determine the data ranges and assembly instructions that need to be sent to the server, and will automatically fill in an internal Upload object. The client can then invoke the getInputStream method, which will return a stream which should be used as the body of a PUT request.

E.g:

< code> UploadMaker um = new UploadMaker(File clientFile, File zsFile);
InputStream putData = um.getInputStream();

Note: This is one of two classes that can be used to create a ZSync upload. The other class, UploadMakerEx, performs the same functions but may perform better for certain rare cases.

Author:
Nick
  • Field Details

    • localCopy

      public final File localCopy
      The local file that will replace the server file
    • serversMetafile

      public final File serversMetafile
      The .zsync of the server file to be replaced
  • Constructor Details

    • UploadMaker

      public UploadMaker(File sourceFile, File destMeta) throws IOException
      Constructor that automatically creates and fills in an internal upload object.
      Parameters:
      sourceFile - The local file to be uploaded
      destMeta - The zsync of the server's file
      Throws:
      IOException
  • Method Details

    • makeUpload

      public InputStream makeUpload() throws IOException
      Throws:
      IOException
    • init

      public void init() throws IOException
      Throws:
      IOException
    • serversMissingRanges

      public static InputStream serversMissingRanges(long[] fileMap, File local, int blockSize) throws IOException
      Determines the byte ranges of new data that need to be sent to the server to update its file.

      The fileMap argument should be an array that maps matching blocks from the server's file (the side that sent the metadata) to those in the client file, such that fileMap[seq] == off means that block number seq in the server's file matches the block in the local file beginning at byte off. An invalid offset is ignored and should be used to indicate that the local file contains no match for that block. The fileMap array can be obtained from the MakeContext class.

      Parameters:
      fileMap - An array mapping blocks in server file to their offsets in local file
      fileLength - The length of the local file to be uploaded
      blockSize - The size of a block. Must correspond to block size used in fileMap
      Returns:
      The List of byte Ranges that need to be sent
      Throws:
      IOException
    • serversRelocationRanges

      public static InputStream serversRelocationRanges(long[] fileMap, int blockSize, long fileLength, boolean combineRanges) throws IOException
      Returns the assembly instructions needed by the server to relocate the blocks it already has.

      The combineRanges argument determines whether contiguous matching blocks should be combined into a single range, e.g. given a blockSize of 100, whether 0-10/500, 10-20/600, 20-30/700 should be combined into the single RelocateRange of 0-30/500.

      Parameters:
      fileMap - An array mapping blocks in the server file to their matches in the local file
      blockSize - The block size used by fileMap
      fileLength - The length of the local file to be uploaded
      combineRanges - Whether consecutive matches should be combined into a single RelocateRange
      Returns:
      A list of RelocateRange instructions to be sent to the server
      Throws:
      IOException
    • getDataRanges

      public static List<DataRange> getDataRanges(List<Range> ranges, File local) throws IOException
      Returns the List of DataRange objects containing the portions of the client file to be uploaded to the server. Currently unused.
      Parameters:
      ranges - The List of Ranges from the client file needed by the server, which can be obtained from #serversMissingRanges(long[], long, int)
      local - The client file to be uploaded
      Returns:
      The List of DataRange objects containing client file portions to be uploaded
      Throws:
      IOException
    • getInputStream

      public InputStream getInputStream() throws UnsupportedEncodingException, IOException
      Returns the stream of bytes to be used as the body of a ZSync PUT.

      Note: Any temporary files used to store the data for the stream will be deleted once the stream is closed, so a second invocation of this method may not work.

      Returns:
      The InputStream containing the data for a ZSync PUT
      Throws:
      UnsupportedEncodingException
      IOException
    • getRelocStream

      public static InputStream getRelocStream(List<RelocateRange> relocList) throws IOException
      Generates the relocStream portion of an Upload from a List of RelocateRanges.
      Parameters:
      relocList - The List of RelocateRanges
      Returns:
      An InputStream containing the relocStream portion of an Upload
      Throws:
      IOException
    • getDataStream

      public static InputStream getDataStream(List<Range> ranges, File local) throws IOException
      Generates the dataStream portion of an Upload from the local file and a List of Ranges
      Parameters:
      ranges - The List of byte ranges
      local - The local file being uploaded
      Returns:
      The InputStream containing the dataStream portion of an Upload
      Throws:
      IOException