• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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 android.util;
18 
19 import java.io.BufferedOutputStream;
20 import java.io.IOException;
21 
22 /**
23  * {@link BufferedOutputStream} for {@link AtomicFile}.
24  * Allows user-code to write into file output stream backed by {@link AtomicFile}.
25  * In order to "commit" the new content to the file, call {@link #markSuccess()} then
26  * {@link #close()}. Calling{@link #markSuccess()} alone won't update the file.
27  * This class does not confer any file locking semantics. Do not use this class when the file may be
28  * accessed or modified concurrently by multiple threads or processes. The caller is responsible for
29  * ensuring appropriate mutual exclusion invariants whenever it accesses the file.
30  * @hide
31  */
32 @android.ravenwood.annotation.RavenwoodKeepWholeClass
33 public class AtomicFileBufferedOutputStream extends BufferedOutputStream implements AutoCloseable {
34     private static final String TAG = "AtomicFileBufferedOutputStream";
35     private final AtomicFileOutputStream mAtomicFileOutputStream;
36 
37     /**
38      * See {@link AtomicFileOutputStream#AtomicFileOutputStream(AtomicFile)}.
39      */
AtomicFileBufferedOutputStream(AtomicFile file)40     public AtomicFileBufferedOutputStream(AtomicFile file) throws IOException {
41         this(new AtomicFileOutputStream(file));
42     }
43 
AtomicFileBufferedOutputStream(AtomicFileOutputStream atomicFileOutputStream)44     private AtomicFileBufferedOutputStream(AtomicFileOutputStream atomicFileOutputStream) {
45         super(atomicFileOutputStream);
46         mAtomicFileOutputStream = atomicFileOutputStream;
47     }
48 
49     /**
50      * See {@link AtomicFile#startWrite()} with specific buffer size.
51      */
AtomicFileBufferedOutputStream(AtomicFile file, int bufferSize)52     public AtomicFileBufferedOutputStream(AtomicFile file, int bufferSize) throws IOException {
53         this(new AtomicFileOutputStream(file), bufferSize);
54     }
55 
AtomicFileBufferedOutputStream(AtomicFileOutputStream atomicFileOutputStream, int bufferSize)56     private AtomicFileBufferedOutputStream(AtomicFileOutputStream atomicFileOutputStream,
57             int bufferSize) {
58         super(atomicFileOutputStream, bufferSize);
59         mAtomicFileOutputStream = atomicFileOutputStream;
60     }
61 
62     /**
63      * Flushes output stream and marks the writing as finished.
64      */
markSuccess()65     public void markSuccess() throws IOException {
66         flush();
67         mAtomicFileOutputStream.markSuccess();
68     }
69 
70     /**
71      * Creates string representation of the object.
72      */
73     @Override
toString()74     public String toString() {
75         return TAG + "[" + mAtomicFileOutputStream + "]";
76     }
77 }
78