BluetoothInputStream.java revision 0b6955a48bad9aee01ae2f0c06d3f168ca603ab7
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 * TODO: Implement bulk writes (instead of one byte at a time).
280b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * @hide
290b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */
300b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly/*package*/ final class BluetoothInputStream extends InputStream {
310b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    private BluetoothSocket mSocket;
320b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
330b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    /*package*/ BluetoothInputStream(BluetoothSocket s) {
340b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly        mSocket = s;
350b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    }
360b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
370b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    /**
380b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * Return number of bytes available before this stream will block.
390b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     */
400b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    public int available() throws IOException {
410b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly        return mSocket.availableNative();
420b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    }
430b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
440b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    public void close() throws IOException {
450b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly        mSocket.close();
460b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    }
470b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
480b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    /**
490b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * Reads a single byte from this stream and returns it as an integer in the
500b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * range from 0 to 255. Returns -1 if the end of the stream has been
510b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * reached. Blocks until one byte has been read, the end of the source
520b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * stream is detected or an exception is thrown.
530b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     *
540b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * @return the byte read or -1 if the end of stream has been reached.
550b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * @throws IOException
560b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     *             if the stream is closed or another IOException occurs.
570b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * @since Android 1.0
580b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     */
590b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    public int read() throws IOException {
600b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly        return mSocket.readNative();
610b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    }
620b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly}
63