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. 54a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He * @throws IOException if the stream is closed or another IOException occurs. 5547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @since Android 1.5 560b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */ 570b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly public int read() throws IOException { 582992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He byte[] b = new byte[1]; 5971c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly int ret = mSocket.read(b, 0, 1); 6047e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly if (ret == 1) { 61a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He return (int) b[0] & 0xff; 6247e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } else { 6347e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly return -1; 6447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 6547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 6647e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly 6747e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly /** 6847e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * Reads at most {@code length} bytes from this stream and stores them in 6947e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * the byte array {@code b} starting at {@code offset}. 7047e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * 71a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He * @param b the byte array in which to store the bytes read. 72a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He * @param offset the initial position in {@code buffer} to store the bytes read from this 73a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He * stream. 74a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He * @param length the maximum number of bytes to store in {@code b}. 75a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He * @return the number of bytes actually read or -1 if the end of the stream has been reached. 76a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He * @throws IndexOutOfBoundsException if {@code offset < 0} or {@code length < 0}, or if {@code 77a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He * offset + length} is greater than the length of {@code b}. 78a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He * @throws IOException if the stream is closed or another IOException occurs. 7947e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly * @since Android 1.5 8047e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly */ 8147e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly public int read(byte[] b, int offset, int length) throws IOException { 8247e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly if (b == null) { 8347e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly throw new NullPointerException("byte array is null"); 8447e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 8547e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly if ((offset | length) < 0 || length > b.length - offset) { 8647e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly throw new ArrayIndexOutOfBoundsException("invalid offset or length"); 8747e82dee6b18c33fab8c2cdf4f68b20d3663079eNick Pelly } 8871c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly return mSocket.read(b, offset, length); 890b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly } 900b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly} 91