1 /*
2  * Copyright (C) 2009 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.bluetooth;
18 
19 import android.annotation.SuppressLint;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 
24 /**
25  * BluetoothInputStream.
26  *
27  * <p>Used to write to a Bluetooth socket.
28  *
29  * @hide
30  */
31 @SuppressLint("AndroidFrameworkBluetoothPermission")
32 /*package*/ final class BluetoothInputStream extends InputStream {
33     private BluetoothSocket mSocket;
34 
BluetoothInputStream(BluetoothSocket s)35     /*package*/ BluetoothInputStream(BluetoothSocket s) {
36         mSocket = s;
37     }
38 
39     /** Return number of bytes available before this stream will block. */
available()40     public int available() throws IOException {
41         return mSocket.available();
42     }
43 
close()44     public void close() throws IOException {
45         mSocket.close();
46     }
47 
48     /**
49      * Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.
50      * Returns -1 if the end of the stream has been reached. Blocks until one byte has been read,
51      * the end of the source stream is detected or an exception is thrown.
52      *
53      * @return the byte read or -1 if the end of stream has been reached.
54      * @throws IOException if the stream is closed or another IOException occurs.
55      * @since Android 1.5
56      */
read()57     public int read() throws IOException {
58         byte[] b = new byte[1];
59         int ret = mSocket.read(b, 0, 1);
60         if (ret == 1) {
61             return (int) b[0] & 0xff;
62         } else {
63             return -1;
64         }
65     }
66 
67     /**
68      * Reads at most {@code length} bytes from this stream and stores them in the byte array {@code
69      * b} starting at {@code offset}.
70      *
71      * @param b the byte array in which to store the bytes read.
72      * @param offset the initial position in {@code buffer} to store the bytes read from this
73      *     stream.
74      * @param length the maximum number of bytes to store in {@code b}.
75      * @return the number of bytes actually read or -1 if the end of the stream has been reached.
76      * @throws IndexOutOfBoundsException if {@code offset < 0} or {@code length < 0}, or if {@code
77      *     offset + length} is greater than the length of {@code b}.
78      * @throws IOException if the stream is closed or another IOException occurs.
79      * @since Android 1.5
80      */
read(byte[] b, int offset, int length)81     public int read(byte[] b, int offset, int length) throws IOException {
82         if (b == null) {
83             throw new NullPointerException("byte array is null");
84         }
85         if ((offset | length) < 0 || length > b.length - offset) {
86             throw new ArrayIndexOutOfBoundsException("invalid offset or length");
87         }
88         return mSocket.read(b, offset, length);
89     }
90 }
91