• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 com.android.storage.io.read;
18 
19 import com.android.storage.block.read.BlockData;
20 
21 import java.io.BufferedInputStream;
22 import java.io.Closeable;
23 import java.io.DataInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 
27 /**
28  * A typed stream-based typed data accessor.
29  *
30  * See {@link BlockData} for a random-access equivalent.
31  */
32 public final class TypedInputStream implements Closeable {
33 
34     private final DataInputStream mDataInputStream;
35 
36     /** Wraps an InputStream. */
TypedInputStream(InputStream is)37     public TypedInputStream(InputStream is) {
38         mDataInputStream = new DataInputStream(new BufferedInputStream(is, 8192));
39     }
40 
41     /** Skip forward the specified number of bytes. */
skipBytes(int n)42     public int skipBytes(int n) throws IOException {
43         return mDataInputStream.skipBytes(n);
44     }
45 
46     /** Reads the next byte as an unsigned value. */
readUnsignedByte()47     public int readUnsignedByte() throws IOException {
48         return mDataInputStream.readByte() & 0xFF;
49     }
50 
51     /** Reads the next byte as a signed value. */
readSignedByte()52     public byte readSignedByte() throws IOException {
53         return mDataInputStream.readByte();
54     }
55 
56     /**
57      * Reads a tiny (<= 255 entries) array of signed bytes where the length is encoded in the
58      * data.
59      */
readTinyVarByteArray()60     public byte[] readTinyVarByteArray() throws IOException {
61         int arraySize = readUnsignedByte();
62         byte[] bytes = new byte[arraySize];
63         for (int i = 0; i < bytes.length; i++) {
64             bytes[i] = (byte) mDataInputStream.readUnsignedByte();
65         }
66         return bytes;
67     }
68 
69     /**
70      * Reads a tiny (<= 255 entries) array of chars where the length is encoded in the data.
71      */
readTinyVarCharArray()72     public char[] readTinyVarCharArray() throws IOException {
73         int arraySize = readUnsignedByte();
74         char[] chars = new char[arraySize];
75         for (int i = 0; i < chars.length; i++) {
76             chars[i] = mDataInputStream.readChar();
77         }
78         return chars;
79     }
80 
81     /**
82      * Reads a char in network byte order.
83      */
readChar()84     public char readChar() throws IOException {
85         return mDataInputStream.readChar();
86     }
87 
88     /**
89      * Reads a signed int in network byte order.
90      */
readInt()91     public int readInt() throws IOException {
92         return mDataInputStream.readInt();
93     }
94 
95     /**
96      * Reads a signed long in network byte order.
97      */
readLong()98     public long readLong() throws IOException {
99         return mDataInputStream.readLong();
100     }
101 
102     @Override
close()103     public void close() throws IOException {
104         mDataInputStream.close();
105     }
106 }
107