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