gpt4 book ai didi

java - 不太明白 InputStream.read( byte[], int, int ) 是如何工作的

转载 作者:行者123 更新时间:2023-11-29 07:13:33 29 4
gpt4 key购买 nike

我知道这是微不足道的,但对我来说没有意义。 Java 不能将指针/引用作为参数传递,但是 read() 函数被传递了一个 数据被读取到的缓冲区,并且只返回一个 int 作为 读取的字节总数进入缓冲区

我希望从这个设备读取五个单独的字节,但是当我向函数传递一个缓冲区,并尝试在之后访问它时,它仍然是 null。如果我打印出函数的返回值,它会给我 int 5,这是预期的。但是如何访问实际放入缓冲区的数据呢?

这是 JavaDocs 的链接....

编辑:

这是对读取函数的原始调用。

public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case R.id.connect:
startActivityForResult( new Intent( this, DeviceList.class ), 1 );
return true;
case R.id.readTest:
Log.i(TAG, "Before write." );
byte[] b = {'$'};
for( int i = 0 ; i < 3 ; i++ ) {
mService.write( b );
}
Log.i(TAG, "After write." );
return true;

case R.id.readData:
byte[] c = mService.read( 5 );

Toast.makeText(this, Integer.toString( mService.bytes ), Toast.LENGTH_LONG).show();
default:
return super.onContextItemSelected( item );
}
}

注意,这个读取函数是在我的类中声明的一个名为BluetoothService的函数。该类包含另一个名为ConnectedThread的类,它调用InputStream读取...

这是我的阅读功能....

public byte[] read( int length ) {
Log.i( TAG, "Inside read." );
ConnectedThread r;
buffer = null;
synchronized( this ) {
if( mState != STATE_CONNECTED ) return null;
r = mConnectedThread;
}
Log.i(TAG, "Before run." );
r.run( length );
Log.i( TAG, "After run." );
Log.i( TAG, Integer.toString( bytes ) );
return buffer;

}

这里是 ConnectedThread 类,它调用自己读取....

/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

public void run(int length) {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];


// Keep listening to the InputStream while connected
//while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer, 0, length);

// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MainMenu.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothService.this.start();
//break;
}
// }
Log.i(TAG, "MADE IT HERE" );
}

/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);

// Share the sent message back to the UI Activity
mHandler.obtainMessage(MainMenu.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}

public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}

最佳答案

Java can't pass pointers/references as parameters

是的,它可以。

像这样的基本误解会让让你很困惑......

read(...) 的使用方式应该是这样的:

byte[] buffer = new byte[42];
int nosRead = is.read(buffer, 0, buffer.length);

您分配一个缓冲区对象并将其作为参数传递给read 方法。从(在这种情况下)偏移量零开始读取字节并将其复制到缓冲区中。当方法调用返回时,它会返回实际读取的字节数......并且这些字节在缓冲区中准备好供调用者处理。

如您所见,这依赖于将字节数组引用作为参数传递给read 方法。

关于java - 不太明白 InputStream.read( byte[], int, int ) 是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11632741/

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