• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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 package com.google.android.libraries.mobiledatadownload.file.openers;
17 
18 import android.net.Uri;
19 import com.google.android.libraries.mobiledatadownload.file.Behavior;
20 import com.google.android.libraries.mobiledatadownload.file.OpenContext;
21 import com.google.android.libraries.mobiledatadownload.file.Opener;
22 import com.google.errorprone.annotations.CanIgnoreReturnValue;
23 import com.google.protobuf.MessageLite;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.util.List;
28 
29 /** Writes a proto to a file atomically. */
30 public final class WriteProtoOpener implements Opener<Void> {
31 
32   private final MessageLite proto;
33   private Behavior[] behaviors;
34 
WriteProtoOpener(MessageLite proto)35   private WriteProtoOpener(MessageLite proto) {
36     this.proto = proto;
37   }
38 
create(MessageLite proto)39   public static WriteProtoOpener create(MessageLite proto) {
40     return new WriteProtoOpener(proto);
41   }
42 
43   /**
44    * Supports adding options to writes. For example, SyncBehavior will force data to be flushed and
45    * durably persisted.
46    */
47   @CanIgnoreReturnValue
withBehaviors(Behavior... behaviors)48   public WriteProtoOpener withBehaviors(Behavior... behaviors) {
49     this.behaviors = behaviors;
50     return this;
51   }
52 
53   @Override
open(OpenContext openContext)54   public Void open(OpenContext openContext) throws IOException {
55     Uri tempUri = ScratchFile.scratchUri(openContext.encodedUri());
56     OutputStream backendOutput = openContext.backend().openForWrite(tempUri);
57     List<OutputStream> chain = openContext.chainTransformsForWrite(backendOutput);
58     if (behaviors != null) {
59       for (Behavior behavior : behaviors) {
60         behavior.forOutputChain(chain);
61       }
62     }
63     try (OutputStream out = chain.get(0)) {
64       proto.writeTo(out);
65       if (behaviors != null) {
66         for (Behavior behavior : behaviors) {
67           behavior.commit();
68         }
69       }
70     } catch (Exception ex) {
71       try {
72         openContext.backend().deleteFile(tempUri);
73       } catch (FileNotFoundException ex2) {
74         // Ignore.
75       }
76       if (ex instanceof IOException) {
77         throw (IOException) ex;
78       }
79       throw new IOException(ex);
80     }
81     openContext.backend().rename(tempUri, openContext.encodedUri());
82     return null;
83   }
84 }
85