Monday, 19 March 2018

AES Encryption in Javascript and Decryption in Java

AES stands for Advanced Encryption System and it’s a symmetric encryption algorithm.Many times we require to encrypt some plain-text such as password at the client side and send it to server and then server decrypts it to process further.AES encryption and decryption is easier to implement in the same platform such as Android client and Java server but sometimes it becomes challenging to decrypt an AES encrypted password in cross platform environment such as Javascript client and Java Server such as in spring mvc framework because if any system defaults do not match then the decryption will fail.

Friday, 16 March 2018

SMPP Java Example(Client)

This post provides SMPP java example by creating a simple SMPP client that sends short messages to mobile subscriber.Using this client either we can make simple submit to send message to a single mobile subscriber or can broadcast a message to multiple mobile subscribers in one shot.Also, we will verify the delivery receipt. For the client purpose we will be using existing java SMPP client library – jSMPP

Wednesday, 14 March 2018

AES Encryption and Decryption in Java(CBC Mode)

Transmitting confidential data such as plain text password through wire is always vulnerable to security.It is always recommended to encrypt such information and use SSL to transmit those confidential data.Java provides multiple encryption algorithm for this.In this post, we will be discussing about AES(Advanced Encryption Standard) symmetric encryption algorithm in java with CBC mode which is faster and more secure than 3DES.

Monday, 12 March 2018

JDBC - Difference between PreparedStatement and Statement in Java

JDBC API provides three types of Statement for wrapping an SQL query and sending for execution to the database, they are aptly named as Statement, PreparedStatement, and CallableStatement. First one, Statement is used to execute normal SQL queries e.g. select count(*) from Courses. You can also use it to execute DDL, DML and DCL SQL statements. The second one, PreparedStatement is specialized to execute parameterized queries e.g. select * from Courses where courseId=?, you can execute this SQL multiple times by just changing the course parameters. They are compiled and cached at database end, hence quite fast for repeated execution. The third member of this family is CallableStatement, which is there to execute or call stored procedures stored in the database.

Friday, 9 March 2018

A Simple Example to Check if File is Empty in Java

One of the readers of my blog Javarevisited emailed me yesterday about how to check if a file is empty in Java and that's why I am writing this post to show that using an example. The common approach for checking if a file is empty or not is to first check if the file exists or not and then check if it contains any content, but Java has done that hard work for you. Well, it's pretty easy to check emptiness for a file in Java by using length() method of the class. This method returns zero if the file is empty, but the good thing is it also returns zero if the file doesn't exist. Which means you don't need to check if the file exists or not.

Wednesday, 7 March 2018

How to implement Radix Sort in Java - Algorithm Example

The radix sort, like counting sort and bucket sort, is an integer based algorithm (i.e. the values of the input array are assumed to be integers). Hence radix sort is among the fastest sorting algorithms around, in theory. It is also one of the few O(n) sorting algorithm along with a bucket and counting sort. The particular distinction for radix sort is that it creates a bucket for each cipher (i.e. digit); as such, similar to bucket sort, each bucket in radix sort must be a growable list that may admit different keys.

Sunday, 4 March 2018

Mergesort in Java - Algorithm Example

The Merge sort algorithm is a divide and conquers algorithm. In the divide and conquer paradigm, a problem is broken into small problems where each small problem still retains all the properties of the larger problem -- except its size. To solve the original problem, each piece is solved individually; then the pieces are merged back together. For example, imagine you have to sort an array of 200 elements using the selection sort algorithm. Since selection sort takes O(n^2) time, it would take about 40,000-time units to sort the array. Now imagine splitting the array into ten equal pieces and sorting each piece individually still using selection sort. Now it would take 400-time units to sort each piece; for a grand total of 10*400 = 4000.

Tuesday, 27 February 2018

How to read a text file into ArrayList in Java

Prior to Java 7, reading a text file into an ArrayList involves lot of boiler plate coding, as you need to read the file line by line and insert each line into an ArrayList, but from Java 7 onward, you can use the utility method Files.readAllLines() to read all lines of a text file into a List. This method return a List of String which contains all lines of files. Later you can convert this List to ArrayList, LinkedList, or whatever list you want to. Btw, this the fourth article in the series of reading a text file in Java. In the earlier parts, you have learned how to read a file using Scanner and BufferedReader. Then, reading the whole file as String and finally reading a text file into array. This program is not very different from those in terms of fundamentals. We are still going to use read() method for Java 6 solution and will read all text until this method return -1 which signals the end of file.

Monday, 26 February 2018

Dealing with Files and Directories in Java - 10 Thing Java Developer Should Know

The File API is one of the important parts of any programming language or API and even though Java's file API both new and old, are powerful, they are not intuitive enough compared to other languages e.g. Python. Apart from knowing the essential classes and abstractions e.g. File, InputStream, OutputStream, Reader, Writer, Channel etc, you also need to know and remember some nitty gritty detail to avoid subtle issues. There are many articles out there on the internet which can teach you how to read and write data from the file but there are very few which will tell you to do it in right way.

Friday, 23 February 2018

How to create a ZIP File in Java? ZipEntry and ZipOutputStream Compression Example

Since compressing and archiving old log file is an essential housekeeping job in any Java application environment, a Java programmer should know how to compress files in .zip format and then how to read them programmatically if required. The JDK provides full support to create and read ZIP files in Java. There is a separate package to hold all classes related zipping and unzipping files and streams. In this series of article, you will learn how to use those classes e.g. ZipFile, ZipEntry, ZipInputStream, and ZipOutputStream etc. This is the second article about how to work with compressed archives in Java e.g. .zip files.