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.OutputStream; |
| 21 | |
| 22 | /** |
| 23 | * BluetoothOutputStream. |
| 24 | * |
| 25 | * Used to read from a Bluetooth socket. |
| 26 | * |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 27 | * @hide |
| 28 | */ |
| 29 | /*package*/ final class BluetoothOutputStream extends OutputStream { |
| 30 | private BluetoothSocket mSocket; |
| 31 | |
| 32 | /*package*/ BluetoothOutputStream(BluetoothSocket s) { |
| 33 | mSocket = s; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Close this output stream and the socket associated with it. |
| 38 | */ |
| 39 | public void close() throws IOException { |
| 40 | mSocket.close(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Writes a single byte to this stream. Only the least significant byte of |
| 45 | * the integer {@code oneByte} is written to the stream. |
| 46 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 47 | * @param oneByte the byte to be written. |
| 48 | * @throws IOException if an error occurs while writing to this stream. |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 49 | * @since Android 1.0 |
| 50 | */ |
| 51 | public void write(int oneByte) throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 52 | byte[] b = new byte[1]; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 53 | b[0] = (byte) oneByte; |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 54 | mSocket.write(b, 0, 1); |
Nick Pelly | 47e82de | 2009-06-01 19:09:37 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Writes {@code count} bytes from the byte array {@code buffer} starting |
| 59 | * at position {@code offset} to this stream. |
| 60 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 61 | * @param b the buffer to be written. |
| 62 | * @param offset the start position in {@code buffer} from where to get bytes. |
| 63 | * @param count the number of bytes from {@code buffer} to write to this stream. |
| 64 | * @throws IOException if an error occurs while writing to this stream. |
| 65 | * @throws IndexOutOfBoundsException if {@code offset < 0} or {@code count < 0}, or if {@code |
| 66 | * offset + count} is bigger than the length of {@code buffer}. |
Nick Pelly | 47e82de | 2009-06-01 19:09:37 -0700 | [diff] [blame] | 67 | * @since Android 1.0 |
| 68 | */ |
| 69 | public void write(byte[] b, int offset, int count) throws IOException { |
| 70 | if (b == null) { |
| 71 | throw new NullPointerException("buffer is null"); |
| 72 | } |
| 73 | if ((offset | count) < 0 || count > b.length - offset) { |
| 74 | throw new IndexOutOfBoundsException("invalid offset or length"); |
| 75 | } |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 76 | mSocket.write(b, offset, count); |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 77 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 78 | |
zzy | 71bfafc | 2013-04-16 17:17:37 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Wait until the data in sending queue is emptied. A polling version |
| 81 | * for flush implementation. Use it to ensure the writing data afterwards will |
| 82 | * be packed in the new RFCOMM frame. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 83 | * |
| 84 | * @throws IOException if an i/o error occurs. |
zzy | 71bfafc | 2013-04-16 17:17:37 -0700 | [diff] [blame] | 85 | * @since Android 4.2.3 |
| 86 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 87 | public void flush() throws IOException { |
zzy | 71bfafc | 2013-04-16 17:17:37 -0700 | [diff] [blame] | 88 | mSocket.flush(); |
| 89 | } |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 90 | } |