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
  • Field Details

    • tempFilePath

      protected final Path tempFilePath
    • targetFile

      protected final RandomAccessFile targetFile
    • writeCompleteHandler

      protected final ChunkedPacketHandler.Callback writeCompleteHandler
    • lock

      protected final Lock lock
    • writtenFileParts

      protected int writtenFileParts
    • expectedFileParts

      protected Integer 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

      public boolean handleChunkPart(int chunkPosition, @NonNull @NonNull DataBuf dataBuf)
      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:
      handleChunkPart in interface ChunkedPacketHandler
      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:
      callback in interface ChunkedPacketHandler
      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:
      1. The current status is RUNNING.
      2. The amount of chunk parts of the transfer is known.
      3. The amount of written chunk parts matches the amount of expected chunk parts.