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.InputStream; 210b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 220b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly/** 230b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * BluetoothInputStream. 240b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * 250b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Used to write to a Bluetooth socket. 260b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * 270b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * @hide 280b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */ 290b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly/*package*/ final class BluetoothInputStream extends InputStream { 300b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly private BluetoothSocket mSocket; 310b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 320b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly /*package*/ BluetoothInputStream(BluetoothSocket s) { 330b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly mSocket = s; 340b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly } 350b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 360b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly /** 370b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Return number of bytes available before this stream will block. 380b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */ 390b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly public int available() throws IOException { 4071c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly return mSocket.available(); 410b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly } 420b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 430b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly public void close() throws IOException { 440b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly mSocket.close(); 450b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly } 460b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly 470b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly /** 480b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Reads a single byte from this stream and returns it as an integer in the 490b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * range from 0 to 255. Returns -1 if the end of the stream has been 500b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * reached. Blocks until one byte has been read, the end of the source 510b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * stream is detected or an exception is thrown. 520b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * 530b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * @return the byte read or -1 if the end of stream has been reached. 540b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * @throws IOException 550b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * if the stream is closed or another IOException occurs. 5647e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @since Android 1.5 570b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */ 580b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly public int read() throws IOException { 5947e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly byte b[] = new byte[1]; 6071c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly int ret = mSocket.read(b, 0, 1); 6147e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly if (ret == 1) { 628edae3dfd3f594ca1b24c8365561f1f8c059440eNick Pelly return (int)b[0] & 0xff; 6347e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } else { 6447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly return -1; 6547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 6647e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 6747e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly 6847e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly /** 6947e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * Reads at most {@code length} bytes from this stream and stores them in 7047e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * the byte array {@code b} starting at {@code offset}. 7147e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * 7247e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @param b 7347e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * the byte array in which to store the bytes read. 7447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @param offset 7547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * the initial position in {@code buffer} to store the bytes 7647e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * read from this stream. 7747e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @param length 7847e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * the maximum number of bytes to store in {@code b}. 7947e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @return the number of bytes actually read or -1 if the end of the stream 8047e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * has been reached. 8147e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @throws IndexOutOfBoundsException 8247e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * if {@code offset < 0} or {@code length < 0}, or if 8347e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * {@code offset + length} is greater than the length of 8447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * {@code b}. 8547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @throws IOException 8647e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * if the stream is closed or another IOException occurs. 8747e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @since Android 1.5 8847e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly */ 8947e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly public int read(byte[] b, int offset, int length) throws IOException { 9047e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly if (b == null) { 9147e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly throw new NullPointerException("byte array is null"); 9247e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 9347e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly if ((offset | length) < 0 || length > b.length - offset) { 9447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly throw new ArrayIndexOutOfBoundsException("invalid offset or length"); 9547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 9671c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly return mSocket.read(b, offset, length); 970b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly } 980b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly} 99