In the process of Android development, there are a lot of things are often used, in order to improve efficiency, the usual methods to make a record.
1. In network programming, if you have not established a socket to use send write, there will be an exception, the package is no problem (if you send byte[] type to change the parameter type):
public static Boolean sendmsg (OutputStream outs,string str) {
Boolean isconnect=false;
if (outs!=null)
try {
Outs.write (Str.getbytes ());
Outs.flush ();
Isconnect=true;
} catch (IOException e) {
Isconnect=false;
}
return isconnect;
}
2. When receiving a picture message, usually receive the size first, then receive the content, and the content may be received several times, we can encapsulate such a class:
public class Myutil {
public static byte[] Read (Bufferedinputstream bin, int size, int max) {
byte[] image = new Byte[size];
int hasread = 0;
while (true) {
if (Max > Size-hasread) {
max = Size-hasread; ///The remaining bytes are less than Max, then the remaining bytes are assigned to Max
}
try {
Hasread = Hasread + bin.read (image, Hasread, Max); bytes read//accumulated
} catch (IOException e) {
E.printstacktrace ();
}
if (Hasread = = size) {
Break
}
}
return image;
}
}
Common methods for Android application development Encapsulation (i)