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 * 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 * 47 * @param oneByte 48 * the byte to be written. 49 * @throws IOException 50 * if an error occurs while writing to this stream. 51 * @since Android 1.0 52 */ 53 public void write(int oneByte) throws IOException { 54 byte b[] = new byte[1]; 55 b[0] = (byte)oneByte; 56 mSocket.write(b, 0, 1); 57 } 58 59 /** 60 * Writes {@code count} bytes from the byte array {@code buffer} starting 61 * at position {@code offset} to this stream. 62 * 63 * @param b 64 * the buffer to be written. 65 * @param offset 66 * the start position in {@code buffer} from where to get bytes. 67 * @param count 68 * the number of bytes from {@code buffer} to write to this 69 * stream. 70 * @throws IOException 71 * if an error occurs while writing to this stream. 72 * @throws IndexOutOfBoundsException 73 * if {@code offset < 0} or {@code count < 0}, or if 74 * {@code offset + count} is bigger than the length of 75 * {@code buffer}. 76 * @since Android 1.0 77 */ 78 public void write(byte[] b, int offset, int count) throws IOException { 79 if (b == null) { 80 throw new NullPointerException("buffer is null"); 81 } 82 if ((offset | count) < 0 || count > b.length - offset) { 83 throw new IndexOutOfBoundsException("invalid offset or length"); 84 } 85 mSocket.write(b, offset, count); 86 } 87 /** 88 * Wait until the data in sending queue is emptied. A polling version 89 * for flush implementation. Use it to ensure the writing data afterwards will 90 * be packed in the new RFCOMM frame. 91 * @throws IOException 92 * if an i/o error occurs. 93 * @since Android 4.2.3 94 */ 95 public void flush() throws IOException { 96 mSocket.flush(); 97 } 98 } 99