Ventrix’s Code Folds

j2me

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

Leave a Comment : more...

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

Comments Off more...

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);
}
}
Leave a Comment more...

Creating random numbers in j2me and android within specific space

by ventrix on Nov.23, 2008, under Uncategorized, android, j2me, java

Creating random numbers in j2me within specific space

import java.util.Random;
/**
 * Random Number Generator within specific space
 * @param min is the minimum number to be produced
 * @param max is the maximum number to be produced
 * @return the random number
 * @author ventrix
 */
public final class RandomCreator{
    public static int getRandomInt(int min,int max)
    {
        try {
            //Give the currentTimeMillis some time for the seed
            Thread.sleep(2);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        Random randomizer = new Random(System.currentTimeMillis());
        return randomizer.nextInt(max-min+1)+min;
    }
}

Use it as: System.out.println(RandomCreator.getRandomInt(5,10));

Leave a Comment : more...

JSR180 SIP API for J2ME javadoc documentation

by ventrix on Oct.26, 2008, under j2me, java

Ok, I have found the javadoc on the internet, but I want to have it at my own server.

So, here it is: JSR180 SIP API

Leave a Comment more...

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"); 
Leave a Comment : more...

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”

Leave a Comment : more...

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

Leave a Comment : more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...