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