Category Archives: Java Programming

Write large files in java

One thing many people forget is that string concatenation is a polynomial time process. (O(n^2)).
Because every time it extends some characters, a new string is created and the original string and the extending characters are copied over.

In some programs, people try to write large arrays of data in to file, and to make it simple, they use a String variable to contain all the data, then write to a file. But the problem is, you have to loop through the array and adding each element to the string, this will take significantly more time than just using a BufferedWriter and just keep writing to it.

Tree and Tree node implementation

A tree is a very useful structure, many people like to use

class TreeNode{
E nodeData;
TreeNode child1;
TreeNode child2;
...
}

to represent nodes in a tree, I find it complicated and hoping to get create a way such that tree nodes can be any class that implement a TreeNode class. For example, anytime you try to get the child or parent, you'll have to throw away this wrapper of TreeNode. The code in my mind to use the node would be as easy as
class employee implements TreeNode{
...
}
Then we can use
employee e1= new employee();
employee e2= new employee();
e2.addChild(e1);

But this bring up a problem. The tree node function such as, getChildren, getParent, getDecendants, they should be inherited instead of re-declared in each of the subclass that implements TreeNode.
So a better way is to use abstract class instead of interface.
public abstract class Treenode<T extends Treenode<T>> {
T next;
T myself=(T) this;
T parent;
public void addChild(T node) {
next=node;
node.parent=myself;
}
public T getChild() {
return next;
}
public T getParent(){
return parent;
}

}
The logic here is that any class that extends this Treenode class will automatically get the following functions without reprogramming them. The ugly part of this snippet is the T myself=(T) this;. Here the "this" is considered to be any Treenode, but myself is considered to be the subclass that extends it. This have to be a cast, but I can't see any reason such a cast could fail. Unless
class class1 extends Treenode{};
class class2 extends Treenode{};

Then for some reason you want

class1 c=new class1();
c.add(new class2());

This should not happen as a strongly typed programming practice. Period.

Java: read table with empty elements.

Suppose you have a table, tab or "," delimited. Look like this:

element1,element2,element3,element4

element21,,,element24

If you read each line and do
split(",");

The result will be a 2 element String[] for 2nd line. Because java will ignore the repeated ",".

The solution to this is to do split(",",-1);

SiriProxy ported in Java. NO RUBY,NO LINUX, EASIEST SETUP in any OS

Siri has been out for a while now, if you are looking at this post, I guess you have already looked at siriproxy, spire, and all other stuff on how to setup a proxy in linux system.

To do that, you have to install a linux system. (for hardcore developers, you probably already using linux) for windows users and mac users... not so much.

Then you have to install all these stuff, taking like an hour, and using that RUBY program that you probably are not familiar with.

Then eventually to keep the server running you have to keep a LINUX computer running, probably on a virtual box.

To me, that's a lot of work. It would be MUCH MUCH easier if you can just get a windows service in a windows computer, or a java program in Mac background.

So here, I have ported the siriproxy in java using socket programming.

How does the java program work

The way it works in simple. You run the java either in command line, or you can use a eclipse or netbean or any IDE to open the source code. Then run the servertest.java.

Few things to note are:

1. The server do require a ssl secure connection, which means you still need a pair of private and public certificate. Just like what you'd do in regular siriproxy, you can use them with this server too. Just go into servertest.java, and change the filename to the server certificate.

2. The server require additional libraries. plist for java SSL server for java

The program will require privilege to create file in the current folder( the folder the program is in). It will create a validation.dat file which contain the iphone 4S validation information when an iphone 4S is connected. Then you can use iphone 4 with a spire to connect to the server it should also work.

How to connect to the server.

To connect to the server.

if your computer is in a local network, which means the computer is connected to a router then to internet. Then you just need to setup a port forwarding to forward 443 to your computer's local ip address.

if your computer is not in a local network, which means it's directly connected to internet. Then don't have to do anything.

Just type "my ip" in google search you'll know your ip address from outside world. remember this IP address.

Setup Iphone 4 to connect to the computer

1. Jailbreak your iphone and install spire and iFile.

2. in iFile, browse to /etc/host. add a line "xxx.xxx.xxx.xxx guzzoni.apple.com" where the xxx.xxx.xxx.xxx is the IP address that you remembered.

In iphone 4S, it should be the same.

After this step, your siri should connect to the ip address you entered whenever you activate siri.

Download Link:siriJava
feel free to leave comments if it's confusing. if this post helped you in some way, please like it and share it to others. Thanks

Using I/O, When to close

In java, when using classes such as BufferedReader, it create a i/o stream to reads from the source. But one important for this is that the stream is always remain open unless you close it. Even after you scope out of the method that you created the instance, the stream is still there and not disposed.

This stream, if not closed, will still be forced quit after the program exit. Therefore if the program only use few streams, the problem is not detectable when running it.

BUT when you have a loop outside of the reader, to read thousands of files, you will get some kind of "stream reach its limit" exception. Meaning that the number of stream that's open is exceeded the limit. This limit is usually configured based on OS, in linux it's usually 10000. Theoriaticall if you have

while(true){

BufferedReader br = new   BufferedReader(new InputStream);

br.read();

}

You will get that exceed error after 10000 runs.

Now it occurs to me, that disposable doesn't do anything to close the stream, then switching to a different reader shouldn't do it neither.

Which means:

Buffered br= new BufferedReader(some stream);

br=new BufferedReader(another stream);

When br, this reference is redirected to another instance of BufferedReader, The original one is still not closed.

To verify this, I have run a while(true) loop on both cases that were mentioned above. (with Ubuntu 11.10)The program throws FileNotFoundException after 12000~14000 open streams. But does not throw any exceptions if the BufferedReader is properly closed.

This experiment is just to remind people that, most of us remembered to close a stream after the read. But did not realize that we also need to close the br before redeclaring the variable to something else.

P.S. This bufferedReader is just an example, other streams or readers, such as PrintStream, DataInputStream also need to be closed the same way.