10b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly/* 20b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Copyright (C) 2009 The Android Open Source Project 30b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * 40b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License"); 50b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * you may not use this file except in compliance with the License. 60b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * You may obtain a copy of the License at 70b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * 80b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * http://www.apache.org/licenses/LICENSE-2.0 90b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * 100b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Unless required by applicable law or agreed to in writing, software 110b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS, 120b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * See the License for the specific language governing permissions and 140b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * limitations under the License. 150b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */ 160b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 170b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pellypackage android.bluetooth; 180b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 190b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pellyimport java.io.IOException; 200b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pellyimport java.io.OutputStream; 210b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 220b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly/** 230b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * BluetoothOutputStream. 240b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * 250b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Used to read from a Bluetooth socket. 260b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * 270b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * @hide 280b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */ 290b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly/*package*/ final class BluetoothOutputStream extends OutputStream { 300b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly private BluetoothSocket mSocket; 310b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 320b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly /*package*/ BluetoothOutputStream(BluetoothSocket s) { 330b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly mSocket = s; 340b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly } 350b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 360b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly /** 370b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Close this output stream and the socket associated with it. 380b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */ 390b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly public void close() throws IOException { 400b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly mSocket.close(); 410b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly } 420b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 430b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly /** 440b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Writes a single byte to this stream. Only the least significant byte of 450b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * the integer {@code oneByte} is written to the stream. 460b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * 470b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * @param oneByte 480b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * the byte to be written. 490b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * @throws IOException 500b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * if an error occurs while writing to this stream. 510b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * @since Android 1.0 520b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */ 530b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly public void write(int oneByte) throws IOException { 5447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly byte b[] = new byte[1]; 5547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly b[0] = (byte)oneByte; 5671c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly mSocket.write(b, 0, 1); 5747e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 5847e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly 5947e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly /** 6047e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * Writes {@code count} bytes from the byte array {@code buffer} starting 6147e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * at position {@code offset} to this stream. 6247e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * 6347e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @param b 6447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * the buffer to be written. 6547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @param offset 6647e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * the start position in {@code buffer} from where to get bytes. 6747e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @param count 6847e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * the number of bytes from {@code buffer} to write to this 6947e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * stream. 7047e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @throws IOException 7147e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * if an error occurs while writing to this stream. 7247e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @throws IndexOutOfBoundsException 7347e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * if {@code offset < 0} or {@code count < 0}, or if 7447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * {@code offset + count} is bigger than the length of 7547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * {@code buffer}. 7647e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @since Android 1.0 7747e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly */ 7847e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly public void write(byte[] b, int offset, int count) throws IOException { 7947e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly if (b == null) { 8047e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly throw new NullPointerException("buffer is null"); 8147e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 8247e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly if ((offset | count) < 0 || count > b.length - offset) { 8347e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly throw new IndexOutOfBoundsException("invalid offset or length"); 8447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 8571c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly mSocket.write(b, offset, count); 860b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly } 8771bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy /** 8871bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy * Wait until the data in sending queue is emptied. A polling version 8971bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy * for flush implementation. Use it to ensure the writing data afterwards will 9071bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy * be packed in the new RFCOMM frame. 9171bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy * @throws IOException 9271bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy * if an i/o error occurs. 9371bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy * @since Android 4.2.3 9471bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy */ 9571bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy public void flush() throws IOException { 9671bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy mSocket.flush(); 9771bfafc84af4b820748b12e1a1010b0dfa7bdea6zzy } 980b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly} 99