j2ee
Java socket timeout
by ventrix on Jan.30, 2009, under android, j2ee, j2me, j2se, java
private String socketIP="192.168.1.1"; private int socketPort=2424; private int timeout=5000; //5 seconds Socket socket1 = new Socket(); socket1.connect(new InetSocketAddress(this.socketIP, this.socketPort), timeout); //or else Socket socket1 = new Socket(); socket1.connect(new InetSocketAddress(this.socketIP, this.socketPort)); socket1.setSoTimeout(timeout);
http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html
Java float problems
by ventrix on Dec.23, 2008, under android, j2ee, j2se, java
If you want 0.6917 - 0.6911 to be 0.0006 then:
http://www.velocityreviews.com/forums/showpost.php?s=bfd8bcba71b3b530733952c65e86772a&p=1604265&postcount=4
There are three solutions:
1. If you’ve got enough accuracy but just need the numbers to look
better when you display them, then you can simply use
java.text.DecimalFormat to format the answers to a more reasonable
length when they are displayed. Rounding will occur as appropriate in
the display layer, and the result should look just fine.
2. If you aren’t getting enough precision, then you could look into the
double data type instead of float. Double has not only an immensely
larger range, but also approximately twice the precision in digits.
3. If you need exact math in decimal (often required for financial
calculations, for example) then the class java.math.BigDecimal is what
you’re looking for. BigDecimal lets you store values of any arbitrary
precision; it stores them in decimal, so you need not worry about
rounding that results from binary conversion, and you can customize the
rounding mode on operations that still need it, like division… or even
disabling rounding entirely so that the runtime will throw an exception
to let you know when a calculation won’t have an exact result.
Closing a stream
by ventrix on Dec.10, 2008, under android, j2ee, j2me, j2se, java
/**
* Closes the specified stream.
*
* @param stream The stream to close.
*/
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e("IO", "Could not close stream", e);
}
}
Use it like: closeStream(in);
Copy the content of the input stream into the output stream
by ventrix on Dec.10, 2008, under android, j2ee, j2me, j2se, java
Copy the content of the input stream into the output stream
/**
* Copy the content of the input stream into the output stream, using a temporary
* byte array buffer whose size is defined by {@link #IO_BUFFER_SIZE}.
*
* @param in The input stream to copy from.
* @param out The output stream to copy to.
*
* @throws IOException If any error occurs during the copy.
*/
private static final int IO_BUFFER_SIZE = 4 * 1024;
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
Jain-Sip javadoc Documentation
by ventrix on Oct.25, 2008, under j2ee, java
I’m currently working with Jain-Sip and the java doc site is down. So I uploaded it here.
You can download it for offline use here
Java execution time
by ventrix on Oct.16, 2008, under j2ee, j2me, j2se, java
A very simple way to see how much time needed to run your application.
//variables
private long start;
private long end;
//Put this before the main code
start = System.currentTimeMillis();
//Main code here
//Put this at the end of the code
end = System.currentTimeMillis();
System.out.println("Completed in +"+(end-start)+"ms");
Java Substring using strings v2
by ventrix on Oct.14, 2008, under j2ee, j2me, j2se, java
Updated version 2: Added the includeStartString boolean variable.
public static String returnSubsting(String text, String startString, String endString, Boolean includeStartString) {
int start = 0;
int end = 0;
start = text.indexOf(startString);
if (start >= 0) {
if (!includeStartString)
{
start+=startString.length();
}
end = text.indexOf(endString, start);
if (end >= 0) {
return text.substring(start, end);
} else {
return null;
}
} else {
return null;
}
}
It’s a kind of self documented, so I will only give an example:
String temp=”123ventrix321″;
System.out.println(returnSubsting(temp,”ven”,”321″,true));
will print “ventrix”
System.out.println(returnSubsting(temp,”ven”,”321″,false));
will print “trix”
Java String array to String
by ventrix on Oct.13, 2008, under j2ee, j2me, j2se, java
This is a method I find very useful. It’s usage is pretty obvious… It takes a array of Strings and a seperator and returns a string with the string items separated.
private static String arrayToString(String[] items, String seperator) {
if ((items == null) || (items.length == 0)) {
return "";
} else {
StringBuffer buffer = new StringBuffer(items[0]);
for (int i = 1; i < items.length; i++) {
buffer.append(seperator);
buffer.append(items[i]);
}
return buffer.toString();
}
}
Example:
String[] browsers = {"firefox", "internet explorer", "konqueror", "safari"};
String output = Class.arrayToString(browsers,", ");
System.out.println(output);
Prints: firefox, internet explorer, konqueror, safari