ReplaceFileCorruptionHandler


public final class ReplaceFileCorruptionHandler<T extends Object>


A corruption handler that attempts to replace the on-disk data with data from produceNewData.

If the handler successfully replaces the data, the original exception is swallowed. If the handler encounters an exception when attempting to replace data, the new exception is added as a suppressed exception to the original exception and the original exception is thrown.

Summary

Public constructors

<T extends Object> ReplaceFileCorruptionHandler(
    @NonNull Function1<@NonNull CorruptionException, @NonNull T> produceNewData
)

Public methods

@NonNull T

This function will be called by DataStore when it encounters corruption.

Public constructors

ReplaceFileCorruptionHandler

public <T extends Object> ReplaceFileCorruptionHandler(
    @NonNull Function1<@NonNull CorruptionException, @NonNull T> produceNewData
)
Parameters
@NonNull Function1<@NonNull CorruptionException, @NonNull T> produceNewData

The provided callback returns the data to be written to disk. If the callback fails, nothing will be written to disk. Since the exception will be swallowed after writing the data, this is a good place to log the exception.

Public methods

handleCorruption

public @NonNullhandleCorruption(@NonNull CorruptionException ex)

This function will be called by DataStore when it encounters corruption. If the implementation of this function throws an exception, it will be propagated to the original call to DataStore. Otherwise, the returned data will be written to disk.

This function should not interact with any DataStore API - doing so can result in a deadlock.

Parameters
@NonNull CorruptionException ex

is the exception encountered when attempting to deserialize data from disk.

Returns
@NonNull T

The value that DataStore should attempt to write to disk.