Android NIO Channel byteBuffer 在接收器上为空
我遇到了一个问题,我在 Android 设备上打开本地文件,并尝试将其发送到正在侦听端口的另一台设备。它正在发送信息(我在mappedByteBuffer 中看到数据)。但是,当侦听器接收到数据并查看 byteBuffer 时,数据全是空白。有人可以指出我做错了什么吗?谢谢!
发送者:
WritableByteChannel channel;
FileChannel fic;
long fsize;
ByteBuffer byteBuffer;
MappedByteBuffer mappedByteBuffer;
connection = new Socket(Resource.LAN_IP_ADDRESS, Resource.LAN_SOCKET_PORT);
out = connection.getOutputStream();
File f = new File(filename);
in = new FileInputStream(f);
fic = in.getChannel();
fsize = fic.size();
channel = Channels.newChannel(out);
//other code
//Send file
long currPos = 0;
while (currPos < fsize)
{
if (fsize - currPos < Resource.MEMORY_ALLOC_SIZE)
{
mappedByteBuffer = fic.map(FileChannel.MapMode.READ_ONLY, currPos, fsize - currPos);
channel.write(mappedByteBuffer);
currPos = fsize;
}
else
{
mappedByteBuffer = fic.map(FileChannel.MapMode.READ_ONLY, currPos, Resource.MEMORY_ALLOC_SIZE);
channel.write(mappedByteBuffer);
currPos += Resource.MEMORY_ALLOC_SIZE;
}
}
closeAllConnections(); //closes connection, fic, channel, in, out
监听者
FileChannel foc;
ByteBuffer byteBuffer;
ReadableByteChannel channel;
serverSoc = new ServerSocket(myPort);
connection = serverSoc.accept();
connection.setSoTimeout(3600000);
connection.setReceiveBufferSize(Resource.MEMORY_ALLOC_SIZE);
in = connection.getInputStream();
out = new FileOutputStream(new File(currentFileName));
foc = out.getChannel();
channel = Channels.newChannel(in);
//other code
while (fileSize > 0)
{
if (fileSize < Resource.MEMORY_ALLOC_SIZE)
{
byteBuffer = ByteBuffer.allocate((int)fileSize);
channel.read(byteBuffer);
//byteBuffer is blank!
foc.write(byteBuffer);
fileSize = 0;
}
else
{
byteBuffer = ByteBuffer.allocate(Resource.MEMORY_ALLOC_SIZE);
channel.read(byteBuffer);
//byteBuffer is blank!
foc.write(byteBuffer);
fileSize -= Resource.MEMORY_ALLOC_SIZE;
}
}
closeAllConnections(); //closes connection, foc, channel, in, out, serverSoc
注意: 内存分配大小 = 32768
I'm running into an issue where I'm opening a local file on my Android device and I'm trying to send it to another device that's listening on a port. It's sending the information (I see data in mappedByteBuffer). However, when the data is received on the listener and I view byteBuffer, the data is all blank. Can someone point out what I'm doing wrong? Thanks!
Sender:
WritableByteChannel channel;
FileChannel fic;
long fsize;
ByteBuffer byteBuffer;
MappedByteBuffer mappedByteBuffer;
connection = new Socket(Resource.LAN_IP_ADDRESS, Resource.LAN_SOCKET_PORT);
out = connection.getOutputStream();
File f = new File(filename);
in = new FileInputStream(f);
fic = in.getChannel();
fsize = fic.size();
channel = Channels.newChannel(out);
//other code
//Send file
long currPos = 0;
while (currPos < fsize)
{
if (fsize - currPos < Resource.MEMORY_ALLOC_SIZE)
{
mappedByteBuffer = fic.map(FileChannel.MapMode.READ_ONLY, currPos, fsize - currPos);
channel.write(mappedByteBuffer);
currPos = fsize;
}
else
{
mappedByteBuffer = fic.map(FileChannel.MapMode.READ_ONLY, currPos, Resource.MEMORY_ALLOC_SIZE);
channel.write(mappedByteBuffer);
currPos += Resource.MEMORY_ALLOC_SIZE;
}
}
closeAllConnections(); //closes connection, fic, channel, in, out
Listener
FileChannel foc;
ByteBuffer byteBuffer;
ReadableByteChannel channel;
serverSoc = new ServerSocket(myPort);
connection = serverSoc.accept();
connection.setSoTimeout(3600000);
connection.setReceiveBufferSize(Resource.MEMORY_ALLOC_SIZE);
in = connection.getInputStream();
out = new FileOutputStream(new File(currentFileName));
foc = out.getChannel();
channel = Channels.newChannel(in);
//other code
while (fileSize > 0)
{
if (fileSize < Resource.MEMORY_ALLOC_SIZE)
{
byteBuffer = ByteBuffer.allocate((int)fileSize);
channel.read(byteBuffer);
//byteBuffer is blank!
foc.write(byteBuffer);
fileSize = 0;
}
else
{
byteBuffer = ByteBuffer.allocate(Resource.MEMORY_ALLOC_SIZE);
channel.read(byteBuffer);
//byteBuffer is blank!
foc.write(byteBuffer);
fileSize -= Resource.MEMORY_ALLOC_SIZE;
}
}
closeAllConnections(); //closes connection, foc, channel, in, out, serverSoc
Note:
MEMORY_ALLOC_SIZE = 32768
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现以这种方式写入通道的最佳方法如下(不要使用我原来的方式,它会产生丢失的字符和额外的空格):
Found the best way to write to the channels this way is the following (don't use my original way, it produces missing characters and extra spaces):