Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b14200a2 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Optimise input stack for better performance using buffers."

parents e1529828 8a0b6fcd
Loading
Loading
Loading
Loading
+10 −12
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.util.Slog;

import com.android.internal.annotations.VisibleForTesting;
import com.android.server.integrity.model.RuleMetadata;
import com.android.server.integrity.parser.RandomAccessObject;
import com.android.server.integrity.parser.RuleBinaryParser;
import com.android.server.integrity.parser.RuleIndexRange;
import com.android.server.integrity.parser.RuleIndexingController;
@@ -64,10 +65,8 @@ public class IntegrityFileManager {
    // update rules atomically.
    private final File mStagingDir;

    @Nullable
    private RuleMetadata mRuleMetadataCache;
    @Nullable
    private RuleIndexingController mRuleIndexingController;
    @Nullable private RuleMetadata mRuleMetadataCache;
    @Nullable private RuleIndexingController mRuleIndexingController;

    /** Get the singleton instance of this class. */
    public static synchronized IntegrityFileManager getInstance() {
@@ -164,13 +163,12 @@ public class IntegrityFileManager {
            }

            // Read the rules based on the index information when available.
            try (FileInputStream inputStream =
                         new FileInputStream(new File(mRulesDir, RULES_FILE))) {
                List<Rule> rules = mRuleParser.parse(inputStream, ruleReadingIndexes);
            File ruleFile = new File(mRulesDir, RULES_FILE);
            List<Rule> rules =
                    mRuleParser.parse(RandomAccessObject.ofFile(ruleFile), ruleReadingIndexes);
            return rules;
        }
    }
    }

    /** Read the metadata of the current rules in storage. */
    @Nullable
+17 −34
Original line number Diff line number Diff line
@@ -19,26 +19,21 @@ package com.android.server.integrity.model;
import java.io.IOException;
import java.io.InputStream;

/** A wrapper class for reading a stream of bits. */
/** A wrapper class for reading a stream of bits.
 *
 * <p>Note: this class reads from underlying stream byte-by-byte. It is advised to apply buffering
 * to underlying streams.
 */
public class BitInputStream {

    private long mBitPointer;
    private boolean mReadFromStream;
    private long mBitsRead;

    private byte[] mRuleBytes;
    private InputStream mRuleInputStream;
    private InputStream mInputStream;

    private byte mCurrentRuleByte;

    public BitInputStream(byte[] ruleBytes) {
        this.mRuleBytes = ruleBytes;
        this.mBitPointer = 0;
        this.mReadFromStream = false;
    }
    private byte mCurrentByte;

    public BitInputStream(InputStream ruleInputStream) {
        this.mRuleInputStream = ruleInputStream;
        this.mReadFromStream = true;
    public BitInputStream(InputStream inputStream) {
        mInputStream = inputStream;
    }

    /**
@@ -52,15 +47,15 @@ public class BitInputStream {
        int count = 0;

        while (count++ < numOfBits) {
            if (mBitPointer % 8 == 0) {
                mCurrentRuleByte = getNextByte();
            if (mBitsRead % 8 == 0) {
                mCurrentByte = getNextByte();
            }
            int offset = 7 - (int) (mBitPointer % 8);
            int offset = 7 - (int) (mBitsRead % 8);

            component <<= 1;
            component |= (mCurrentRuleByte >>> offset) & 1;
            component |= (mCurrentByte >>> offset) & 1;

            mBitPointer++;
            mBitsRead++;
        }

        return component;
@@ -68,22 +63,10 @@ public class BitInputStream {

    /** Check if there are bits left in the stream. */
    public boolean hasNext() throws IOException {
        if (mReadFromStream) {
            return mRuleInputStream.available() > 0;
        } else {
            return mBitPointer / 8 < mRuleBytes.length;
        }
        return mInputStream.available() > 0;
    }

    private byte getNextByte() throws IOException {
        if (mReadFromStream) {
            return (byte) mRuleInputStream.read();
        } else {
            int idx = (int) (mBitPointer / 8);
            if (idx >= mRuleBytes.length) {
                throw new IllegalArgumentException(String.format("Invalid byte index: %d", idx));
            }
            return mRuleBytes[idx];
        }
        return (byte) mInputStream.read();
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.integrity.model;

import static com.android.server.integrity.model.ComponentBitSize.BYTE_BITS;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
@@ -24,7 +26,6 @@ import java.util.Arrays;
public class BitOutputStream {

    private static final int BUFFER_SIZE = 4 * 1024;
    private static final int BYTE_BITS = 8;

    private int mNextBitIndex;

+0 −76
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.integrity.model;

import java.io.IOException;
import java.io.InputStream;

/**
 * An input stream that tracks the total number read bytes since construction and allows moving
 * fast forward to a certain byte any time during the execution.
 *
 * This class is used for efficient reading of rules based on the rule indexing.
 */
public class BitTrackedInputStream extends BitInputStream {

    private int mReadBitsCount;

    /** Constructor with byte array. */
    public BitTrackedInputStream(byte[] inputStream) {
        super(inputStream);
        mReadBitsCount = 0;
    }

    /** Constructor with input stream. */
    public BitTrackedInputStream(InputStream inputStream) {
        super(inputStream);
        mReadBitsCount = 0;
    }

    /** Obtains an integer value of the next {@code numOfBits}. */
    @Override
    public int getNext(int numOfBits) throws IOException {
        mReadBitsCount += numOfBits;
        return super.getNext(numOfBits);
    }

    /** Returns the current cursor position showing the number of bits that are read. */
    public int getReadBitsCount() {
        return mReadBitsCount;
    }

    /**
     * Returns true if we can read more rules by checking whether the end index is not reached yet.
     */
    public boolean canReadMoreRules(int endIndexBytes) {
        return mReadBitsCount < endIndexBytes * 8;
    }

    /**
     * Sets the cursor to the specified byte location.
     *
     * Note that the integer parameter specifies the location in bytes -- not bits.
     */
    public void setCursorToByteLocation(int byteLocation) throws IOException {
        int bitCountToRead = byteLocation * 8 - mReadBitsCount;
        if (bitCountToRead < 0) {
            throw new IllegalStateException("The byte position is already read.");
        }
        super.getNext(bitCountToRead);
        mReadBitsCount = byteLocation * 8;
    }
}
+2 −5
Original line number Diff line number Diff line
@@ -27,8 +27,6 @@ import java.io.OutputStream;
 */
public class ByteTrackedOutputStream extends OutputStream {

    private static final int INT_BYTES = 4;

    private int mWrittenBytesCount;
    private final OutputStream mOutputStream;

@@ -39,7 +37,7 @@ public class ByteTrackedOutputStream extends OutputStream {

    @Override
    public void write(int b) throws IOException {
        mWrittenBytesCount += INT_BYTES;
        mWrittenBytesCount++;
        mOutputStream.write(b);
    }

@@ -49,8 +47,7 @@ public class ByteTrackedOutputStream extends OutputStream {
     */
    @Override
    public void write(byte[] bytes) throws IOException {
        mWrittenBytesCount += bytes.length;
        mOutputStream.write(bytes);
        write(bytes, 0, bytes.length);
    }

    @Override
Loading