我正在尝试使用以下代码读取Java中的NamedPipe:
ByteBuffer buffer = ByteBuffer.allocate(4*1024);
IntByReference bytesRead = new IntByReference(buffer.capacity());
int lastError = 0;
if (kernel32.PeekNamedPipe(pipeHandle, buffer, buffer.capacity(), bytesRead, null, null))
while ( !(kernel32.ReadFile(pipeHandle, /*(!)ERROR HERE==>*/buffer, buffer.capacity(), bytesRead, overlapped)) || (lastError=kernel32.GetLastError()) == Kernel32.ERROR_MORE_DATA){
if (lastError == Kernel32.ERROR_PIPE_NOT_CONNECTED || overlapped.Internal.intValue() != WinNT.ERROR_IO_PENDING)
break;
}
但它失败并显示以下错误:incompatible types: ByteBuffer cannot be converted to byte[]
。我试图用byte[] b = new byte[buffer.remaining()];
的b
替换buffer
,但程序开始出错。欢迎任何有关如何修复错误的想法。谢谢。
问题已通过以下方式解决:
byte[] b = new byte[buffer.remaining()];
再次检查了程序的返回。
谢谢大家。