Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2017 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 @file:Suppress("NOTHING_TO_INLINE") // Aliases to other public API.
     18 
     19 package androidx.core.util
     20 
     21 import android.util.AtomicFile
     22 import androidx.annotation.RequiresApi
     23 import java.io.FileOutputStream
     24 import java.nio.charset.Charset
     25 
     26 /**
     27  * Perform the write operations inside [block] on this file. If [block] throws an exception the
     28  * write will be failed. Otherwise the write will be applied atomically to the file.
     29  */
     30 @RequiresApi(17)
     31 inline fun AtomicFile.tryWrite(block: (out: FileOutputStream) -> Unit) {
     32     val stream = startWrite()
     33     var success = false
     34     try {
     35         block(stream)
     36         success = true
     37     } finally {
     38         if (success) {
     39             finishWrite(stream)
     40         } else {
     41             failWrite(stream)
     42         }
     43     }
     44 }
     45 
     46 /**
     47  * Sets the content of this file as an [array] of bytes.
     48  */
     49 @RequiresApi(17)
     50 fun AtomicFile.writeBytes(array: ByteArray) {
     51     tryWrite {
     52         it.write(array)
     53     }
     54 }
     55 
     56 /**
     57  * Sets the content of this file as [text] encoded using UTF-8 or specified [charset].
     58  * If this file exists, it becomes overwritten.
     59  */
     60 @RequiresApi(17)
     61 fun AtomicFile.writeText(text: String, charset: Charset = Charsets.UTF_8) {
     62     writeBytes(text.toByteArray(charset))
     63 }
     64 
     65 /**
     66  * Gets the entire content of this file as a byte array.
     67  *
     68  * This method is not recommended on huge files. It has an internal limitation of 2 GB file size.
     69  */
     70 @RequiresApi(17)
     71 inline fun AtomicFile.readBytes(): ByteArray = readFully()
     72 
     73 /**
     74  * Gets the entire content of this file as a String using UTF-8 or specified [charset].
     75  *
     76  * This method is not recommended on huge files. It has an internal limitation of 2 GB file size.
     77  */
     78 @RequiresApi(17)
     79 fun AtomicFile.readText(charset: Charset = Charsets.UTF_8): String {
     80     return readFully().toString(charset)
     81 }
     82