1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.conscrypt;
19
20import java.io.IOException;
21import java.io.InputStream;
22
23/**
24 * This class is a base for all input stream classes used
25 * in protocol implementation. It extends an InputStream with
26 * some additional read methods allowing to read TLS specific
27 * data types such as uint8, uint32 etc (see TLS v 1 specification
28 * at http://www.ietf.org/rfc/rfc2246.txt).
29 */
30public abstract class SSLInputStream extends InputStream {
31
32    @Override
33    public abstract int available() throws IOException;
34
35    /**
36     * Reads the following byte value. Note that in the case of
37     * reaching of the end of the data this methods throws the
38     * exception, not return -1. The type of exception depends
39     * on implementation. It was done for simplifying and speeding
40     * up of processing of such cases.
41     * @see org.conscrypt.SSLStreamedInput#read()
42     * @see org.conscrypt.SSLBufferedInput#read()
43     * @see org.conscrypt.HandshakeIODataStream#read()
44     */
45    @Override
46    public abstract int read() throws IOException;
47
48    /**
49     * Reads and returns uint8 value.
50     */
51    public int readUint8() throws IOException {
52        return read() & 0x00FF;
53    }
54
55    /**
56     * Reads and returns uint16 value.
57     */
58    public int readUint16() throws IOException {
59        return (read() << 8) | (read() & 0x00FF);
60    }
61
62    /**
63     * Reads and returns uint24 value.
64     */
65    public int readUint24() throws IOException {
66        return (read() << 16) | (read() << 8) | (read() & 0x00FF);
67    }
68
69    /**
70     * Reads and returns uint32 value.
71     */
72    public long readUint32() throws IOException {
73        return (read() << 24) | (read() << 16)
74              | (read() << 8) | (read() & 0x00FF);
75    }
76
77    /**
78     * Reads and returns uint64 value.
79     */
80    public long readUint64() throws IOException {
81        long hi = readUint32();
82        long lo = readUint32();
83        return (hi << 32) | lo;
84    }
85
86    /**
87     * Returns the vector of opaque values of specified length;
88     * @param length - the length of the vector to be read.
89     * @return the read data
90     * @throws IOException if read operation could not be finished.
91     */
92    public byte[] read(int length) throws IOException {
93        byte[] res = new byte[length];
94        for (int i=0; i<length; i++) {
95            res[i] = (byte) read();
96        }
97        return res;
98    }
99
100    @Override
101    public int read(byte[] b, int off, int len) throws IOException {
102        int read_b;
103        int i = 0;
104        do {
105            if ((read_b = read()) == -1) {
106                return (i == 0) ? -1 : i;
107            }
108            b[off+i] = (byte) read_b;
109            i++;
110        } while ((available() != 0) && (i<len));
111        return i;
112    }
113}
114