• 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.samples;
17 
18 import android.net.Uri;
19 import com.google.android.libraries.mobiledatadownload.file.common.Sizable;
20 import com.google.android.libraries.mobiledatadownload.file.spi.Transform;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import javax.annotation.Nullable;
25 
26 /**
27  * This is a toy transform that is useful to illustrate that the invocation order is correct when
28  * combined with a base64 transform.
29  */
30 public final class CapitalizationTransform implements Transform {
31 
32   @Override
name()33   public String name() {
34     return "capitalize";
35   }
36 
37   @Override
wrapForRead(Uri uri, InputStream wrapped)38   public InputStream wrapForRead(Uri uri, InputStream wrapped) {
39     return new CapitalizationInputStream(wrapped);
40   }
41 
42   @SuppressWarnings("InputStreamSlowMultibyteRead")
43   // NOTE: Not bothering to override read(byte[],int,int) b/c this transform
44   // is never intended to be used for anything besides an example.
45   private static class CapitalizationInputStream extends InputStream implements Sizable {
46 
47     private final InputStream in;
48 
CapitalizationInputStream(InputStream in)49     private CapitalizationInputStream(InputStream in) {
50       this.in = in;
51     }
52 
53     @Override
read()54     public int read() throws IOException {
55       int b = in.read();
56       if (b == -1) {
57         return b;
58       }
59       return Character.toString((char) b).toUpperCase().charAt(0);
60     }
61 
62     @Override
size()63     public @Nullable Long size() throws IOException {
64       if (!(in instanceof Sizable)) {
65         return null;
66       }
67       return ((Sizable) in).size();
68     }
69   }
70 
71   @Override
wrapForWrite(Uri uri, OutputStream wrapped)72   public OutputStream wrapForWrite(Uri uri, OutputStream wrapped) {
73     OutputStream stream =
74         new OutputStream() {
75           @Override
76           public void write(int b) throws IOException {
77             wrapped.write(Character.toString((char) b).toLowerCase().charAt(0));
78           }
79 
80           @Override
81           public void close() throws IOException {
82             wrapped.close();
83           }
84         };
85     return stream;
86   }
87 
88   @Override
wrapForAppend(Uri uri, OutputStream wrapped)89   public OutputStream wrapForAppend(Uri uri, OutputStream wrapped) throws IOException {
90     return wrapForWrite(uri, wrapped);
91   }
92 
93   @Override
encode(Uri uri, String filename)94   public String encode(Uri uri, String filename) {
95     return filename.toLowerCase();
96   }
97 
98   @Override
decode(Uri uri, String filename)99   public String decode(Uri uri, String filename) {
100     return filename.toUpperCase();
101   }
102 }
103