Class DefaultFileChunkedPacketHandler
java.lang.Object
eu.cloudnetservice.driver.network.chunk.defaults.DefaultChunkedPacketProvider
eu.cloudnetservice.driver.network.chunk.defaults.DefaultFileChunkedPacketHandler
- All Implemented Interfaces:
ChunkedPacketHandler,ChunkedPacketProvider
public class DefaultFileChunkedPacketHandler
extends DefaultChunkedPacketProvider
implements ChunkedPacketHandler
Represents a handler for a chunked packet transfer which transfers a file.
- Since:
- 4.0
-
Nested Class Summary
Nested classes/interfaces inherited from interface eu.cloudnetservice.driver.network.chunk.ChunkedPacketHandler
ChunkedPacketHandler.Callback -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Integerprotected final Lockprotected final RandomAccessFileprotected final Pathprotected final ChunkedPacketHandler.Callbackprotected intFields inherited from class eu.cloudnetservice.driver.network.chunk.defaults.DefaultChunkedPacketProvider
chunkSessionInformation, transferStatus -
Constructor Summary
ConstructorsConstructorDescriptionDefaultFileChunkedPacketHandler(@NonNull ChunkSessionInformation sessionInformation, @Nullable ChunkedPacketHandler.Callback completeHandler) Creates the session handler initially.DefaultFileChunkedPacketHandler(@NonNull ChunkSessionInformation sessionInformation, @Nullable ChunkedPacketHandler.Callback completeHandler, @NonNull Path tempFilePath) Creates the session handler initially. -
Method Summary
Modifier and TypeMethodDescriptioncallback()Get the callback which will be called when the full data is received and available for further processing.booleanhandleChunkPart(int chunkPosition, @NonNull DataBuf dataBuf) Handles a part of chunked data which gets supplied to the current component.protected voidUpdates the status of the file transfer.protected voidwritePacketContent(int chunkPosition, @NonNull DataBuf dataBuf) Writes the content of a chunk part to the backing file, increasing the amount of written bytes by one.Methods inherited from class eu.cloudnetservice.driver.network.chunk.defaults.DefaultChunkedPacketProvider
sessionInformation, transferStatusMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface eu.cloudnetservice.driver.network.chunk.ChunkedPacketProvider
sessionInformation, transferStatus
-
Field Details
-
tempFilePath
-
targetFile
-
writeCompleteHandler
-
lock
-
writtenFileParts
protected int writtenFileParts -
expectedFileParts
-
-
Constructor Details
-
DefaultFileChunkedPacketHandler
public DefaultFileChunkedPacketHandler(@NonNull @NonNull ChunkSessionInformation sessionInformation, @Nullable @Nullable ChunkedPacketHandler.Callback completeHandler) Creates the session handler initially. Sessions should be manged by some sort of handler which is responsible for handling incoming chunk parts as well.- Parameters:
sessionInformation- the information transferred by the sender initially.completeHandler- the handler to call when the file transfer finished successfully.- Throws:
NullPointerException- if the given session information is null.
-
DefaultFileChunkedPacketHandler
public DefaultFileChunkedPacketHandler(@NonNull @NonNull ChunkSessionInformation sessionInformation, @Nullable @Nullable ChunkedPacketHandler.Callback completeHandler, @NonNull @NonNull Path tempFilePath) Creates the session handler initially. Sessions should be manged by some sort of handler which is responsible for handling incoming chunk parts as well.- Parameters:
sessionInformation- the information transferred by the sender initially.completeHandler- the handler to call when the file transfer finished successfully.tempFilePath- the path to the temp file to write the received data to.- Throws:
NullPointerException- if the given session information or temp path is null.
-
-
Method Details
-
handleChunkPart
Handles a part of chunked data which gets supplied to the current component. The data chunk is required to be from the same size as announced in the initial transfer information, unless the packet is the last packet of the chunked data stream.- Specified by:
handleChunkPartin interfaceChunkedPacketHandler- Parameters:
chunkPosition- the position of the chunk, starting from 0.dataBuf- the data in the chunk.- Returns:
- true, if the chunk was the last chunk and the callback was called, false otherwise or in case of a failure.
-
callback
Get the callback which will be called when the full data is received and available for further processing.- Specified by:
callbackin interfaceChunkedPacketHandler- Returns:
- the callback which will be called when the full data is received.
-
writePacketContent
protected void writePacketContent(int chunkPosition, @NonNull @NonNull DataBuf dataBuf) throws IOException Writes the content of a chunk part to the backing file, increasing the amount of written bytes by one.- Parameters:
chunkPosition- the index of the chunk to write.dataBuf- the buf transferred to this handler, the next content should be the actual chunk data.- Throws:
IOException- if an i/o error occurs during the chunk write.NullPointerException- if the given buffer is null.
-
updateStatus
protected void updateStatus()Updates the status of the file transfer. This method will set the status to completed when:- The current status is
RUNNING. - The amount of chunk parts of the transfer is known.
- The amount of written chunk parts matches the amount of expected chunk parts.
- The current status is
-