public final class

FileDataProvider

extends Object
implements WritableDataProvider
java.lang.Object
   ↳ com.pspdfkit.api.providers.FileDataProvider

Class Overview

A DataProvider for a standard filesystem file .

Summary

[Expand]
Inherited Constants
From interface com.pspdfkit.api.providers.DataProvider
Public Constructors
FileDataProvider(File file)
Creates an DataProvider to allow PSPDFKit to be able to read from a File.
Public Methods
boolean canWrite()
Indicates whether this data provider is writable.
boolean finishWrite()
Called by PSPDFKit after all data has been written via write(byte[]) method.
File getFile()
Get the File from the data provider.
long getSize()
Return the actual file size of the data source which is provided by this data provider.
String getTitle()
Displayable document title to be used if PDF document does not contain metadata.
byte[] read(long size, long offset)
Called by PSPDFKit to read data from the document.
void release()
Called when the provided document is being closed.
boolean startWrite(WritableDataProvider.WriteMode writeMode)
Called before writing to the source begins so the provider can prepare data output.
boolean supportsAppending()
Reports whether this provider supports appending to output file as opposed to always rewriting it.
boolean write(byte[] data)
Called by PSPDFKit to write data to the document.
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.pspdfkit.api.providers.DataProvider
From interface com.pspdfkit.api.providers.WritableDataProvider

Public Constructors

public FileDataProvider (File file)

Creates an DataProvider to allow PSPDFKit to be able to read from a File.

Parameters
file The file to read.

Public Methods

public boolean canWrite ()

Indicates whether this data provider is writable.

Returns
  • true if the data provider can be written to, false if not.

public boolean finishWrite ()

Called by PSPDFKit after all data has been written via write(byte[]) method. This allows the data provider to close all opened input streams. In case of REWRITE access, the data provider should here replace the original data with the written temporary data.

Returns
  • true if operation succeeded, false if it failed.

public File getFile ()

Get the File from the data provider. Allows for direct file IO access in PSPDFKit.

Returns

public long getSize ()

Return the actual file size of the data source which is provided by this data provider. If the data provider can't determine the correct size (e.g. in case of an error) this method has to return FILE_SIZE_UNKNOWN.

Returns
  • Returns size of PDF document data in bytes, or FILE_SIZE_UNKNOWN in case of an error.

public String getTitle ()

Displayable document title to be used if PDF document does not contain metadata. For example, for file-based providers this should return the filename. `.pdf` extensions will be stripped automatically.

Returns
  • Fallback document title if PDF document does not contain the required metadata. May be null.

public byte[] read (long size, long offset)

Called by PSPDFKit to read data from the document. The returned byte array can be reused in subsequent calls to prevent excessive allocations.

Parameters
size Size of the data chunk to be read in bytes.
offset Offset from start of document of the data chunk to be read in bytes.
Returns
  • Byte array with document data of the exact same size or larger size than size parameter.

public void release ()

Called when the provided document is being closed. Implementations are expected to release all resources (like closing of all streams and freeing of allocations).

public boolean startWrite (WritableDataProvider.WriteMode writeMode)

Called before writing to the source begins so the provider can prepare data output.

Parameters
writeMode Determines how the data should be written to the output. For APPEND the data should be added to the end of existing file data. For REWRITE PSPDFKit will write full data load and the data should be written to a temporary location. The temporary location should replace then original data in finishWrite() callback method. Original data must not be written to when rewriting is in progress - PSPDFKit will issue read(long, long) calls while writing and attempting to write output to the same input will result in a corrupted file.
Returns
  • true if operation succeeded, false if writing should be aborted.

public boolean supportsAppending ()

Reports whether this provider supports appending to output file as opposed to always rewriting it. If this returns false, only REWRITE will be used and the data will always be fully rewritten on save.

Note that if true is returned here, both WritableDataProvider.WriteMode options must be supported by this DataProvider since some data cannot be appended.

Returns
  • true if this provider supports APPEND save mode.

public boolean write (byte[] data)

Called by PSPDFKit to write data to the document. This callback will be called multiple times in a writing operation.

Parameters
data Data to be written to the file.
Returns
  • true if writing succeeded, false if the write operation should be aborted.