Java: StringBuilder versus StringBuffer

When it comes to building / joining the strings there are some options. The most popular way is to use '+' operator. However it is not always a good idea because in some circumstances it can cause out of memory related errors. The reason behind that is that whenever you using '+' operator the new string object is created. If this is done in a long loop you may get into troubles ;)

The other options are to use existing Java classes: StringBuilder or StringBuffer.

StringBuilder was introduced in Java 5 so there is a good chance that some old geeks are get used to StringBuffer instead.

In most cases StringBuilder should be used. It is more efficient than his older brother but it does not support multi-threaded environment. In other hand StringBuffer is synchronized and should be used multithreads.

But please do not give up on using '+' operator - it is still good way when there are no loops and when it comes to quickly join some lines intro a string.



Tomcat as a Windows Service

When an application is ready to be deployed/installed it is often useful to run it as a service rather than directly from console. Tomcat provides some utilz which make it easier.

First step is to install new service. Existing service.bat script can be used:
service.bat install service_name

In order to configure existing service use Tomcat7w GUI application:
tomcat7w.exe //ES//service_name

Apart for service name and description settings you can also setup additional variables, configure memory settings and set Java VM parameters such as for example size of the permanent generation:
-XX:MaxPermSize=256m

To monitor a service run:
tomcat7w.exe //MS//service_name
which will put an icon in the system tray

Another useful tomcat application is Tomcat7 itself. Some most popular usage below:
tomcat7.exe //RS//service_name - run the service
tomcat7.exe //SS//service_name - stop the service
tomcat7.exe //DS//service_name - delete service

You can also install Windows Administration Tools which contains "Services". Additional options can be set such as startup method (manual/ automatic) or recovery options (for example restart at first failure).

Secure copying via SCP

SCP allows to securely transfer files between a local host and a remote host or between two remote hosts. It is based on the SSH protocol.

Copying file to host:
scp file user@host:directory/

Copying file from host to current directory:
scp user@host:directory/file .

To copy entire folders use -r parameter:
scp -r user@host:directory/folder .

SCP by default uses port 22. Sometimes remote host may have it blocked or just uses other port. You can specify the different port using -P parameter.

To copy all files from remote host using port 23: 
scp -P 23 user@host:/directory/* .

To copy all files from current directory to remote host using port 23:
scp -P 23 * user@host:/directory

How to set up SSH keys

To use SSH you will need the keys. To generate them run the command:

ssh-keygen

It will prompt you for a password or just press <ENTER> to use an empty one.

Two keys have been generated under hidden directory /home/your_username/.ssh
  • id_rsa (private)
  • id_rsa.pub (public)

The last and very important step is to restric access to your keys for the others. Run the commands:

chmod 600 id_rsa
chmod 600 id_rsa.pub

That's all for initial setup. Done!

To log in using your keys use the command:
ssh username@address

If you want to bind some address/port use the command:
ssh -L port:host:hostport username@address

for example: ssh -L 8888:localhost:8888 username@address