Android NIO Channel byteBuffer 在接收器上为空

发布于 2024-12-29 05:46:49 字数 2297 浏览 6 评论 0原文

我遇到了一个问题,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平安喜乐 2025-01-05 05:46:49

发现以这种方式写入通道的最佳方法如下(不要使用我原来的方式,它会产生丢失的字符和额外的空格):

while (channel.read(byteBuffer) != -1) 
{  
    byteBuffer.flip();  
    foc.write(byteBuffer);  
    byteBuffer.compact();  
}

byteBuffer.flip();  
while (byteBuffer.hasRemaining()) 
{  
    foc.write(byteBuffer);  
}

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):

while (channel.read(byteBuffer) != -1) 
{  
    byteBuffer.flip();  
    foc.write(byteBuffer);  
    byteBuffer.compact();  
}

byteBuffer.flip();  
while (byteBuffer.hasRemaining()) 
{  
    foc.write(byteBuffer);  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文