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.common.testing; 17 18 import android.net.Uri; 19 import com.google.android.libraries.mobiledatadownload.file.common.internal.ForwardingInputStream; 20 import com.google.android.libraries.mobiledatadownload.file.common.internal.ForwardingOutputStream; 21 import com.google.android.libraries.mobiledatadownload.file.spi.Transform; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.OutputStream; 25 26 /** A Transform that throws IOExceptions for any read or write operation. */ 27 public class AlwaysThrowsTransform implements Transform { 28 29 private static final String NAME = "alwaysthrows"; 30 31 @Override name()32 public String name() { 33 return NAME; 34 } 35 36 @Override wrapForRead(Uri uri, InputStream wrapped)37 public InputStream wrapForRead(Uri uri, InputStream wrapped) throws IOException { 38 return new ForwardingInputStream(wrapped) { 39 @Override 40 public int read(byte[] b, int off, int len) throws IOException { 41 throw new IOException("throwing"); 42 } 43 44 @Override 45 public int read(byte[] b) throws IOException { 46 throw new IOException("throwing"); 47 } 48 49 @Override 50 public int read() throws IOException { 51 throw new IOException("throwing"); 52 } 53 }; 54 } 55 56 @Override 57 public OutputStream wrapForWrite(Uri uri, OutputStream wrapped) throws IOException { 58 return new ForwardingOutputStream(wrapped) { 59 @Override 60 public void write(byte[] b) throws IOException { 61 throw new IOException("throwing"); 62 } 63 64 @Override 65 public void write(byte[] b, int off, int len) throws IOException { 66 throw new IOException("throwing"); 67 } 68 69 @Override 70 public void write(int b) throws IOException { 71 throw new IOException("throwing"); 72 } 73 }; 74 } 75 76 @Override 77 public OutputStream wrapForAppend(Uri uri, OutputStream wrapped) throws IOException { 78 return wrapForWrite(uri, wrapped); 79 } 80 } 81