Package io.milton.zsync
Class Upload
java.lang.Object
io.milton.zsync.Upload
A container for the information transmitted in a ZSync PUT upload. The information currently consists of some
headers (file length, block size, etc...), an InputStream containing a list of RelocateRanges for relocating matching blocks,
and an InputStream containing a sequence of data chunks (along with their ranges). The Upload class also contains methods for
translating to/from a stream (getInputStream and parse, respectively).
- Author:
- Nick
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAnEnumerationwrapper for an Iterator.static classAn object representing a (Key, Value) pair of Strings. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final StringThe character encoding used to convert Strings to bytes.A String that marks the beginning of a range of uploaded bytes.static final Stringstatic final charThe character marking the end of a line.static final StringThe total number of bytes of new data to be transmitted.static final Stringstatic final Stringstatic final Stringstatic final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongGets the blocksize used in the upload.Gets the list of uploaded data chunks ( byte Ranges and their associated data ).longGets the length of the (assembled) source file being uploadedReturns an InputStream containing a complete ZSync upload (Params, Relocate stream, and ByteRange stream), ready to be sent as the body of a PUT request.Returns the list of headers in String format, in the proper format for upload.Gets the list of RelocateRanges, which tells the server which blocks of the previous file to keep, and where to place them in the new file.getSha1()Gets the checksum for the entire source fileGets the zsync version of the upload sender (client)static StringparamString(String key, Object value) static Uploadparse(InputStream in) Parses the InputStream into an Upload object.static StringreadToken(InputStream in, byte[] delimiters, int maxsearch) Returns the next String terminated by one of the specified delimiters or the end of the InputStream.static StringreadValue(InputStream in, int maxsearch) Helper method that reads the String preceding the first newline in the InputStream.voidsetBlocksize(long blocksize) Sets the blocksize used in the upload.voidsetDataStream(InputStream dataStream) Sets the list of data chunks to be uploaded ( byte Ranges and their associated data ).voidsetFilelength(long filelength) Sets the length of the (assembled) source file being uploadedvoidsetRelocStream(InputStream relocStream) Sets the list of RelocateRanges, which tells the server which blocks of the previous file to keep, and where to place them in the new file.voidSets the checksum for the entire source file, which allow the server to validate the new file after assembling it.voidsetVersion(String version) Sets the zsync version of the upload sender (client)
-
Field Details
-
CHARSET
The character encoding used to convert Strings to bytes. The default is US-ASCII. The methods involved in parsing assume one byte per character.- See Also:
-
LF
public static final char LFThe character marking the end of a line. The default is '\n'- See Also:
-
DIV
A String that marks the beginning of a range of uploaded bytes. Currently unused. -
VERSION
- See Also:
-
BLOCKSIZE
- See Also:
-
FILELENGTH
- See Also:
-
NEWDATA
The total number of bytes of new data to be transmitted. Currently Unused.- See Also:
-
SHA_1
- See Also:
-
RELOCATE
- See Also:
-
RANGE
- See Also:
-
-
Constructor Details
-
Upload
public Upload()Constructs an empty Upload object. Its fields need to be set individually.
-
-
Method Details
-
getParams
Returns the list of headers in String format, in the proper format for upload. The list is terminated by the LF character.- Returns:
- A String containing the headers
-
paramString
-
parse
Parses the InputStream into an Upload object. The method initially parses the headers from the InputStream by reading the sequence of keys (the String preceding the first colon in each line) and values ( the String following the colon and terminated by the LF character ) and invokingparseParam(java.lang.String, java.lang.String)on each key value pair. If the key is RELOCATE, then the value is not read, but is copied into a BufferingOutputStream and stored in the relocStream field. Parsing of headers continues until a "blank" line is reached, ie a line that is null or contains only whitespace, which indicates the beginning of the data section. A reference to the remaining InputStream is then stored in the dataStream field.- Parameters:
in- The InputStream containing the ZSync upload- Returns:
- A filled in Upload object
-
readToken
public static String readToken(InputStream in, byte[] delimiters, int maxsearch) throws ParseException, IOException Returns the next String terminated by one of the specified delimiters or the end of the InputStream. This method simply reads from an InputStream one byte at a time, up to maxsearch bytes, until it reads a byte equal to one of the delimiters or reaches the end of the stream. It uses the CHARSET encoding to translate the bytes read into a String, which it returns with delimiter excluded, or it throws a ParseException if maxSearch bytes are read without reaching a delimiter or the end of the stream. A non-buffering method is used because a buffering reader would likely pull in part of the binary data from the InputStream. An alternative is to use a BufferedReader with a given buffer size and use mark and reset to get back binary data pulled into the buffer.- Parameters:
in- The InputStream to read fromdelimiters- A list of byte values, each of which indicates the end of a tokenmaxsearch- The maximum number of bytes to search for a delimiter- Returns:
- The String containing the CHARSET decoded String with delimiter excluded
- Throws:
IOExceptionParseException- If a delimiter byte is not found within maxsearch reads
-
readValue
Helper method that reads the String preceding the first newline in the InputStream.- Parameters:
in- The InputStream to read frommaxsearch- The maximum number of bytes allowed in the value- Returns:
- The CHARSET encoded String that was read
- Throws:
ParseException- If a newline or end of input is not reached within maxsearch readsIOException
-
getInputStream
Returns an InputStream containing a complete ZSync upload (Params, Relocate stream, and ByteRange stream), ready to be sent as the body of a PUT request. Note: In this implementation, any temporary file used to store the RelocateRanges will be automatically deleted when this stream is closed, so a second invocation of this method on the same Upload object is likely to throw an exception. Therefore, this method should be used only once per Upload object.- Returns:
- The complete ZSync upload
- Throws:
UnsupportedEncodingExceptionIOException
-
getVersion
Gets the zsync version of the upload sender (client) -
setVersion
Sets the zsync version of the upload sender (client) -
getSha1
Gets the checksum for the entire source file -
setSha1
Sets the checksum for the entire source file, which allow the server to validate the new file after assembling it. -
getBlocksize
public long getBlocksize()Gets the blocksize used in the upload. -
setBlocksize
public void setBlocksize(long blocksize) Sets the blocksize used in the upload. The server needs this to translate block ranges into byte ranges -
getFilelength
public long getFilelength()Gets the length of the (assembled) source file being uploaded -
setFilelength
public void setFilelength(long filelength) Sets the length of the (assembled) source file being uploaded -
getRelocStream
Gets the list of RelocateRanges, which tells the server which blocks of the previous file to keep, and where to place them in the new file. The current format is a comma separated list terminated by LF. -
setRelocStream
Sets the list of RelocateRanges, which tells the server which blocks of the previous file to keep, and where to place them in the new file. The current format is a comma separated list terminated by LF.- Parameters:
relocStream-
-
getDataStream
Gets the list of uploaded data chunks ( byte Ranges and their associated data ). -
setDataStream
Sets the list of data chunks to be uploaded ( byte Ranges and their associated data ). The stream should contain no leading whitespace.
-