Home | History | Annotate | Download | only in apk
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      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 
     17 package android.util.apk;
     18 
     19 import java.io.IOException;
     20 import java.nio.ByteBuffer;
     21 import java.security.DigestException;
     22 
     23 /**
     24  * {@link DataSource} which provides data from a {@link ByteBuffer}.
     25  */
     26 class ByteBufferDataSource implements DataSource {
     27     /**
     28      * Underlying buffer. The data is stored between position 0 and the buffer's capacity.
     29      * The buffer's position is 0 and limit is equal to capacity.
     30      */
     31     private final ByteBuffer mBuf;
     32 
     33     ByteBufferDataSource(ByteBuffer buf) {
     34         // Defensive copy, to avoid changes to mBuf being visible in buf, and to ensure position is
     35         // 0 and limit == capacity.
     36         mBuf = buf.slice();
     37     }
     38 
     39     @Override
     40     public long size() {
     41         return mBuf.capacity();
     42     }
     43 
     44     @Override
     45     public void feedIntoDataDigester(DataDigester md, long offset, int size)
     46             throws IOException, DigestException {
     47         // There's no way to tell MessageDigest to read data from ByteBuffer from a position
     48         // other than the buffer's current position. We thus need to change the buffer's
     49         // position to match the requested offset.
     50         //
     51         // In the future, it may be necessary to compute digests of multiple regions in
     52         // parallel. Given that digest computation is a slow operation, we enable multiple
     53         // such requests to be fulfilled by this instance. This is achieved by serially
     54         // creating a new ByteBuffer corresponding to the requested data range and then,
     55         // potentially concurrently, feeding these buffers into MessageDigest instances.
     56         ByteBuffer region;
     57         synchronized (mBuf) {
     58             mBuf.position(0);
     59             mBuf.limit((int) offset + size);
     60             mBuf.position((int) offset);
     61             region = mBuf.slice();
     62         }
     63 
     64         md.consume(region);
     65     }
     66 }
     67