/ app / src / main / java / com / reandroid / archive / BlockInputSource.java
BlockInputSource.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 com.reandroid.arsc.base.Block;
19  import com.reandroid.arsc.base.BlockRefresh;
20  import com.reandroid.utils.CRCDigest;
21  
22  import java.io.IOException;
23  import java.io.OutputStream;
24  
25  public class BlockInputSource<T extends Block> extends ByteInputSource {
26  
27      private final T mBlock;
28  
29      public BlockInputSource(String name, T block) {
30          super(new byte[0], name);
31          this.mBlock=block;
32      }
33      public BlockInputSource(InputSource inputSource, T block) {
34          this(inputSource.getAlias(), block);
35          setMethod(inputSource.getMethod());
36          setSort(inputSource.getSort());
37      }
38  
39      public T getBlock() {
40          T block = this.mBlock;
41          if(block instanceof BlockRefresh){
42              ((BlockRefresh) block).refresh();
43          }
44          return block;
45      }
46      @Override
47      public long getLength() throws IOException{
48          Block block = getBlock();
49          return block.countBytes();
50      }
51      @Override
52      public long getCrc() throws IOException{
53          Block block = getBlock();
54          CRCDigest outputStream = new CRCDigest();
55          block.writeBytes(outputStream);
56          return outputStream.getValue();
57      }
58      @Override
59      public long write(OutputStream outputStream) throws IOException {
60          return getBlock().writeBytes(outputStream);
61      }
62      @Override
63      public byte[] getBytes() {
64          return getBlock().getBytes();
65      }
66  }