1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.datastore.core.okio
18 
19 import okio.BufferedSink
20 import okio.BufferedSource
21 
22 /**
23  * The OkioSerializer determines the on-disk format and API for accessing it.
24  *
25  * The type T MUST be immutable. Mutable types will result in broken DataStore functionality.
26  */
27 public interface OkioSerializer<T> {
28 
29     /** Value to return if there is no data on disk. */
30     public val defaultValue: T
31 
32     /**
33      * Unmarshal object from source.
34      *
35      * @param source the BufferedSource with the data to deserialize
36      * @throws androidx.datastore.core.CorruptionException if the data from [input] is corrupted
37      *   and/or unparseable, e.g. [InvalidProtocolBufferException] when the type [T] is a protobuf
38      *   message and it is corrupted. Other unrecoverable [IOException] from the file system should
39      *   not be thrown as [CorruptionException].
40      */
readFromnull41     public suspend fun readFrom(source: BufferedSource): T
42 
43     /**
44      * Marshal object to a Sink.
45      *
46      * @param t the data to write to output
47      * @param sink the BufferedSink to serialize data to
48      */
49     public suspend fun writeTo(t: T, sink: BufferedSink)
50 }
51