无法使用 InputStream 读取 API 读取所有字节? [英] Unable to read all bytes using InputStream read API?

查看:65
本文介绍了无法使用 InputStream 读取 API 读取所有字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 java 套接字中读取图像字节时遇到问题.我的 iOS 客户端在这里发送图像,它需要读取总字节数并将其作为图像存储在服务器端.当我通过 iOS 模拟器进行测试时,它工作得很好.因为,如果我在模拟器中测试,它会将图像发送到 46,577 字节.它读取所有内容非常快速和正确.如果我测试从 iPhone 设备发送图像的相同代码,它也会发送大约45, 301 字节",但套接字代码只能读取一些21, 720 字节"强>",所以只有一半的图像出现,(或)有时它读取的内容非常少,只有4,000 字节".

I am having problem reading image bytes in java socket. My iOS client is sending an image here, and it needs to read the total bytes and store that as image on the server end. It works very fine, when i tested through iOS simulator. Because, If I test in simulator, it sends the image upto 46,577 bytes. It reads all very quickly and properly. If I test the same code sending image from an iPhone device, its also sending around "45, 301 bytes", but socket code is able to read only some "21, 720 bytes", so only half of the image is coming, (or) sometimes it reads very less around "4,000 bytes" only.

我不明白为什么它不能只读取来自设备的相同大小的数据?有人可以指导我解决这个问题吗?

I couldn't understand why it is not able to read the same size data coming from device only? Could someone please guide me on this to solve?

InputStream input = socket.getInputStream();

            byte[] data = new byte[0];
            byte[] buffer = new byte[1024];             

            try {
                do {
                bytesRead = input.read(buffer);
                System.out.println("Reading..bytesRead: " + bytesRead);

                // construct an array large enough to hold the data we currently have
                byte[] newData = new byte[data.length + bytesRead];
                // copy data that was previously read into newData
                System.arraycopy(data, 0, newData, 0, data.length);
                // append new data from buffer into newData
                System.arraycopy(buffer, 0, newData, data.length, bytesRead);
                // set data equal to newData in prep for next block of data
                data = newData;

                } while (input.available() != 0);
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }

            System.out.println("data: " + data.length);

推荐答案

您滥用了 available().它不能作为流结束的测试.请参阅 Javadoc.

You're misusing available(). It is not valid as a test for end of stream. See the Javadoc.

您也不需要所有的数组复制.试试这个:

You don't need all that array copying either. Try this:

ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}
byte[] data = out.toByteArray();

如果您在接收端存储图像,您应该直接写入 FileOutputStream 而不是上面的 ByteArrayOutputStream,而忘记 data 一共.

If you're storing the image at the receiving end you should just write direct to a FileOutputStream instead of the ByteArrayOutputStream above, and forget about data altogether.

这篇关于无法使用 InputStream 读取 API 读取所有字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆