Java/Android:通过套接字读取/写入字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4633808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java/Android: Reading/writing a byte array over a socket
提问by user489481
I have an Android application where I'm trying to send a picture to a server. I did this using Base64 encoding and it worked quite well, but it took too much memory (and time) to encode the picture before sending it.
我有一个 Android 应用程序,我试图将图片发送到服务器。我使用 Base64 编码进行了此操作,效果很好,但是在发送图片之前对图片进行编码占用了太多内存(和时间)。
I'm trying to strip the Android application down to where it just simply sends the byte array and doesn't fiddle around with any kind of encoding scheme so it'll save as much memory and CPU cycles as possible.
我试图将 Android 应用程序剥离到它只是简单地发送字节数组并且不摆弄任何类型的编码方案的地方,因此它将尽可能多地节省内存和 CPU 周期。
This is what I would like the Android code to look like:
这就是我希望 Android 代码的样子:
public String sendPicture(byte[] picture, String address) {
try {
Socket clientSocket = new Socket(address, 8000);
OutputStream out = clientSocket.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.write(picture);
return in.readLine();
}
catch(IOException ioe) {
Log.v("test", ioe.getMessage());
}
return " ";
}
The server is written in Java. How do I write the server code so I can properly retrieve the exact same byte array? My goal is to save as many CPU cycles on the Android as possible.
服务器是用Java编写的。如何编写服务器代码以便正确检索完全相同的字节数组?我的目标是在 Android 上尽可能多地节省 CPU 周期。
So far, all the methods I've tried resulted in corrupt data or a thrown exception.
到目前为止,我尝试过的所有方法都导致数据损坏或抛出异常。
Any help will be appreciated.
任何帮助将不胜感激。
采纳答案by Droidman
Based on Robert's and Zaki's comment, here is the modified code that should perform better.
根据 Robert 和 Zaki 的评论,这里是应该执行更好的修改后的代码。
public byte[] getPicture(InputStream in) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int length = 0;
while ((length = in.read(data))!=-1) {
out.write(data,0,length);
}
return out.toByteArray();
} catch(IOException ioe) {
//handle it
}
return null;
}
回答by thejh
Try something like this:
尝试这样的事情:
public byte[] getPicture(InputStream in) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int data;
while ((data = in.read())>=0) {
out.write(data);
}
return out.toByteArray();
} catch(IOException ioe) {
//handle it
}
return new byte[]{};
}
回答by mtraut
If you want bi-directional communication, the server must know when you're ready - you should prepend a 4 byte length field to your sender side indicating the number of bytes to come.
如果你想要双向通信,服务器必须知道你什么时候准备好了 - 你应该在你的发送方前面添加一个 4 字节长度的字段,指示即将到来的字节数。
On the server side you read the length and then stay listening until everything has arrived. Then you can reply your acknowledge string.
在服务器端,您阅读长度,然后继续收听,直到一切都到达为止。然后您可以回复您的确认字符串。
If it is enough to send only the picture, you can simply send the data and then close the connection. The server side is implemented as shown by @thejh.
如果只发送图片就足够了,您可以简单地发送数据,然后关闭连接。服务器端的实现如@thejh 所示。