gpt4 book ai didi

java - 如果关闭 BluetoothSocket,是否需要关闭 InputStream/OutputStream?

转载 作者:行者123 更新时间:2023-11-29 21:01:19 37 4
gpt4 key购买 nike

我有一个 BluetoothSocket 和两个流。

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket s = (BluetoothSocket) m.invoke(device, 1);

s.connect();

InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();

有时我想关闭套接字。我必须关闭流吗?问题是每个 close() 都可能抛出异常,我必须捕获它们,代码变得臃肿。

在某些类似情况下,IIRC 足以关闭主要对象(在本例中为套接字),其他对象会自动关闭。但是这种行为没有为 BluetoothSocket 记录(或者我找不到它)。

所以:

如果我关闭蓝牙套接字,是否必须关闭它的流?

(那 Socket 呢?它们有什么不同吗?BluetoothSocket 不继承自 Socket。)

最佳答案

我最近一直在研究 Android 蓝牙。我检查了来源。而且似乎您不需要关闭流。

确实,你的流分别是BluetoothSocket构造函数中BluetoothInputStream和BluetoothOutputStream类型的对象:

mInputStream = new BluetoothInputStream(this);
mOutputStream = new BluetoothOutputStream(this);

这些是您调用时返回的流:

InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();

但是当您在这些流上调用 .close() 时,您会调用:

public void close() throws IOException {
mSocket.close();
}

所以你只能再次关闭BluetoothSocket。

总而言之,您不需要关闭这些流。

对于你的第二个问题,Socket 和 BluetoothSocket 的唯一共同点是它们都实现了 Closable:它们将有一个 .close() 方法。这并不意味着他们做同样的事情

这里是 BluetoothOutputStream 的完整代码:

/*package*/ final class BluetoothOutputStream extends OutputStream {
private BluetoothSocket mSocket;

/*package*/ BluetoothOutputStream(BluetoothSocket s) {
mSocket = s;
}

/**
* Close this output stream and the socket associated with it.
*/
public void close() throws IOException {
mSocket.close();
}

/**
* Writes a single byte to this stream. Only the least significant byte of
* the integer {@code oneByte} is written to the stream.
*
* @param oneByte
* the byte to be written.
* @throws IOException
* if an error occurs while writing to this stream.
* @since Android 1.0
*/
public void write(int oneByte) throws IOException {
byte b[] = new byte[1];
b[0] = (byte)oneByte;
mSocket.write(b, 0, 1);
}

/**
* Writes {@code count} bytes from the byte array {@code buffer} starting
* at position {@code offset} to this stream.
*
* @param b
* the buffer to be written.
* @param offset
* the start position in {@code buffer} from where to get bytes.
* @param count
* the number of bytes from {@code buffer} to write to this
* stream.
* @throws IOException
* if an error occurs while writing to this stream.
* @throws IndexOutOfBoundsException
* if {@code offset < 0} or {@code count < 0}, or if
* {@code offset + count} is bigger than the length of
* {@code buffer}.
* @since Android 1.0
*/
public void write(byte[] b, int offset, int count) throws IOException {
if (b == null) {
throw new NullPointerException("buffer is null");
}
if ((offset | count) < 0 || count > b.length - offset) {
throw new IndexOutOfBoundsException("invalid offset or length");
}
mSocket.write(b, offset, count);
}
/**
* Wait until the data in sending queue is emptied. A polling version
* for flush implementation. Use it to ensure the writing data afterwards will
* be packed in the new RFCOMM frame.
* @throws IOException
* if an i/o error occurs.
* @since Android 4.2.3
*/
public void flush() throws IOException {
mSocket.flush();
}

关于java - 如果关闭 BluetoothSocket,是否需要关闭 InputStream/OutputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26034079/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com