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.OutputStream;
23 
24 /**
25  * BluetoothOutputStream.
26  *
27  * Used to read from a Bluetooth socket.
28  *
29  * @hide
30  */
31 @SuppressLint("AndroidFrameworkBluetoothPermission")
32 /*package*/ final class BluetoothOutputStream extends OutputStream {
33     private BluetoothSocket mSocket;
34 
BluetoothOutputStream(BluetoothSocket s)35     /*package*/ BluetoothOutputStream(BluetoothSocket s) {
36         mSocket = s;
37     }
38 
39     /**
40      * Close this output stream and the socket associated with it.
41      */
close()42     public void close() throws IOException {
43         mSocket.close();
44     }
45 
46     /**
47      * Writes a single byte to this stream. Only the least significant byte of
48      * the integer {@code oneByte} is written to the stream.
49      *
50      * @param oneByte the byte to be written.
51      * @throws IOException if an error occurs while writing to this stream.
52      * @since Android 1.0
53      */
write(int oneByte)54     public void write(int oneByte) throws IOException {
55         byte[] b = new byte[1];
56         b[0] = (byte) oneByte;
57         mSocket.write(b, 0, 1);
58     }
59 
60     /**
61      * Writes {@code count} bytes from the byte array {@code buffer} starting
62      * at position {@code offset} to this stream.
63      *
64      * @param b the buffer to be written.
65      * @param offset the start position in {@code buffer} from where to get bytes.
66      * @param count the number of bytes from {@code buffer} to write to this stream.
67      * @throws IOException if an error occurs while writing to this stream.
68      * @throws IndexOutOfBoundsException if {@code offset < 0} or {@code count < 0}, or if {@code
69      * offset + count} is bigger than the length of {@code buffer}.
70      * @since Android 1.0
71      */
write(byte[] b, int offset, int count)72     public void write(byte[] b, int offset, int count) throws IOException {
73         if (b == null) {
74             throw new NullPointerException("buffer is null");
75         }
76         if ((offset | count) < 0 || count > b.length - offset) {
77             throw new IndexOutOfBoundsException("invalid offset or length");
78         }
79         mSocket.write(b, offset, count);
80     }
81 }
82