Class PasteFileHandler

java.lang.Object
com.vaadin.flow.component.clipboard.PasteFileHandler
All Implemented Interfaces:
Serializable

public final class PasteFileHandler extends Object implements Serializable
Factory for paste-aware UploadHandlers that group the parallel fetch uploads of a single paste gesture and surface the result to application code. Two flavours are offered, both producing an UploadHandler suitable for Clipboard.onFilePaste:
  • perFile(SerializableConsumer) — a single per-file callback that receives every file as a PasteFile, with PasteFile.newPaste() flagging the first file of each paste.
  • batch() — a three-step batch listener: onStart fires once per paste with the declared file count, onFile fires per file, and onComplete fires once after the last file has been delivered.
Each paste runs as its own batch and completes on its own timeline: pastes that overlap in transit (the tail of an older paste's uploads arriving after a newer paste has started) still run through their own onStart / onFile / onComplete lifecycle, and no file is dropped on the application's behalf. Batch state is tracked per owning component, so it is released when the component is detached or the session ends rather than living in the handler — a handler instance shared across components or UIs cannot accumulate state. As a further bound, only a limited number of unfinished pastes are tracked per component at once: if a paste's declared Clipboard.PASTE_FILE_COUNT_HEADER count never fully arrives (a stalled upload, or a missing/garbage header), the oldest such batch is dropped once that many are open. The limit is far above any real overlap, so it only ever trims abandoned or malformed batches.

Application code that wants "show the latest paste only" semantics tracks the highest paste id seen and filters in its own callbacks — the handler does not drop any files of a live paste on the application's behalf.

Resource notes

  • perFile(SerializableConsumer) and the onFile step of batch() read each uploaded file fully into memory as a byte[] before invoking the callback. A very large paste is therefore held entirely in heap; cap it with the request/file size limits on a custom UploadHandler, or read the stream yourself, when that matters.
  • PasteFile.fileName() is the browser-supplied name and is not sanitized. Treat it as untrusted — never use it directly as a filesystem path without sanitizing it first.
Since:
25.2
See Also:
  • Method Details

    • perFile

      public static UploadHandler perFile(SerializableConsumer<PasteFile> consumer)
      Builds an UploadHandler that reads each upload fully into memory, packages it as a PasteFile, and delivers it to the supplied consumer on the UI thread.
      Parameters:
      consumer - invoked for each accepted file on the UI thread, not null
      Returns:
      an upload handler suitable for Clipboard.onFilePaste
    • batch

      public static PasteFileHandler.BatchBuilder batch()
      Starts a batch handler builder that emits onStart once per paste before any file, onFile per file, and onComplete once the paste's declared file count has been delivered. Any callback may be omitted.
      Returns:
      a fresh, unconfigured batch builder