RenamedInputSource.java
1 /* 2 * Copyright (C) 2022 github.com/REAndroid 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.reandroid.archive; 17 18 import java.io.File; 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.io.OutputStream; 22 23 public class RenamedInputSource<T extends InputSource> extends InputSource { 24 private final T inputSource; 25 public RenamedInputSource(String name, T input){ 26 super(name); 27 this.inputSource=input; 28 super.setMethod(input.getMethod()); 29 super.setSort(input.getSort()); 30 } 31 32 @SuppressWarnings("unchecked") 33 public<T1 extends InputSource> T1 getParentInputSource(Class<T1> instance){ 34 InputSource inputSource = getInputSource(); 35 if(instance.isInstance(inputSource)){ 36 return (T1) inputSource; 37 } 38 if(inputSource instanceof RenamedInputSource){ 39 RenamedInputSource<?> parent = (RenamedInputSource<?>) inputSource; 40 return parent.getParentInputSource(instance); 41 } 42 return null; 43 } 44 public T getInputSource() { 45 return inputSource; 46 } 47 @Override 48 public void close(InputStream inputStream) throws IOException { 49 getInputSource().close(inputStream); 50 } 51 @Override 52 public long getLength() throws IOException { 53 return getInputSource().getLength(); 54 } 55 @Override 56 public long getCrc() throws IOException { 57 return getInputSource().getCrc(); 58 } 59 @Override 60 public void write(File file) throws IOException { 61 getInputSource().write(file); 62 } 63 @Override 64 public long write(OutputStream outputStream) throws IOException { 65 return getInputSource().write(outputStream); 66 } 67 @Override 68 public InputStream openStream() throws IOException { 69 return getInputSource().openStream(); 70 } 71 }