Home | History | Annotate | Download | only in jsse
      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 
     18 package org.apache.harmony.xnet.provider.jsse;
     19 
     20 import java.io.IOException;
     21 import 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  */
     30 public 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.apache.harmony.xnet.provider.jsse.SSLStreamedInput#read()
     42      * @see org.apache.harmony.xnet.provider.jsse.SSLBufferedInput#read()
     43      * @see org.apache.harmony.xnet.provider.jsse.HandshakeIODataStream#read()
     44      */
     45     @Override
     46     public abstract int read() throws IOException;
     47 
     48     @Override
     49     public long skip(long n) throws IOException {
     50         long skept = n;
     51         while (n > 0) {
     52             read();
     53             n--;
     54         }
     55         return skept;
     56     }
     57 
     58     /**
     59      * Reads and returns uint8 value.
     60      */
     61     public int readUint8() throws IOException {
     62         return read() & 0x00FF;
     63     }
     64 
     65     /**
     66      * Reads and returns uint16 value.
     67      */
     68     public int readUint16() throws IOException {
     69         return (read() << 8) | (read() & 0x00FF);
     70     }
     71 
     72     /**
     73      * Reads and returns uint24 value.
     74      */
     75     public int readUint24() throws IOException {
     76         return (read() << 16) | (read() << 8) | (read() & 0x00FF);
     77     }
     78 
     79     /**
     80      * Reads and returns uint32 value.
     81      */
     82     public long readUint32() throws IOException {
     83         return (read() << 24) | (read() << 16)
     84               | (read() << 8) | (read() & 0x00FF);
     85     }
     86 
     87     /**
     88      * Reads and returns uint64 value.
     89      */
     90     public long readUint64() throws IOException {
     91         // BEGIN android-changed
     92         long hi = readUint32();
     93         long lo = readUint32();
     94         return (hi << 32) | lo;
     95         // END android-changed
     96     }
     97 
     98     /**
     99      * Returns the vector of opaque values of specified length;
    100      * @param length - the length of the vector to be read.
    101      * @return the read data
    102      * @throws IOException if read operation could not be finished.
    103      */
    104     public byte[] read(int length) throws IOException {
    105         byte[] res = new byte[length];
    106         for (int i=0; i<length; i++) {
    107             res[i] = (byte) read();
    108         }
    109         return res;
    110     }
    111 
    112     @Override
    113     public int read(byte[] b, int off, int len) throws IOException {
    114         int read_b;
    115         int i = 0;
    116         do {
    117             if ((read_b = read()) == -1) {
    118                 return (i == 0) ? -1 : i;
    119             }
    120             b[off+i] = (byte) read_b;
    121             i++;
    122         } while ((available() != 0) && (i<len));
    123         return i;
    124     }
    125 }
    126