• 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 com.google.android.libraries.mobiledatadownload.file.Behavior;
19 import com.google.android.libraries.mobiledatadownload.file.OpenContext;
20 import com.google.android.libraries.mobiledatadownload.file.Opener;
21 import com.google.android.libraries.mobiledatadownload.file.common.internal.Charsets;
22 import com.google.errorprone.annotations.CanIgnoreReturnValue;
23 import java.io.IOException;
24 import java.nio.charset.Charset;
25 
26 /** An opener that reads entire contents of file into a String. */
27 public final class WriteStringOpener implements Opener<Void> {
28   private final String string;
29   private Charset charset = Charsets.UTF_8;
30   private Behavior[] behaviors;
31 
WriteStringOpener(String string)32   WriteStringOpener(String string) {
33     this.string = string;
34   }
35 
create(String string)36   public static WriteStringOpener create(String string) {
37     return new WriteStringOpener(string);
38   }
39 
40   @CanIgnoreReturnValue
withCharset(Charset charset)41   public WriteStringOpener withCharset(Charset charset) {
42     this.charset = charset;
43     return this;
44   }
45 
46   @CanIgnoreReturnValue
withBehaviors(Behavior... behaviors)47   public WriteStringOpener withBehaviors(Behavior... behaviors) {
48     this.behaviors = behaviors;
49     return this;
50   }
51 
52   @Override
open(OpenContext openContext)53   public Void open(OpenContext openContext) throws IOException {
54     WriteByteArrayOpener.create(string.getBytes(charset))
55         .withBehaviors(behaviors)
56         .open(openContext);
57     return null;
58   }
59 }
60